~ubuntu-branches/ubuntu/quantal/libkcompactdisc/quantal-updates

« back to all changes in this revision

Viewing changes to wmlib/plat_sun_cdda.c

  • Committer: Package Import Robot
  • Author(s): Philip Muškovac
  • Date: 2012-05-26 15:25:35 UTC
  • Revision ID: package-import@ubuntu.com-20120526152535-60v997qsp1solymv
Tags: upstream-4.8.80a
Import upstream version 4.8.80a

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
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>
 
5
 *
 
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.
 
10
 *
 
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.
 
15
 *
 
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
 
19
 *
 
20
 *
 
21
 * Sun (really Solaris) CDDA functions.
 
22
 */
 
23
 
 
24
#include "include/wm_cdda.h"
 
25
 
 
26
#if defined(sun) || defined(__sun__) && defined(SYSV)
 
27
 
 
28
 
 
29
#include "include/wm_struct.h"
 
30
#include "include/wm_cdda.h"
 
31
/* types.h and cdio.h are included by wm_cdda.h */
 
32
 
 
33
#include <stdio.h>
 
34
#include <math.h>
 
35
#include <sys/ioctl.h>
 
36
#include <malloc.h>
 
37
#include <errno.h>
 
38
 
 
39
#define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
 
40
 
 
41
#define CDDABLKSIZE 2368
 
42
#define SAMPLES_PER_BLK 588
 
43
 
 
44
/* Address of next block to read. */
 
45
int     current_position = 0;
 
46
 
 
47
/* Address of first and last blocks to read. */
 
48
int     starting_position = 0;
 
49
int     ending_position = 0;
 
50
 
 
51
/* Playback direction. */
 
52
int     direction = 1;
 
53
 
 
54
/* Number of blocks to read at once; initialize to the maximum. */
 
55
/* (was 30. Set to 15 for INTeL. Maybe config option? */
 
56
int     numblocks = 15;
 
57
 
 
58
/*
 
59
 * This is the fastest way to convert from BCD to 8-bit.
 
60
 */
 
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
 
78
};
 
79
 
 
80
static long wmcdda_normalize(struct cdda_block *block);
 
81
 
 
82
/*
 
83
 * Initialize the CDDA data buffer and open the appropriate device.
 
84
 *
 
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
 
87
 * buffer.
 
88
 *
 
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.
 
91
 */
 
92
int
 
93
wmcdda_init(struct cdda_device* pdev, struct cdda_block *block)
 
94
{
 
95
  struct cdrom_cdda     cdda;
 
96
  int i;
 
97
 
 
98
  if (pdev->fd > -1)
 
99
    return -1;
 
100
 
 
101
  for (i = 0; i < pdev->numblocks; i++) {
 
102
    /* in Linux const */
 
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)
 
106
      return -ENOMEM;
 
107
  }
 
108
 
 
109
  pdev->fd = open(pdev->devname, 0);
 
110
  if (pdev->fd == -1)
 
111
    pdev->fd = open("/dev/rdsk/c0t6d0s2", 0);
 
112
 
 
113
        if (pdev->fd > -1)
 
114
        {
 
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;
 
119
 
 
120
                if (ioctl(pdev->fd, CDROMCDDA, &cdda) < 0)
 
121
                {
 
122
                        block->status = WM_CDM_STOPPED;
 
123
                        return -1;
 
124
                } else {
 
125
                  block->status = WM_CDM_STOPPED;
 
126
                  return 0;
 
127
                }
 
128
        } else {
 
129
            block->status = WM_CDM_EJECTED;
 
130
            return -1;
 
131
        }
 
132
}
 
133
 
 
134
/*
 
135
 * Close the CD-ROM device in preparation for exiting.
 
136
 */
 
137
int
 
138
wmcdda_close(struct cdda_device* pdev)
 
139
{
 
140
    int i;
 
141
 
 
142
    if(-1 == pdev->fd)
 
143
        return -1;
 
144
 
 
145
    close(pdev->fd);
 
146
    pdev->fd = -1;
 
147
 
 
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;
 
152
    }
 
153
 
 
154
    return 0;
 
155
}
 
156
 
 
157
/*
 
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.
 
160
 */
 
161
int
 
162
wmcdda_setup(int start, int end)
 
163
{
 
164
        current_position = start - 150;
 
165
        ending_position = end - 150;
 
166
 
 
167
        /*
 
168
         * Special case: don't start at the "end" of a track if we're
 
169
         * playing backwards!
 
170
         */
 
171
        if (direction == -1)
 
172
                current_position = ending_position - numblocks;
 
173
  return 0;
 
174
}
 
175
 
 
176
/*
 
177
 * Read some blocks from the CD.  Stop if we hit the end of the current region.
 
178
 *
 
179
 * Returns number of bytes read, -1 on error, 0 if stopped for a benign reason.
 
180
 */
 
181
long
 
182
wmcdda_read(struct cdda_device* pdev, struct cdda_block *block)
 
183
{
 
184
    struct cdrom_cdda   cdda;
 
185
    int                 blk;
 
186
    unsigned char       *q;
 
187
    extern int          speed;
 
188
    unsigned char*      rawbuf = block->buf;
 
189
 
 
190
    if(pdev->fd < 0 && (wmcdda_init(pdev, block) < 0)) {
 
191
        return -1;
 
192
    }
 
193
 
 
194
    /*
 
195
     * Hit the end of the CD, probably.
 
196
     */
 
197
    if ((direction > 0 && current_position >= ending_position) ||
 
198
        (direction < 0 && current_position < starting_position))
 
199
    {
 
200
        block->status = WM_CDM_TRACK_DONE;
 
201
        return (0);
 
202
    }
 
203
 
 
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;
 
207
    else
 
208
        cdda.cdda_length = pdev->frames_at_once;
 
209
    cdda.cdda_data = (unsigned char*)block->buf;
 
210
    cdda.cdda_subcode = CDROM_DA_SUBQ;
 
211
 
 
212
    if (ioctl(pdev->fd, CDROMCDDA, &cdda) < 0)
 
213
    {
 
214
        if (errno == ENXIO)     /* CD ejected! */
 
215
        {
 
216
            block->status = WM_CDM_EJECTED;
 
217
            return (-1);
 
218
        }
 
219
 
 
220
        /* Sometimes it fails once, dunno why */
 
221
        if (ioctl(pdev->fd, CDROMCDDA, &cdda) < 0)
 
222
        {
 
223
            if (ioctl(pdev->fd, CDROMCDDA, &cdda) < 0)
 
224
            {
 
225
                if (ioctl(pdev->fd, CDROMCDDA, &cdda) < 0)
 
226
                {
 
227
                    perror("CDROMCDDA");
 
228
                    block->status = WM_CDM_CDDAERROR;
 
229
                    return (-1);
 
230
                }
 
231
            }
 
232
        }
 
233
    }
 
234
 
 
235
    if (speed > 148)
 
236
    {
 
237
        /*
 
238
         * We want speed=148 to advance by cdda_length, but
 
239
         * speed=256 to advance cdda_length * 4.
 
240
         */
 
241
        current_position = current_position +
 
242
            (cdda.cdda_length * direction * (speed - 112)) / 36;
 
243
    }
 
244
    else
 
245
        current_position = current_position + cdda.cdda_length * direction;
 
246
 
 
247
    for (blk = 0; blk < numblocks; blk++)
 
248
    {
 
249
        /*
 
250
         * New valid Q-subchannel information?  Update the block
 
251
         * status.
 
252
         */
 
253
        q = &rawbuf[blk * CDDABLKSIZE + SAMPLES_PER_BLK * 4];
 
254
        if (*q == 1)
 
255
        {
 
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;
 
263
        }
 
264
    }
 
265
 
 
266
    return wmcdda_normalize(block);
 
267
}
 
268
 
 
269
/*
 
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.
 
273
 *
 
274
 * Scanning is handled here too.
 
275
 *
 
276
 * XXX - do byte swapping on Intel boxes?
 
277
 */
 
278
long
 
279
wmcdda_normalize(struct cdda_block *block)
 
280
{
 
281
        int             i, nextq;
 
282
        long buflen = block->buflen;
 
283
        int             blocks = buflen / CDDABLKSIZE;
 
284
        unsigned char *rawbuf = block->buf;
 
285
        unsigned char   *dest = rawbuf;
 
286
        unsigned char   tmp;
 
287
        long            *buf32 = (long *)rawbuf, tmp32;
 
288
 
 
289
/*
 
290
 * this was #ifndef LITTLEENDIAN
 
291
 * in wmcdda it was called LITTLE_ENDIAN. Was this a flaw?
 
292
 */
 
293
#if WM_BIG_ENDIAN
 
294
        if (blocks--)
 
295
                for (i = 0; i < SAMPLES_PER_BLK * 2; i++)
 
296
                {
 
297
                        /* Only need to use temp buffer on first block. */
 
298
                        tmp = *rawbuf++;
 
299
                        *dest++ = *rawbuf++;
 
300
                        *dest++ = tmp;
 
301
                }
 
302
#endif
 
303
 
 
304
        while (blocks--)
 
305
        {
 
306
                /* Skip over Q data. */
 
307
                rawbuf += 16;
 
308
 
 
309
                for (i = 0; i < SAMPLES_PER_BLK * 2; i++)
 
310
                {
 
311
#if WM_LITTLE_ENDIAN
 
312
                        *dest++ = *rawbuf++;
 
313
                        *dest++ = *rawbuf++;
 
314
#else
 
315
                        *dest++ = rawbuf[1];
 
316
                        *dest++ = rawbuf[0];
 
317
                        rawbuf += 2;
 
318
#endif
 
319
                }
 
320
        }
 
321
 
 
322
        buflen -= ((buflen / CDDABLKSIZE) * 16);
 
323
 
 
324
        /*
 
325
         * Reverse the data here if we're playing backwards.
 
326
         * XXX - ideally this should be done above.
 
327
         */
 
328
        if (direction < 0)
 
329
        {
 
330
                buflen /= 4;    /* we can move 32 bits at a time. */
 
331
 
 
332
                for (i = 0; i < buflen / 2; i++)
 
333
                {
 
334
                        tmp32 = buf32[i];
 
335
                        buf32[i] = buf32[buflen - i - 1];
 
336
                        buf32[buflen - i - 1] = tmp32;
 
337
                }
 
338
 
 
339
                buflen *= 4;
 
340
        }
 
341
 
 
342
        return (buflen);
 
343
}
 
344
 
 
345
/*
 
346
 * Set the playback direction.
 
347
 */
 
348
void
 
349
wmcdda_direction(int newdir)
 
350
{
 
351
        if (newdir == 0)
 
352
        {
 
353
                numblocks = 20;
 
354
                direction = 1;
 
355
        }
 
356
        else
 
357
        {
 
358
                numblocks = 30;
 
359
                direction = -1;
 
360
        }
 
361
}
 
362
 
 
363
/*
 
364
 * Do system-specific stuff to get ready to play at a particular speed.
 
365
 */
 
366
void
 
367
wmcdda_speed(int speed)
 
368
{
 
369
        if (speed > 128)
 
370
                numblocks = 12;
 
371
        else
 
372
                numblocks = direction > 0 ? 20 : 30;
 
373
}
 
374
 
 
375
#endif /* } */