void SoundRecorder::start(void) { /* Do nothing if already started: */ if(active) return; #if SOUND_CONFIG_HAVE_ALSA /* Reset the number of recorded frames: */ numRecordedFrames=0; /* Write an empty audio file header if necessary: */ if(outputFileFormat==WAV) writeWAVHeader(); /* Start the PCM device: */ pcmDevice.start(); /* Start the background recording thread: */ recordingThread.start(this,&SoundRecorder::recordingThreadMethod); #endif active=true; }
SoundRecorder::~SoundRecorder(void) { #if SOUND_CONFIG_HAVE_ALSA /* Stop the recording thread if still active: */ if(active) { recordingThread.cancel(); recordingThread.join(); /* Stop the PCM device: */ pcmDevice.drop(); /* Write the final audio file header if necessary: */ if(outputFileFormat==WAV) writeWAVHeader(); } /* Close the output file: */ delete outputFile; /* Delete the sample buffer: */ delete[] sampleBuffer; #endif }
WAVWriter::WAVWriter(const SYSCHAR* fileName) : AudioDriver_NULL(), f(NULL), mixFreq(44100) { TWAVHeader hdr; f = new XMFile(fileName, true); if (!f->isOpenForWriting()) { delete f; f = NULL; } else { // build wav header memcpy(hdr.RIFF, "RIFF", 4); hdr.length = 44 - 8; memcpy(hdr.WAVE, "WAVE", 4); memcpy(hdr.FMT, "fmt ", 4); hdr.fmtDataLength = 16; hdr.encodingTag = 1; hdr.numChannels = 2; hdr.sampleRate = mixFreq; hdr.numBits = 16; hdr.blockAlign = (hdr.numChannels*hdr.numBits) / 8; hdr.bytesPerSecond = hdr.sampleRate*hdr.blockAlign; memcpy(hdr.DATA, "data", 4); hdr.dataLength = 0; writeWAVHeader(f, hdr); } }
mp_sint32 WAVWriter::closeDevice() { if (!f) return MP_DEVICE_ERROR; TWAVHeader hdr; // build wav header memcpy(hdr.RIFF, "RIFF", 4); hdr.length = 44 + numSamplesWritten*4 - 8; memcpy(hdr.WAVE, "WAVE", 4); memcpy(hdr.FMT, "fmt ", 4); hdr.fmtDataLength = 16; hdr.encodingTag = 1; hdr.numChannels = 2; hdr.sampleRate = mixFreq; hdr.numBits = 16; hdr.blockAlign = (hdr.numChannels*hdr.numBits) / 8; hdr.bytesPerSecond = hdr.sampleRate*hdr.blockAlign; memcpy(hdr.DATA, "data", 4); hdr.dataLength = (numSamplesWritten*2*hdr.numBits)/8; f->seek(0); writeWAVHeader(f, hdr); return MP_OK; }
SoundRecorder::~SoundRecorder(void) { #if SOUND_CONFIG_HAVE_ALSA /* Stop the recording thread if still active: */ if(active) { /* Stop the recording thread at the next opportunity: */ keepReading=false; recordingThread.join(); /* Write the final audio file header if necessary: */ if(outputFileFormat==WAV) writeWAVHeader(); } /* Delete the sample buffer: */ delete[] sampleBuffer; #endif }
void SoundRecorder::stop(void) { /* Do nothing if not started: */ if(!active) return; #if SOUND_CONFIG_HAVE_ALSA /* Kill the background recording thread: */ recordingThread.cancel(); recordingThread.join(); /* Stop the PCM device: */ pcmDevice.drop(); /* Write the final audio file header if necessary: */ if(outputFileFormat==WAV) writeWAVHeader(); #endif active=false; }