adrianstevens / Xamarin-Plugins

Cross-platform Plugins for Xamarin, Xamarin.Forms and Windows
https://www.nuget.org/packages/Xam.Plugin.SimpleAudioPlayer/
MIT License
133 stars 53 forks source link

Balance Doesn't Work #40

Closed robbpriestley closed 5 years ago

robbpriestley commented 6 years ago

I can't seem to get balance to work.

Assigning it doubles in the range -1 to 1.

I have verified volume is working.

Is it possibly because balance only works with monophonic sounds and I am using all stereo sounds?

Tested with the iOS implementation.

robbpriestley commented 6 years ago

OK, I've just performed some tests. Using monophonic sounds, I tested various values for Balance. It doesn't seem to matter what Balance value I use, to my ears it sounds the same. Examples: (-0.9999, -0.00001, 0, 0.5, 0.999, 3).

Except for extremely small positive values such as 0.00001 and 0.00000001. These don't actually seem to affect balance. Instead, oddly enough, they seem to affect the duration of the audible sound. Although this could just be some sort of sonic "artifact" as I don't believe it is actually trying to limit the length.

I'm pretty sure there's a bug related to the use of Balance.

robbpriestley commented 6 years ago

I forked the code and I've just spent the evening exploring the balance situation.


Bug #1 /Xamarin-Plugins/SimpleAudioPlayer/SimpleAudioPlayer/Plugin.SimpleAudioPlayer.iOS/SimpleAudioPlayerImplementation.cs in SetVolume() line 187

player.SetVolume((float)left, (float)right); is incorrect because it takes floats for volume, duration (unlike the Android version).

I think what we should be using is player.Volume and player.Pan. But Pan takes a float -0.1 <= x <= 1.0 so converting to left, right is not necessary.

Now, my dev problem here is that whenever I call our ISimpleAudioPlayer.Volume or ISimpleAudioPlayer.Balance, the audio shoots over to the far right channel. Debugging the iOS code is not working well, but I can see the Pan is getting set to 0.9999 --- somewhere??


Bug #2 /Xamarin-Plugins/SimpleAudioPlayer/SimpleAudioPlayer/Plugin.SimpleAudioPlayer.Android/SimpleAudioPlayerImplementation.cs in SetVolume() line 221

Setting the opposite side to "volume" is probably incorrect.

Let's say volume = 1 (max) and Balance is set to -0.8. This should put 80% of the sound output to left and 20% to right, but the way the code is written, 80% will go left and 100% will go right. This will result in basically no perceptible difference.

Now, my dev problem here is that when I updated the code to fix the problem I just outlined, I got no sound whatsoever out of the player.


I was hoping to be able to submit a pull request, but I had some issues I wasn't able to resolve. Still, I hope this information helps. Maybe I will be able to look at it some more later on.

References:

robbpriestley commented 6 years ago

Update

After further research and experimentation, I created Pull Request #41. There are comments in the Pull Request.

Basically, iOS was easy to fix as it provides a Pan property. Android required some math.

My comments in section "Bug #2" above are not 100% accurate. What I was imagining there was something called the "linear pan rule." In the end I chose the superior "constant power pan rule" to help keep the volume amplitude up.

I hope this helps.

adrianstevens commented 5 years ago

Thanks @robbpriestley - I've merged your PR - I'll try and get a new build out next week

Cheers!

Adrian

adrianstevens commented 5 years ago

version 1.3.0 is now live - this should be all fixed

thank you again!

PierreLucB commented 5 years ago

Great Job I had the problem too, I'm glad to hear it's fixed ! Many thanks !

adrianstevens commented 5 years ago

Thanks again everyone - closing issue