-rw-r--r-- | noncore/multimedia/opierec/wavFile.cpp | 2 |
1 files changed, 1 insertions, 1 deletions
diff --git a/noncore/multimedia/opierec/wavFile.cpp b/noncore/multimedia/opierec/wavFile.cpp index 360b884..80453e1 100644 --- a/noncore/multimedia/opierec/wavFile.cpp +++ b/noncore/multimedia/opierec/wavFile.cpp | |||
@@ -1,314 +1,314 @@ | |||
1 | //wavFile.cpp | 1 | //wavFile.cpp |
2 | #include "wavFile.h" | 2 | #include "wavFile.h" |
3 | #include "qtrec.h" | 3 | #include "qtrec.h" |
4 | 4 | ||
5 | /* OPIE */ | 5 | /* OPIE */ |
6 | #include <opie2/odebug.h> | 6 | #include <opie2/odebug.h> |
7 | #include <qpe/config.h> | 7 | #include <qpe/config.h> |
8 | using namespace Opie::Core; | 8 | using namespace Opie::Core; |
9 | 9 | ||
10 | /* QT */ | 10 | /* QT */ |
11 | #include <qmessagebox.h> | 11 | #include <qmessagebox.h> |
12 | #include <qdir.h> | 12 | #include <qdir.h> |
13 | 13 | ||
14 | /* STD */ | 14 | /* STD */ |
15 | #include <errno.h> | 15 | #include <errno.h> |
16 | #include <sys/time.h> | 16 | #include <sys/time.h> |
17 | #include <sys/types.h> | 17 | #include <sys/types.h> |
18 | #include <sys/vfs.h> | 18 | #include <sys/vfs.h> |
19 | #include <fcntl.h> | 19 | #include <fcntl.h> |
20 | #include <math.h> | 20 | #include <math.h> |
21 | #include <mntent.h> | 21 | #include <mntent.h> |
22 | #include <stdio.h> | 22 | #include <stdio.h> |
23 | #include <stdlib.h> | 23 | #include <stdlib.h> |
24 | #include <unistd.h> | 24 | #include <unistd.h> |
25 | 25 | ||
26 | WavFile::WavFile( QObject * parent,const QString &fileName, bool makeNwFile, int sampleRate, | 26 | WavFile::WavFile( QObject * parent,const QString &fileName, bool makeNwFile, int sampleRate, |
27 | int channels, int resolution, int format ) | 27 | int channels, int resolution, int format ) |
28 | : QObject( parent) | 28 | : QObject( parent) |
29 | { | 29 | { |
30 | //odebug << "new wave file" << oendl; | 30 | //odebug << "new wave file" << oendl; |
31 | bool b = makeNwFile; | 31 | bool b = makeNwFile; |
32 | wavSampleRate=sampleRate; | 32 | wavSampleRate=sampleRate; |
33 | wavFormat=format; | 33 | wavFormat=format; |
34 | wavChannels=channels; | 34 | wavChannels=channels; |
35 | wavResolution=resolution; | 35 | wavResolution=resolution; |
36 | useTmpFile=false; | 36 | useTmpFile=false; |
37 | if( b) { | 37 | if( b) { |
38 | newFile(); | 38 | newFile(); |
39 | } else { | 39 | } else { |
40 | openFile(fileName); | 40 | openFile(fileName); |
41 | } | 41 | } |
42 | } | 42 | } |
43 | 43 | ||
44 | bool WavFile::newFile() { | 44 | bool WavFile::newFile() { |
45 | 45 | ||
46 | // odebug << "Set up new file" << oendl; | 46 | // odebug << "Set up new file" << oendl; |
47 | Config cfg("OpieRec"); | 47 | Config cfg("OpieRec"); |
48 | cfg.setGroup("Settings"); | 48 | cfg.setGroup("Settings"); |
49 | 49 | ||
50 | currentFileName=cfg.readEntry("directory",QDir::homeDirPath()); | 50 | currentFileName=cfg.readEntry("directory",QDir::homeDirPath()); |
51 | QString date; | 51 | QString date; |
52 | QDateTime dt = QDateTime::currentDateTime(); | 52 | QDateTime dt = QDateTime::currentDateTime(); |
53 | date = dt.toString();//TimeString::dateString( QDateTime::currentDateTime(),false,true); | 53 | date = dt.toString();//TimeString::dateString( QDateTime::currentDateTime(),false,true); |
54 | date.replace(QRegExp("'"),""); | 54 | date.replace(QRegExp("'"),""); |
55 | date.replace(QRegExp(" "),"_"); | 55 | date.replace(QRegExp(" "),"_"); |
56 | date.replace(QRegExp(":"),"."); | 56 | date.replace(QRegExp(":"),"-"); |
57 | date.replace(QRegExp(","),""); | 57 | date.replace(QRegExp(","),""); |
58 | 58 | ||
59 | QString currentFile=date; | 59 | QString currentFile=date; |
60 | if(currentFileName.right(1).find("/",0,true) == -1) | 60 | if(currentFileName.right(1).find("/",0,true) == -1) |
61 | currentFileName += "/" + date; | 61 | currentFileName += "/" + date; |
62 | else | 62 | else |
63 | currentFileName += date; | 63 | currentFileName += date; |
64 | currentFileName+=".wav"; | 64 | currentFileName+=".wav"; |
65 | 65 | ||
66 | // odebug << "set up file for recording: "+currentFileName << oendl; | 66 | // odebug << "set up file for recording: "+currentFileName << oendl; |
67 | char pointer[] = "/tmp/opierec-XXXXXX"; | 67 | char pointer[] = "/tmp/opierec-XXXXXX"; |
68 | int fd = 0; | 68 | int fd = 0; |
69 | 69 | ||
70 | if( currentFileName.find("/mnt",0,true) == -1 | 70 | if( currentFileName.find("/mnt",0,true) == -1 |
71 | && currentFileName.find("/tmp",0,true) == -1 ) { | 71 | && currentFileName.find("/tmp",0,true) == -1 ) { |
72 | // if destination file is most likely in flash (assuming jffs2) | 72 | // if destination file is most likely in flash (assuming jffs2) |
73 | // we have to write to a different filesystem first | 73 | // we have to write to a different filesystem first |
74 | 74 | ||
75 | useTmpFile = true; | 75 | useTmpFile = true; |
76 | if(( fd = mkstemp( pointer)) < 0 ) { | 76 | if(( fd = mkstemp( pointer)) < 0 ) { |
77 | perror("mkstemp failed"); | 77 | perror("mkstemp failed"); |
78 | return false; | 78 | return false; |
79 | } | 79 | } |
80 | 80 | ||
81 | // odebug << "Opening tmp file " << pointer << "" << oendl; | 81 | // odebug << "Opening tmp file " << pointer << "" << oendl; |
82 | track.setName( pointer); | 82 | track.setName( pointer); |
83 | 83 | ||
84 | } else { //just use regular file.. no moving | 84 | } else { //just use regular file.. no moving |
85 | 85 | ||
86 | useTmpFile = false; | 86 | useTmpFile = false; |
87 | track.setName( currentFileName); | 87 | track.setName( currentFileName); |
88 | } | 88 | } |
89 | if(!track.open( IO_ReadWrite | IO_Truncate)) { | 89 | if(!track.open( IO_ReadWrite | IO_Truncate)) { |
90 | QString errorMsg=(QString)strerror(errno); | 90 | QString errorMsg=(QString)strerror(errno); |
91 | odebug << errorMsg << oendl; | 91 | odebug << errorMsg << oendl; |
92 | QMessageBox::message("Note", "Error opening file.\n" +errorMsg); | 92 | QMessageBox::message("Note", "Error opening file.\n" +errorMsg); |
93 | 93 | ||
94 | return false; | 94 | return false; |
95 | } else { | 95 | } else { |
96 | setWavHeader( track.handle() , &hdr); | 96 | setWavHeader( track.handle() , &hdr); |
97 | } | 97 | } |
98 | return true; | 98 | return true; |
99 | } | 99 | } |
100 | 100 | ||
101 | WavFile::~WavFile() { | 101 | WavFile::~WavFile() { |
102 | 102 | ||
103 | closeFile(); | 103 | closeFile(); |
104 | } | 104 | } |
105 | 105 | ||
106 | void WavFile::closeFile() { | 106 | void WavFile::closeFile() { |
107 | if(track.isOpen()) | 107 | if(track.isOpen()) |
108 | track.close(); | 108 | track.close(); |
109 | } | 109 | } |
110 | 110 | ||
111 | int WavFile::openFile(const QString ¤tFileName) { | 111 | int WavFile::openFile(const QString ¤tFileName) { |
112 | // odebug << "open play file "+currentFileName << oendl; | 112 | // odebug << "open play file "+currentFileName << oendl; |
113 | closeFile(); | 113 | closeFile(); |
114 | 114 | ||
115 | track.setName(currentFileName); | 115 | track.setName(currentFileName); |
116 | 116 | ||
117 | if(!track.open(IO_ReadOnly)) { | 117 | if(!track.open(IO_ReadOnly)) { |
118 | QString errorMsg=(QString)strerror(errno); | 118 | QString errorMsg=(QString)strerror(errno); |
119 | odebug << "<<<<<<<<<<< "+errorMsg+currentFileName << oendl; | 119 | odebug << "<<<<<<<<<<< "+errorMsg+currentFileName << oendl; |
120 | QMessageBox::message("Note", "Error opening file.\n" +errorMsg); | 120 | QMessageBox::message("Note", "Error opening file.\n" +errorMsg); |
121 | return -1; | 121 | return -1; |
122 | } else { | 122 | } else { |
123 | parseWavHeader( track.handle()); | 123 | parseWavHeader( track.handle()); |
124 | } | 124 | } |
125 | return track.handle(); | 125 | return track.handle(); |
126 | } | 126 | } |
127 | 127 | ||
128 | bool WavFile::setWavHeader(int fd, wavhdr *hdr) { | 128 | bool WavFile::setWavHeader(int fd, wavhdr *hdr) { |
129 | 129 | ||
130 | strncpy((*hdr).riffID, "RIFF", 4); // RIFF | 130 | strncpy((*hdr).riffID, "RIFF", 4); // RIFF |
131 | strncpy((*hdr).wavID, "WAVE", 4); //WAVE | 131 | strncpy((*hdr).wavID, "WAVE", 4); //WAVE |
132 | strncpy((*hdr).fmtID, "fmt ", 4); // fmt | 132 | strncpy((*hdr).fmtID, "fmt ", 4); // fmt |
133 | (*hdr).fmtLen = 16; // format length = 16 | 133 | (*hdr).fmtLen = 16; // format length = 16 |
134 | 134 | ||
135 | if( wavFormat == WAVE_FORMAT_PCM) { | 135 | if( wavFormat == WAVE_FORMAT_PCM) { |
136 | (*hdr).fmtTag = 1; // PCM | 136 | (*hdr).fmtTag = 1; // PCM |
137 | // odebug << "set header WAVE_FORMAT_PCM" << oendl; | 137 | // odebug << "set header WAVE_FORMAT_PCM" << oendl; |
138 | } | 138 | } |
139 | else { | 139 | else { |
140 | (*hdr).fmtTag = WAVE_FORMAT_DVI_ADPCM; //intel ADPCM | 140 | (*hdr).fmtTag = WAVE_FORMAT_DVI_ADPCM; //intel ADPCM |
141 | // odebug << "set header WAVE_FORMAT_DVI_ADPCM" << oendl; | 141 | // odebug << "set header WAVE_FORMAT_DVI_ADPCM" << oendl; |
142 | } | 142 | } |
143 | 143 | ||
144 | // (*hdr).nChannels = 1;//filePara.channels;// ? 2 : 1*/; // channels | 144 | // (*hdr).nChannels = 1;//filePara.channels;// ? 2 : 1*/; // channels |
145 | (*hdr).nChannels = wavChannels;// ? 2 : 1*/; // channels | 145 | (*hdr).nChannels = wavChannels;// ? 2 : 1*/; // channels |
146 | 146 | ||
147 | (*hdr).sampleRate = wavSampleRate; //samples per second | 147 | (*hdr).sampleRate = wavSampleRate; //samples per second |
148 | (*hdr).avgBytesPerSec = (wavSampleRate)*( wavChannels*(wavResolution/8)); // bytes per second | 148 | (*hdr).avgBytesPerSec = (wavSampleRate)*( wavChannels*(wavResolution/8)); // bytes per second |
149 | (*hdr).nBlockAlign = wavChannels*( wavResolution/8); //block align | 149 | (*hdr).nBlockAlign = wavChannels*( wavResolution/8); //block align |
150 | (*hdr).bitsPerSample = wavResolution; //bits per sample 8, or 16 | 150 | (*hdr).bitsPerSample = wavResolution; //bits per sample 8, or 16 |
151 | 151 | ||
152 | strncpy((*hdr).dataID, "data", 4); | 152 | strncpy((*hdr).dataID, "data", 4); |
153 | 153 | ||
154 | write( fd,hdr, sizeof(*hdr)); | 154 | write( fd,hdr, sizeof(*hdr)); |
155 | odebug << "writing header: bitrate " << wavResolution << ", samplerate " << wavSampleRate << ", channels " << wavChannels << oendl; | 155 | odebug << "writing header: bitrate " << wavResolution << ", samplerate " << wavSampleRate << ", channels " << wavChannels << oendl; |
156 | return true; | 156 | return true; |
157 | } | 157 | } |
158 | 158 | ||
159 | bool WavFile::adjustHeaders(int fd, int total) { | 159 | bool WavFile::adjustHeaders(int fd, int total) { |
160 | lseek(fd, 4, SEEK_SET); | 160 | lseek(fd, 4, SEEK_SET); |
161 | int i = total + 36; | 161 | int i = total + 36; |
162 | write( fd, &i, sizeof(i)); | 162 | write( fd, &i, sizeof(i)); |
163 | lseek( fd, 40, SEEK_SET); | 163 | lseek( fd, 40, SEEK_SET); |
164 | write( fd, &total, sizeof(total)); | 164 | write( fd, &total, sizeof(total)); |
165 | odebug << "adjusting header " << total << "" << oendl; | 165 | odebug << "adjusting header " << total << "" << oendl; |
166 | return true; | 166 | return true; |
167 | } | 167 | } |
168 | 168 | ||
169 | int WavFile::parseWavHeader(int fd) { | 169 | int WavFile::parseWavHeader(int fd) { |
170 | odebug << "Parsing wav header" << oendl; | 170 | odebug << "Parsing wav header" << oendl; |
171 | char string[4]; | 171 | char string[4]; |
172 | int found; | 172 | int found; |
173 | short fmt; | 173 | short fmt; |
174 | unsigned short ch, bitrate; | 174 | unsigned short ch, bitrate; |
175 | unsigned long samplerrate, longdata; | 175 | unsigned long samplerrate, longdata; |
176 | 176 | ||
177 | if (read(fd, string, 4) < 4) { | 177 | if (read(fd, string, 4) < 4) { |
178 | odebug << " Could not read from sound file." << oendl; | 178 | odebug << " Could not read from sound file." << oendl; |
179 | return -1; | 179 | return -1; |
180 | } | 180 | } |
181 | if (strncmp(string, "RIFF", 4)) { | 181 | if (strncmp(string, "RIFF", 4)) { |
182 | odebug << " not a valid WAV file." << oendl; | 182 | odebug << " not a valid WAV file." << oendl; |
183 | return -1; | 183 | return -1; |
184 | } | 184 | } |
185 | lseek(fd, 4, SEEK_CUR); | 185 | lseek(fd, 4, SEEK_CUR); |
186 | if (read(fd, string, 4) < 4) { | 186 | if (read(fd, string, 4) < 4) { |
187 | odebug << "Could not read from sound file." << oendl; | 187 | odebug << "Could not read from sound file." << oendl; |
188 | return -1; | 188 | return -1; |
189 | } | 189 | } |
190 | if (strncmp(string, "WAVE", 4)) { | 190 | if (strncmp(string, "WAVE", 4)) { |
191 | odebug << "not a valid WAV file." << oendl; | 191 | odebug << "not a valid WAV file." << oendl; |
192 | return -1; | 192 | return -1; |
193 | } | 193 | } |
194 | found = 0; | 194 | found = 0; |
195 | 195 | ||
196 | while (!found) { | 196 | while (!found) { |
197 | if (read(fd, string, 4) < 4) { | 197 | if (read(fd, string, 4) < 4) { |
198 | odebug << "Could not read from sound file." << oendl; | 198 | odebug << "Could not read from sound file." << oendl; |
199 | return -1; | 199 | return -1; |
200 | } | 200 | } |
201 | if (strncmp(string, "fmt ", 4)) { | 201 | if (strncmp(string, "fmt ", 4)) { |
202 | if (read(fd, &longdata, 4) < 4) { | 202 | if (read(fd, &longdata, 4) < 4) { |
203 | odebug << "Could not read from sound file." << oendl; | 203 | odebug << "Could not read from sound file." << oendl; |
204 | return -1; | 204 | return -1; |
205 | } | 205 | } |
206 | lseek(fd, longdata, SEEK_CUR); | 206 | lseek(fd, longdata, SEEK_CUR); |
207 | } else { | 207 | } else { |
208 | lseek(fd, 4, SEEK_CUR); | 208 | lseek(fd, 4, SEEK_CUR); |
209 | if (read(fd, &fmt, 2) < 2) { | 209 | if (read(fd, &fmt, 2) < 2) { |
210 | odebug << "Could not read format chunk." << oendl; | 210 | odebug << "Could not read format chunk." << oendl; |
211 | return -1; | 211 | return -1; |
212 | } | 212 | } |
213 | if (fmt != WAVE_FORMAT_PCM && fmt != WAVE_FORMAT_DVI_ADPCM) { | 213 | if (fmt != WAVE_FORMAT_PCM && fmt != WAVE_FORMAT_DVI_ADPCM) { |
214 | odebug << "Wave file contains unknown format. Unable to continue." << oendl; | 214 | odebug << "Wave file contains unknown format. Unable to continue." << oendl; |
215 | return -1; | 215 | return -1; |
216 | } | 216 | } |
217 | wavFormat = fmt; | 217 | wavFormat = fmt; |
218 | // compressionFormat=fmt; | 218 | // compressionFormat=fmt; |
219 | odebug << "compressionFormat is " << fmt << "" << oendl; | 219 | odebug << "compressionFormat is " << fmt << "" << oendl; |
220 | if (read(fd, &ch, 2) < 2) { | 220 | if (read(fd, &ch, 2) < 2) { |
221 | odebug << "Could not read format chunk." << oendl; | 221 | odebug << "Could not read format chunk." << oendl; |
222 | return -1; | 222 | return -1; |
223 | } else { | 223 | } else { |
224 | wavChannels = ch; | 224 | wavChannels = ch; |
225 | odebug << "File has " << ch << " channels" << oendl; | 225 | odebug << "File has " << ch << " channels" << oendl; |
226 | } | 226 | } |
227 | if (read(fd, &samplerrate, 4) < 4) { | 227 | if (read(fd, &samplerrate, 4) < 4) { |
228 | odebug << "Could not read from format chunk." << oendl; | 228 | odebug << "Could not read from format chunk." << oendl; |
229 | return -1; | 229 | return -1; |
230 | } else { | 230 | } else { |
231 | wavSampleRate = samplerrate; | 231 | wavSampleRate = samplerrate; |
232 | // sampleRate = samplerrate; | 232 | // sampleRate = samplerrate; |
233 | odebug << "File has samplerate of " << (int) samplerrate << "" << oendl; | 233 | odebug << "File has samplerate of " << (int) samplerrate << "" << oendl; |
234 | } | 234 | } |
235 | lseek(fd, 6, SEEK_CUR); | 235 | lseek(fd, 6, SEEK_CUR); |
236 | if (read(fd, &bitrate, 2) < 2) { | 236 | if (read(fd, &bitrate, 2) < 2) { |
237 | odebug << "Could not read format chunk." << oendl; | 237 | odebug << "Could not read format chunk." << oendl; |
238 | return -1; | 238 | return -1; |
239 | } else { | 239 | } else { |
240 | wavResolution=bitrate; | 240 | wavResolution=bitrate; |
241 | // resolution = bitrate; | 241 | // resolution = bitrate; |
242 | odebug << "File has bitrate of " << bitrate << "" << oendl; | 242 | odebug << "File has bitrate of " << bitrate << "" << oendl; |
243 | } | 243 | } |
244 | found++; | 244 | found++; |
245 | } | 245 | } |
246 | } | 246 | } |
247 | found = 0; | 247 | found = 0; |
248 | while (!found) { | 248 | while (!found) { |
249 | if (read(fd, string, 4) < 4) { | 249 | if (read(fd, string, 4) < 4) { |
250 | odebug << "Could not read from sound file." << oendl; | 250 | odebug << "Could not read from sound file." << oendl; |
251 | return -1; | 251 | return -1; |
252 | } | 252 | } |
253 | 253 | ||
254 | if (strncmp(string, "data", 4)) { | 254 | if (strncmp(string, "data", 4)) { |
255 | if (read(fd, &longdata, 4)<4) { | 255 | if (read(fd, &longdata, 4)<4) { |
256 | odebug << "Could not read from sound file." << oendl; | 256 | odebug << "Could not read from sound file." << oendl; |
257 | return -1; | 257 | return -1; |
258 | } | 258 | } |
259 | 259 | ||
260 | lseek(fd, longdata, SEEK_CUR); | 260 | lseek(fd, longdata, SEEK_CUR); |
261 | } else { | 261 | } else { |
262 | if (read(fd, &longdata, 4) < 4) { | 262 | if (read(fd, &longdata, 4) < 4) { |
263 | odebug << "Could not read from sound file." << oendl; | 263 | odebug << "Could not read from sound file." << oendl; |
264 | return -1; | 264 | return -1; |
265 | } else { | 265 | } else { |
266 | wavNumberSamples = longdata; | 266 | wavNumberSamples = longdata; |
267 | odebug << "file hase length of " << (int)longdata << "" | 267 | odebug << "file hase length of " << (int)longdata << "" |
268 | << "lasting " | 268 | << "lasting " |
269 | << (int)(( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)) | 269 | << (int)(( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)) |
270 | << " seconds" << oendl; | 270 | << " seconds" << oendl; |
271 | // wavSeconds = (( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)); | 271 | // wavSeconds = (( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)); |
272 | 272 | ||
273 | return longdata; | 273 | return longdata; |
274 | } | 274 | } |
275 | } | 275 | } |
276 | } | 276 | } |
277 | 277 | ||
278 | lseek(fd, 0, SEEK_SET); | 278 | lseek(fd, 0, SEEK_SET); |
279 | 279 | ||
280 | return 0; | 280 | return 0; |
281 | } | 281 | } |
282 | 282 | ||
283 | QString WavFile::trackName() { | 283 | QString WavFile::trackName() { |
284 | return track.name(); | 284 | return track.name(); |
285 | } | 285 | } |
286 | 286 | ||
287 | int WavFile::wavHandle(){ | 287 | int WavFile::wavHandle(){ |
288 | return track.handle(); | 288 | return track.handle(); |
289 | } | 289 | } |
290 | 290 | ||
291 | int WavFile::getFormat() { | 291 | int WavFile::getFormat() { |
292 | return wavFormat; | 292 | return wavFormat; |
293 | } | 293 | } |
294 | 294 | ||
295 | int WavFile::getResolution() { | 295 | int WavFile::getResolution() { |
296 | return wavResolution; | 296 | return wavResolution; |
297 | } | 297 | } |
298 | 298 | ||
299 | int WavFile::getSampleRate() { | 299 | int WavFile::getSampleRate() { |
300 | return wavSampleRate; | 300 | return wavSampleRate; |
301 | } | 301 | } |
302 | 302 | ||
303 | int WavFile::getNumberSamples() { | 303 | int WavFile::getNumberSamples() { |
304 | return wavNumberSamples; | 304 | return wavNumberSamples; |
305 | } | 305 | } |
306 | 306 | ||
307 | bool WavFile::isTempFile() { | 307 | bool WavFile::isTempFile() { |
308 | return useTmpFile; | 308 | return useTmpFile; |
309 | } | 309 | } |
310 | 310 | ||
311 | int WavFile::getChannels() { | 311 | int WavFile::getChannels() { |
312 | 312 | ||
313 | return wavChannels; | 313 | return wavChannels; |
314 | } | 314 | } |