Placeholder-Software / Dissonance

Unity Voice Chat Asset
69 stars 5 forks source link

[bug] Bluetooth Audio and Switching Audio Devices #194

Closed cjp585 closed 3 years ago

cjp585 commented 4 years ago

184 Context

Using the PUN2 Game World Demo, I noticed the audio on my bluetooth headphones (AirPods) was choppy and distorted. When I removed my headphones, switching the audio to the Macbook speakers, the audio sounds fine with no choppiness. If I put my bluetooth headphones back on there is no more sound and I can't recover it by switching back to my computer's speakers. And anytime I switch audio devices I get an error: image

Some more tests I ran and the results:

  1. The first time you run a new standalone build, the audio through bluetooth sounds good until you authorize access to the microphone. After authorizing, the audio becomes choppy.

  2. If the build can't find the Opus plugins like in this bug (https://github.com/Placeholder-Software/Dissonance/issues/193) the audio resets and goes from being choppy to sounding OK again. image

  3. I've run this test with both networked voice chat audio and a standalone audiosource. The issue appears via both sources.

The next test I want to run is switching out PUN 2 for Forge Networking Remastered to see if the issue lies with PUN 2.

Let me know if there's any more info I can gather to help.

Your Environment

martindevans commented 4 years ago

It sounds like this may be an issue with the airpods lacking bandwidth to do high quality audio playback and capture at the same time (a common problem with bluetooth). Could you try this script to see if you get the same problem without Dissonance in the loop at all.

If you get the same issue in that script then there are a couple of things that can be done:

cjp585 commented 4 years ago

The issue did exist when running that script. I played with the DSP and sample rate settings. Changing sample rates didn't seem to have any effect, but changing the DSP to "Good latency" fixed the issue! I verified that it worked with a Dissonance voice chat too. Thank you for the help.

Any suggestions for the switching audio devices errors?

martindevans commented 4 years ago

The switching errors aren't something I've seen before. Do they only show up when you're using Dissonance, or can you reproduce them in a simpler setup (e.g. switch audio devices while just playing a sound with unity)?

cjp585 commented 4 years ago

Those specific errors have only shown up with Dissonance though the failure mode has shown up in other voice chats I've tried (ie Photon Voice). When just playing a sound it works fine. I can switch between my Mac speakers and headphones and the audio routes properly.

cjp585 commented 4 years ago

Another oddity with Bluetooth headphones, probably related to the previous issue: when I use wired headphones, Dissonance's internal spatialization works well, but when I switch to bluetooth headphones spatialization stops. It sounds purely 2D.

The network jitter was much higher when using bluetooth headphones (~50 sigma ms for wired, ~140 sigma ms for bluetooth). Does Dissonance switch off spatialization when the network jitter is too high? Also, for both wired and bluetooth headphones the slider for 2D-3D spatialization was jumping between the two on the audio source on the PlaybackPrefab.

edit - I tried this with the microphone test script you sent above and found the same issue so it's not coming from Dissonance.

martindevans commented 4 years ago

Does Dissonance switch off spatialization when the network jitter is too high?

No, spatialisation is controlled entirely by what you set on the voice broadcast trigger (the Use Positional Data checkbox).

for both wired and bluetooth headphones the slider for 2D-3D spatialization was jumping between the two

This is just an artifact of the way Dissonance applies spatialisation. When no speech is being played it sets the slider to it's default value. When speech is being played it moves it to whatever the current speech playback requires. If you're using VAD it'll jump around every time you take a breath and the speech stream stops briefly.

I tried this with the microphone test script you sent above

Perhaps the airpods disable stereo playback and force everything to be downmixed to mono when the microphone is in use (to save bandwidth).

Switching error...

Going back to the switching audio device error, I found some related errors: this very old one which is marked as fixed, then this more recent one marked as won't fix! I also found an old bug report on Dissonance itself, this person had the same error message but despite that audio still worked just fine.

Could you try capturing the audio configuration when the application starts up and sound is working fine with var startConfig = AudioSettings.GetConfiguration(); and add in a button that call AudioSettings.Reset(startConfig). When you get the error after switching devices see if clicking the button fixes audio output (try with both Dissonance and a normal AudioSource, if possible). Thanks.

cjp585 commented 4 years ago

Tried this with a normal AudioSource first. Resetting the AudioSettings kills the audio just like switching headphones does. So the scenarios were:

  1. Start with audio playing out of laptop and it sounds good. Switch to bluetooth headphones and the audio dies. Resetting the AudioSettings does not recover it. The reverse is also true (bluetooth -> laptop speakers).
  2. Start with audio playing out of laptop (or bluetooth) -> don't switch devices, just press key to reset AudioSettings -> audio dies

Once the audio dies (either by switching devices or resetting the AudioSettings) I found you can get the audio back by de-activating then re-activating the AudioSource.

martindevans commented 4 years ago

This sounds like it might be worth reporting as a bug to Unity. AudioSettings.Reset isn't documented as interrupting all running audio sources and it doesn't sound like something that it should do either.

As for a workaround in Dissonance - if you can detect when this happens I think the easiest thing to do might be to disconnect from the voice session entirely and reconnect (after resetting the audio device). This will totally reset all the input and output components of Dissonance and will hopefully restore voice audio to normal functioning.

martindevans commented 4 years ago

Hi @cjp585 did you make any progress with reporting this to Unity and/or did the workaround I suggested help?

cjp585 commented 4 years ago

I haven't reported a bug to Unity because I found documentation that showed you have to explicitly re-play and AudioSource after resetting the Audio Configuration: https://docs.unity3d.com/ScriptReference/AudioSettings.Reset.html

When I tried this with my standalone AudioSource it worked.

However, the issue still remains when simultaneously recording from a microphone and emitting from a AudioSource as would be the case with Dissonance. I haven't fully dug into resetting the mic after resetting the Audio Configuration.

martindevans commented 3 years ago

It looks like this issue is pretty stale so I'll close it for now. If it's still an issue you need help with don't hesitate to re-open it :)