1 audio programming naudio m1 slides
TRANSCRIPT
Outline
What is NAudio? Installing NAudio The NAudio Demo Applications Background Understanding
Digital Audio Fundamentals http://pluralsight.com/training/Courses/TableOfContents/digital-audio-
fundamentals
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
NAudio provides… Managed wrappers for audio APIs
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… Mixer, MixerLine, MixerOut
acm… AcmStream, WaveFormatConversionStream
midiIn…, midiOut… MidiIn, MidiOut
DirectSound DirectSoundOut
Modern Audio API Wrappers
WASAPI (Windows Audio Session API) Windows Vista and above WasapiOut, WasapiCapture, WasapiLoopbackCapture
DirectX Media Objects ResamplerDmoStream
Media Foundation API MediaFoundationReader MediaFoundationEncoder
Steinberg ASIO (Audio Stream Input/Output) AsioOut
Windows Media Format SDK 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…
MediaFoundationReader (e.g. AAC)
Platform Support
Built against .NET 3.5 Works with .NET 4 and 4.5
Windows Versions Some APIs not available on XP Some codecs not included on all 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
NAudio WPF Demo Media Foundation Transforms
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
Codecs encoding, decoding, bit rates
File Formats WAV, MP3
Effects Manipulating audio at the sample level
Signal Chains Inputs, outputs, effects, visualisations, mixing, busses
Digital Audio Fundamentals Course http://pluralsight.com/training/Courses/TableOfContents/digital-audio-
fundamentals
Audio and the .NET Framework
Performance JIT compilation Garbage Collection
Your process can be interrupted Can cause low latency audio to “glitch”
Your structures can be moved in memory Must be “pinned”
Language Support Pointers only available with the “unsafe” keyword byte[] not castable to float[] or short[]
NAudio Signal Chains
What is a signal chain? End to end audio processing path Guitar -> Tuner -> Distortion -> Delay -> Amplifier
What can be in a signal chain? 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:
Length and Position in bytes Read returns number of bytes written into buffer
0 indicates end of stream.
long Length { get; }long Position { get; set; }int Read(byte[] buffer, int offset, int count);
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:
Problem: Not easy to get at individual samples Want short[] for 16 bit audio Want float[] for 32 bit floating point audio
int Read(byte[] buffer, int offset, int count);WaveFormat WaveFormat { get; }
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
Wave Providers BufferedWaveProvider VolumeWaveProvider16
Sample Providers VolumeSampleProvider MixingSampleProvider MultiplexingSampleProvider OffsetSampleProvider
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-audio-
fundamentals
NAudio Signal Chains