2023-12-30 04:27:31 -05:00
|
|
|
using NAudio.Wave;
|
|
|
|
|
|
|
|
public class NetworkWavSound
|
|
|
|
{
|
|
|
|
public byte[] AudioData { get; private set; }
|
|
|
|
public WaveFormat WaveFormat { get; private set; }
|
|
|
|
|
|
|
|
public NetworkWavSound(string uri)
|
|
|
|
{
|
2024-06-16 20:19:31 -04:00
|
|
|
using (var mfr = new MediaFoundationReader(uri))
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
WaveFormat = mfr.WaveFormat;
|
|
|
|
|
|
|
|
byte[] buffer = new byte[4096];
|
|
|
|
int read = 0;
|
2024-06-16 20:19:31 -04:00
|
|
|
using (var ms = new MemoryStream())
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
while ((read = mfr.Read(buffer, 0, buffer.Length)) > 0)
|
|
|
|
ms.Write(buffer, 0, read);
|
|
|
|
AudioData = ms.ToArray();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class CachedWavProvider : IWaveProvider
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
private readonly NetworkWavSound _sound;
|
|
|
|
private readonly RawSourceWaveStream _stream;
|
2023-12-30 04:27:31 -05:00
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
public WaveFormat WaveFormat { get => _sound.WaveFormat; }
|
2023-12-30 04:27:31 -05:00
|
|
|
|
|
|
|
|
|
|
|
public CachedWavProvider(NetworkWavSound cachedSound)
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
_sound = cachedSound;
|
|
|
|
_stream = new RawSourceWaveStream(new MemoryStream(_sound.AudioData), _sound.WaveFormat);
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public int Read(byte[] buffer, int offset, int count)
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
return _stream.Read(buffer, offset, count);
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
}
|