From 5a40751a8353703aa5ae914def6fef8b40213033 Mon Sep 17 00:00:00 2001 From: jules Date: Fri, 16 May 2008 15:31:40 +0000 Subject: [PATCH] --- .../audio/devices/juce_AudioDeviceManager.cpp | 40 ++++++++++++++++++- .../audio/devices/juce_AudioDeviceManager.h | 25 +++++++++++- 2 files changed, 62 insertions(+), 3 deletions(-) diff --git a/src/juce_appframework/audio/devices/juce_AudioDeviceManager.cpp b/src/juce_appframework/audio/devices/juce_AudioDeviceManager.cpp index 9c819dd866..85379b2c9f 100644 --- a/src/juce_appframework/audio/devices/juce_AudioDeviceManager.cpp +++ b/src/juce_appframework/audio/devices/juce_AudioDeviceManager.cpp @@ -106,7 +106,7 @@ const String AudioDeviceManager::initialise (const int numInputChannelsNeeded, setMidiInputEnabled (allMidiIns[i], enabledMidiIns.contains (allMidiIns[i])); if (error.isNotEmpty() && selectDefaultDeviceOnFailure) - error = initialise (numInputChannelsNeeded, numOutputChannelsNeeded, 0, + error = initialise (numInputChannelsNeeded, numOutputChannelsNeeded, 0, false, preferredDefaultDeviceName); setDefaultMidiOutput (e->getStringAttribute (T("defaultMidiOutput"))); @@ -357,6 +357,44 @@ void AudioDeviceManager::stopDevice() currentAudioDevice->stop(); } +void AudioDeviceManager::closeAudioDevice() +{ + if (currentAudioDevice != 0) + { + lastRunningDevice = currentAudioDevice->getName(); + lastRunningBlockSize = currentAudioDevice->getCurrentBufferSizeSamples(); + lastRunningSampleRate = currentAudioDevice->getCurrentSampleRate(); + lastRunningIns = inputChannels; + lastRunningOuts = outputChannels; + + stopDevice(); + + setAudioDevice (String::empty, 0, 0, 0, 0, false); + } +} + +void AudioDeviceManager::restartLastAudioDevice() +{ + if (currentAudioDevice == 0) + { + if (lastRunningDevice.isEmpty()) + { + // This method will only reload the last device that was running + // before closeAudioDevice() was called - you need to actually open + // one first, with setAudioDevice(). + jassertfalse + return; + } + + setAudioDevice (lastRunningDevice, + lastRunningBlockSize, + lastRunningSampleRate, + &lastRunningIns, + &lastRunningOuts, + false); + } +} + void AudioDeviceManager::setInputChannels (const BitArray& newEnabledChannels, const bool treatAsChosenDevice) { diff --git a/src/juce_appframework/audio/devices/juce_AudioDeviceManager.h b/src/juce_appframework/audio/devices/juce_AudioDeviceManager.h index 636645c490..5b7ca03263 100644 --- a/src/juce_appframework/audio/devices/juce_AudioDeviceManager.h +++ b/src/juce_appframework/audio/devices/juce_AudioDeviceManager.h @@ -109,12 +109,12 @@ public: fails to open, then a default device will be used instead. If false, then on failure, no device is opened. - @param preferredDefaultDeviceName if this is not empty, and there's a device with this + @param preferredDefaultDeviceName if this is not empty, and there's a device with this name, then that will be used as the default device (assuming that there wasn't one specified in the XML). The string can actually be a simple wildcard, containing "*" and "?" characters - + @returns an error message if anything went wrong, or an empty string if it worked ok. */ const String initialise (const int numInputChannelsNeeded, @@ -195,6 +195,23 @@ public: const BitArray* outputChans, const bool treatAsChosenDevice); + /** Closes the currently-open device. + + You can call restartLastAudioDevice() later to reopen it in the same state + that it was just in. + */ + void closeAudioDevice(); + + /** Tries to reload the last audio device that was running. + + Note that this only reloads the last device that was running before + closeAudioDevice() was called - it doesn't reload any kind of saved-state, + and can only be called after a device has been opened with SetAudioDevice(). + + If a device is already open, this call will do nothing. + */ + void restartLastAudioDevice(); + /** Returns the name of the currently selected audio device. This will be an empty string if none is active. @@ -384,6 +401,10 @@ private: }; CallbackHandler callbackHandler; + String lastRunningDevice; + int lastRunningBlockSize; + double lastRunningSampleRate; + BitArray lastRunningIns, lastRunningOuts; friend class CallbackHandler;