# Problems with setAudioDeviceSetup of AudioDeviceManager

**URL:** <https://forum.juce.com/t/problems-with-setaudiodevicesetup-of-audiodevicemanager/9176>\
**Category:** General JUCE discussion\
**Created:** [August 30, 2012, 9:26am UTC](https://forum.juce.com/t/problems-with-setaudiodevicesetup-of-audiodevicemanager/9176 "2012-08-30T09:26:18Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![andrea](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andrea](https://forum.juce.com/u/andrea)\
**Post date:** [August 30, 2012, 9:26am UTC](https://forum.juce.com/t/problems-with-setaudiodevicesetup-of-audiodevicemanager/9176/1 "2012-08-30T09:26:18Z")

</div>

Hi,  
I have problems using `setAudioDeviceSetup (const AudioDeviceSetup &newSetup, bool treatAsChosenDevice)`  
It always return error. The returned strings are “Can’t open the audio device! This may be because another application is currently using the same device - if so, you should close any other applications and try again!” or “Couldn’t open the input device!”

I am writing a simple audioserver, so I want to remotely control the behaviour of the soundcard.  
My server creates the AudioDeviceManager and initialise it, `playTestSound()` produces sound (I call it remotely using a JSON message) so I think the audio system is working properly.  
Now the client says “Server change buffer\_size 512”, the server calls `getAudioDeviceSetup (AudioDeviceSetup &setup)` to obtain the actual config, then tweaks the bufferSize param and calls `setAudioDeviceSetup (const AudioDeviceSetup &newSetup, bool treatAsChosenDevice)`

Looking at the code of AudioDeviceSelectorComponent it seems to me to do the same thing but probably not as my app doesn’t work… 🙂  
instead AudioDemo works perfectly!

Hope someone could help me,  
bye

Andrea

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 30, 2012, 9:52am UTC](https://forum.juce.com/t/problems-with-setaudiodevicesetup-of-audiodevicemanager/9176/2 "2012-08-30T09:52:43Z")

</div>

You should debug the process of opening the device, and see how far it gets.

---

<div class="post-metadata">

**Author:** ![andrea](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andrea](https://forum.juce.com/u/andrea)\
**Post date:** [August 30, 2012, 11:56am UTC](https://forum.juce.com/t/problems-with-setaudiodevicesetup-of-audiodevicemanager/9176/3 "2012-08-30T11:56:18Z")

</div>

Debugging I noticed where things go wrong but I cannot find a solution:

I have initialised with

```auto

```

Then I ask the audioDeviceManager to change to 2nd output device (as an example)

`audioDeviceManager->getAudioDeviceSetup(actualSetup);
actualSetup.outputDeviceName = "mySecondOutputDevice";
actualSetup.useDefaultOutputChannels = true;
audioDeviceManager->setAudioDeviceSetup(actualSetup, false);
`  
Now in juce\_AudioDeviceManager.cpp (line 386)

```auto

```

returns nullptr

In particular `device.initialise()` in juce\_win32\_WASAPI.cpp (line 1035) fails.  
initialise() calls createDevice(), the 1st condition is verified

```auto

```

so it return false…

After that… I continue not to understand why when the audioEngine starts I can listen sound but I cannot change anything…

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 30, 2012, 12:03pm UTC](https://forum.juce.com/t/problems-with-setaudiodevicesetup-of-audiodevicemanager/9176/4 "2012-08-30T12:03:27Z")

</div>

Well, if the OS can’t even create an instance of the device enumerator, then something is pretty screwed-up! I’ve no idea what you’d have to do to make that fail!

You’re calling it from the main message thread, right?

---

<div class="post-metadata">

**Author:** ![andrea](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andrea](https://forum.juce.com/u/andrea)\
**Post date:** [August 30, 2012, 12:45pm UTC](https://forum.juce.com/t/problems-with-setaudiodevicesetup-of-audiodevicemanager/9176/5 "2012-08-30T12:45:54Z")

</div>

mmm, maybe not:  
audioDeviceManager is a member variable of MyClass\_ServiceHandler.

MyClass\_ServiceHandler has a method called “handleMessage” that is called by MyClass\_Connection::messageReceived(const MemoryBlock &message)  
so the operation on audioDeviceManager is executed by the thread of MyClass\_Connection.

The interaction is something like:  
MyClass\_Connection (inherits from InterprocessConnection) when receives a message calls MyClass\_ServiceHandler::handleMessage that based on the message could calls audioDeviceManager to tweak the soundcard.

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 30, 2012, 12:52pm UTC](https://forum.juce.com/t/problems-with-setaudiodevicesetup-of-audiodevicemanager/9176/6 "2012-08-30T12:52:28Z")

</div>

Well WASAPI uses COM objects, so it’ll fail if you try to call it on a thread that hasn’t had CoInitialize called.

---

<div class="post-metadata">

**Author:** ![andrea](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andrea](https://forum.juce.com/u/andrea)\
**Post date:** [August 30, 2012, 1:14pm UTC](https://forum.juce.com/t/problems-with-setaudiodevicesetup-of-audiodevicemanager/9176/7 "2012-08-30T13:14:00Z")

</div>

My knowledge stops here… ☹  
How to call CoInitialize(0) in the thread of connection? I have tried to add that in the constructor of MyClass\_Connection but obviously it is not accepted as it is not a method of MyClass\_Connection, InterprocessConnection, Thread…

---

<div class="post-metadata">

**Author:** ![jules](https://avatars.discourse-cdn.com/v4/letter/j/41988e/32.png) [@jules](https://forum.juce.com/u/jules)\
**Post date:** [August 30, 2012, 2:44pm UTC](https://forum.juce.com/t/problems-with-setaudiodevicesetup-of-audiodevicemanager/9176/8 "2012-08-30T14:44:39Z")

</div>

If you’re using an InterprocessConnection, why not just set it to use the message thread? It’s an option in the constructor.

---

<div class="post-metadata">

**Author:** ![andrea](https://avatars.discourse-cdn.com/v4/letter/a/c37758/32.png) [@andrea](https://forum.juce.com/u/andrea)\
**Post date:** [August 30, 2012, 2:55pm UTC](https://forum.juce.com/t/problems-with-setaudiodevicesetup-of-audiodevicemanager/9176/9 "2012-08-30T14:55:18Z")

</div>

You are my idol !!!  
It is great when only changing a param from “false” to “true” makes everything to work!

Thank you!

Andrea
