Local Voices
Overview
A LocalVoice is one outgoing stream. A client can run several at once, for example a microphone and an in-game radio, and each has its own id, returned by GetId().
Creating a Local Voice
VoiceClient::CreateLocalVoice() takes LocalVoiceOptions and returns a std::unique_ptr<LocalVoice>:
C++
LocalVoiceOptions options;
options.SamplingRate = 48000;
options.Channels = 1;
std::unique_ptr<LocalVoice> microphone = voice.CreateLocalVoice(options);
It returns nullptr when an option is invalid, when the user data cannot be sent or when no voice id is left. The reason is written to the log.
A local voice can be created before or after joining a room. It is announced as soon as the client is in a room.
Stream Format
| Option | Default | Valid Values |
|---|---|---|
SamplingRate |
48000 |
8000, 12000, 16000, 24000, 48000 |
Channels |
1 |
1, 2 |
FrameDurationMs |
20 |
10, 20, 40, 60 |
Bitrate |
30000 |
Greater than 0, in bits per second |
InputSamplingRate |
0 |
0 (same as SamplingRate) or the capture rate in Hz |
Sampling Rate and Channels
SamplingRate and Channels set the format of the encoded stream. Receivers get the audio in this format. 48 kHz mono is the recommendation for speech.
All clients should send the same format. The reason is given in Use One Stream Format.
Frame Duration
FrameDurationMs is the length of audio in one encoded frame. Shorter frames lower the latency but send more packets with more overhead. Longer frames save overhead, but each lost packet costs more audio. The default is 20 ms.
Bitrate
Bitrate is the encoder's target in bits per second. The default of 30 kbps is enough for speech. Music needs more.
Input Sampling Rate and Resampling
When the capture device runs at another rate than the stream, for example 44.1 kHz, InputSamplingRate tells the voice to resample. One frame must be a whole number of input samples: 44.1 kHz works with every frame duration, 22.05 kHz not with 10 ms. Otherwise the stream drifts and an error is logged. The channel count is not converted. Audio passed to PushAudio() must have Channels interleaved channels, which means a stereo microphone is mixed down before it is pushed into a mono voice.
The built-in resampler is basic. Capturing at the stream rate gives the best quality.
Pushing Audio
PushAudio() takes interleaved float samples in the range [-1, 1] at the input rate. It accepts any number of samples and cuts them into frames itself. Forwarding the samples of a capture callback:
C++
void OnCapture(const float* samples, std::size_t sampleCount)
{
microphone->PushAudio(std::span<const float>(samples, sampleCount));
}
PushAudio() may be called from any thread, as long as it is always the same one, typically the capture callback. It copies the samples and returns. Encoding runs on a thread of its own.
Sources that deliver 16-bit integer samples convert them with ConvertInt16ToFloat(), described in Audio Integration.
Transmit Control
SetTransmitEnabled(false) mutes the stream without removing it, for example for push-to-talk, and GetTransmitEnabled() reads the setting back:
C++
void OnPushToTalk(bool pressed)
{
microphone->SetTransmitEnabled(pressed);
}
IsTransmitting() returns false while transmitting is disabled or voice detection hears silence, which makes it the source for a "talking" indicator.
Voice detection, switched with the VoiceDetection option or SetVoiceDetection(), sends only while the input is above a threshold and saves bandwidth during silence. It is off by default and rudimentary. See Known Limitations.
Input Level
GetLevel() returns the peak amplitude of the recent input, between 0 and 1. It drives microphone meters and shows whether capture works.
Delivery Options
By default, frames are sent unreliably and unencrypted. A frame of live audio that arrives late is worthless, and resending it only adds delay.
Reliable
With Reliable = true, lost frames are resent. Under packet loss, each resend holds back every later frame, and those then arrive in a burst.
Playback catches up with such a burst only coarsely: once more than VoiceClientOptions::MaxPlaybackDelayMs (400 ms) piles up, the oldest audio is dropped down to VoiceClientOptions::PlaybackDelayMs (100 ms). A smaller backlog stays as extra delay until the next underrun, which makes reliable delivery rarely useful for live voice. The buffer's behavior is covered in Underruns and Overruns.
Encryption
With Encrypt = true, the audio frames are encrypted like events sent with the Encrypt option, at some CPU cost on both ends. Custom Events describes the event option and Connection and Regions the encryption of the connection itself.
User Data
UserData is a RealtimeValue sent with the stream announcement, and receivers read it from RemoteVoiceInfo::UserData. Any value described in Structured Payloads works, from a single number or string to a map, for example to tag a stream as a radio channel or to attach a character id:
C++
RealtimeMap userData;
userData.Set(RealtimeCore::Common::StringType(PHOTON_STR("kind")), RealtimeValue(PHOTON_STR("radio")));
userData.Set(RealtimeCore::Common::StringType(PHOTON_STR("character")), RealtimeValue(17));
LocalVoiceOptions options;
options.UserData = userData;
std::unique_ptr<LocalVoice> radio = voice.CreateLocalVoice(options);
The user data travels once per announcement, not with every frame, and is fixed when the voice is created. A value that cannot be sent makes CreateLocalVoice() fail. A value a receiver cannot decode arrives there as null.
Removing a Local Voice
Destroying the std::unique_ptr removes the stream, and receivers get OnRemoteVoiceRemoved. Stop the thread that calls PushAudio() first. It must never push into a destroyed voice.
Additional Resources
Last updated on
Back to top