Cross-platform audio engine library for desktop platforms (Windows, Linux, macOS)
$ dotnet add package OwnAudioSharpOwnaudio is a cross-platform C# audio library that provides a high-level API for audio playback, recording, and processing. By default, it uses FFmpeg for audio decoding and PortAudio for audio I/O. If ffmpeg or portaudio is not installed, it automatically substitutes the missing one for Miniaudio. This way, it can work without any external dependencies using Miniaudio. The implementation of Miniaudio also allowed the API to be used on mobile platforms.
The table below summarizes the supported operating systems, the APIs used, and their testing status.
| System | APIs | Status |
|---|---|---|
| Windows | Portaudio 2, Miniaudio, Ffmpeg 6 | Tested |
| Linux | Portaudio 2, Miniaudio, Ffmpeg 6 | Tested |
| MacOs | Portaudio 2, Miniaudio, Ffmpeg 6 | Tested |
| Android | Miniaudio | Not tested |
| Ios | Miniaudio | Not tested |
The library will attempt to find these dependencies in standard system locations but also supports specifying custom paths.
You can add this library to your project via NuGet (OwnAudioSharp) or by directly referencing the project.
By default, our code includes Miniaudio, which is ready to use for all systems, so you can get started right away!
If you want to use Portaudio and FFmpeg on certain platforms for extended functionality, you can configure them as follows:
Grab the files and extract them to a folder.
Copy the Portaudio 2 DLL file to the same folder.
When you initialize OwnAudio in your code, just point to the folder path.
portaudio19-dev (this usually provides Portaudio v2) and ffmpeg (version 6 or compatible).sudo apt update
sudo apt install portaudio19-dev ffmpeg
(Note: Package names may vary slightly depending on your Linux distribution. Make sure you get libraries compatible with FFmpeg version 6.)
OwnAudio is smart and will automatically find and use them if they are installed systemwide.brew install portaudio
brew install ffmpeg@6
Here's a quick example of how to use Ownaudio to play an audio file:
using Ownaudio;
using Ownaudio.Sources;
using System;
using System.Threading;
// Initialize Ownaudio
OwnAudio.Initialize();
// Create a source manager
var sourceManager = SourceManager.Instance;
// Add an audio file
await sourceManager.AddOutputSource("path/to/audio.mp3");
// Play the audio
sourceManager.Play();
// Wait for the audio to finish
Console.WriteLine("Press any key to stop playback...");
Console.ReadKey();
// Stop playback and clean up
sourceManager.Stop();
OwnAudio.Free();// Add multiple audio files
await sourceManager.AddOutputSource("path/to/audio1.mp3");
await sourceManager.AddOutputSource("path/to/audio2.mp3");
// Adjust volume for individual sources
sourceManager.SetVolume(0, 0.8f); // 80% volume for first source
sourceManager.SetVolume(1, 0.6f); // 60% volume for second source
// Play mixed audio
sourceManager.Play();// Add an input source
await sourceManager.AddInputSource();
// Start recording
sourceManager.Play("output.wav", 16); // 16-bit recording// Change tempo without affecting pitch (value range -20 to +20)
sourceManager.SetTempo(0, 10.0); // Speed up by 10%
// Change pitch without affecting tempo (value range -6 to +6 semitones)
sourceManager.SetPitch(0, 2.0); // Raise pitch by 2 semitones// Seek to a specific position
sourceManager.Seek(TimeSpan.FromSeconds(30)); // Seek to 30 secondsYou can implement custom audio processing by implementing the SampleProcessorBase class:
public class MyAudioProcessor : SampleProcessorBase
{
public override void Process(Span<float> samples)
{
// Process audio samples
for (int i = 0; i < samples.Length; i++)
{
// Example: Simple gain adjustment
samples[i] *= 0.5f; // 50% volume
}
}
}
// Apply the processor to a source or source manager
var processor = new MyAudioProcessor();
sourceManager.CustomSampleProcessor = processor;// We load the source audio data into a byte array.
byte[] audioByte = sourceManager.Sources[0].GetByteAudioData(TimeSpan.Zero);
// We load the source audio data into a float array.
float[] audioFloat = sourceManager.Sources[0].GetFloatAudioData(TimeSpan.Zero);A flexible, resource-efficient audio waveform visualization component for Avalonia applications.
The following example demonstrates how to use the WaveAvaloniaDisplay component in an Avalonia application:
<Window xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:audio="using:Ownaudio.Utilities"
x:Class="MyAudioApp.MainWindow"
Title="Audio Visualizer" Height="450" Width="800">
<Grid>
<audio:WaveAvaloniaDisplay x:Name="waveformDisplay"
WaveformBrush="DodgerBlue"
PlaybackPositionBrush="Red"
VerticalScale="1.0"
DisplayStyle="MinMax"/>
</Grid>
</Window>waveformDisplay.SetAudioData(SourceManager.Instance.Sources[0].GetFloatAudioData(TimeSpan.Zero));
waveformDisplay.PlaybackPositionChanged += OnPlaybackPositionChanged;// MiniAudio preferred (default)
waveformDisplayLoadFromAudioFile("audio.mp3");
// FFmpeg preferred
waveformDisplay.LoadFromAudioFile("audio.mp3", preferFFmpeg: true);
// Asynchronous loading
await waveformDisplay.LoadFromAudioFileAsync("large_audio.wav");
// Loading from stream
using var fileStream = File.OpenRead("audio.mp3");
waveformDisplay.LoadFromAudioStream(fileStream);| Property | Type | Description |
|---|---|---|
| WaveformBrush | IBrush | The color of the waveform |
| PlaybackPositionBrush | IBrush | The color of the playback position indicator |
| VerticalScale | double | Vertical scaling of the waveform (1.0 = original size) |
| DisplayStyle | WaveformDisplayStyle | The waveform display style (MinMax, Positive, RMS) |
| ZoomFactor | double | Zoom factor (1.0 = full view, larger values = more detailed view) |
| ScrollOffset | double | Horizontal scroll position (0.0 - 1.0) |
| PlaybackPosition | double | Current playback position (0.0 - 1.0) |
| Event | Parameter | Description |
|---|---|---|
| PlaybackPositionChanged | double | Triggered when the user changes the playback position |
The library follows a layered architecture:
If you find this project helpful, consider buying me a coffee!
Special thanks to the creators of the following repositories, whose code was instrumental in the development of Ownaudio: