Diffstat (limited to 'noncore/multimedia/opierec/wavFile.cpp') (more/less context) (ignore whitespace changes)
-rw-r--r-- | noncore/multimedia/opierec/wavFile.cpp | 86 |
1 files changed, 44 insertions, 42 deletions
diff --git a/noncore/multimedia/opierec/wavFile.cpp b/noncore/multimedia/opierec/wavFile.cpp index 7e9b50f..fc0130c 100644 --- a/noncore/multimedia/opierec/wavFile.cpp +++ b/noncore/multimedia/opierec/wavFile.cpp | |||
@@ -24,13 +24,13 @@ using namespace Opie::Core; | |||
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; |
@@ -40,13 +40,13 @@ WavFile::WavFile( QObject * parent,const QString &fileName, bool makeNwFile, int | |||
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(); |
@@ -60,40 +60,40 @@ bool WavFile::newFile() { | |||
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 | } |
@@ -101,47 +101,47 @@ return true; | |||
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 |
@@ -149,130 +149,132 @@ bool WavFile::setWavHeader(int fd, wavhdr *hdr) { | |||
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 | qDebug("writing header: bitrate%d, samplerate %d, channels %d", | 155 | odebug << "writing header: bitrate " << wavResolution |
156 | wavResolution, wavSampleRate, wavChannels); | 156 | << ", samplerate " << wavSampleRate |
157 | << ", channels " << wavChannels << oendl; | ||
157 | return true; | 158 | return true; |
158 | } | 159 | } |
159 | 160 | ||
160 | bool WavFile::adjustHeaders(int fd, int total) { | 161 | bool WavFile::adjustHeaders(int fd, int total) { |
161 | lseek(fd, 4, SEEK_SET); | 162 | lseek(fd, 4, SEEK_SET); |
162 | int i = total + 36; | 163 | int i = total + 36; |
163 | write( fd, &i, sizeof(i)); | 164 | write( fd, &i, sizeof(i)); |
164 | lseek( fd, 40, SEEK_SET); | 165 | lseek( fd, 40, SEEK_SET); |
165 | write( fd, &total, sizeof(total)); | 166 | write( fd, &total, sizeof(total)); |
166 | odebug << "adjusting header " << total << "" << oendl; | 167 | odebug << "adjusting header " << total << "" << oendl; |
167 | return true; | 168 | return true; |
168 | } | 169 | } |
169 | 170 | ||
170 | int WavFile::parseWavHeader(int fd) { | 171 | int WavFile::parseWavHeader(int fd) { |
171 | odebug << "Parsing wav header" << oendl; | 172 | odebug << "Parsing wav header" << oendl; |
172 | char string[4]; | 173 | char string[4]; |
173 | int found; | 174 | int found; |
174 | short fmt; | 175 | short fmt; |
175 | unsigned short ch, bitrate; | 176 | unsigned short ch, bitrate; |
176 | unsigned long samplerrate, longdata; | 177 | unsigned long samplerrate, longdata; |
177 | 178 | ||
178 | if (read(fd, string, 4) < 4) { | 179 | if (read(fd, string, 4) < 4) { |
179 | odebug << " Could not read from sound file.\n" << oendl; | 180 | odebug << " Could not read from sound file.\n" << oendl; |
180 | return -1; | 181 | return -1; |
181 | } | 182 | } |
182 | if (strncmp(string, "RIFF", 4)) { | 183 | if (strncmp(string, "RIFF", 4)) { |
183 | odebug << " not a valid WAV file.\n" << oendl; | 184 | odebug << " not a valid WAV file.\n" << oendl; |
184 | return -1; | 185 | return -1; |
185 | } | 186 | } |
186 | lseek(fd, 4, SEEK_CUR); | 187 | lseek(fd, 4, SEEK_CUR); |
187 | if (read(fd, string, 4) < 4) { | 188 | if (read(fd, string, 4) < 4) { |
188 | odebug << "Could not read from sound file.\n" << oendl; | 189 | odebug << "Could not read from sound file.\n" << oendl; |
189 | return -1; | 190 | return -1; |
190 | } | 191 | } |
191 | if (strncmp(string, "WAVE", 4)) { | 192 | if (strncmp(string, "WAVE", 4)) { |
192 | odebug << "not a valid WAV file.\n" << oendl; | 193 | odebug << "not a valid WAV file.\n" << oendl; |
193 | return -1; | 194 | return -1; |
194 | } | 195 | } |
195 | found = 0; | 196 | found = 0; |
196 | 197 | ||
197 | while (!found) { | 198 | while (!found) { |
198 | if (read(fd, string, 4) < 4) { | 199 | if (read(fd, string, 4) < 4) { |
199 | odebug << "Could not read from sound file.\n" << oendl; | 200 | odebug << "Could not read from sound file.\n" << oendl; |
200 | return -1; | 201 | return -1; |
201 | } | 202 | } |
202 | if (strncmp(string, "fmt ", 4)) { | 203 | if (strncmp(string, "fmt ", 4)) { |
203 | if (read(fd, &longdata, 4) < 4) { | 204 | if (read(fd, &longdata, 4) < 4) { |
204 | odebug << "Could not read from sound file.\n" << oendl; | 205 | odebug << "Could not read from sound file.\n" << oendl; |
205 | return -1; | 206 | return -1; |
206 | } | 207 | } |
207 | lseek(fd, longdata, SEEK_CUR); | 208 | lseek(fd, longdata, SEEK_CUR); |
208 | } else { | 209 | } else { |
209 | lseek(fd, 4, SEEK_CUR); | 210 | lseek(fd, 4, SEEK_CUR); |
210 | if (read(fd, &fmt, 2) < 2) { | 211 | if (read(fd, &fmt, 2) < 2) { |
211 | odebug << "Could not read format chunk.\n" << oendl; | 212 | odebug << "Could not read format chunk.\n" << oendl; |
212 | return -1; | 213 | return -1; |
213 | } | 214 | } |
214 | if (fmt != WAVE_FORMAT_PCM && fmt != WAVE_FORMAT_DVI_ADPCM) { | 215 | if (fmt != WAVE_FORMAT_PCM && fmt != WAVE_FORMAT_DVI_ADPCM) { |
215 | qDebug("Wave file contains unknown format." | 216 | odebug << "Wave file contains unknown format. Unable to continue.\n" << oendl; |
216 | " Unable to continue.\n"); | ||
217 | return -1; | 217 | return -1; |
218 | } | 218 | } |
219 | wavFormat = fmt; | 219 | wavFormat = fmt; |
220 | // compressionFormat=fmt; | 220 | // compressionFormat=fmt; |
221 | odebug << "compressionFormat is " << fmt << "" << oendl; | 221 | odebug << "compressionFormat is " << fmt << "" << oendl; |
222 | if (read(fd, &ch, 2) < 2) { | 222 | if (read(fd, &ch, 2) < 2) { |
223 | odebug << "Could not read format chunk.\n" << oendl; | 223 | odebug << "Could not read format chunk.\n" << oendl; |
224 | return -1; | 224 | return -1; |
225 | } else { | 225 | } else { |
226 | wavChannels = ch; | 226 | wavChannels = ch; |
227 | odebug << "File has " << ch << " channels" << oendl; | 227 | odebug << "File has " << ch << " channels" << oendl; |
228 | } | 228 | } |
229 | if (read(fd, &samplerrate, 4) < 4) { | 229 | if (read(fd, &samplerrate, 4) < 4) { |
230 | odebug << "Could not read from format chunk.\n" << oendl; | 230 | odebug << "Could not read from format chunk.\n" << oendl; |
231 | return -1; | 231 | return -1; |
232 | } else { | 232 | } else { |
233 | wavSampleRate = samplerrate; | 233 | wavSampleRate = samplerrate; |
234 | // sampleRate = samplerrate; | 234 | // sampleRate = samplerrate; |
235 | odebug << "File has samplerate of " << (int) samplerrate << "" << oendl; | 235 | odebug << "File has samplerate of " << (int) samplerrate << "" << oendl; |
236 | } | 236 | } |
237 | lseek(fd, 6, SEEK_CUR); | 237 | lseek(fd, 6, SEEK_CUR); |
238 | if (read(fd, &bitrate, 2) < 2) { | 238 | if (read(fd, &bitrate, 2) < 2) { |
239 | odebug << "Could not read format chunk.\n" << oendl; | 239 | odebug << "Could not read format chunk.\n" << oendl; |
240 | return -1; | 240 | return -1; |
241 | } else { | 241 | } else { |
242 | wavResolution=bitrate; | 242 | wavResolution=bitrate; |
243 | // resolution = bitrate; | 243 | // resolution = bitrate; |
244 | odebug << "File has bitrate of " << bitrate << "" << oendl; | 244 | odebug << "File has bitrate of " << bitrate << "" << oendl; |
245 | } | 245 | } |
246 | found++; | 246 | found++; |
247 | } | 247 | } |
248 | } | 248 | } |
249 | found = 0; | 249 | found = 0; |
250 | while (!found) { | 250 | while (!found) { |
251 | if (read(fd, string, 4) < 4) { | 251 | if (read(fd, string, 4) < 4) { |
252 | odebug << "Could not read from sound file.\n" << oendl; | 252 | odebug << "Could not read from sound file.\n" << oendl; |
253 | return -1; | 253 | return -1; |
254 | } | 254 | } |
255 | 255 | ||
256 | if (strncmp(string, "data", 4)) { | 256 | if (strncmp(string, "data", 4)) { |
257 | if (read(fd, &longdata, 4)<4) { | 257 | if (read(fd, &longdata, 4)<4) { |
258 | odebug << "Could not read from sound file.\n" << oendl; | 258 | odebug << "Could not read from sound file.\n" << oendl; |
259 | return -1; | 259 | return -1; |
260 | } | 260 | } |
261 | 261 | ||
262 | lseek(fd, longdata, SEEK_CUR); | 262 | lseek(fd, longdata, SEEK_CUR); |
263 | } else { | 263 | } else { |
264 | if (read(fd, &longdata, 4) < 4) { | 264 | if (read(fd, &longdata, 4) < 4) { |
265 | odebug << "Could not read from sound file.\n" << oendl; | 265 | odebug << "Could not read from sound file.\n" << oendl; |
266 | return -1; | 266 | return -1; |
267 | } else { | 267 | } else { |
268 | wavNumberSamples = longdata; | 268 | wavNumberSamples = longdata; |
269 | qDebug("file has length of %d \nlasting %d seconds", (int)longdata, | 269 | odebug << "file hase length of " << (int)longdata << "\n" |
270 | (int)(( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)) ); | 270 | << "lasting " |
271 | << (int)(( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)) | ||
272 | << " seconds" << oendl; | ||
271 | // wavSeconds = (( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)); | 273 | // wavSeconds = (( longdata / wavSampleRate) / wavChannels) / ( wavChannels*( wavResolution/8)); |
272 | 274 | ||
273 | return longdata; | 275 | return longdata; |
274 | } | 276 | } |
275 | } | 277 | } |
276 | } | 278 | } |
277 | 279 | ||
278 | lseek(fd, 0, SEEK_SET); | 280 | lseek(fd, 0, SEEK_SET); |