Use RtAudio for all audio channels

This commit is contained in:
michal.szwaj 2018-03-24 03:28:31 +01:00
parent bd6013081a
commit 66610efd33
3 changed files with 25 additions and 17 deletions

View File

@ -52,8 +52,7 @@ private:
uint32_t sampleSize_; uint32_t sampleSize_;
uint32_t sampleRate_; uint32_t sampleRate_;
SequentialBuffer audioBuffer_; SequentialBuffer audioBuffer_;
bool playbackStarted_; std::unique_ptr<RtAudio> dac_;
RtAudio dac_;
std::mutex mutex_; std::mutex mutex_;
}; };

View File

@ -32,25 +32,34 @@ RtAudioOutput::RtAudioOutput(uint32_t channelCount, uint32_t sampleSize, uint32_
: channelCount_(channelCount) : channelCount_(channelCount)
, sampleSize_(sampleSize) , sampleSize_(sampleSize)
, sampleRate_(sampleRate) , sampleRate_(sampleRate)
, playbackStarted_(false) , dac_()
{ {
try
{
dac_ = std::make_unique<RtAudio>(RtAudio::LINUX_PULSE);
}
catch(...)
{
// fallback
dac_ = std::make_unique<RtAudio>();
}
} }
bool RtAudioOutput::open() bool RtAudioOutput::open()
{ {
std::lock_guard<decltype(mutex_)> lock(mutex_); std::lock_guard<decltype(mutex_)> lock(mutex_);
if(dac_.getDeviceCount() > 0) if(dac_->getDeviceCount() > 0)
{ {
RtAudio::StreamParameters parameters; RtAudio::StreamParameters parameters;
parameters.deviceId = dac_.getDefaultOutputDevice(); parameters.deviceId = dac_->getDefaultOutputDevice();
parameters.nChannels = channelCount_; parameters.nChannels = channelCount_;
parameters.firstChannel = 0; parameters.firstChannel = 0;
try try
{ {
uint32_t bufferFrames = 256; uint32_t bufferFrames = 1024;
dac_.openStream(&parameters, nullptr, RTAUDIO_SINT16, sampleRate_, &bufferFrames, &RtAudioOutput::audioBufferReadHandler, static_cast<void*>(this)); dac_->openStream(&parameters, nullptr, RTAUDIO_SINT16, sampleRate_, &bufferFrames, &RtAudioOutput::audioBufferReadHandler, static_cast<void*>(this));
return audioBuffer_.open(QIODevice::ReadWrite); return audioBuffer_.open(QIODevice::ReadWrite);
} }
@ -76,11 +85,11 @@ void RtAudioOutput::start()
{ {
std::lock_guard<decltype(mutex_)> lock(mutex_); std::lock_guard<decltype(mutex_)> lock(mutex_);
if(dac_.isStreamOpen() && !dac_.isStreamRunning()) if(dac_->isStreamOpen() && !dac_->isStreamRunning())
{ {
try try
{ {
dac_.startStream(); dac_->startStream();
} }
catch(const RtAudioError& e) catch(const RtAudioError& e)
{ {
@ -95,9 +104,9 @@ void RtAudioOutput::stop()
this->suspend(); this->suspend();
if(dac_.isStreamOpen()) if(dac_->isStreamOpen())
{ {
dac_.closeStream(); dac_->closeStream();
} }
} }
@ -105,11 +114,11 @@ void RtAudioOutput::suspend()
{ {
std::lock_guard<decltype(mutex_)> lock(mutex_); std::lock_guard<decltype(mutex_)> lock(mutex_);
if(!dac_.isStreamOpen() && !dac_.isStreamRunning()) if(!dac_->isStreamOpen() && !dac_->isStreamRunning())
{ {
try try
{ {
dac_.stopStream(); dac_->stopStream();
} }
catch(const RtAudioError& e) catch(const RtAudioError& e)
{ {

View File

@ -72,13 +72,13 @@ ServiceList ServiceFactory::create(aasdk::messenger::IMessenger::Pointer messeng
if(configuration_->speechAudioChannelEnabled()) if(configuration_->speechAudioChannelEnabled())
{ {
IAudioOutput::Pointer speechAudioOutput(new QtAudioOutput(1, 16, 16000), std::bind(&QObject::deleteLater, std::placeholders::_1)); //IAudioOutput::Pointer speechAudioOutput(new QtAudioOutput(1, 16, 16000), std::bind(&QObject::deleteLater, std::placeholders::_1));
//auto speechAudioOutput(std::make_shared<RtAudioOutput>(1, 16, 1600)); auto speechAudioOutput(std::make_shared<RtAudioOutput>(1, 16, 16000));
serviceList.emplace_back(std::make_shared<SpeechAudioService>(ioService_, messenger, std::move(speechAudioOutput))); serviceList.emplace_back(std::make_shared<SpeechAudioService>(ioService_, messenger, std::move(speechAudioOutput)));
} }
IAudioOutput::Pointer systemAudioOutput(new QtAudioOutput(1, 16, 16000), std::bind(&QObject::deleteLater, std::placeholders::_1)); //IAudioOutput::Pointer systemAudioOutput(new QtAudioOutput(1, 16, 16000), std::bind(&QObject::deleteLater, std::placeholders::_1));
//auto systemAudioOutput(std::make_shared<RtAudioOutput>(1, 16, 1600)); auto systemAudioOutput(std::make_shared<RtAudioOutput>(1, 16, 16000));
serviceList.emplace_back(std::make_shared<SystemAudioService>(ioService_, messenger, std::move(systemAudioOutput))); serviceList.emplace_back(std::make_shared<SystemAudioService>(ioService_, messenger, std::move(systemAudioOutput)));
serviceList.emplace_back(std::make_shared<SensorService>(ioService_, messenger)); serviceList.emplace_back(std::make_shared<SensorService>(ioService_, messenger));