Reset sample rate to initial state on OSX output

This commit is contained in:
Christian Kröner 2018-03-07 22:38:09 +01:00
parent 4be80982a4
commit 5b456cadc4
1 changed files with 41 additions and 2 deletions

View File

@ -62,7 +62,7 @@ struct OSXOutput final : AudioOutput {
AudioDeviceID dev_id; AudioDeviceID dev_id;
AudioComponentInstance au; AudioComponentInstance au;
AudioStreamBasicDescription asbd; AudioStreamBasicDescription asbd;
Float64 sample_rate; Float64 initial_sample_rate;
Manual<PcmExport> pcm_export; Manual<PcmExport> pcm_export;
boost::lockfree::spsc_queue<uint8_t> *ring_buffer; boost::lockfree::spsc_queue<uint8_t> *ring_buffer;
@ -649,6 +649,24 @@ OSXOutput::Enable()
pcm_export.Destruct(); pcm_export.Destruct();
throw; throw;
} }
AudioObjectPropertyAddress aopa = {
kAudioDevicePropertyNominalSampleRate,
kAudioObjectPropertyScopeOutput,
kAudioObjectPropertyElementMaster
};
UInt32 property_size = sizeof(initial_sample_rate);
status = AudioObjectGetPropertyData(dev_id,
&aopa,
0,
NULL,
&property_size,
&initial_sample_rate);
if(status != noErr) {
AudioComponentInstanceDispose(au);
pcm_export.Destruct();
throw std::runtime_error("Cannot get sample rate of macOS output device");
}
if (hog_device) if (hog_device)
osx_output_hog_device(dev_id, true); osx_output_hog_device(dev_id, true);
@ -667,9 +685,29 @@ OSXOutput::Disable() noexcept
void void
OSXOutput::Close() noexcept OSXOutput::Close() noexcept
{ {
AudioObjectPropertyAddress aopa = {
kAudioDevicePropertyNominalSampleRate,
kAudioObjectPropertyScopeOutput,
kAudioObjectPropertyElementMaster
};
OSStatus err;
AudioOutputUnitStop(au); AudioOutputUnitStop(au);
AudioUnitUninitialize(au); AudioUnitUninitialize(au);
// Reset sample rate to initial state
if(sync_sample_rate
#ifdef ENABLE_DSD
|| dop_setting
#endif
) {
err = AudioObjectSetPropertyData(dev_id,
&aopa,
0,
NULL,
sizeof(initial_sample_rate),
&initial_sample_rate);
if(err != noErr)
FormatWarning(osx_output_domain, "Unable to reset sample rate of macOS output device");
}
delete ring_buffer; delete ring_buffer;
} }
@ -680,6 +718,7 @@ OSXOutput::Open(AudioFormat &audio_format)
#ifdef ENABLE_DSD #ifdef ENABLE_DSD
bool dop = dop_setting; bool dop = dop_setting;
#endif #endif
Float64 sample_rate = initial_sample_rate;
PcmExport::Params params; PcmExport::Params params;
params.alsa_channel_order = true; params.alsa_channel_order = true;
params.dop = false; params.dop = false;