Add project

This commit is contained in:
Allen Wolf
2024-01-10 00:16:59 -06:00
parent 78364d6665
commit 9a31bbade2
23 changed files with 3121 additions and 0 deletions

34
src/CachedSound.cs Executable file
View File

@ -0,0 +1,34 @@
using System.Collections.Generic;
using System.Linq;
using NAudio.Wave;
// https://mark-dot-net.blogspot.de/2014/02/fire-and-forget-audio-playback-with.html
// Mark Heath 2014
namespace SpeechBot
{
class CachedSound
{
public float[] AudioData { get; private set; }
public WaveFormat WaveFormat { get; private set; }
public CachedSound(string audioFileName)
{
using (var audioFileReader = new AudioFileReader(audioFileName))
{
// TODO: could add resampling in here if required
WaveFormat = audioFileReader.WaveFormat;
var wholeFile = new List<float>((int)(audioFileReader.Length / 4));
var readBuffer = new float[audioFileReader.WaveFormat.SampleRate * audioFileReader.WaveFormat.Channels];
int samplesRead;
while ((samplesRead = audioFileReader.Read(readBuffer, 0, readBuffer.Length)) > 0)
{
wholeFile.AddRange(readBuffer.Take(samplesRead));
}
AudioData = wholeFile.ToArray();
}
}
}
}