Skip to content

Commit

Permalink
Handle device selection a little less aggressively to work around a p…
Browse files Browse the repository at this point in the history
…ossible but on rpi
  • Loading branch information
TomMettam committed Aug 11, 2021
1 parent 1aa8935 commit 34c8ce3
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 4 deletions.
33 changes: 30 additions & 3 deletions src/implementation/RtAudioStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@

namespace CasperTech
{
std::map<uint32_t, std::string> RtAudioStream::devices;
RtAudio::DeviceInfo RtAudioStream::defaultDevice;
int RtAudioStream::defaultDeviceId = -1;

RtAudioStream::RtAudioStream()
: _ringBuffer(std::make_unique<RingBuffer>(16384))
Expand All @@ -18,14 +21,25 @@ namespace CasperTech

std::map<uint32_t, std::string> RtAudioStream::getDevices()
{
std::map<uint32_t, std::string> devices;
if (!devices.empty())
{
return devices;
}
devices = {};
uint32_t deviceCount = _rtAudio->getDeviceCount();
RtAudio::DeviceInfo info;
for(uint32_t i = 0; i < deviceCount; i++)
{
try
{
info = _rtAudio->getDeviceInfo(i);
if (info.isDefaultOutput && info.outputChannels > 0)
{
defaultDevice = info;
defaultDeviceId = i;
_selectedDevice = info;
_selectedDeviceId = i;
}
devices[i] = info.name;
}
catch(RtAudioError&)
Expand All @@ -38,6 +52,12 @@ namespace CasperTech

void RtAudioStream::selectDevice(uint32_t device)
{
if (defaultDeviceId > -1 && device == defaultDeviceId)
{
_selectedDevice = defaultDevice;
_selectedDeviceId = defaultDeviceId;
return;
}
try
{
_selectedDevice = _rtAudio->getDeviceInfo(device);
Expand All @@ -51,8 +71,15 @@ namespace CasperTech

void RtAudioStream::selectDefaultDevice()
{
if (defaultDeviceId > -1)
{
_selectedDevice = defaultDevice;
_selectedDeviceId = defaultDeviceId;
return;
}
uint32_t deviceCount = _rtAudio->getDeviceCount();
RtAudio::DeviceInfo info;
std::cout << "DeviceCount: " << deviceCount << std::endl;
for(uint32_t i = 0; i < deviceCount; i++)
{
try
Expand All @@ -71,9 +98,9 @@ namespace CasperTech
return;
}
}
catch(RtAudioError&)
catch(RtAudioError& e)
{

std::cout << e.what() << std::endl << std::flush;
}
}
#ifdef _DEBUG
Expand Down
3 changes: 3 additions & 0 deletions src/implementation/RtAudioStream.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ namespace CasperTech
double streamTime, RtAudioStreamStatus status, void *data);
int fillBuffer(void *outputBuffer, void *inputBuffer, unsigned int nBufferFrames,
double streamTime, RtAudioStreamStatus status);
static std::map<uint32_t, std::string> devices;
static RtAudio::DeviceInfo defaultDevice;
static int defaultDeviceId;

int _selectedDeviceId = 0;
std::unique_ptr<RtAudio> _rtAudio;
Expand Down

0 comments on commit 34c8ce3

Please sign in to comment.