hermes-client/Chat/Speech/NetworkCachedSound.cs

44 lines
1.1 KiB
C#
Raw Normal View History

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)
{
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;
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
{
private readonly NetworkWavSound _sound;
private readonly RawSourceWaveStream _stream;
2023-12-30 04:27:31 -05:00
public WaveFormat WaveFormat { get => _sound.WaveFormat; }
2023-12-30 04:27:31 -05:00
public CachedWavProvider(NetworkWavSound cachedSound)
{
_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)
{
return _stream.Read(buffer, offset, count);
2023-12-30 04:27:31 -05:00
}
}