From f9b6e2ef69692527e637f741ebc5fe0d79d7ed35 Mon Sep 17 00:00:00 2001 From: reuk Date: Wed, 9 Nov 2022 11:22:08 +0000 Subject: [PATCH] CoreAudio: Refactoring --- .../native/juce_mac_CoreAudio.cpp | 57 +++++++++---------- 1 file changed, 27 insertions(+), 30 deletions(-) diff --git a/modules/juce_audio_devices/native/juce_mac_CoreAudio.cpp b/modules/juce_audio_devices/native/juce_mac_CoreAudio.cpp index f3963f3c6e..ffdf9856b8 100644 --- a/modules/juce_audio_devices/native/juce_mac_CoreAudio.cpp +++ b/modules/juce_audio_devices/native/juce_mac_CoreAudio.cpp @@ -460,8 +460,8 @@ public: auto newBufferSizes = getBufferSizesFromDevice(); auto newSampleRates = getSampleRatesFromDevice(); - std::unique_ptr newInput (inStream != nullptr ? new Stream (true, *this, activeIns) : nullptr); - std::unique_ptr newOutput (outStream != nullptr ? new Stream (false, *this, activeOuts) : nullptr); + auto newInput = rawToUniquePtr (inStream != nullptr ? new Stream (true, *this, activeIns) : nullptr); + auto newOutput = rawToUniquePtr (outStream != nullptr ? new Stream (false, *this, activeOuts) : nullptr); auto newBitDepth = jmax (getBitDepth (newInput), getBitDepth (newOutput)); @@ -616,8 +616,9 @@ public: //============================================================================== String reopen (const BigInteger& ins, const BigInteger& outs, double newSampleRate, int bufferSizeSamples) { - String error; callbacksAllowed = false; + const ScopeGuard scope { [&] { callbacksAllowed = true; } }; + stopTimer(); stop (false); @@ -625,37 +626,33 @@ public: if (! setNominalSampleRate (newSampleRate)) { updateDetailsFromDevice (ins, outs); - error = "Couldn't change sample rate"; + return "Couldn't change sample rate"; } - else + + if (! audioObjectSetProperty (deviceID, { kAudioDevicePropertyBufferFrameSize, + kAudioObjectPropertyScopeGlobal, + juceAudioObjectPropertyElementMain }, + static_cast (bufferSizeSamples), err2log())) { - if (! audioObjectSetProperty (deviceID, { kAudioDevicePropertyBufferFrameSize, - kAudioObjectPropertyScopeGlobal, - juceAudioObjectPropertyElementMain }, - static_cast (bufferSizeSamples), err2log())) - { - updateDetailsFromDevice (ins, outs); - error = "Couldn't change buffer size"; - } - else - { - // Annoyingly, after changing the rate and buffer size, some devices fail to - // correctly report their new settings until some random time in the future, so - // after calling updateDetailsFromDevice, we need to manually bodge these values - // to make sure we're using the correct numbers.. - updateDetailsFromDevice (ins, outs); - sampleRate = newSampleRate; - bufferSize = bufferSizeSamples; - - if (sampleRates.size() == 0) - error = "Device has no available sample-rates"; - else if (bufferSizes.size() == 0) - error = "Device has no available buffer-sizes"; - } + updateDetailsFromDevice (ins, outs); + return "Couldn't change buffer size"; } - callbacksAllowed = true; - return error; + // Annoyingly, after changing the rate and buffer size, some devices fail to + // correctly report their new settings until some random time in the future, so + // after calling updateDetailsFromDevice, we need to manually bodge these values + // to make sure we're using the correct numbers.. + updateDetailsFromDevice (ins, outs); + sampleRate = newSampleRate; + bufferSize = bufferSizeSamples; + + if (sampleRates.size() == 0) + return "Device has no available sample-rates"; + + if (bufferSizes.size() == 0) + return "Device has no available buffer-sizes"; + + return {}; } bool start (AudioIODeviceCallback* callbackToNotify)