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
|
/*
cdda input class based on cdparanoia
Copyright (C) 2000 Martin Vogt
This program 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.
For more information look at the file COPYRIGHT in this package
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#ifdef CDDA_PARANOIA
#include <iostream>
using namespace std;
#include "cddaInputStream.h"
#include "inputDetector.h"
void paranoiaCallback(long, int) {
//cout << "long:"<<a<<" int:"<<b<<endl;
}
CDDAInputStream::CDDAInputStream() {
drive = NULL;
paranoia=NULL;
device=NULL;
track=1;
}
CDDAInputStream::~CDDAInputStream() {
close();
}
// here we should encdoe the track Nr. as well
// eg: /dev/cdrom#1
int CDDAInputStream::getTrackAndDevice(const char* url) {
int matches=0;
// dest = "cdda:/dev/cdrom/track01.cda"
char* noprotoString=InputDetector::removeProtocol(url);
// noprotoString="/dev/cdrom/track01.cda"
char* filename=InputDetector::getFilename(noprotoString);
// filename="track01.cda"
char* filenameNoExt=InputDetector::getWithoutExtension(filename);
// filenameNoExt="track01"
char* dir=InputDetector::removeExtension(noprotoString,filename);
// dir="/dev/cdrom/"
device=InputDetector::removeSlash(dir);
track=1;
if (filenameNoExt != NULL) {
matches=sscanf(filenameNoExt,"track%02d",&track);
}
if (matches == 0) {
cout << "no trackNumber found using default"<<endl;
}
cout << "device:"<<device<<" track:"<<track<<endl;
if (noprotoString != NULL) {
delete noprotoString;
}
if (filename != NULL) {
delete filename;
}
if (filenameNoExt != NULL) {
delete filenameNoExt;
}
if (dir != NULL) {
delete dir;
}
if (device == NULL) {
cout << "no device found, using any"<<endl;
return false;
}
return true;
}
int CDDAInputStream::open(const char* dest) {
if (getTrackAndDevice(dest) == true) {
drive = cdda_identify(device, CDDA_MESSAGE_PRINTIT, 0);
}
if (drive == NULL) {
cout << "cdda_identify failed trying to find a device"<<endl;
drive=cdda_find_a_cdrom(CDDA_MESSAGE_PRINTIT, 0);
}
if (drive == NULL) {
cout << "nope. nothing found. give up"<<endl;
return false;
}
cout << "cdda_open -s"<<endl;
if (cdda_open(drive) != 0) {
cout << "cdda_open(drive) failed"<<endl;
close();
return false;
}
cout << "cdda_open -e"<<endl;
// debug things a bit
int trackCount = drive->tracks;
for (int i = 1; i <= trackCount; i++) {
if (IS_AUDIO(drive, i)) {
printf("track%02d.cda\n", i);
} else {
printf("no audio:%d\n",i);
}
}
paranoia = paranoia_init(drive);
if (paranoia == NULL) {
cout << "paranoia init failed"<<endl;
close();
return false;
}
firstSector=cdda_track_firstsector(drive, track);
lastSector=cdda_track_lastsector(drive, track);
currentSector=firstSector;
// paranoia && drive != NULL -> initialized!
int paranoiaLevel = PARANOIA_MODE_FULL ^ PARANOIA_MODE_NEVERSKIP;
paranoia_modeset(paranoia, paranoiaLevel);
cdda_verbose_set(drive, CDDA_MESSAGE_PRINTIT, CDDA_MESSAGE_PRINTIT);
paranoia_seek(paranoia, firstSector, SEEK_SET);
return true;
}
void CDDAInputStream::close() {
if (isOpen() == false) {
return;
}
cdda_close(drive);
drive=NULL;
if (paranoia != NULL) {
paranoia_free(paranoia);
paranoia = 0;
}
if (device != NULL) {
delete device;
device=NULL;
}
}
int CDDAInputStream::isOpen() {
return (drive != NULL);
}
int CDDAInputStream::eof() {
if (isOpen()==false) {
return true;
}
if (currentSector >= lastSector) {
return true;
}
return false;
}
int CDDAInputStream::read(char* dest,int len) {
if (len != 2*CD_FRAMESIZE_RAW) {
cout << "len must be 2*CD_FRAMESIZE_RAW"<<endl;
exit(0);
}
int16_t * buf = paranoia_read(paranoia, paranoiaCallback);
currentSector++;
if (buf == NULL) {
cout << "paranoia_read failed"<<endl;
close();
return 0;
}
memcpy(dest,buf,sizeof(int16_t)*CD_FRAMESIZE_RAW);
return CD_FRAMESIZE_RAW;
}
int CDDAInputStream::seek(long bytePos) {
int byteLength=getByteLength();
float ratio=(float)bytePos/(float)(byteLength+1);
float wantSector=ratio*(float)((lastSector-firstSector));
if (isOpen()) {
currentSector=(int)wantSector;
cout << "paranoia_seek:"<<currentSector<<endl;
paranoia_seek(paranoia, currentSector, SEEK_SET);
}
return true;
}
void CDDAInputStream::clear() {
cout << "direct virtual call CDDAInputStream::clear:"<<endl;
}
long CDDAInputStream::getByteLength() {
int sectors=lastSector-firstSector;
int bytes=sectors*CD_FRAMESIZE_RAW*sizeof(int16_t);
cout << "getByteLength:"<<bytes<<endl;
return bytes;
}
long CDDAInputStream::getBytePosition() {
int readSectors=currentSector-firstSector;
int bytes=readSectors*CD_FRAMESIZE_RAW*sizeof(int16_t);
return bytes;
}
#endif
//CDDA_PARANOIA
|