MVH/NAudio-2.2.1/NAudio.Extras/CachedSound.cs

31 lines
1.1 KiB
C#
Raw Normal View History

2024-06-07 00:47:07 +02:00
using System.Collections.Generic;
using System.Linq;
using NAudio.Wave;
namespace NAudio.Extras
{
/// <summary>
/// Used by AudioPlaybackEngine
/// </summary>
public class CachedSound
{
public float[] AudioData { get; }
public WaveFormat WaveFormat { get; }
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();
}
}
}
}