2
* This file is part of WorkMan, the civilized CD player library
3
* Copyright (C) 1991-1997 by Steven Grimm <koreth@midwinter.com>
4
* Copyright (C) by Dirk Försterling <milliByte@DeathsDoor.com>
6
* This library is free software; you can redistribute it and/or
7
* modify it under the terms of the GNU Library General Public
8
* License as published by the Free Software Foundation; either
9
* version 2 of the License, or (at your option) any later version.
11
* This library is distributed in the hope that it will be useful,
12
* but WITHOUT ANY WARRANTY; without even the implied warranty of
13
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14
* Library General Public License for more details.
16
* You should have received a copy of the GNU Library General Public
17
* License along with this library; if not, write to the Free
18
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21
* Sun (really Solaris) CDDA functions.
24
#include "include/wm_cdda.h"
26
#if defined(sun) || defined(__sun__) && defined(SYSV)
29
#include "include/wm_struct.h"
30
#include "include/wm_cdda.h"
31
/* types.h and cdio.h are included by wm_cdda.h */
35
#include <sys/ioctl.h>
39
#define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
41
#define CDDABLKSIZE 2368
42
#define SAMPLES_PER_BLK 588
44
/* Address of next block to read. */
45
int current_position = 0;
47
/* Address of first and last blocks to read. */
48
int starting_position = 0;
49
int ending_position = 0;
51
/* Playback direction. */
54
/* Number of blocks to read at once; initialize to the maximum. */
55
/* (was 30. Set to 15 for INTeL. Maybe config option? */
59
* This is the fastest way to convert from BCD to 8-bit.
61
unsigned char unbcd[256] = {
62
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0,0,0,0,0,0,
63
10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 0,0,0,0,0,0,
64
20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 0,0,0,0,0,0,
65
30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 0,0,0,0,0,0,
66
40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 0,0,0,0,0,0,
67
50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 0,0,0,0,0,0,
68
60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 0,0,0,0,0,0,
69
70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 0,0,0,0,0,0,
70
80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 0,0,0,0,0,0,
71
90, 91, 92, 93, 94, 95, 96, 97, 98, 99, 0,0,0,0,0,0,
72
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
73
0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
74
0,0,0,0,0,0,0,0,0,0,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
80
static long wmcdda_normalize(struct cdda_block *block);
83
* Initialize the CDDA data buffer and open the appropriate device.
85
* NOTE: We allocate twice as much space as we need to actually read a block;
86
* this lets us do audio manipulations without bothering to malloc a second
89
* Also, test to see if we can actually *do* CDDA on this drive; if not, we
90
* need to exit right away so the UI doesn't show the user any CDDA controls.
93
wmcdda_init(struct cdda_device* pdev, struct cdda_block *block)
95
struct cdrom_cdda cdda;
101
for (i = 0; i < pdev->numblocks; i++) {
103
pdev->blocks[i].buflen = pdev->frames_at_once * CDDABLKSIZE;
104
pdev->blocks[i].buf = malloc(pdev->blocks[i].buflen);
105
if (!pdev->blocks[i].buf)
109
pdev->fd = open(pdev->devname, 0);
111
pdev->fd = open("/dev/rdsk/c0t6d0s2", 0);
115
cdda.cdda_addr = 200;
116
cdda.cdda_length = 1;
117
cdda.cdda_data = pdev->blocks[0].buf;
118
cdda.cdda_subcode = CDROM_DA_SUBQ;
120
if (ioctl(pdev->fd, CDROMCDDA, &cdda) < 0)
122
block->status = WM_CDM_STOPPED;
125
block->status = WM_CDM_STOPPED;
129
block->status = WM_CDM_EJECTED;
135
* Close the CD-ROM device in preparation for exiting.
138
wmcdda_close(struct cdda_device* pdev)
148
for (i = 0; i < pdev->numblocks; i++) {
149
free(pdev->blocks[i].buf);
150
pdev->blocks[i].buf = 0;
151
pdev->blocks[i].buflen = 0;
158
* Set up for playing the CD. Actually this doesn't play a thing, just sets a
159
* couple variables so we'll know what to do when we're called.
162
wmcdda_setup(int start, int end)
164
current_position = start - 150;
165
ending_position = end - 150;
168
* Special case: don't start at the "end" of a track if we're
172
current_position = ending_position - numblocks;
177
* Read some blocks from the CD. Stop if we hit the end of the current region.
179
* Returns number of bytes read, -1 on error, 0 if stopped for a benign reason.
182
wmcdda_read(struct cdda_device* pdev, struct cdda_block *block)
184
struct cdrom_cdda cdda;
188
unsigned char* rawbuf = block->buf;
190
if(pdev->fd < 0 && (wmcdda_init(pdev, block) < 0)) {
195
* Hit the end of the CD, probably.
197
if ((direction > 0 && current_position >= ending_position) ||
198
(direction < 0 && current_position < starting_position))
200
block->status = WM_CDM_TRACK_DONE;
204
cdda.cdda_addr = current_position;
205
if (ending_position && current_position + pdev->frames_at_once > ending_position)
206
cdda.cdda_length = ending_position - current_position;
208
cdda.cdda_length = pdev->frames_at_once;
209
cdda.cdda_data = (unsigned char*)block->buf;
210
cdda.cdda_subcode = CDROM_DA_SUBQ;
212
if (ioctl(pdev->fd, CDROMCDDA, &cdda) < 0)
214
if (errno == ENXIO) /* CD ejected! */
216
block->status = WM_CDM_EJECTED;
220
/* Sometimes it fails once, dunno why */
221
if (ioctl(pdev->fd, CDROMCDDA, &cdda) < 0)
223
if (ioctl(pdev->fd, CDROMCDDA, &cdda) < 0)
225
if (ioctl(pdev->fd, CDROMCDDA, &cdda) < 0)
228
block->status = WM_CDM_CDDAERROR;
238
* We want speed=148 to advance by cdda_length, but
239
* speed=256 to advance cdda_length * 4.
241
current_position = current_position +
242
(cdda.cdda_length * direction * (speed - 112)) / 36;
245
current_position = current_position + cdda.cdda_length * direction;
247
for (blk = 0; blk < numblocks; blk++)
250
* New valid Q-subchannel information? Update the block
253
q = &rawbuf[blk * CDDABLKSIZE + SAMPLES_PER_BLK * 4];
256
block->track = unbcd[q[1]];
257
block->index = unbcd[q[2]];
258
/*block->minute = unbcd[q[7]];
259
block->second = unbcd[q[8]];*/
260
block->frame = unbcd[q[9]];
261
block->status = WM_CDM_PLAYING;
262
block->buflen = cdda.cdda_length;
266
return wmcdda_normalize(block);
270
* Normalize a bunch of CDDA data. Basically this means ripping out the
271
* Q subchannel data and doing byte-swapping, since the CD audio is in
272
* littleendian format.
274
* Scanning is handled here too.
276
* XXX - do byte swapping on Intel boxes?
279
wmcdda_normalize(struct cdda_block *block)
282
long buflen = block->buflen;
283
int blocks = buflen / CDDABLKSIZE;
284
unsigned char *rawbuf = block->buf;
285
unsigned char *dest = rawbuf;
287
long *buf32 = (long *)rawbuf, tmp32;
290
* this was #ifndef LITTLEENDIAN
291
* in wmcdda it was called LITTLE_ENDIAN. Was this a flaw?
295
for (i = 0; i < SAMPLES_PER_BLK * 2; i++)
297
/* Only need to use temp buffer on first block. */
306
/* Skip over Q data. */
309
for (i = 0; i < SAMPLES_PER_BLK * 2; i++)
322
buflen -= ((buflen / CDDABLKSIZE) * 16);
325
* Reverse the data here if we're playing backwards.
326
* XXX - ideally this should be done above.
330
buflen /= 4; /* we can move 32 bits at a time. */
332
for (i = 0; i < buflen / 2; i++)
335
buf32[i] = buf32[buflen - i - 1];
336
buf32[buflen - i - 1] = tmp32;
346
* Set the playback direction.
349
wmcdda_direction(int newdir)
364
* Do system-specific stuff to get ready to play at a particular speed.
367
wmcdda_speed(int speed)
372
numblocks = direction > 0 ? 20 : 30;