Skip to content

Commit

Permalink
Refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
darbyjohnston committed Jul 30, 2024
1 parent 3dd68fc commit 718ce62
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 44 deletions.
145 changes: 101 additions & 44 deletions lib/tlCore/AudioSystem.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -90,12 +90,6 @@ namespace tl
ISystem::_init("tl::audio::System", context);
TLRENDER_P();

p.devices = observer::List<Device>::create();
p.defaultOutputDevice = observer::Value<int>::create(-1);
p.defaultOutputInfo = observer::Value<Info>::create();
p.defaultInputDevice = observer::Value<int>::create(-1);
p.defaultInputInfo = observer::Value<Info>::create();

#if defined(TLRENDER_AUDIO)
try
{
Expand All @@ -117,14 +111,37 @@ namespace tl
}

p.rtAudio.reset(new RtAudio);
p.rtAudio->showWarnings(false);
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Cannot initialize audio system: " << e.what();
_log(ss.str(), log::Type::Error);
}
#endif // TLRENDER_AUDIO

std::vector<Device> devices = _getDevices();
int defaultOutputDevice = -1;
Info defaultOutputInfo;
_getDefaultOutputDevice(devices, defaultOutputDevice, defaultOutputInfo);
int defaultInputDevice = -1;
Info defaultInputInfo;
_getDefaultInputDevice(devices, defaultInputDevice, defaultInputInfo);

p.devices = observer::List<Device>::create(devices);
p.defaultOutputDevice = observer::Value<int>::create(defaultOutputDevice);
p.defaultOutputInfo = observer::Value<Info>::create(defaultOutputInfo);
p.defaultInputDevice = observer::Value<int>::create(defaultInputDevice);
p.defaultInputInfo = observer::Value<Info>::create(defaultInputInfo);

p.mutex.devices = devices;
p.mutex.defaultOutputDevice = defaultOutputDevice;
p.mutex.defaultOutputInfo = defaultOutputInfo;
p.mutex.defaultInputDevice = defaultInputDevice;
p.mutex.defaultInputInfo = defaultInputInfo;

#if defined(TLRENDER_AUDIO)
if (p.rtAudio)
{
p.thread.running = true;
Expand Down Expand Up @@ -263,19 +280,13 @@ namespace tl
}
}

void System::_run()
std::vector<Device> System::_getDevices()
{
TLRENDER_P();
std::vector<Device> out;
#if defined(TLRENDER_AUDIO)

std::vector<Device> devices;
int defaultOutputDevice = -1;
Info defaultOutputInfo;
int defaultInputDevice = -1;
Info defaultInputInfo;
try
{
// Get the devices.
const unsigned int rtDeviceCount = p.rtAudio->getDeviceCount();
for (unsigned int i = 0; i < rtDeviceCount; ++i)
{
Expand Down Expand Up @@ -316,62 +327,108 @@ namespace tl
{
device.nativeFormats.push_back(DeviceFormat::F64);
}
devices.push_back(device);
out.push_back(device);
}
}
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Cannot get audio devices: " << e.what();
_log(ss.str(), log::Type::Error);
}
#endif // TLRENDER_AUDIO
return out;
}

// Get the output device.
unsigned int device = p.rtAudio->getDefaultOutputDevice();
if (device < devices.size() && devices[device].outputChannels > 0)
void System::_getDefaultOutputDevice(const std::vector<Device>& devices, int& index, Info& info)
{
TLRENDER_P();
index = -1;
#if defined(TLRENDER_AUDIO)
try
{
unsigned int tmp = p.rtAudio->getDefaultOutputDevice();
if (tmp < devices.size() && devices[tmp].outputChannels > 0)
{
defaultOutputDevice = device;
index = tmp;
}
if (defaultOutputDevice >= 0 && defaultOutputDevice < devices.size())
if (index >= 0 && index < devices.size())
{
const auto& device = devices[defaultOutputDevice];
defaultOutputInfo.channelCount = device.outputChannels;
const auto& device = devices[index];
info.channelCount = device.outputChannels;
switch (getBestFormat(device.nativeFormats))
{
case DeviceFormat::S8: defaultOutputInfo.dataType = DataType::S8; break;
case DeviceFormat::S16: defaultOutputInfo.dataType = DataType::S16; break;
case DeviceFormat::S8: info.dataType = DataType::S8; break;
case DeviceFormat::S16: info.dataType = DataType::S16; break;
case DeviceFormat::S24:
case DeviceFormat::S32: defaultOutputInfo.dataType = DataType::S32; break;
case DeviceFormat::F32: defaultOutputInfo.dataType = DataType::F32; break;
case DeviceFormat::F64: defaultOutputInfo.dataType = DataType::F64; break;
default: defaultOutputInfo.dataType = DataType::F32; break;
case DeviceFormat::S32: info.dataType = DataType::S32; break;
case DeviceFormat::F32: info.dataType = DataType::F32; break;
case DeviceFormat::F64: info.dataType = DataType::F64; break;
default: info.dataType = DataType::F32; break;
}
defaultOutputInfo.sampleRate = device.preferredSampleRate;
info.sampleRate = device.preferredSampleRate;
}
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Cannot get default audio output device: " << e.what();
_log(ss.str(), log::Type::Error);
}
#endif // TLRENDER_AUDIO
}

// Get the input device.
device = p.rtAudio->getDefaultInputDevice();
if (device < devices.size() && devices[device].inputChannels > 0)
void System::_getDefaultInputDevice(const std::vector<Device>& devices, int& index, Info& info)
{
TLRENDER_P();
index = -1;
#if defined(TLRENDER_AUDIO)
try
{
unsigned int tmp = p.rtAudio->getDefaultInputDevice();
if (tmp < devices.size() && devices[tmp].inputChannels > 0)
{
defaultInputDevice = device;
index = tmp;
}
if (defaultInputDevice >= 0 && defaultInputDevice < devices.size())
if (index >= 0 && index < devices.size())
{
const auto& device = devices[defaultInputDevice];
defaultInputInfo.channelCount = device.inputChannels;
const auto& device = devices[index];
info.channelCount = device.inputChannels;
switch (getBestFormat(device.nativeFormats))
{
case DeviceFormat::S8: defaultInputInfo.dataType = DataType::S8; break;
case DeviceFormat::S16: defaultInputInfo.dataType = DataType::S16; break;
case DeviceFormat::S8: info.dataType = DataType::S8; break;
case DeviceFormat::S16: info.dataType = DataType::S16; break;
case DeviceFormat::S24:
case DeviceFormat::S32: defaultInputInfo.dataType = DataType::S32; break;
case DeviceFormat::F32: defaultInputInfo.dataType = DataType::F32; break;
case DeviceFormat::F64: defaultInputInfo.dataType = DataType::F64; break;
default: defaultInputInfo.dataType = DataType::F32; break;
case DeviceFormat::S32: info.dataType = DataType::S32; break;
case DeviceFormat::F32: info.dataType = DataType::F32; break;
case DeviceFormat::F64: info.dataType = DataType::F64; break;
default: info.dataType = DataType::F32; break;
}
defaultInputInfo.sampleRate = device.preferredSampleRate;
info.sampleRate = device.preferredSampleRate;
}
}
catch (const std::exception& e)
{
std::stringstream ss;
ss << "Cannot get audio information: " << e.what();
ss << "Cannot get default audio input device: " << e.what();
_log(ss.str(), log::Type::Error);
}
#endif // TLRENDER_AUDIO
}

void System::_run()
{
TLRENDER_P();
#if defined(TLRENDER_AUDIO)

std::vector<Device> devices = _getDevices();
int defaultOutputDevice = -1;
Info defaultOutputInfo;
_getDefaultOutputDevice(devices, defaultOutputDevice, defaultOutputInfo);
int defaultInputDevice = -1;
Info defaultInputInfo;
_getDefaultInputDevice(devices, defaultInputDevice, defaultInputInfo);

if (devices != p.thread.devices)
{
Expand Down
4 changes: 4 additions & 0 deletions lib/tlCore/AudioSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,10 @@ namespace tl
std::chrono::milliseconds getTickTime() const override;

private:
std::vector<Device> _getDevices();
void _getDefaultOutputDevice(const std::vector<Device>&, int&, Info&);
void _getDefaultInputDevice(const std::vector<Device>&, int&, Info&);

void _run();

TLRENDER_PRIVATE();
Expand Down
1 change: 1 addition & 0 deletions lib/tlTimeline/Player.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ namespace tl
try
{
p.thread.rtAudio.reset(new RtAudio);
p.thread.rtAudio->showWarnings(false);
}
catch (const std::exception& e)
{
Expand Down

0 comments on commit 718ce62

Please sign in to comment.