2
* $Id: plat_linux_cdda.c,v 1.4 2000/08/30 04:40:36 dfoerste Exp $
4
* This file is part of WorkMan, the civilized CD player library
5
* (c) 1991-1997 by Steven Grimm (original author)
6
* (c) by Dirk Fļæ½rsterling (current 'author' = maintainer)
7
* The maintainer can be contacted by his e-mail address:
8
* milliByte@DeathsDoor.com
10
* This library is free software; you can redistribute it and/or
11
* modify it under the terms of the GNU Library General Public
12
* License as published by the Free Software Foundation; either
13
* version 2 of the License, or (at your option) any later version.
15
* This library is distributed in the hope that it will be useful,
16
* but WITHOUT ANY WARRANTY; without even the implied warranty of
17
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18
* Library General Public License for more details.
20
* You should have received a copy of the GNU Library General Public
21
* License along with this library; if not, write to the Free
22
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
25
* Linux CDDA functions. Derived from the SUN module.
28
#include "include/wm_config.h"
30
static char plat_linux_cdda_id[] = "$Id: plat_linux_cdda.c,v 1.4 2000/08/30 04:40:36 dfoerste Exp $";
32
#if defined(__linux__) && defined(BUILD_CDDA) /* { */
34
#include "include/wm_cdda.h"
35
/* types.h and cdio.h are included by wmcdda.h */
39
#include <sys/ioctl.h>
43
#define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
45
#define CDDABLKSIZE 2368
46
#define SAMPLES_PER_BLK 588
48
/* Address of next block to read. */
49
int current_position = 0;
51
/* Address of first and last blocks to read. */
52
int starting_position = 0;
53
int ending_position = 0;
55
/* Playback direction. */
58
/* Number of blocks to read at once; initialize to the maximum. */
62
* This is the fastest way to convert from BCD to 8-bit.
64
unsigned char unbcd[256] = {
65
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,0,0,0,0,0,
66
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0,0,0,0,0,0,
67
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 0,0,0,0,0,0,
68
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 0,0,0,0,0,0,
69
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 0,0,0,0,0,0,
70
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 0,0,0,0,0,0,
71
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 0,0,0,0,0,0,
72
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 0,0,0,0,0,0,
73
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 0,0,0,0,0,0,
74
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0,0,0,0,0,0,
75
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
76
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
77
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
78
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
79
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
80
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
84
* Initialize the CDDA data buffer and open the appropriate device.
86
* NOTE: We allocate twice as much space as we need to actually read a block;
87
* this lets us do audio manipulations without bothering to malloc a second
90
* Also, test to see if we can actually *do* CDDA on this drive; if not, we
91
* need to exit right away so the UI doesn't show the user any CDDA controls.
94
wmcdda_init(char **bufadr, long *buflenadr, int init_fd, char *devname)
96
struct cdrom_cdda cdda;
98
*bufadr = malloc(numblocks * CDDABLKSIZE * 2);
102
*buflenadr = numblocks * CDDABLKSIZE;
104
/*init_fd = open(devname, 0);
106
init_fd = open("/dev/rdsk/c0t6d0s2", 0);
108
init_fd = wmcdda_open(devname);
112
cdda.cdda_addr = 200;
113
cdda.cdda_length = 1;
114
cdda.cdda_data = *bufadr;
115
cdda.cdda_subcode = CDROM_DA_SUBQ;
117
if (ioctl(init_fd, CDROMCDDA, &cdda) < 0)
128
* Try to open the CD device
131
wmcdda_open(char *devname)
135
fd = open(devname, 0);
137
fd = open("/dev/rdsk/c0t6d0s2", 0);
144
* Close the CD-ROM device in preparation for exiting.
153
* Set up for playing the CD. Actually this doesn't play a thing, just sets a
154
* couple variables so we'll know what to do when we're called.
157
wmcdda_setup(int start, int end, int realstart)
159
current_position = start - 150;
160
ending_position = end - 150;
161
starting_position = realstart - 150;
164
* Special case: don't start at the "end" of a track if we're
167
if (direction == -1 && start == realstart)
168
current_position = ending_position - numblocks;
172
* Read some blocks from the CD. Stop if we hit the end of the current region.
174
* Returns number of bytes read, -1 on error, 0 if stopped for a benign reason.
177
wmcdda_read(int fd, unsigned char *rawbuf, long buflen,
178
struct cdda_block *block)
180
struct cdrom_cdda cdda;
185
if ((direction > 0 && current_position >= ending_position) ||
186
(direction < 0 && current_position < starting_position))
188
block->status = WMCDDA_DONE;
192
cdda.cdda_addr = current_position;
193
if (ending_position && current_position + numblocks > ending_position)
194
cdda.cdda_length = ending_position - current_position;
196
cdda.cdda_length = numblocks;
197
cdda.cdda_data = rawbuf;
198
cdda.cdda_subcode = CDROM_DA_SUBQ;
200
if (ioctl(fd, CDROMCDDA, &cdda) < 0)
202
if (errno == ENXIO) /* CD ejected! */
204
block->status = WMCDDA_EJECTED;
208
if (current_position + numblocks > ending_position)
211
* Hit the end of the CD, probably.
213
block->status = WMCDDA_DONE;
217
/* Sometimes it fails once, dunno why */
218
if (ioctl(fd, CDROMCDDA, &cdda) < 0)
220
if (ioctl(fd, CDROMCDDA, &cdda) < 0)
222
if (ioctl(fd, CDROMCDDA, &cdda) < 0)
225
block->status = WMCDDA_ERROR;
235
* We want speed=148 to advance by cdda_length, but
236
* speed=256 to advance cdda_length * 4.
238
current_position = current_position +
239
(cdda.cdda_length * direction * (speed - 112)) / 36;
242
current_position = current_position +
243
cdda.cdda_length * direction;
245
for (blk = 0; blk < numblocks; blk++)
248
* New valid Q-subchannel information? Update the block
251
q = &rawbuf[blk * CDDABLKSIZE + SAMPLES_PER_BLK * 4];
254
block->status = WMCDDA_OK;
255
block->track = unbcd[q[1]];
256
block->index = unbcd[q[2]];
257
block->minute = unbcd[q[7]];
258
block->second = unbcd[q[8]];
259
block->frame = unbcd[q[9]];
263
return (cdda.cdda_length * CDDABLKSIZE);
267
* Normalize a bunch of CDDA data. Basically this means ripping out the
268
* Q subchannel data and doing byte-swapping, since the CD audio is in
269
* littleendian format.
271
* Scanning is handled here too.
273
* XXX - do byte swapping on Intel boxes?
276
wmcdda_normalize(unsigned char *rawbuf, long buflen, struct cdda_block *block)
279
int blocks = buflen / CDDABLKSIZE;
280
unsigned char *dest = rawbuf;
282
long *buf32 = (long *)rawbuf, tmp32;
285
* this was #ifndef LITTLEENDIAN
286
* in wmcdda it was called LITTLE_ENDIAN. Was this a flaw?
290
for (i = 0; i < SAMPLES_PER_BLK * 2; i++)
292
/* Only need to use temp buffer on first block. */
301
/* Skip over Q data. */
304
for (i = 0; i < SAMPLES_PER_BLK * 2; i++)
317
buflen -= ((buflen / CDDABLKSIZE) * 16);
320
* Reverse the data here if we're playing backwards.
321
* XXX - ideally this should be done above.
325
buflen /= 4; /* we can move 32 bits at a time. */
327
for (i = 0; i < buflen / 2; i++)
330
buf32[i] = buf32[buflen - i - 1];
331
buf32[buflen - i - 1] = tmp32;
341
* Set the playback direction.
344
wmcdda_direction(int newdir)
359
* Do system-specific stuff to get ready to play at a particular speed.
362
wmcdda_speed(int speed)
367
numblocks = direction > 0 ? 20 : 30;