189
162
bool AudioRecord::openFile()
191
164
bool result = false;
193
if (not fileExists()) {
194
DEBUG("Filename does not exist, creating one");
197
if (fileType_ == FILE_RAW)
198
result = setRawFile();
199
else if (fileType_ == FILE_WAV)
200
result = setWavFile();
202
DEBUG("Filename already exists, opening it");
203
if (fileType_ == FILE_RAW)
204
result = openExistingRawFile();
205
else if (fileType_ == FILE_WAV)
206
result = openExistingWavFile();
166
const bool doAppend = fileExists();
167
const int access = doAppend ? SFM_RDWR : SFM_WRITE;
169
fileHandle_ = new SndfileHandle(savePath_.c_str(), access, SF_FORMAT_WAV | SF_FORMAT_PCM_16, channels_, sndSmplRate_);
171
// check overloaded boolean operator
173
WARN("Could not open WAV file!");
179
if (doAppend and fileHandle_->seek(0, SEEK_END) < 0)
180
WARN("Couldn't seek to the end of the file ");
212
185
void AudioRecord::closeFile()
214
if (fileHandle_ == 0) return;
216
if (fileType_ == FILE_RAW)
218
else if (fileType_ == FILE_WAV)
222
191
bool AudioRecord::isOpenFile() const
250
221
recordingEnabled_ = false;
253
bool AudioRecord::setRawFile()
255
fileHandle_ = fopen(savePath_.c_str(), "wb");
258
WARN("Could not create RAW file!");
262
DEBUG("created RAW file.");
268
std::string header_to_string(const wavhdr &hdr)
270
std::stringstream ss;
271
ss << hdr.riff << "\0 "
272
<< hdr.file_size << " "
275
<< hdr.chunk_size << " "
276
<< hdr.format_tag << " "
277
<< hdr.num_chans << " "
278
<< hdr.sample_rate << " "
279
<< hdr.bytes_per_sec << " "
280
<< hdr.bytes_per_samp << " "
281
<< hdr.bits_per_samp << " "
288
bool AudioRecord::setWavFile()
290
DEBUG("Create new wave file %s, sampling rate: %d", savePath_.c_str(), sndSmplRate_);
292
fileHandle_ = fopen(savePath_.c_str(), "wb");
295
WARN("Could not create WAV file.");
299
/* The text fields are NOT supposed to be null terminated, so we have to
300
* write them as arrays since strings enclosed in quotes include a
302
wavhdr hdr = {{'R', 'I', 'F', 'F'},
304
{'W', 'A', 'V', 'E'},
310
-1, /* initialized below */
311
-1, /* initialized below */
313
{'d', 'a', 't', 'a'},
316
hdr.bytes_per_samp = channels_ * hdr.bits_per_samp / 8;
317
hdr.bytes_per_sec = hdr.sample_rate * hdr.bytes_per_samp;
319
if (fwrite(&hdr, 4, 11, fileHandle_) != 11) {
320
WARN("Could not write WAV header for file. ");
324
DEBUG("Wrote wave header \"%s\"", header_to_string(hdr).c_str());
328
bool AudioRecord::openExistingRawFile()
330
fileHandle_ = fopen(filename_.c_str(), "ab+");
333
WARN("could not create RAW file!");
340
bool AudioRecord::openExistingWavFile()
342
DEBUG("Opening %s", filename_.c_str());
344
fileHandle_ = fopen(filename_.c_str(), "rb+");
347
WARN("Could not open WAV file!");
351
if (fseek(fileHandle_, 40, SEEK_SET) != 0) // jump to data length
352
WARN("Couldn't seek offset 40 in the file ");
354
if (fread(&byteCounter_, 4, 1, fileHandle_))
355
WARN("bytecounter Read successfully ");
357
if (fseek(fileHandle_, 0 , SEEK_END) != 0)
358
WARN("Couldn't seek at the en of the file ");
361
if (fclose(fileHandle_) != 0)
362
WARN("Can't close file r+ ");
364
fileHandle_ = fopen(filename_.c_str(), "ab+");
367
WARN("Could not createopen WAV file ab+!");
371
if (fseek(fileHandle_, 4 , SEEK_END) != 0)
372
WARN("Couldn't seek at the en of the file ");
378
void AudioRecord::closeWavFile()
224
void AudioRecord::recData(AudioBuffer& buffer)
226
if (not recordingEnabled_)
380
229
if (fileHandle_ == 0) {
381
DEBUG("Can't closeWavFile, a file has not yet been opened!");
230
DEBUG("Can't record data, a file has not yet been opened!");
385
DEBUG("Close wave file");
387
SINT32 bytes = byteCounter_ * channels_;
389
// jump to data length
390
if (fseek(fileHandle_, 40, SEEK_SET) != 0)
391
WARN("Could not seek in file");
393
if (ferror(fileHandle_))
394
WARN("Can't reach offset 40 while closing");
396
fwrite(&bytes, sizeof(SINT32), 1, fileHandle_);
398
if (ferror(fileHandle_))
399
WARN("Can't write bytes for data length ");
401
bytes = byteCounter_ * channels_ + 44; // + 44 for the wave header
404
if (fseek(fileHandle_, 4, SEEK_SET) != 0)
405
WARN("Could not seek in file");
407
if (ferror(fileHandle_))
408
WARN("Can't reach offset 4");
410
fwrite(&bytes, 4, 1, fileHandle_);
412
if (ferror(fileHandle_))
413
WARN("Can't reach offset 4");
415
if (fclose(fileHandle_) != 0)
416
WARN("Can't close file");
419
void AudioRecord::recData(SFLDataFormat* buffer, size_t nSamples)
421
if (recordingEnabled_) {
422
if (fileHandle_ == 0) {
423
DEBUG("Can't record data, a file has not yet been opened!");
427
if (fwrite(buffer, sizeof(SFLDataFormat), nSamples, fileHandle_) != nSamples)
428
WARN("Could not record data! ");
431
byteCounter_ += nSamples * sizeof(SFLDataFormat);
234
const int nSamples = buffer.frames();
237
if (fileHandle_->write(buffer.getChannel(0)->data(), nSamples) != nSamples) {
238
WARN("Could not record data!");
240
fileHandle_->writeSync();