Digital Audio - Axwabo/SecretLabNAudio GitHub Wiki
Digital Audio Basics
Digital audio is stored in various codings and container formats (e.g. Ogg Vorbis in an .ogg file).
When reading an audio file the bytes form PCM data, representing 8-bit, 16-bit, 32-bit (or 24-bit) values that correspond to audio samples (amplitude at a given time). We need to convert them to samples (floats) to be able to process them easily.
The number of channels (that NAudio supports at least) specifies whether the audio is stereo (2) or mono (1). Stereo audio allows for different data in the left and right ear (simplified for brevity).
The sample rate defines how many samples are stored per second per channel. It's measured in Hertz (e.g. 48000 Hz).
For example, a 10-second long stereo file at 48000 Hz will contain 10 * 48000 * 2 samples.
Resampling is the process of converting audio from one sample rate to another.
Downmixing is when the number of channels is decreased, e.g. merging stereo channels to get mono audio.
SCP:SL's voice chat expects 48000 Hz mono.
ISampleProvider
The ISampleProvider interface is a minimal abstraction layer provided by NAudio.
SecretLabNAudio v2 introduced the IAudioProcessor interface that extends this,
simplifying resource disposal for the user.
The IWaveProvider interface similar, but provides PCM data as bytes.
It can be converted to an ISampleProvider by using the ToSampleProvider extension method.
There are various built-in sample & wave providers, which can be combined to create complex processing layers.
[!TIP] See also: providers
[!CAUTION] The amount of samples requested may not be constant. Do not calculate timings based on the number of calls to
ReadThe
AudioPlayercomponent always requests480samples. Your provider might process audio in stereo or at a different rate (which might be passed to a resampler). It shouldn't rely on a fixed count.
StreamAudioProcessor
This is an audio processor that wraps a WaveStream
Its source might be a file, memory, or a network stream.
When the processor is disposed, the underlying stream is also disposed. SecretLabNAudio handles disposal for you by default, but you can choose to manage resources yourself.
[!TIP] See also: streaming music
If the source is a file or a memory stream, you can most likely seek to any position within the stream.
[!NOTE]
CurrentTimeandTotalTimemay be inaccurate for compressed files.
WaveStream
The WaveStream base class is a (usually) repositionable stream implementing the IWaveProvider interface.
Sources include files, memory, or the network (MediaFoundationReader).
It provides PCM data, so it needs to be converted to an ISampleProvider
to be able to use the samples.
[!CAUTION] Make sure to dispose of the stream when you're done using it. Prefer using the StreamAudioProcessor instead.
[!TIP] See also: streaming music
The class defines CurrentTime and TotalTime properties. These are TimeSpan structs which allow you to
get the duration and get/set the position independently of wave format.
[!NOTE] WaveStreams' current and total time may be estimates for compressed files.