-
Notifications
You must be signed in to change notification settings - Fork 0
Home
Welcome to the SecretLabNAudio wiki! This project is not affiliated with, nor is endorsed by NAudio
For audio playback, SecretLabNAudio reads from an ISampleProvider to play audio in real time.
The AudioPlayer is a component added to a SpeakerToy managing the reading & encoding of audio.
A SendEngine sends the encoded audio messages to clients.
An IAudioPacketMonitor can monitor the samples independently of provider or send engine. It can be used to analyze audio, e.g. for visualization.
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.
NAudio provides a WaveStream base class for PCM data. Call ToSampleProvider to get a sample provider.
The ISampleProvider interface is a minimal abstraction layer provided by NAudio.
It defines a wave format and an int Read(float[] buffer, int offset, int count) method.
When Read is called, it tries to fill the buffer with the amount of requested samples.
The method returns the read sample count, which can signal that the provider ended by returning 0 or an amount less than requested (e.g. there isn't enough data available).
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 Read
The AudioPlayer component always requests 480 samples. 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.
The WaveStream base class is a (usually) repositionable stream implementing the IWaveProvider interface.
Sources include files, memory, or the network (MediaFoundationReader).
Important
Make sure to dispose of the stream when you're done using it.
Tip
See also: file reading
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.
This struct defines the following properties for a SpeakerToy
-
IsSpatial- whether the speaker is 3D (affected by listener position and rotation) -
Volume- the base volume -
MinDistance- audio is heard atVolumeto this distance, then begins fading out (doesn't apply to 2D speakers) -
MaxDistance- at this distance and beyond, the speaker becomes inaudible
You can apply them to a SpeakerToy or an AudioPlayer using the ApplySettings extension method.