R/Ryujinx.Audio/Renderer/Utils/FileHardwareDevice.cs
sharmander cb43cc7e32
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
2021-12-23 13:33:56 -03:00

116 lines
3.4 KiB
C#

//
// Copyright (c) 2019-2021 Ryujinx
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <https://www.gnu.org/licenses/>.
//
using Ryujinx.Audio.Integration;
using System;
using System.IO;
using System.Runtime.InteropServices;
using System.Text;
namespace Ryujinx.Audio.Renderer.Utils
{
/// <summary>
/// A <see cref="IHardwareDevice"/> that outputs to a wav file.
/// </summary>
public class FileHardwareDevice : IHardwareDevice
{
private FileStream _stream;
private uint _channelCount;
private uint _sampleRate;
private const int HeaderSize = 44;
public FileHardwareDevice(string path, uint channelCount, uint sampleRate)
{
_stream = File.OpenWrite(path);
_channelCount = channelCount;
_sampleRate = sampleRate;
_stream.Seek(HeaderSize, SeekOrigin.Begin);
}
private void UpdateHeader()
{
var writer = new BinaryWriter(_stream);
long currentPos = writer.Seek(0, SeekOrigin.Current);
writer.Seek(0, SeekOrigin.Begin);
writer.Write(Encoding.ASCII.GetBytes("RIFF"));
writer.Write((int)(writer.BaseStream.Length - 8));
writer.Write(Encoding.ASCII.GetBytes("WAVE"));
writer.Write(Encoding.ASCII.GetBytes("fmt "));
writer.Write(16);
writer.Write((short)1);
writer.Write((short)GetChannelCount());
writer.Write(GetSampleRate());
writer.Write(GetSampleRate() * GetChannelCount() * sizeof(short));
writer.Write((short)(GetChannelCount() * sizeof(short)));
writer.Write((short)(sizeof(short) * 8));
writer.Write(Encoding.ASCII.GetBytes("data"));
writer.Write((int)(writer.BaseStream.Length - HeaderSize));
writer.Seek((int)currentPos, SeekOrigin.Begin);
}
public void AppendBuffer(ReadOnlySpan<short> data, uint channelCount)
{
_stream.Write(MemoryMarshal.Cast<short, byte>(data));
UpdateHeader();
_stream.Flush();
}
public void SetVolume(float volume)
{
// Do nothing, volume is not used for FileHardwareDevice at the moment.
}
public float GetVolume()
{
// FileHardwareDevice does not incorporate volume.
return 0;
}
public uint GetChannelCount()
{
return _channelCount;
}
public uint GetSampleRate()
{
return _sampleRate;
}
public void Dispose()
{
Dispose(true);
}
protected virtual void Dispose(bool disposing)
{
if (disposing)
{
_stream?.Flush();
_stream?.Dispose();
_stream = null;
}
}
}
}