2023-12-30 04:27:31 -05:00
|
|
|
using NAudio.Wave;
|
|
|
|
using NAudio.Extras;
|
|
|
|
using NAudio.Wave.SampleProviders;
|
|
|
|
|
2024-08-04 19:46:10 -04:00
|
|
|
public sealed class AudioPlaybackEngine : IDisposable
|
2023-12-30 04:27:31 -05:00
|
|
|
{
|
2024-01-05 05:07:41 -05:00
|
|
|
public int SampleRate { get; }
|
2024-08-04 19:46:10 -04:00
|
|
|
|
|
|
|
private readonly IWavePlayer _outputDevice;
|
|
|
|
private readonly MixingSampleProvider _mixer;
|
2023-12-30 04:27:31 -05:00
|
|
|
|
2024-08-04 19:46:10 -04:00
|
|
|
public AudioPlaybackEngine(int sampleRate = 44100, int channelCount = 2)
|
2023-12-30 04:27:31 -05:00
|
|
|
{
|
2024-01-05 05:07:41 -05:00
|
|
|
SampleRate = sampleRate;
|
2024-08-04 19:46:10 -04:00
|
|
|
_outputDevice = new WaveOutEvent();
|
2024-06-16 20:19:31 -04:00
|
|
|
|
2024-08-04 19:46:10 -04:00
|
|
|
_mixer = new MixingSampleProvider(WaveFormat.CreateIeeeFloatWaveFormat(sampleRate, channelCount));
|
|
|
|
_mixer.ReadFully = true;
|
2024-01-05 05:07:41 -05:00
|
|
|
|
2024-08-04 19:46:10 -04:00
|
|
|
_outputDevice.Init(_mixer);
|
|
|
|
_outputDevice.Play();
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
|
2024-03-12 14:05:27 -04:00
|
|
|
private ISampleProvider ConvertToRightChannelCount(ISampleProvider? input)
|
2023-12-30 04:27:31 -05:00
|
|
|
{
|
2024-03-15 08:27:35 -04:00
|
|
|
if (input == null)
|
2024-03-12 14:05:27 -04:00
|
|
|
throw new NullReferenceException(nameof(input));
|
|
|
|
|
2024-08-04 19:46:10 -04:00
|
|
|
if (input.WaveFormat.Channels == _mixer.WaveFormat.Channels)
|
2023-12-30 04:27:31 -05:00
|
|
|
return input;
|
2024-08-04 19:46:10 -04:00
|
|
|
if (input.WaveFormat.Channels == 1 && _mixer.WaveFormat.Channels == 2)
|
2023-12-30 04:27:31 -05:00
|
|
|
return new MonoToStereoSampleProvider(input);
|
2024-08-04 19:46:10 -04:00
|
|
|
if (input.WaveFormat.Channels == 2 && _mixer.WaveFormat.Channels == 1)
|
2024-06-24 18:11:36 -04:00
|
|
|
return new StereoToMonoSampleProvider(input);
|
2023-12-30 04:27:31 -05:00
|
|
|
throw new NotImplementedException("Not yet implemented this channel count conversion");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void PlaySound(string fileName)
|
|
|
|
{
|
|
|
|
var input = new AudioFileReader(fileName);
|
2024-01-05 05:07:41 -05:00
|
|
|
AddMixerInput(new WdlResamplingSampleProvider(ConvertToRightChannelCount(new AutoDisposeFileReader(input)), SampleRate));
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void PlaySound(NetworkWavSound sound)
|
|
|
|
{
|
|
|
|
AddMixerInput(new CachedWavProvider(sound));
|
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public ISampleProvider ConvertSound(IWaveProvider provider)
|
|
|
|
{
|
2024-03-12 14:05:27 -04:00
|
|
|
ISampleProvider? converted = null;
|
2024-06-16 20:19:31 -04:00
|
|
|
if (provider.WaveFormat.Encoding == WaveFormatEncoding.Pcm)
|
|
|
|
{
|
|
|
|
if (provider.WaveFormat.BitsPerSample == 8)
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
converted = new Pcm8BitToSampleProvider(provider);
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
else if (provider.WaveFormat.BitsPerSample == 16)
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
converted = new Pcm16BitToSampleProvider(provider);
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
else if (provider.WaveFormat.BitsPerSample == 24)
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
converted = new Pcm24BitToSampleProvider(provider);
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
else if (provider.WaveFormat.BitsPerSample == 32)
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
converted = new Pcm32BitToSampleProvider(provider);
|
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
else if (provider.WaveFormat.Encoding == WaveFormatEncoding.IeeeFloat)
|
|
|
|
{
|
|
|
|
if (provider.WaveFormat.BitsPerSample == 64)
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
converted = new WaveToSampleProvider64(provider);
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
converted = new WaveToSampleProvider(provider);
|
|
|
|
}
|
2024-06-16 20:19:31 -04:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2023-12-30 04:27:31 -05:00
|
|
|
throw new ArgumentException("Unsupported source encoding while adding to mixer.");
|
|
|
|
}
|
2024-01-05 05:07:41 -05:00
|
|
|
return ConvertToRightChannelCount(converted);
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AddMixerInput(ISampleProvider input)
|
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
_mixer.AddMixerInput(input);
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
public void AddMixerInput(IWaveProvider input)
|
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
_mixer.AddMixerInput(input);
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public void RemoveMixerInput(ISampleProvider sound)
|
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
_mixer.RemoveMixerInput(sound);
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public void AddOnMixerInputEnded(EventHandler<SampleProviderEventArgs> e)
|
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
_mixer.MixerInputEnded += e;
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
|
2024-06-16 20:19:31 -04:00
|
|
|
public void Dispose()
|
|
|
|
{
|
2024-08-04 19:46:10 -04:00
|
|
|
_outputDevice.Dispose();
|
2023-12-30 04:27:31 -05:00
|
|
|
}
|
|
|
|
}
|