JAJ.Packages.MiniAudioEx 1.5.0

There is a newer version of this package available.
See the version list below for details.
dotnet add package JAJ.Packages.MiniAudioEx --version 1.5.0                
NuGet\Install-Package JAJ.Packages.MiniAudioEx -Version 1.5.0                
This command is intended to be used within the Package Manager Console in Visual Studio, as it uses the NuGet module's version of Install-Package.
<PackageReference Include="JAJ.Packages.MiniAudioEx" Version="1.5.0" />                
For projects that support PackageReference, copy this XML node into the project file to reference the package.
paket add JAJ.Packages.MiniAudioEx --version 1.5.0                
#r "nuget: JAJ.Packages.MiniAudioEx, 1.5.0"                
#r directive can be used in F# Interactive and Polyglot Notebooks. Copy this into the interactive tool or source code of the script to reference the package.
// Install JAJ.Packages.MiniAudioEx as a Cake Addin
#addin nuget:?package=JAJ.Packages.MiniAudioEx&version=1.5.0

// Install JAJ.Packages.MiniAudioEx as a Cake Tool
#tool nuget:?package=JAJ.Packages.MiniAudioEx&version=1.5.0                

MiniAudioExNET

A .NET wrapper for MiniAudioEx. MiniAudioEx is a a modified version of MiniAudio, see this repository. The goal of MiniAudioExNET is to make it easy to add audio playback to .NET applications. I've tried several libraries in the past and none of them could offer all of the things I was looking for:

  • Easy to set up and interact with.
  • Spatial audio.
  • Cross platform.
  • A permissive license.

This library ticks all these boxes. There are some (in my opinion) minor things missing such as more audio format decoders, but at least 3 widely used formats are supported which is sufficient for my needs. If you would like to have support for more formats, then please make your request here.

Features

  • Playback of various audio formats such as WAV/MP3/FLAC.
  • Stream audio from disk or from memory.
  • Callbacks for effects processing and generating audio.
  • Spatial properties like doppler effect, pitching, distance attenuation and panning.

Platforms

MiniAudio was designed to work on every major platform, however I do not have a Mac so I can not build a library for Mac OS. As a result only Windows and Linux libraries are currently available.

Installation

dotnet add package JAJ.Packages.MiniAudioEx --version 1.5.0

General gotchas

  • Reuse audio clips. If you have loaded an AudioClip from memory, then the library allocates memory that the garbage collector doesn't free. All memory is freed after calling MiniAudioEx.Deinitialize. It is perfectly fine to reuse audio clips across multiple audio sources, so you don't have to load multiple clips with the same sound. A good strategy is to store your audio clips in an array or a list for the lifetime of your application.
  • Call MiniAudioEx.Update from your main thread loop. This method calculates a delta time, and is responsible for moving messages from the audio thread to the main thread. If not called (regularly), the End callback will never be able to run.
  • The Process and Read event run on a separate thread as well. You should not call any MiniAudioEx API functions from these callbacks.
  • When using the Process and Read events in netstandard 2.0, it is important to know that the size/length of the buffer you receive in the callbacks might not necessarily be the size of the actual data. Since netstandard 2.0 does not have the Span<T> type, a workaround has been applied that tries to be efficient with memory allocations for these buffers. To get the actual size (or number of elements) of the data you simply multiply the number of frames with the number of channels that the callback gives you. If you write more data than is available, the excess data is simply ignored.

Example 1

Playing audio from a file on disk.

using System;
using System.Threading;
using MiniAudioExNET;

namespace MiniAudioExExample
{
    class Program
    {
        static readonly uint SAMPLE_RATE = 44100;
        static readonly uint CHANNELS = 2;

        static void Main(string[] args)
        {
            Console.CancelKeyPress += OnCancelKeyPress;

            MiniAudioEx.Initialize(SAMPLE_RATE, CHANNELS);
            
            AudioSource source = new AudioSource();

            AudioClip clip = new AudioClip("some_audio.mp3");
            source.Play(clip);
            
            while(true)
            {
                MiniAudioEx.Update();
                Thread.Sleep(10);
            }
        }

        static void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            MiniAudioEx.Deinitialize();
        }
    }
}

Example 2

An example of how to procedurally generate sound with the Read callback.

using System;
using System.Threading;
using MiniAudioExNET;

namespace MiniAudioExExample
{
    class Program
    {
        static readonly uint SAMPLE_RATE = 44100;
        static readonly uint CHANNELS = 2;

        static void Main(string[] args)
        {
            Console.CancelKeyPress += OnCancelKeyPress;

            MiniAudioEx.Initialize(SAMPLE_RATE, CHANNELS);
            
            AudioSource source = new AudioSource();
            source.Read += OnAudioRead;
            source.Play();
            
            while(true)
            {
                MiniAudioEx.Update();
                Thread.Sleep(10);
            }
        }

        static void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            MiniAudioEx.Deinitialize();
        }

        static long timeCounter = 0;

        static void OnAudioRead(Span<float> framesOut, ulong frameCount, int channels)
        {
            float sample = 0.0f;
            float frequency = 440.0f;

            for(int i = 0; i < framesOut.Length; i+=channels)
            {
                sample = (float)Math.Sin(2 * Math.PI * frequency * timeCounter / SAMPLE_RATE);
                framesOut[i] = sample;
                if(channels == 2)
                    framesOut[i+1] = sample;
                timeCounter++;
            }
        }
    }
}

Example 3

A minimal example of spatial audio.

using System;
using System.Threading;
using MiniAudioExNET;

namespace MiniAudioExExample
{
    class Program
    {
        static readonly uint SAMPLE_RATE = 44100;
        static readonly uint CHANNELS = 2;

        static void Main(string[] args)
        {
            Console.CancelKeyPress += OnCancelKeyPress;

            MiniAudioEx.Initialize(SAMPLE_RATE, CHANNELS);

            AudioListener listener = new AudioListener();
            listener.Position = new Vector3f(0, 0, 0);
            
            AudioSource source = new AudioSource();

            AudioClip clip = new AudioClip("some_audio.mp3", true);
            source.Loop = true;
            source.Spatial = true;
            source.Position = new Vector3f(10, 0, 0);
            source.Play(clip);
            
            while(true)
            {
                MiniAudioEx.Update();
                Thread.Sleep(10);
            }
        }

        private static void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            MiniAudioEx.Deinitialize();
        }
    }
}

Example 4

This shows how you can use the End callback to play audio from a playlist.

using System;
using System.Threading;
using System.Collections.Generic;
using MiniAudioExNET;

namespace MiniAudioExExample
{
    class Program
    {
        static readonly uint SAMPLE_RATE = 44100;
        static readonly uint CHANNELS = 2;
        static AudioSource source;
        static List<AudioClip> audioClips;
        static int currentClip = 0;

        static void Main(string[] args)
        {
            Console.CancelKeyPress += OnCancelKeyPress;

            MiniAudioEx.Initialize(SAMPLE_RATE, CHANNELS);

            audioClips = new List<AudioClip>();            
            audioClips.Add(new AudioClip("track_1.mp3"));
            audioClips.Add(new AudioClip("track_2.mp3"));
            audioClips.Add(new AudioClip("track_3.mp3"));
            audioClips.Add(new AudioClip("track_4.mp3"));
            audioClips.Add(new AudioClip("track_5.mp3"));

            source = new AudioSource();
            
            //End callback will not trigger if source has Loop set to true
            //Will also not trigger if you don't call MiniAudioEx.Update
            source.End += OnPlaybackEnded;

            source.Play(audioClips[currentClip]);
            
            while(true)
            {
                MiniAudioEx.Update();
                Thread.Sleep(10);
            }
        }

        static void OnPlaybackEnded()
        {
            currentClip++;
            if(currentClip >= audioClips.Count)
                currentClip = 0;
            source.Play(audioClips[currentClip]);
        }

        static void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            MiniAudioEx.Deinitialize();
        }
    }
}

Example 5

A more advanced example of generating a sine wave, applying a tremolo effect to it, and have it play in 3D space.

using System;
using System.Threading;
using MiniAudioExNET;

namespace MiniAudioExExample
{
    class Program
    {
        static readonly uint SAMPLE_RATE = 44100;
        static readonly uint CHANNELS = 2;

        static void Main(string[] args)
        {
            Console.CancelKeyPress += OnCancelKeyPress;

            MiniAudioEx.Initialize(SAMPLE_RATE, CHANNELS);

            AudioListener listener = new AudioListener();
            listener.Position = new Vector3f(0, 0, 0);
            
            AudioSource source = new AudioSource();

            var sineGenerator = new SineGenerator(440);
            var tremoloEffect = new TremoloEffect(8);

            source.AddGenerator(sineGenerator);
            source.AddEffect(tremoloEffect);
            source.DopplerFactor = 0.1f;
            source.Position = new Vector3f(0, 0, 0);
            source.MinDistance = 1.0f;
            source.MaxDistance = 200.0f;
            source.AttenuationModel = AttenuationModel.Exponential;
            //Simply set Spatial to false to disable any 3D effects on the source
            source.Spatial = true;
            source.Play();

            double timer = 0;
            
            while(true)
            {
                MiniAudioEx.Update();

                float direction = (float)Math.Sin(timer * 0.5);
                source.Position = new Vector3f(100, 0, 0) * direction;
                source.Velocity = source.GetCalculatedVelocity();

                timer += MiniAudioEx.DeltaTime;

                Thread.Sleep(10);
            }            
        }

        private static void OnCancelKeyPress(object sender, ConsoleCancelEventArgs e)
        {
            MiniAudioEx.Deinitialize();
        }
    }

    public class TremoloEffect : IAudioEffect
    {
        private long tickTimer;
        private float frequency;

        public TremoloEffect(float frequency)
        {
            this.frequency = frequency;
            tickTimer = 0;
        }

        public void OnProcess(Span<float> framesOut, ulong frameCount, int channels)
        {
            float sample = 0;
            for(int i = 0; i < framesOut.Length; i+=channels)
            {
                sample = (float)Math.Sin(2 * Math.PI * frequency * tickTimer / MiniAudioEx.SampleRate);
                framesOut[i] *= sample;
                if(channels == 2)
                    framesOut[i+1] *= sample;
                tickTimer++;
            }
        }
    }

    public class SineGenerator : IAudioGenerator
    {
        private long tickTimer;
        private float frequency;

        public SineGenerator(float frequency)
        {
            this.frequency = frequency;
            tickTimer = 0;
        }

        public void OnGenerate(Span<float> framesOut, ulong frameCount, int channels)
        {
            float sample = 0;
            for(int i = 0; i < framesOut.Length; i+=channels)
            {
                sample = (float)Math.Sin(2 * Math.PI * frequency * tickTimer / MiniAudioEx.SampleRate);
                framesOut[i] = sample;
                if(channels == 2)
                    framesOut[i+1] = sample;
                tickTimer++;
            }
        }
    }
}
Product Compatible and additional computed target framework versions.
.NET net5.0 was computed.  net5.0-windows was computed.  net6.0 was computed.  net6.0-android was computed.  net6.0-ios was computed.  net6.0-maccatalyst was computed.  net6.0-macos was computed.  net6.0-tvos was computed.  net6.0-windows was computed.  net7.0 is compatible.  net7.0-android was computed.  net7.0-ios was computed.  net7.0-maccatalyst was computed.  net7.0-macos was computed.  net7.0-tvos was computed.  net7.0-windows was computed.  net8.0 was computed.  net8.0-android was computed.  net8.0-browser was computed.  net8.0-ios was computed.  net8.0-maccatalyst was computed.  net8.0-macos was computed.  net8.0-tvos was computed.  net8.0-windows was computed. 
.NET Core netcoreapp2.0 was computed.  netcoreapp2.1 was computed.  netcoreapp2.2 was computed.  netcoreapp3.0 was computed.  netcoreapp3.1 was computed. 
.NET Standard netstandard2.0 is compatible.  netstandard2.1 is compatible. 
.NET Framework net461 was computed.  net462 was computed.  net463 was computed.  net47 was computed.  net471 was computed.  net472 was computed.  net48 was computed.  net481 was computed. 
MonoAndroid monoandroid was computed. 
MonoMac monomac was computed. 
MonoTouch monotouch was computed. 
Tizen tizen40 was computed.  tizen60 was computed. 
Xamarin.iOS xamarinios was computed. 
Xamarin.Mac xamarinmac was computed. 
Xamarin.TVOS xamarintvos was computed. 
Xamarin.WatchOS xamarinwatchos was computed. 
Compatible target framework(s)
Included target framework(s) (in package)
Learn more about Target Frameworks and .NET Standard.
  • .NETStandard 2.0

    • No dependencies.
  • .NETStandard 2.1

    • No dependencies.
  • net7.0

    • No dependencies.

NuGet packages (1)

Showing the top 1 NuGet packages that depend on JAJ.Packages.MiniAudioEx:

Package Downloads
Bliss

A fast, Cross-platform .NET 8 C# 12 Render Framework.

GitHub repositories

This package is not used by any popular GitHub repositories.

Version Downloads Last updated
1.7.2 183 7/25/2024
1.7.1 116 5/27/2024
1.7.0 113 5/22/2024
1.6.4 110 5/20/2024
1.6.3 118 5/19/2024
1.6.2 102 5/18/2024
1.6.1 116 5/17/2024
1.6.0 108 5/16/2024
1.5.4 110 5/15/2024
1.5.3 104 5/15/2024
1.5.2 101 5/13/2024
1.5.1 88 5/13/2024
1.5.0 95 5/13/2024
1.4.2 102 5/12/2024
1.3.0 101 5/11/2024
1.2.0 106 5/10/2024
1.1.0 106 5/9/2024
1.0.3 122 4/24/2024
1.0.2 122 4/23/2024
1.0.1 133 2/3/2024
1.0.0 194 12/16/2023