File: alurecdplay.c

package info (click to toggle)
alure 1.2-11
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 832 kB
  • sloc: cpp: 4,333; ansic: 629; javascript: 535; makefile: 23
file content (382 lines) | stat: -rw-r--r-- 9,374 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
#include <string.h>
#include <stdlib.h>
#include <stdio.h>

#include "AL/alure.h"

#ifdef __linux__
/* Linux implementation for reading CD digital audio */
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <sys/ioctl.h>
#include <linux/cdrom.h>


typedef struct {
    int fd;
    int start;
    int current;
    int end;
} CDDAData;

static const char *cd_device = "/dev/cdrom1";

static void *cdda_open_file(const char *fname)
{
    struct cdrom_tochdr cdtochdr;
    struct cdrom_tocentry cdtocentry;
    int startaddr=-1, endaddr=-1, idx;
    CDDAData *dat;
    int fd, ret;

    /* Make sure the filename has the appropriate URI and extract the track
     * number */
    if(strncmp(fname, "cdda://", 7) != 0)
        return NULL;
    idx = atoi(fname+7);

    /* Open the block device and get the TOC header */
    fd = open(cd_device, O_RDONLY | O_NONBLOCK);
    if(fd == -1) return NULL;

    ret = ioctl(fd, CDROMREADTOCHDR, &cdtochdr);
    if(ret != -1)
    {
        if(idx < cdtochdr.cdth_trk0 || idx >= cdtochdr.cdth_trk1)
            ret = -1;
    }
    if(ret != -1)
    {
        /* Get the start address for the requested track, and the start address
         * of the next track as the end point */
        cdtocentry.cdte_format = CDROM_LBA;
        cdtocentry.cdte_track = idx;
        ret = ioctl(fd, CDROMREADTOCENTRY, &cdtocentry);
        if(ret != -1 && !(cdtocentry.cdte_ctrl&CDROM_DATA_TRACK))
            startaddr = cdtocentry.cdte_addr.lba;
    }
    if(ret != -1)
    {
        cdtocentry.cdte_format = CDROM_LBA;
        cdtocentry.cdte_track = ++idx;
        ret = ioctl(fd, CDROMREADTOCENTRY, &cdtocentry);
        if(ret != -1)
            endaddr = cdtocentry.cdte_addr.lba;
    }

    if(ret == -1 || startaddr == -1 || endaddr == -1)
    {
        close(fd);
        return NULL;
    }

    dat = malloc(sizeof(*dat));
    dat->fd = fd;
    dat->start = startaddr;
    dat->current = startaddr;
    dat->end = endaddr;

    return dat;
}

static ALboolean cdda_get_format(void *instance, ALenum *format, ALuint *samplerate, ALuint *blocksize)
{
    /* These values are specified by the red-book audio standard and do not
     * change */
    *format = AL_FORMAT_STEREO16;
    *samplerate = 44100;
    *blocksize = CD_FRAMESIZE_RAW;

    return AL_TRUE;
    (void)instance;
}

static ALuint cdda_decode(void *instance, ALubyte *data, ALuint bytes)
{
    CDDAData *self = instance;

    ALuint got = 0;
    while(bytes-got >= CD_FRAMESIZE_RAW && self->current < self->end)
    {
        struct cdrom_read_audio cdra;

        /* Read as many frames that are left that will fit */
        cdra.addr.lba = self->current;
        cdra.addr_format = CDROM_LBA;
        cdra.nframes = (bytes-got) / CD_FRAMESIZE_RAW;
        cdra.buf = data;

        if(cdra.nframes > self->end-self->current)
            cdra.nframes = self->end-self->current;

        if(ioctl(self->fd, CDROMREADAUDIO, &cdra) == -1)
        {
            cdra.nframes = 1;
            memset(cdra.buf, 0, CD_FRAMESIZE_RAW);
        }

        self->current += cdra.nframes;
        data += CD_FRAMESIZE_RAW*cdra.nframes;
        got += CD_FRAMESIZE_RAW*cdra.nframes;
    }

    return got;
}

static ALboolean cdda_rewind(void *instance)
{
    CDDAData *self = instance;

    self->current = self->start;
    return AL_TRUE;
}

static void cdda_close(void *instance)
{
    CDDAData *self = instance;

    close(self->fd);
    free(self);
}

#elif defined(HAVE_DDK_NTDDCDRM_H)
/* Windows implementation for reading CD digital audio */
#include <windows.h>
#include <ddk/ntddcdrm.h>

/* Defined by red-book standard; do not change! */
#define CD_FRAMESIZE_RAW  (2352)

#define CDFRAMES_PERSEC  (75)
#define CDFRAMES_PERMIN  (CDFRAMES_PERSEC * 60)
#define FRAME_OF_ADDR(a) ((a)[1] * CDFRAMES_PERMIN + (a)[2] * CDFRAMES_PERSEC + (a)[3])
#define FRAME_OF_TOC(toc, idx)  FRAME_OF_ADDR((toc).TrackData[idx - (toc).FirstTrack].Address)

/* All of this is pretty similar to the Linux version, except using the WinAPI
 * device functions instead of POSIX */
typedef struct {
    HANDLE fd;
    DWORD start;
    DWORD current;
    DWORD end;
} CDDAData;

static const char *cd_device = "D";

static void *cdda_open_file(const char *fname)
{
    /* Device filename is of the format "\\.\D:", where "D" is the letter of
     * the CD drive */
    const char cd_drv[] = { '\\','\\','.','\\',*cd_device,':', 0 };
    DWORD br, idx;
    CDROM_TOC toc;
    CDDAData *dat;
    HANDLE fd;
    int ret;

    if(strncmp(fname, "cdda://", 7) != 0)
        return NULL;
    idx = atoi(fname+7);

    fd = CreateFileA(cd_drv, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, 0);
    if(fd == (HANDLE)-1) return NULL;

    ret = DeviceIoControl(fd, IOCTL_CDROM_READ_TOC, NULL, 0,
                          &toc, sizeof(toc), &br, NULL);
    if(ret == 0 || idx < toc.FirstTrack || idx > toc.LastTrack ||
       (toc.TrackData[idx-toc.FirstTrack].Control&4))
    {
        CloseHandle(fd);
        return NULL;
    }

    dat = malloc(sizeof(*dat));
    dat->fd = fd;
    dat->start = FRAME_OF_TOC(toc, idx) - FRAME_OF_TOC(toc, toc.FirstTrack);
    dat->current = dat->start;
    dat->end = FRAME_OF_TOC(toc, idx+1) - FRAME_OF_TOC(toc, toc.FirstTrack);

    return dat;
}

static ALboolean cdda_get_format(void *instance, ALenum *format, ALuint *samplerate, ALuint *blocksize)
{
    *format = AL_FORMAT_STEREO16;
    *samplerate = 44100;
    *blocksize = CD_FRAMESIZE_RAW;

    return AL_TRUE;
    (void)instance;
}

static ALuint cdda_decode(void *instance, ALubyte *data, ALuint bytes)
{
    CDDAData *self = instance;

    ALuint got = 0;
    while(bytes-got >= CD_FRAMESIZE_RAW && self->current < self->end)
    {
        RAW_READ_INFO rdInfo;
        DWORD br;

        rdInfo.DiskOffset.QuadPart = (LONGLONG)self->current<<11;
        rdInfo.SectorCount = (bytes-got) / CD_FRAMESIZE_RAW;
        rdInfo.TrackMode = CDDA;

        if(rdInfo.SectorCount > self->end-self->current)
            rdInfo.SectorCount = self->end-self->current;

        if(!DeviceIoControl(self->fd, IOCTL_CDROM_RAW_READ,
                            &rdInfo, sizeof(rdInfo), data, bytes-got,
                            &br, NULL))
        {
            rdInfo.SectorCount = 1;
            memset(data, 0, CD_FRAMESIZE_RAW);
        }

        self->current += rdInfo.SectorCount;
        data += CD_FRAMESIZE_RAW*rdInfo.SectorCount;
        got += CD_FRAMESIZE_RAW*rdInfo.SectorCount;
    }

    return got;
}

static ALboolean cdda_rewind(void *instance)
{
    CDDAData *self = instance;

    self->current = self->start;
    return AL_TRUE;
}

static void cdda_close(void *instance)
{
    CDDAData *self = instance;

    CloseHandle(self->fd);
    free(self);
}

#else

static const char *cd_device = "(unknown)";

static void *cdda_open_file(const char *fname)
{
    if(strncmp(fname, "cdda://", 7) == 0)
        fprintf(stderr, "CD Digital Audio was not compiled for this system\n");
    return NULL;
}

static ALboolean cdda_get_format(void *instance, ALenum *format, ALuint *samplerate, ALuint *blocksize)
{
    return AL_FALSE;
    (void)instance;
}

static ALuint cdda_decode(void *instance, ALubyte *data, ALuint bytes)
{
    return 0;
    (void)instance;
    (void)data;
    (void)bytes;
}

static ALboolean cdda_rewind(void *instance)
{
    return AL_FALSE;
    (void)instance;
}

static void cdda_close(void *instance)
{
    (void)instance;
}

#endif


static void eos_callback(void *param, ALuint unused)
{
    *(volatile int*)param = 1;
    (void)unused;
}

#define NUM_BUFS 3


int main(int argc, char **argv)
{
    volatile int isdone;
    alureStream *stream;
    const char *fname;
    ALuint src;

    if(argc < 2 || (strcmp(argv[1], "-cd-device") == 0 && argc < 4))
    {
        fprintf(stderr, "Usage %s [-cd-device <device>] cdda://<tracknum>\n", argv[0]);
        fprintf(stderr, "Default CD device is %s\n", cd_device);
        return 1;
    }

    if(strcmp(argv[1], "-cd-device") != 0)
        fname = argv[1];
    else
    {
        cd_device = argv[2];
        fname = argv[3];
    }

    alureInstallDecodeCallbacks(-1, cdda_open_file, NULL, cdda_get_format,
                                cdda_decode, cdda_rewind, cdda_close);

    if(!alureInitDevice(NULL, NULL))
    {
        fprintf(stderr, "Failed to open OpenAL device: %s\n", alureGetErrorString());
        return 1;
    }

    alGenSources(1, &src);
    if(alGetError() != AL_NO_ERROR)
    {
        fprintf(stderr, "Failed to create OpenAL source!\n");
        alureShutdownDevice();
        return 1;
    }

    alureStreamSizeIsMicroSec(AL_TRUE);

    stream = alureCreateStreamFromFile(fname, 250000, 0, NULL);
    if(!stream)
    {
        fprintf(stderr, "Could not load %s: %s\n", fname, alureGetErrorString());
        alDeleteSources(1, &src);

        alureShutdownDevice();
        return 1;
    }

    isdone = 0;
    if(!alurePlaySourceStream(src, stream, NUM_BUFS, 0, eos_callback, (void*)&isdone))
    {
        fprintf(stderr, "Failed to play stream: %s\n", alureGetErrorString());
        isdone = 1;
    }

    while(!isdone)
    {
        alureSleep(0.125);
        alureUpdate();
    }
    alureStopSource(src, AL_FALSE);

    alDeleteSources(1, &src);
    alureDestroyStream(stream, 0, NULL);

    alureShutdownDevice();
    return 0;
}