summaryrefslogtreecommitdiffstats
path: root/kscd/libwm/cdda.c
blob: ca8d76ba901001c4e7a5271c9599c783cca66ec3 (plain)
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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
/***************************************************************************
                          cdda.c  -  description
                             -------------------
    begin                : Mon Jan 27 2003
    copyright            : (C) 2003 by Alex Kern
    email                : [email protected]
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/
/***************************************************************************
 *                                                                         *
 *   This is a common cddamaster piece of code                             *
 *                                                                         *
 ***************************************************************************/

#include <string.h>
#include <sys/poll.h>
#include <sys/wait.h>
#include <stdio.h>
#include <unistd.h>
#include "include/wm_config.h"
#include "include/wm_struct.h"
#include "include/wm_cdda.h"
#include "include/wm_cdrom.h"
#include "audio/audio.h"
#include <pthread.h>

#if defined(BUILD_CDDA)

static pthread_t thread_read;
static pthread_t thread_play;

int get_next_block(int x);
void *cdda_fct_read(void* arg);
void *cdda_fct_play(void* arg);

#define NUMBLOCKS 2
#define NUMFRAMES 10

static struct cdda_block blks[NUMBLOCKS];
static pthread_mutex_t blks_mutex[NUMBLOCKS];

static struct cdda_device dev;
static pthread_cond_t wakeup_audio;

/*
 * Loudness setting, plus the floating volume multiplier and decaying-average
 * volume level.
 */
static unsigned int loudness = 0, volume = 32768, level;

/*
 * This is non-null if we're saving audio to a file.
 */
static FILE *output = NULL;

/*
 * These are driverdependent oops
 *
 */
static struct audio_oops *oops = NULL;

/*
 * Audio file header format.
 */
typedef unsigned long u_32;
struct auheader {
	u_32 magic;
	u_32 hdr_size;
	u_32 data_size;
	u_32 encoding;
	u_32 sample_rate;
	u_32 channels;
};

/* had to change #ifdef to #if   -> see wm_cdda.h */
#ifdef __FreeBSD__
/* Phungus not with htonl on FreeBSD */
#include <sys/param.h>
#else
#if WM_BIG_ENDIAN
# ifndef htonl
#  define htonl(x) (x)
# endif
#else
extern unsigned long htonl(unsigned long);
#endif
#endif

/*
 * Try to initialize the CDDA slave.  Returns 0 on success.
 */
int
gen_cdda_init( struct wm_drive *d )
{
    int ret = 0;

    if (d->cdda_slave > -1)
        return 0;

    memset(&blks, 0, sizeof(blks));

    dev.fd = -1;
    dev.frames_at_once = NUMFRAMES;
    dev.blocks = blks;
    dev.numblocks = NUMBLOCKS;
    dev.status = WM_CDM_UNKNOWN;
    dev.devname = d->cd_device;

    if ((ret = wmcdda_init(&dev)))
        return ret;

    oops = setup_soundsystem(d->soundsystem, d->sounddevice, d->ctldevice);
    if (!oops) {
        ERRORLOG("cdda: setup_soundsystem failed\n");
        wmcdda_close(&dev);
        return -1;
    }

    if(pthread_create(&thread_read, NULL, cdda_fct_read, &dev)) {
        ERRORLOG("error by create pthread");
        oops->wmaudio_close();
        wmcdda_close(&dev);
        return -1;
    }

    if(pthread_create(&thread_play, NULL, cdda_fct_play, &dev)) {
        ERRORLOG("error by create pthread");
        oops->wmaudio_close();
        wmcdda_close(&dev);
        return -1;
    }
    d->cdda_slave = 0;
    return 0;
}

int
cdda_get_drive_status( struct wm_drive *d, int oldmode,
  int *mode, int *frame, int *track, int *ind )
{
    if (d->cdda_slave > -1) {
        if(dev.status)
          *mode = dev.status;
        else
          *mode = oldmode;

        if (*mode == WM_CDM_PLAYING) {
            *track = dev.track;
            *ind = dev.index;
            *frame = dev.frame;
        } else if (*mode == WM_CDM_CDDAERROR) {
            /*
             * An error near the end of the CD probably
             * just means we hit the end.
             */
            *mode = WM_CDM_TRACK_DONE;
        }
        return 0;
    }

    return -1;
}

int
cdda_play( struct wm_drive *d, int start, int end, int realstart )
{
    if (d->cdda_slave > -1) {
        dev.command = WM_CDM_STOPPED;

        wmcdda_setup(start, end, realstart);

        level = 2500;
        volume = 1 << 15;

        dev.track =  -1;
        dev.index =  0;
        dev.frame = start;
        dev.command = WM_CDM_PLAYING;

        return 0;
    }

    return -1;
}

int
cdda_pause( struct wm_drive *d )
{
    if (d->cdda_slave > -1) {
        if(WM_CDM_PLAYING == dev.command) {
            dev.command = WM_CDM_PAUSED;
        } else {
            dev.command = WM_CDM_PLAYING;
        }

        return 0;
    }

    return -1;
}

int
cdda_stop( struct wm_drive *d )
{
    if (d->cdda_slave > -1) {
        dev.command = WM_CDM_STOPPED;
        oops->wmaudio_stop();
        return 0;
    }

    return -1;
}

int
cdda_eject( struct wm_drive *d )
{
    if (d->cdda_slave > -1) {
        dev.command = WM_CDM_EJECTED;
        oops->wmaudio_stop();
        /*wmcdda_close(&dev);*/
        return 0;
    }

    return -1;
}

int
cdda_set_volume( struct wm_drive *d, int left, int right )
{
    if (d->cdda_slave > -1) {
        int bal, vol;

        bal = (right - left) + 100;
        bal *= 255;
        bal /= 200;
        if (right > left)
            vol = right;
        else
            vol = left;
        vol *= 255;
        vol /= 100;

        if(oops->wmaudio_balance)
            oops->wmaudio_balance(bal);
        if(oops->wmaudio_volume)
            oops->wmaudio_volume(vol);

        return 0;
    }

    return -1;
}

/*
 * Read the initial volume from the drive, if available.  Each channel
 * ranges from 0 to 100, with -1 indicating data not available.
 */
int
cdda_get_volume( struct wm_drive *d, int *left, int *right )
{
    if (d->cdda_slave > -1) {
        if(!oops->wmaudio_state) {
            dev.volume = -1;
            dev.balance = 128;
        }

        *left = *right = (dev.volume * 100 + 254) / 255;

        if (dev.balance < 110)
            *right = (((dev.volume * dev.balance + 127) / 128) * 100 + 254) / 255;
        else if (dev.balance > 146)
            *left = (((dev.volume * (255 - dev.balance) + 127) / 128) * 100 + 254) / 255;

        return 0;
    }
 
    return -1;
}

/*
 * Turn off the CDDA slave.
 */
void
cdda_kill( struct wm_drive *d )
{
    if (d->cdda_slave > -1) {
        dev.command = WM_CDM_STOPPED;
        oops->wmaudio_stop();
        sleep(1);
        wmcdda_close(&dev);
        oops->wmaudio_close();
    
        dev.blocks = NULL;
        wait(NULL);
        d->cdda_slave = -1;
    }
}

/*
 * Tell the CDDA slave to set the loudness level.
 */
void
cdda_set_loudness( struct wm_drive *d, int loud )
{
    if (d->cdda_slave > -1) {
        loudness = loud;
    }
}

/*
 * Tell the CDDA slave to start (or stop) saving to a file.
 */
void
cdda_save( struct wm_drive *d, char *filename )
{
  #if 0
  int len;

  if (filename == NULL || filename[0] == '\0')
    len = 0;
  else
    len = strlen(filename);
  write(d->cdda_slave, "F", 1);
  write(d->cdda_slave, &len, sizeof(len));
  if (len)
    write(d->cdda_slave, filename, len);


    		read(0, &namelen, sizeof(namelen));
		if (output != NULL) {
			fclose(output);
			output = NULL;
		}
		if (namelen) {
			filename = malloc(namelen + 1);
			if (filename == NULL) {
				perror("cddas");
				wmcdda_close(dev);
				oops->wmaudio_close();
				exit(1);
			}

			read(0, filename, namelen);
			filename[namelen] = '\0';
			output = fopen(filename, "w");
			if (output == NULL) {
				perror(filename);
			} else {
				/* Write an .au file header. */
				hdr.magic = htonl(0x2e736e64);
				hdr.hdr_size = htonl(sizeof(hdr) + 28);
				hdr.data_size = htonl(~0);
				hdr.encoding = htonl(3); /* linear-16 */
				hdr.sample_rate = htonl(44100);
				hdr.channels = htonl(2);

				fwrite(&hdr, sizeof(hdr), 1, output);
				fwrite("Recorded from CD by WorkMan", 28, 1, output);
			}
			free(filename);

#endif
}

int get_next_block(int x)
{
    int y = ++x;
    return (y < NUMBLOCKS)?y:0;
}

void *cdda_fct_read(void* arg)
{
    struct cdda_device *cddadev = (struct cdda_device*)arg;
    int i, j, wakeup;
    long result;

    while (cddadev->blocks) {
        while(cddadev->command != WM_CDM_PLAYING) {
            cddadev->status = cddadev->command;
            sleep(1);
        }
        
        i = 0;
        pthread_mutex_lock(&blks_mutex[i]);
        wakeup = 1;
        
        while(cddadev->command == WM_CDM_PLAYING) {

            result = wmcdda_read(cddadev, &blks[i]);
            
            if (result <= 0 && blks[i].status != WM_CDM_TRACK_DONE) {
                ERRORLOG("cdda: wmcdda_read failed, stop playing\n");
                cddadev->command = WM_CDM_STOPPED;
                break;
            } else {
                if (output)
                    fwrite(blks[i].buf, blks[i].buflen, 1, output);
            }
            
            j = get_next_block(i);
            pthread_mutex_lock(&blks_mutex[j]);
            
            if(wakeup) {
                wakeup = 0;
                pthread_cond_signal(&wakeup_audio);
            }
            
            pthread_mutex_unlock(&blks_mutex[i]);
            /* audio can start here */
            
            i = j;
        }

        pthread_mutex_unlock(&blks_mutex[i]);
    }
    
    return 0;
}

void *cdda_fct_play(void* arg)
{
    struct cdda_device *cddadev = (struct cdda_device*)arg;
    int i = 0;

    while (cddadev->blocks) {
        if(cddadev->command != WM_CDM_PLAYING) {
            i = 0;
            pthread_mutex_lock(&blks_mutex[i]);
            pthread_cond_wait(&wakeup_audio, &blks_mutex[i]);
        } else {
            i = get_next_block(i);
            pthread_mutex_lock(&blks_mutex[i]);    
        }
        
        if (oops->wmaudio_play(&blks[i])) {
            oops->wmaudio_stop();
            ERRORLOG("cdda: wmaudio_play failed\n");
            cddadev->command = WM_CDM_STOPPED;
        }
        cddadev->frame = blks[i].frame;
        cddadev->track = blks[i].track;
        cddadev->index = blks[i].index;
        cddadev->status = blks[i].status;
        
        pthread_mutex_unlock(&blks_mutex[i]);    
    }
    
    return 0;
}

#endif