Professional Documents
Culture Documents
Outline
What is NAudio? Installing NAudio The NAudio Demo Applications Background Understanding
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
Read and write support for several audio file formats Base interfaces for signal chain construction Useful audio utilities and signal chain components
WaveOut, WaveIn Mixer, MixerLine, MixerOut AcmStream, WaveFormatConversionStream MidiIn, MidiOut DirectSoundOut
mixer
acm
midiIn, midiOut
DirectSound
Windows Vista and above WasapiOut, WasapiCapture, WasapiLoopbackCapture ResamplerDmoStream MediaFoundationReader MediaFoundationEncoder AsioOut NAudio.WindowsMedia.dll WmaFileReader, WmaWriter
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
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
Windows Store support preview in NAudio 1.7 Silverlight no official support Windows Phone no official support
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
Audio File Inspector Windows Store App Demo Other apps built with NAudio
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
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[]
Language Support
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)
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
WaveFormat WaveFormat { get; } TimeSpan CurrentTime { get; set; } TimeSpan TotalTime { get; }
WaveFormat
Describes the format audio is stored in Encoding
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
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
Usually read from source provider & then analyse or process audio
Summary
What is NAudio? Installing NAudio The NAudio Demo Applications Background Understanding