mirror of
https://github.com/Ale32bit/Capy64.git
synced 2025-01-18 18:46:43 +00:00
idk how to implement audio 💀
This commit is contained in:
parent
2949239598
commit
a298f1630f
1 changed files with 11 additions and 63 deletions
|
@ -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);
|
||||
|
||||
public void test()
|
||||
{
|
||||
var workingBuffer = new float[SamplesPerBuffer];
|
||||
FillWorkingBuffer(workingBuffer);
|
||||
var buffer = DivideBuffer(workingBuffer);
|
||||
_instance.SubmitBuffer(buffer);
|
||||
}
|
||||
soundEffect.Play();
|
||||
|
||||
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 soundEffect.Duration;
|
||||
}
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue