Added back volume arg

This commit is contained in:
Alessandro Proto 2023-02-16 19:12:50 +01:00
parent 9dd01041d0
commit 9907232af7
2 changed files with 4 additions and 13 deletions

View file

@ -14,16 +14,7 @@
// limitations under the License. // limitations under the License.
using Microsoft.Xna.Framework.Audio; using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Graphics;
using System; using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Runtime.CompilerServices;
using System.Security.Cryptography;
using System.Text;
using System.Threading.Tasks;
namespace Capy64.Core; namespace Capy64.Core;
@ -54,7 +45,7 @@ public class Audio : IDisposable
return Sound.GetSampleDuration(buffer.Length); return Sound.GetSampleDuration(buffer.Length);
} }
public byte[] GenerateWave(Waveform form, double frequency, TimeSpan time) public byte[] GenerateWave(Waveform form, double frequency, TimeSpan time, float volume = 1f)
{ {
var size = Sound.GetSampleSizeInBytes(time); var size = Sound.GetSampleSizeInBytes(time);
var buffer = new byte[size]; var buffer = new byte[size];
@ -74,7 +65,7 @@ public class Audio : IDisposable
}; };
value = Math.Clamp(value, -1, 1); value = Math.Clamp(value, -1, 1);
var sample = (short)(value >= 0.0f ? value * short.MaxValue : value * short.MinValue * -1); var sample = (short)((value >= 0.0f ? value * short.MaxValue : value * short.MinValue * -1) * volume);
if (!BitConverter.IsLittleEndian) if (!BitConverter.IsLittleEndian)
{ {
buffer[i] = (byte)(sample >> 8); buffer[i] = (byte)(sample >> 8);

View file

@ -157,7 +157,7 @@ public class Audio : IComponent
var freq = L.OptNumber(1, 440); var freq = L.OptNumber(1, 440);
var time = L.OptNumber(2, 1); var time = L.OptNumber(2, 1);
var volume = L.OptNumber(3, 1); var volume = (float)L.OptNumber(3, 1);
volume = Math.Clamp(volume, 0, 1); volume = Math.Clamp(volume, 0, 1);
var timespan = TimeSpan.FromSeconds(time); var timespan = TimeSpan.FromSeconds(time);
@ -172,7 +172,7 @@ public class Audio : IComponent
null, null,
}); });
var buffer = _game.Audio.GenerateWave((Waveform)form, freq, timespan); var buffer = _game.Audio.GenerateWave((Waveform)form, freq, timespan, volume);
try try
{ {