From a298f1630fe42cf2be5babfe3e30225717eb59c1 Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Fri, 3 Feb 2023 17:21:27 +0100 Subject: [PATCH] idk how to implement audio :skull: --- Capy64/Core/Audio.cs | 74 +++++++------------------------------------- 1 file changed, 11 insertions(+), 63 deletions(-) diff --git a/Capy64/Core/Audio.cs b/Capy64/Core/Audio.cs index c8b5377..fa864ae 100644 --- a/Capy64/Core/Audio.cs +++ b/Capy64/Core/Audio.cs @@ -1,73 +1,21 @@ -using Microsoft.Xna.Framework; -using Microsoft.Xna.Framework.Audio; +using Microsoft.Xna.Framework.Audio; using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; namespace Capy64.Core; public class Audio { public const int SampleRate = 48000; - public const int SamplesPerBuffer = 3000; - const int bytesPerSample = 2; - - private readonly DynamicSoundEffectInstance _instance; - private double _time = 0.0; - public Audio() + public static TimeSpan Play(byte[] buffer) { - _instance = new(SampleRate, AudioChannels.Mono); + var soundEffect = new SoundEffect(buffer, SampleRate, AudioChannels.Mono); + + soundEffect.Play(); + + return soundEffect.Duration; } - - public void test() - { - var workingBuffer = new float[SamplesPerBuffer]; - FillWorkingBuffer(workingBuffer); - var buffer = DivideBuffer(workingBuffer); - _instance.SubmitBuffer(buffer); - } - - private byte[] DivideBuffer(float[] from) - { - var outBuffer = new byte[SamplesPerBuffer * bytesPerSample]; - - for (int i = 0; i < from.Length; i++) - { - var floatSample = MathHelper.Clamp(from[i], -1.0f, 1.0f); - var shortSample = (short)(floatSample * short.MaxValue); - - int index = (i * bytesPerSample) + bytesPerSample; - - if (!BitConverter.IsLittleEndian) - { - outBuffer[index] = (byte)(shortSample >> 8); - outBuffer[index + 1] = (byte)shortSample; - } - else - { - outBuffer[index] = (byte)shortSample; - outBuffer[index + 1] = (byte)(shortSample >> 8); - } - } - - return outBuffer; - } - - public static double SineWave(double time, double frequency) - { - return Math.Sin(time * 2 * Math.PI * frequency); - } - - private void FillWorkingBuffer(float[] buffer) - { - for (int i = 0; i < SamplesPerBuffer; i++) - { - // Here is where you sample your wave function - buffer[i] = (float)SineWave(_time, 440); // Left Channel - - // Advance time passed since beginning - // Since the amount of samples in a second equals the chosen SampleRate - // Then each sample should advance the time by 1 / SampleRate - _time += 1.0 / SampleRate; - } - } - }