-
Notifications
You must be signed in to change notification settings - Fork 0
v1 AudioPlayer
Caution
This page is for v1 of SecretLabNAudio. v2 guide
The AudioPlayer component (attached to a SpeakerToy) handles audio reading & encoding.
To play the same audio through multiple speakers at once, create one AudioPlayer
and set the other speakers' controller IDs to that of the player.
Audio samples are provided by a sample provider
The player keeps track of how many packets need to be sent using Time.deltaTime
At a stable 60 TPS, this means 1-2 packets every frame. 1 packet is 480 samples. When no samples are read, the buffer is cleared
The player reads audio if IsPaused is false and the provider is not null, regardless of
send engine or output monitor
Right before encoding, the samples may be amplified regardless of provider or monitor.
Note
When the component is disabled, it will be reset, and the Destroyed event will be invoked. This usually happens when the speaker is pooled.
Tip
There are various extension methods to set properties on the audio player component.
You can chain these methods as they return the AudioPlayer itself.
When a packet needs to be processed (at least 480 samples should be read), the method runs as follows, repeating until less than 480 samples are needed:
- Return if
IsPausedis true or if theSampleProvideris null - Read from the provider
- If 0 samples were read:
- Set
HasEndedto true - Clear the buffer
- Invoke the
OutputMonitor'sOnEmptymethod - Invoke the
NoSamplesReadevent - Return
- Set
- Clear the remainder of the buffer if less than 480 samples were read
- Invoke the
OutputMonitor'sOnReadmethod - Return if the
SendEngineis null - Amplify the read samples if
MasterAmplificationis not 1 - Encode the samples with Opus
- Pass the encoded data as an
AudioMessageto theSendEngine
You can obtain an audio player by calling:
-
AudioPlayerPool.Rent- recommended method, read more here AudioPlayer.Create- the
AddAudioPlayerextension method on aSpeakerToy
This is the interface which provides audio samples. May be null.
Important
The provider (if not null) must have a wave format matching the following criteria:
- 48000 Hz sample rate
- mono (1 channel)
- IEEEFloat encoding
Use the WithProvider extension method to automatically downmix & resample to the correct format.
Tip
See also: providers and reading from files
The SpeakerToy this component is attached to (read-only).
The send engine that will be used to broadcast audio messages.
Set to null to disable encoding and broadcasting.
Defaults to SendEngine.DefaultEngine
An optional packet monitor that can analyze the outputted samples. null by default.
The output monitor (if set) is invoked even if no send engine is specified.
A volume scalar to multiply the samples by before encoding. The output monitor is not affected by this, meaning it'll observe the samples from the provider as if they were not amplified after reading.
This property lets you amplify the volume further without wrapping
all providers into a VolumeSampleProvider
Note
This property is different from the volume of the SpeakerToy itself.
It can be used to always amplify audio, allowing for easier personalization
When set to true, the player will pause, no updates will be performed.
true if the playback has finished during the previous update (fewer samples were read than requested).
Gets or sets the controller ID of the speaker. If multiple speakers share the same ID, audio from the player will be heard from each of them.
Invoked when the provider reads 0 samples.
Before the event is invoked, HasEnded is set to true, the buffer is cleared,
and the monitor's OnEmpty method is called.
Invoked when the component is destroyed. The event also gets called when the component is disabled (when the speaker is pooled).
Sets the remaining time to 0 and clears the provider's buffer if it's a BufferedSampleProvider
Destroys the game object, removing the speaker and all of its components (including the audio player).
If the provider throws an exception, it is logged with Unity's logger. The player treats this state as if no samples were read.
You can find Unity logs in the LocalAdmin logs directory:
- Linux:
.config/SCP Secret Laboratory/LocalAdminLogs/<port> - Windows:
%appdata%/SCP Secret Laboratory/LocalAdminLogs/<port>
- π Home
- πΌ Digital Audio Basics
- π Examples
- π¦ Supported Formats
- β¬οΈ Migrating from v1
- π AudioPlayer
- πΎ Short Clips
- πΏ Streaming From Disk
- ποΈ Speaker Groups
- π Sample Providers
- β»οΈ Pooling
- π¨ SendEngines
- π§ Personalizing Speakers
- π Monitoring Output
- βοΈ AudioQueue
- πΆ Mixer
- ποΈοΈ ProcessorChain
- π§° Intro to FFmpeg
- π© Installation
- ποΈ FFmpegArguments
- πͺ Adding Short Clips
- π FFmpeg Audio Processors
- ποΈ Caches
Caution
v1 will be out of support soon.