@@ -123,23 +123,6 @@ AudioComponent::~AudioComponent()
123123
124124}
125125
126- void AudioComponent::setBufferSize (int s)
127- {
128- AudioDeviceManager::AudioDeviceSetup setup;
129- deviceManager.getAudioDeviceSetup (setup);
130-
131- if (s > 16 && s < 6000 )
132- {
133- setup.bufferSize = s;
134- deviceManager.setAudioDeviceSetup (setup, false );
135- }
136- else
137- {
138- std::cout << " Buffer size out of range." << std::endl;
139- }
140-
141- }
142-
143126int AudioComponent::getBufferSize ()
144127{
145128 AudioDeviceManager::AudioDeviceSetup setup;
@@ -280,3 +263,74 @@ void AudioComponent::endCallbacks()
280263
281264}
282265
266+ void AudioComponent::saveStateToXml (XmlElement* parent)
267+ {
268+ // JUCE's audioState XML format (includes all info)
269+ ScopedPointer<XmlElement> audioState = deviceManager.createStateXml ();
270+
271+ if (audioState != nullptr )
272+ {
273+ parent->addChildElement (audioState.release ());
274+ }
275+
276+ // Save type, buffer size, and sample rate as attributes separately
277+ // in case part of the audioState is invalid later, like the specific device names.
278+ AudioDeviceManager::AudioDeviceSetup setup;
279+ deviceManager.getAudioDeviceSetup (setup);
280+
281+ parent->setAttribute (" sampleRate" , setup.sampleRate );
282+ parent->setAttribute (" bufferSize" , setup.bufferSize );
283+ parent->setAttribute (" deviceType" , deviceManager.getCurrentAudioDeviceType ());
284+ }
285+
286+ void AudioComponent::loadStateFromXml (XmlElement* parent)
287+ {
288+ forEachXmlChildElement (*parent, child)
289+ {
290+ if (!child->isTextElement ())
291+ {
292+ deviceManager.closeAudioDevice (); // necessary to ensure correct device type gets created
293+
294+ String error = deviceManager.initialise (
295+ 0 , // numInputChannelsNeeded
296+ 2 , // numOutputChannelsNeeded
297+ child, // savedState
298+ true , // selectDefaultDeviceOnFailure
299+ String::empty, // preferred device
300+ nullptr ); // preferred device setup options
301+
302+ if (error.isEmpty ())
303+ {
304+ break ;
305+ }
306+ }
307+ }
308+
309+ // Now the important parameters separately, as a backup (in case the devices have different names or something)
310+ String deviceType = parent->getStringAttribute (" deviceType" , String::empty);
311+ if (!deviceType.isEmpty ())
312+ {
313+ deviceManager.setCurrentAudioDeviceType (deviceType, true );
314+ }
315+
316+ AudioDeviceManager::AudioDeviceSetup setup;
317+ deviceManager.getAudioDeviceSetup (setup);
318+
319+ double sampleRate = parent->getDoubleAttribute (" sampleRate" );
320+ if (sampleRate > 0 )
321+ {
322+ setup.sampleRate = sampleRate;
323+ }
324+
325+ int bufferSize = parent->getIntAttribute (" bufferSize" );
326+ if (bufferSize > 16 && bufferSize < 6000 )
327+ {
328+ setup.bufferSize = bufferSize;
329+ }
330+ else
331+ {
332+ std::cout << " Buffer size out of range." << std::endl;
333+ }
334+
335+ deviceManager.setAudioDeviceSetup (setup, true );
336+ }
0 commit comments