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