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