From cb43cc7e322014ce2bd0ee73b06d403be62fa8d5 Mon Sep 17 00:00:00 2001
From: sharmander <saldabain.dev@gmail.com>
Date: Thu, 23 Dec 2021 11:33:56 -0500
Subject: UI - Add Volume Controls + Mute Toggle (F2) (#2871)

* Add the ability to toggle mute in the status bar.

* Add the ability to toggle mute in the status bar.

* Formatting fixes

* Add hotkey (F2) to mute

* Add default hotkey to config.json

* Add ability to change volume via slider.

* Fix Headless

* Fix SDL2 Problem : Credits to d3xMachina

* Remove unnecessary work

* Address gdk comments

* Toggling with Hotkey now properly restores volume to original level.

* Toggling with Hotkey now properly restores volume to original level.

* Update UI to show Volume % instead of Muted/Unmuted

* Clean up the volume ui a bit.

* Undo unintentionally committed code.

* Implement AudRen Support

* Restore intiial volume level in function definition.

* Finalize UI

* Finalize UI

* Use clamp for bounds check

* Use Math.Clamp for volume in soundio

* Address comments by gdkchan

* Address remaining comments

* Fix missing semicolon

* Address remaining gdkchan comment

* Fix comment

* Change /* to //

* Allow volume slider to change volume immediately.
Also force label text to cast to int to prevent decimals from showing in status bar

* Remove blank line

* Undo setting of volume level when "Cancel" is pressed.

* Fix allignment for settings window code
---
 .../SDL2HardwareDeviceSession.cs                      | 19 ++++++++++++-------
 1 file changed, 12 insertions(+), 7 deletions(-)

(limited to 'Ryujinx.Audio.Backends.SDL2/SDL2HardwareDeviceSession.cs')

diff --git a/Ryujinx.Audio.Backends.SDL2/SDL2HardwareDeviceSession.cs b/Ryujinx.Audio.Backends.SDL2/SDL2HardwareDeviceSession.cs
index 843de01a..33e1632d 100644
--- a/Ryujinx.Audio.Backends.SDL2/SDL2HardwareDeviceSession.cs
+++ b/Ryujinx.Audio.Backends.SDL2/SDL2HardwareDeviceSession.cs
@@ -26,7 +26,7 @@ namespace Ryujinx.Audio.Backends.SDL2
         private float _volume;
         private ushort _nativeSampleFormat;
 
-        public SDL2HardwareDeviceSession(SDL2HardwareDeviceDriver driver, IVirtualMemoryManager memoryManager, SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount) : base(memoryManager, requestedSampleFormat, requestedSampleRate, requestedChannelCount)
+        public SDL2HardwareDeviceSession(SDL2HardwareDeviceDriver driver, IVirtualMemoryManager memoryManager, SampleFormat requestedSampleFormat, uint requestedSampleRate, uint requestedChannelCount, float requestedVolume) : base(memoryManager, requestedSampleFormat, requestedSampleRate, requestedChannelCount)
         {
             _driver = driver;
             _updateRequiredEvent = _driver.GetUpdateRequiredEvent();
@@ -37,7 +37,7 @@ namespace Ryujinx.Audio.Backends.SDL2
             _nativeSampleFormat = SDL2HardwareDeviceDriver.GetSDL2Format(RequestedSampleFormat);
             _sampleCount = uint.MaxValue;
             _started = false;
-            _volume = 1.0f;
+            _volume = requestedVolume;
         }
 
         private void EnsureAudioStreamSetup(AudioBuffer buffer)
@@ -82,7 +82,7 @@ namespace Ryujinx.Audio.Backends.SDL2
 
             if (frameCount == 0)
             {
-                // SDL2 left the responsability to the user to clear the buffer.
+                // SDL2 left the responsibility to the user to clear the buffer.
                 streamSpan.Fill(0);
 
                 return;
@@ -92,11 +92,16 @@ namespace Ryujinx.Audio.Backends.SDL2
 
             _ringBuffer.Read(samples, 0, samples.Length);
 
-            samples.AsSpan().CopyTo(streamSpan);
-            streamSpan.Slice(samples.Length).Fill(0);
+            fixed (byte* p = samples)
+            {
+                IntPtr pStreamSrc = (IntPtr)p;
+
+                // Zero the dest buffer
+                streamSpan.Fill(0);
 
-            // Apply volume to written data
-            SDL_MixAudioFormat(stream, stream, _nativeSampleFormat, (uint)samples.Length, (int)(_volume * SDL_MIX_MAXVOLUME));
+                // Apply volume to written data
+                SDL_MixAudioFormat(stream, pStreamSrc, _nativeSampleFormat, (uint)samples.Length, (int)(_volume * SDL_MIX_MAXVOLUME));
+            }
 
             ulong sampleCount = GetSampleCount(samples.Length);
 
-- 
cgit v1.2.3-70-g09d2