A cross-platform audio playback library that targets .NET Standard 2.1. The main purpose of this project is to provides an easy to use API for playing and streaming audio file.
It uses FFmpeg to decode audio frames (so, it is possible to play audio stream from a video file). And PortAudio for writing audio stream to output device.
This repository include pre-compiled PortAudio binaries for Windows, Linux, macOS that can be found at the libs directory. However, we can construct PortAudio and FFmpeg by using system-wide libraries.
BufdioLib.InitializePortAudio("path/to/portaudio");
BufdioLib.InitializeFFmpeg("path/to/ffmpeg/libraries");
// Or just use system-wide libraries
BufdioLib.InitializePortAudio();
BufdioLib.InitializeFFmpeg();
With PortAudio initialized, now we can retrieve available output devices.
var defaultDevice = BufdioLib.DefaultOutputDevice;
Console.WriteLine(defaultDevice.Name);
Console.WriteLine(defaultDevice.MaxOutputChannels);
Console.WriteLine(defaultDevice.DefaultSampleRate);
Console.WriteLine(defaultDevice.DefaultHighOutputLatency);
// Retrieve all available output devices
foreach (var device in BufdioLib.OutputDevices)
{
Console.WriteLine(device.Name);
}
Bufdio provides high level interface for loading audio and control its playback state.
using IAudioPlayer player = new AudioPlayer();
// Methods
player.LoadAsync("audio-url-or-path");
player.LoadAsync(stream);
player.Play();
player.Pause();
player.Stop();
player.Seek(TimeSpan.FromSeconds(2));
// Properties
player.Volume;
player.CustomSampleProcessor;
player.Logger;
// Properties (read-only)
player.State;
player.IsSeeking;
player.IsLoaded;
player.Duration;
player.Position;
// Events
player.StateChanged += OnStateChanged;
player.PositionChanged += OnPositionChanged;
Bufdio also exposes low level IAudioEngine
interface for sending or writing samples to an output device.
const int SampleRate = 8000;
const float Frequency = 350f;
const float Amplitude = 0.35f * short.MaxValue;
var samples = new float[SampleRate];
var options = new AudioEngineOptions(1, SampleRate);
using IAudioEngine engine = new PortAudioEngine(options);
for (var i = 0; i < samples.Length; i++)
{
samples[i] = (float)(Amplitude * Math.Sin(2 * Math.PI * i * Frequency / SampleRate));
}
Console.WriteLine("Playing 10 times with 1 second delay..");
for (var i = 0; i < 10; i++)
{
engine.Send(samples);
Thread.Sleep(1000);
}
Bufdio is licenced under the MIT license.