-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcd_extractor.cpp
More file actions
129 lines (117 loc) · 3.11 KB
/
Copy pathcd_extractor.cpp
File metadata and controls
129 lines (117 loc) · 3.11 KB
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
#include <unistd.h>
#include "cd_extractor.h"
//#include <QFile>
#include <QEventLoop>
const int CD_Framesize_Raw = 2352;
CD_Extractor::CD_Extractor(QObject *parent) :
QObject(parent)
{
Buffer.clear();
}
void CD_Extractor::Initialize()
{
char ** ppsz_message;
int x;
SectorsRead = 0;
SectorsTotal = 0;
Drive = cdio_cddap_identify(NULL,1,ppsz_message);
if (Drive != NULL)
{
if (0 == cdio_cddap_open(Drive))
{
//Paranoia = cdio_paranoia_init(Drive);
x = cdio_cddap_tracks(Drive);
DriveFound();
}
}
else
DriveNotFound();
}
long CD_Extractor::Progress()
{
if ((SectorsRead == 0) || (SectorsTotal == 0))
return 0;
else
return ((double)SectorsRead / (double)SectorsTotal) * 100;
}
void CD_Extractor::Extract(int tracknum)
{
int size;
char *buf;
int16_t *readresult;
long result;
long x;
long firstsector,lastsector;
long i;
QString fname;
QByteArray arr;
QEventLoop evloop;
Paranoia = cdio_paranoia_init(Drive);
firstsector = cdio_cddap_track_firstsector(Drive, tracknum);
lastsector = cdio_cddap_track_lastsector(Drive, tracknum);
size = (lastsector - firstsector) + 1;
SectorsTotal = size;
SectorsRead = 0;
cdio_cddap_speed_set(Drive,32);
paranoia_modeset(Paranoia, PARANOIA_MODE_FULL); //^PARANOIA_MODE_NEVERSKIP);
paranoia_seek(Paranoia, firstsector, SEEK_SET);
QFile file("Track" + QString::number(tracknum) + ".wav");
file.open(QIODevice::WriteOnly);
WriteWav(file.handle(),size * CD_Framesize_Raw);
for (i = firstsector; i <= lastsector; i++)
{
try
{
//readresult = cdio_paranoia_read(Paranoia,NULL);
readresult = cdio_paranoia_read_limited(Paranoia,NULL,3);
SectorsRead++;
buf = (char*)readresult;
file.write(buf,CD_Framesize_Raw);
UpdateProgress(Progress());
evloop.processEvents();
}
catch(...)
{
break;
}
}
fname = file.fileName();
file.close();
cdio_paranoia_free(Paranoia);
ExtractFinished(fname);
}
void CD_Extractor::WriteWav(int f,long bytes)
{
/* quick and dirty */
write(f,"RIFF",4); /* 0-3 */
PutNum(bytes+44-8,f,0,4); /* 4-7 */
write(f,"WAVEfmt ",8); /* 8-15 */
PutNum(16,f,0,4); /* 16-19 */
PutNum(1,f,0,2); /* 20-21 */
PutNum(2,f,0,2); /* 22-23 */
PutNum(44100,f,0,4); /* 24-27 */
PutNum(44100*2*2,f,0,4); /* 28-31 */
PutNum(4,f,0,2); /* 32-33 */
PutNum(16,f,0,2); /* 34-35 */
write(f,"data",4); /* 36-39 */
PutNum(bytes,f,0,4); /* 40-43 */
}
void CD_Extractor::PutNum(long num,int f,int endianness,int bytes){
int i;
unsigned char c;
if(!endianness)
i=0;
else
i=bytes-1;
while(bytes--){
c=(num>>(i<<3))&0xff;
if(write(f,&c,1)==-1){
perror("Could not write to output.");
return;
}
if(endianness)
i--;
else
i++;
}
}