File: plat_linux_cdda.c

package info (click to toggle)
workman 1.3.4-18
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,236 kB
  • ctags: 1,187
  • sloc: ansic: 14,623; makefile: 144; sh: 78
file content (364 lines) | stat: -rw-r--r-- 8,221 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
/*
 * $Id: plat_linux_cdda.c,v 1.3.4.1 1999/01/15 08:47:35 dirk Exp $
 *
 * This file is part of WorkMan, the civilized CD player program
 * (c) 1991-1997 by Steven Grimm (original author)
 * (c) by Dirk F"orsterling (current 'author' = maintainer)
 * The maintainer can be contacted by his e-mail address:
 * milliByte@DeathsDoor.com
 *
 * 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 program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * Linux CDDA functions. Derived from the SUN module.
 */

#include "config.h"
 
#if defined(linux) && defined(BUILD_CDDA) /* { */

static char plat_linux_cdda_id[] = "$Id: plat_linux_cdda.c,v 1.3.4.1 1999/01/15 08:47:35 dirk Exp $";


#include "wmcdda.h"
/* types.h and cdio.h are included by wmcdda.h */

#include <stdio.h>
#include <math.h>
#include <sys/ioctl.h>
#include <malloc.h>
#include <errno.h>

#define CDDABLKSIZE 2368
#define SAMPLES_PER_BLK 588

/* Address of next block to read. */
int	current_position = 0;

/* Address of first and last blocks to read. */
int	starting_position = 0;
int	ending_position = 0;

/* Playback direction. */
int	direction = 1;

/* Number of blocks to read at once; initialize to the maximum. */
int	numblocks = 30;

/*
 * This is the fastest way to convert from BCD to 8-bit.
 */
unsigned char unbcd[256] = {
  0,  1,  2,  3,  4,  5,  6,  7,  8,  9,  0,0,0,0,0,0,
 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,  0,0,0,0,0,0,
 20, 21, 22, 23, 24, 25, 26, 27, 28, 29,  0,0,0,0,0,0,
 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,  0,0,0,0,0,0,
 40, 41, 42, 43, 44, 45, 46, 47, 48, 49,  0,0,0,0,0,0,
 50, 51, 52, 53, 54, 55, 56, 57, 58, 59,  0,0,0,0,0,0,
 60, 61, 62, 63, 64, 65, 66, 67, 68, 69,  0,0,0,0,0,0,
 70, 71, 72, 73, 74, 75, 76, 77, 78, 79,  0,0,0,0,0,0,
 80, 81, 82, 83, 84, 85, 86, 87, 88, 89,  0,0,0,0,0,0,
 90, 91, 92, 93, 94, 95, 96, 97, 98, 99,  0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0
};

/*
 * Initialize the CDDA data buffer and open the appropriate device.
 *
 * NOTE: We allocate twice as much space as we need to actually read a block;
 * this lets us do audio manipulations without bothering to malloc a second
 * buffer.
 *
 * Also, test to see if we can actually *do* CDDA on this drive; if not, we
 * need to exit right away so the UI doesn't show the user any CDDA controls.
 */
int
wmcdda_init(char **bufadr, long *buflenadr, int init_fd, char *devname)
{
	struct cdrom_cdda	cdda;

	*bufadr = malloc(numblocks * CDDABLKSIZE * 2);
	if (*bufadr == NULL)
		return (-1);

	*buflenadr = numblocks * CDDABLKSIZE;

	/*init_fd = open(devname, 0);
	if (init_fd == -1)
		init_fd = open("/dev/rdsk/c0t6d0s2", 0);
	*/
	init_fd = wmcdda_open(devname);

	if (init_fd > -1)
	{
		cdda.cdda_addr = 200;
		cdda.cdda_length = 1;
		cdda.cdda_data = *bufadr;
		cdda.cdda_subcode = CDROM_DA_SUBQ;

		if (ioctl(init_fd, CDROMCDDA, &cdda) < 0)
		{
			close(init_fd);
			init_fd = -1;
		}
	}

	return (init_fd);
}

/*
 * Try to open the CD device
 */
int
wmcdda_open(char *devname)
{
    int fd;

    fd = open(devname, 0); 
    if (fd == -1)
	fd = open("/dev/rdsk/c0t6d0s2", 0);

    return(fd);
}


/*
 * Close the CD-ROM device in preparation for exiting.
 */
void
wmcdda_close(int fd)
{
	close(fd);
}

/*
 * Set up for playing the CD.  Actually this doesn't play a thing, just sets a
 * couple variables so we'll know what to do when we're called.
 */
void
wmcdda_setup(int start, int end, int realstart)
{
	current_position = start - 150;
	ending_position = end - 150;
	starting_position = realstart - 150;

	/*
	 * Special case: don't start at the "end" of a track if we're
	 * playing backwards!
	 */
	if (direction == -1 && start == realstart)
		current_position = ending_position - numblocks;
}

/*
 * Read some blocks from the CD.  Stop if we hit the end of the current region.
 *
 * Returns number of bytes read, -1 on error, 0 if stopped for a benign reason.
 */
long
wmcdda_read(int fd, unsigned char *rawbuf, long buflen,
	struct cdda_block *block)
{
	struct cdrom_cdda	cdda;
	int			blk;
	unsigned char		*q;
	extern int		speed;

	if ((direction > 0 && current_position >= ending_position) ||
	    (direction < 0 && current_position < starting_position))
	{
		block->status = WMCDDA_DONE;
		return (0);
	}

	cdda.cdda_addr = current_position;
	if (ending_position && current_position + numblocks > ending_position)
		cdda.cdda_length = ending_position - current_position;
	else
		cdda.cdda_length = numblocks;
	cdda.cdda_data = rawbuf;
	cdda.cdda_subcode = CDROM_DA_SUBQ;

	if (ioctl(fd, CDROMCDDA, &cdda) < 0)
	{
		if (errno == ENXIO)	/* CD ejected! */
		{
			block->status = WMCDDA_EJECTED;
			return (-1);
		}

		if (current_position + numblocks > ending_position)
		{
			/*
			 * Hit the end of the CD, probably.
			 */
			block->status = WMCDDA_DONE;
			return (0);
		}

		/* Sometimes it fails once, dunno why */
		if (ioctl(fd, CDROMCDDA, &cdda) < 0)
		{
			if (ioctl(fd, CDROMCDDA, &cdda) < 0)
			{
				if (ioctl(fd, CDROMCDDA, &cdda) < 0)
				{
					perror("CDROMCDDA");
					block->status = WMCDDA_ERROR;
					return (-1);
				}
			}
		}
	}

	if (speed > 148)
	{
		/*
		 * We want speed=148 to advance by cdda_length, but
		 * speed=256 to advance cdda_length * 4.
		 */
		current_position = current_position +
			(cdda.cdda_length * direction * (speed - 112)) / 36;
	}
	else
		current_position = current_position +
			cdda.cdda_length * direction;

	for (blk = 0; blk < numblocks; blk++)
	{
		/*
		 * New valid Q-subchannel information?  Update the block
		 * status.
		 */
		q = &rawbuf[blk * CDDABLKSIZE + SAMPLES_PER_BLK * 4];
		if (*q == 1)
		{
			block->status = WMCDDA_OK;
			block->track =  unbcd[q[1]];
			block->index =  unbcd[q[2]];
			block->minute = unbcd[q[7]];
			block->second = unbcd[q[8]];
			block->frame =  unbcd[q[9]];
		}
	}

	return (cdda.cdda_length * CDDABLKSIZE);
}

/*
 * Normalize a bunch of CDDA data.  Basically this means ripping out the
 * Q subchannel data and doing byte-swapping, since the CD audio is in
 * littleendian format.
 *
 * Scanning is handled here too.
 *
 * XXX - do byte swapping on Intel boxes?
 */
long
wmcdda_normalize(unsigned char *rawbuf, long buflen, struct cdda_block *block)
{
	int		i, nextq;
	int		blocks = buflen / CDDABLKSIZE;
	unsigned char	*dest = rawbuf;
	unsigned char	tmp;
	long		*buf32 = (long *)rawbuf, tmp32;

/*
 * this was #ifndef LITTLEENDIAN
 * in wmcdda it was called LITTLE_ENDIAN. Was this a flaw?
 */
#if WM_BIG_ENDIAN
	if (blocks--)
		for (i = 0; i < SAMPLES_PER_BLK * 2; i++)
		{
			/* Only need to use temp buffer on first block. */
			tmp = *rawbuf++;
			*dest++ = *rawbuf++;
			*dest++ = tmp;
		}
#endif

	while (blocks--)
	{
		/* Skip over Q data. */
		rawbuf += 16;

		for (i = 0; i < SAMPLES_PER_BLK * 2; i++)
		{
#if WM_LITTLE_ENDIAN
			*dest++ = *rawbuf++;
			*dest++ = *rawbuf++;
#else
			*dest++ = rawbuf[1];
			*dest++ = rawbuf[0];
			rawbuf += 2;
#endif
		}
	}

	buflen -= ((buflen / CDDABLKSIZE) * 16);

	/*
	 * Reverse the data here if we're playing backwards.
	 * XXX - ideally this should be done above.
	 */
	if (direction < 0)
	{
		buflen /= 4;	/* we can move 32 bits at a time. */

		for (i = 0; i < buflen / 2; i++)
		{
			tmp32 = buf32[i];
			buf32[i] = buf32[buflen - i - 1];
			buf32[buflen - i - 1] = tmp32;
		}

		buflen *= 4;
	}

	return (buflen);
}

/*
 * Set the playback direction.
 */
void
wmcdda_direction(int newdir)
{
	if (newdir == 0)
	{
		numblocks = 20;
		direction = 1;
	}
	else
	{
		numblocks = 30;
		direction = -1;
	}
}

/*
 * Do system-specific stuff to get ready to play at a particular speed.
 */
void
wmcdda_speed(int speed)
{
	if (speed > 128)
		numblocks = 12;
	else
		numblocks = direction > 0 ? 20 : 30;
}

#endif /* } */