Implemented stereo audio recording.

This commit is contained in:
Maximilian Wagenbach 2015-10-01 15:43:35 +02:00 committed by Lukas Dürrenberger
parent 259811d59c
commit f49c156a31
3 changed files with 75 additions and 7 deletions

View file

@ -63,7 +63,7 @@ bool SoundBufferRecorder::onProcessSamples(const Int16* samples, std::size_t sam
void SoundBufferRecorder::onStop()
{
if (!m_samples.empty())
m_buffer.loadFromSamples(&m_samples[0], m_samples.size(), 1, getSampleRate());
m_buffer.loadFromSamples(&m_samples[0], m_samples.size(), getChannelCount(), getSampleRate());
}

View file

@ -51,7 +51,8 @@ m_thread (&SoundRecorder::record, this),
m_sampleRate (0),
m_processingInterval(milliseconds(100)),
m_isCapturing (false),
m_deviceName (getDefaultDevice())
m_deviceName (getDefaultDevice()),
m_channelCount (1)
{
}
@ -86,8 +87,11 @@ bool SoundRecorder::start(unsigned int sampleRate)
return false;
}
// Open the capture device for capturing 16 bits mono samples
captureDevice = alcCaptureOpenDevice(m_deviceName.c_str(), sampleRate, AL_FORMAT_MONO16, sampleRate);
// Determine the recording format
ALCenum format = (m_channelCount == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
// Open the capture device for capturing 16 bits samples
captureDevice = alcCaptureOpenDevice(m_deviceName.c_str(), sampleRate, format, sampleRate);
if (!captureDevice)
{
err() << "Failed to open the audio capture device with the name: " << m_deviceName << std::endl;
@ -180,8 +184,11 @@ bool SoundRecorder::setDevice(const std::string& name)
m_isCapturing = false;
m_thread.wait();
// Open the requested capture device for capturing 16 bits mono samples
captureDevice = alcCaptureOpenDevice(name.c_str(), m_sampleRate, AL_FORMAT_MONO16, m_sampleRate);
// Determine the recording format
ALCenum format = (m_channelCount == 1) ? AL_FORMAT_MONO16 : AL_FORMAT_STEREO16;
// Open the requested capture device for capturing 16 bits samples
captureDevice = alcCaptureOpenDevice(name.c_str(), m_sampleRate, format, m_sampleRate);
if (!captureDevice)
{
// Notify derived class
@ -210,6 +217,32 @@ const std::string& SoundRecorder::getDevice() const
}
////////////////////////////////////////////////////////////
void SoundRecorder::setChannelCount(unsigned int channelCount)
{
if (m_isCapturing)
{
err() << "It's not possible to change the channels while recording." << std::endl;
return;
}
if (channelCount < 1 || channelCount > 2)
{
err() << "Unsupported channel count: " << channelCount << " Currently only mono (1) and stereo (2) recording is supported." << std::endl;
return;
}
m_channelCount = channelCount;
}
////////////////////////////////////////////////////////////
unsigned int SoundRecorder::getChannelCount() const
{
return m_channelCount;
}
////////////////////////////////////////////////////////////
bool SoundRecorder::isAvailable()
{
@ -267,7 +300,7 @@ void SoundRecorder::processCapturedSamples()
if (samplesAvailable > 0)
{
// Get the recorded samples
m_samples.resize(samplesAvailable);
m_samples.resize(samplesAvailable * getChannelCount());
alcCaptureSamples(captureDevice, &m_samples[0], samplesAvailable);
// Forward them to the derived class