You are on page 1of 21

Introducing NAudio

Mark Heath http://mark-dot-net.blogspot.com mark.heath@gmail.com

Outline
What is NAudio? Installing NAudio The NAudio Demo Applications Background Understanding

Digital Audio Fundamentals http://pluralsight.com/training/Courses/TableOfContents/digital-audiofundamentals

NAudio Signal Chains

What is NAudio?
NAudio is an open source audio library for .NET

Initially created in 2002 Hosted on CodePlex (http://naudio.codeplex.com) Microsoft Public License (Ms-PL) Current version 1.7 Managed wrappers for audio APIs

NAudio provides

Playback, Recording, Codecs

Read and write support for several audio file formats Base interfaces for signal chain construction Useful audio utilities and signal chain components

Legacy Windows Audio API Wrappers


waveOut, waveIn

WaveOut, WaveIn Mixer, MixerLine, MixerOut AcmStream, WaveFormatConversionStream MidiIn, MidiOut DirectSoundOut

mixer

acm

midiIn, midiOut

DirectSound

Modern Audio API Wrappers


WASAPI (Windows Audio Session API)

Windows Vista and above WasapiOut, WasapiCapture, WasapiLoopbackCapture ResamplerDmoStream MediaFoundationReader MediaFoundationEncoder AsioOut NAudio.WindowsMedia.dll WmaFileReader, WmaWriter

DirectX Media Objects

Media Foundation API


Steinberg ASIO (Audio Stream Input/Output)

Windows Media Format SDK


File Formats
WAV files (WaveFileReader, WaveFileWriter) MP3 files (Mp3FileReader) AIFF files (AiffFileReader, AiffFileWriter) WMA files (WmaFileReader, WmaWriter) MIDI files (MidiFile) SF2 files (SoundFont) Other file formats

MediaFoundationReader (e.g. AAC)

Platform Support
Built against .NET 3.5

Works with .NET 4 and 4.5 Some APIs not available on XP Some codecs not included on all Windows versions

Windows Versions

e.g. Windows Server, Windows Azure, Windows 8

Windows Store support preview in NAudio 1.7 Silverlight no official support Windows Phone no official support

Mono Can run in 32 or 64 bit process

Not all codecs available in 64 bit

Installing NAudio
NAudio website

http://naudio.codeplex.com Download release binaries Read documentation View source code Download demo applications Support forum

Also: http://stackoverflow.com/questions/tagged/naudio

NuGet Install

https://www.nuget.org/packages/NAudio Includes pre-release builds

DEMO: Installing NAudio


Download with NuGet

Pre-release versions available

Show NAudio namespaces

Exploring the NAudio Source Code


Show:

Wrappers Readers, Writers Gui Signal chain components Demo apps

DEMO: NAudio Demo Apps


NAudioDemo

Windows Forms Playback, Recording, Codecs Media Foundation Transforms

NAudio WPF Demo

Audio File Inspector Windows Store App Demo Other apps built with NAudio

.NET Voice Recorder MIDI File Mapper Practice Sharp

Background Understanding
Sampled Audio

Sample rates, bit depths, PCM, clipping and headroom encoding, decoding, bit rates WAV, MP3 Manipulating audio at the sample level Inputs, outputs, effects, visualisations, mixing, busses http://pluralsight.com/training/Courses/TableOfContents/digital-audiofundamentals

Codecs

File Formats

Effects

Signal Chains

Digital Audio Fundamentals Course

Audio and the .NET Framework


Performance

JIT compilation Garbage Collection Can cause low latency audio to glitch Must be pinned Pointers only available with the unsafe keyword byte[] not castable to float[] or short[]

Your process can be interrupted

Your structures can be moved in memory

Language Support

NAudio Signal Chains


What is a signal chain?

End to end audio processing path Guitar -> Tuner -> Distortion -> Delay -> Amplifier Inputs (create sound) Effects (modify sound) Codecs (modify audio format) Mixers (combine multiple inputs) Busses (split inputs) Visualisations (analyse sound) Outputs (endpoints for the signal chain)

What can be in a signal chain?


WaveStream
Derives from System.Stream (but read-only) Key methods and properties:
long Length { get; } long Position { get; set; } int Read(byte[] buffer, int offset, int count);

Length and Position in bytes Read returns number of bytes written into buffer

0 indicates end of stream.

WaveFormat WaveFormat { get; } TimeSpan CurrentTime { get; set; } TimeSpan TotalTime { get; }

WaveFormat
Describes the format audio is stored in Encoding

PCM, IEEE float or compressed

Bit depth, sample rate & channel count Bit rate and block alignment
WaveFormatEncoding Encoding { get; } int SampleRate { get; } int BitsPerSample { get; } int Channels { get; } int BlockAlign { get; } int AverageBytesPerSecond { get; }

IWaveProvider
Simplified WaveStream

No concept of repositioning or length A wave format and ability to read

Methods:
int Read(byte[] buffer, int offset, int count); WaveFormat WaveFormat { get; }

Problem:

Not easy to get at individual samples Want short[] for 16 bit audio Want float[] for 32 bit floating point audio

ISampleProvider
For working with 32 bit IEEE floating point samples Typical signal chain:

Decompress to PCM Convert to IEEE floating point Perform effects and analysis Convert to PCM Compress with codec

Sample Provider:
int Read(float[] buffer, int offset, int count); WaveFormat WaveFormat { get; }

Built-in Providers
Wave Streams

WaveFileReader, Mp3FileReader, MediaFoundationReader LoopStream WaveFormatConversionStream BufferedWaveProvider VolumeWaveProvider16 VolumeSampleProvider MixingSampleProvider MultiplexingSampleProvider OffsetSampleProvider

Wave Providers

Sample Providers

Creating Your Own Providers


You can make your own! Often take a source provider Indicate output format with WaveFormat Implement Read

Usually read from source provider & then analyse or process audio

Summary
What is NAudio? Installing NAudio The NAudio Demo Applications Background Understanding

Digital Audio Fundamentals http://pluralsight.com/training/Courses/TableOfContents/digital-audiofundamentals

NAudio Signal Chains

You might also like