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
|
/*
* $Id$
*
* This file is part of WorkMan, the civilized CD player library
* (c) 1991-1997 by Steven Grimm (original author)
* (c) by Dirk F�sterling (current 'author' = maintainer)
* The maintainer can be contacted by his e-mail address:
* milliByte@DeathsDoor.com
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Library General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library 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
* Library General Public License for more details.
*
* You should have received a copy of the GNU Library General Public
* License along with this library; if not, write to the Free
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*
*
* Linux CDDA functions. Derived from the SUN module.
*/
#include "include/wm_cdda.h"
#if defined(__linux__) && defined(BUILD_CDDA)
#ifndef __GNUC__
#define __GNUC__ 1
#endif
/* needed for vanilla kernel headers, which do provide __u64 only
for ansi */
#undef __STRICT_ANSI__
/* needed for non-ansi kernel headers */
#define asm __asm__
#define inline __inline__
#include <asm/types.h>
#include <linux/cdrom.h>
#undef asm
#undef inline
#include <stdio.h>
#include <math.h>
#include <sys/ioctl.h>
#include <sys/types.h>
#include <fcntl.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include "include/wm_struct.h"
#include "include/wm_cdda.h"
#define WM_MSG_CLASS WM_MSG_CLASS_PLATFORM
#define CDDABLKSIZE 2352
/* Address of next block to read. */
static int current_position;
/* Address of last block to read. */
static int ending_position;
static struct cdrom_read_audio cdda;
static long wmcdda_normalize(struct cdda_block *block);
/*
* Initialize the CDDA data buffer and open the appropriate device.
*
*/
int
wmcdda_init(struct cdda_device* pdev)
{
int i;
if (pdev->fd > -1)
return -1;
if(!pdev->devname)
return -1;
for (i = 0; i < pdev->numblocks; i++) {
/* in Linux const */
pdev->blocks[i].buflen = pdev->frames_at_once * CDDABLKSIZE;
pdev->blocks[i].buf = malloc(pdev->blocks[i].buflen);
if (!pdev->blocks[i].buf) {
ERRORLOG("wmcdda_init ENOMEM\n");
return -ENOMEM;
}
}
pdev->fd = open(pdev->devname, O_RDONLY | O_NONBLOCK);
if (pdev->fd > -1) {
cdda.addr_format = CDROM_LBA;
cdda.addr.lba = 200;
cdda.nframes = 1;
cdda.buf = (unsigned char*)pdev->blocks[0].buf;
pdev->status = WM_CDM_STOPPED;
if((ioctl(pdev->fd, CDROMREADAUDIO, &cdda) < 0)) {
if (errno == ENXIO) {
/* CD ejected! */
pdev->status = WM_CDM_EJECTED;
return 0;
} else {
/* Sometimes it fails once, dunno why */
pdev->status = WM_CDM_CDDAERROR;
return 0;
}
}
} else {
ERRORLOG("canot open device, errno %i\n", errno);
pdev->status = WM_CDM_UNKNOWN;
return -1;
}
pdev->status = WM_CDM_UNKNOWN;
return 0;
}
/*
* Close the CD-ROM device in preparation for exiting.
*/
int
wmcdda_close(struct cdda_device* pdev)
{
int i;
if(-1 == pdev->fd)
return -1;
close(pdev->fd);
pdev->fd = -1;
for (i = 0; i < pdev->numblocks; i++) {
free(pdev->blocks[i].buf);
pdev->blocks[i].buf = 0;
pdev->blocks[i].buflen = 0;
}
return 0;
}
/*
* 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.
*/
int
wmcdda_setup(int start, int end, int realstart)
{
current_position = start;
ending_position = end;
return 0;
}
/*
* 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(struct cdda_device* pdev, struct cdda_block *block)
{
if (pdev->fd < 0 && wmcdda_init(pdev)) {
return -1;
}
/* Hit the end of the CD, probably. */
if (current_position >= ending_position) {
block->status = WM_CDM_TRACK_DONE;
return 0;
}
cdda.addr_format = CDROM_LBA;
cdda.addr.lba = current_position - CD_MSF_OFFSET;
if (ending_position && current_position + pdev->frames_at_once > ending_position)
cdda.nframes = ending_position - current_position;
else
cdda.nframes = pdev->frames_at_once;
cdda.buf = (unsigned char*)block->buf;
if (ioctl(pdev->fd, CDROMREADAUDIO, &cdda) < 0) {
if (errno == ENXIO) {
/* CD ejected! */
block->status = WM_CDM_EJECTED;
return 0;
} else {
/* Sometimes it fails once, dunno why */
block->status = WM_CDM_CDDAERROR;
return 0;
}
}
block->track = -1;
block->index = 0;
block->frame = current_position;
block->status = WM_CDM_PLAYING;
block->buflen = cdda.nframes * CDDABLKSIZE;
current_position = current_position + cdda.nframes;
return wmcdda_normalize(block);
}
/*
* Normalize a bunch of CDDA data. Basically this means doing byte-swapping, since the CD audio is in
* littleendian format.
*/
long
wmcdda_normalize(struct cdda_block *block)
{
#if WM_BIG_ENDIAN
int i;
int blocks = block->buflen / CDDABLKSIZE;
char *rawbuf = block->buf;
char *dest = block->buf;
while (blocks--) {
for (i = 0; i < CDDABLKSIZE / 2; i++) {
*dest++ = rawbuf[1];
*dest++ = rawbuf[0];
rawbuf += 2;
}
}
#endif
return block->buflen;
}
/*
* Set the playback direction.
*/
void
wmcdda_direction(int newdir)
{
}
/*
* Do system-specific stuff to get ready to play at a particular speed.
*/
void
wmcdda_speed(int speed)
{
}
#endif
|