Audio Integration

Overview

Voice Core does not touch audio devices: the application captures and plays back. This page covers the glue between an audio library and the voice API.

Sample Format

Both PushAudio() and PlaybackBuffer::Read() work with 32-bit float samples in the range [-1, 1], interleaved when there are two channels.

AudioConvert.h converts between 16-bit integer and float samples. Each function converts as many samples as the shorter of its two spans holds:

C++

#include "RealtimeCore/Voice/AudioConvert.h"

// Capture: 16-bit samples from the device, pushed into the local voice.
void OnCapture(std::span<const int16_t> pcm)
{
    std::vector<float> captured(pcm.size());
    RealtimeCore::Voice::ConvertInt16ToFloat(pcm, captured);
    localVoice->PushAudio(captured);
}

// Playback: the float mix of the remote voices, written to a 16-bit device buffer.
void OnPlayback(std::span<const float> mix, std::span<int16_t> device)
{
    RealtimeCore::Voice::ConvertFloatToInt16(mix, device);
}

Choosing an Audio Library

Any library that delivers sample buffers, by callback or by polling, works: PortAudio, miniaudio, an engine's audio system or a platform API.

Opening the devices at the stream's rate and channel count, 48 kHz mono, avoids resampling.

Threading Model

The service thread calls RealtimeClient::Service() and VoiceClient::Service(). The VoiceClient::OnRemoteVoiceAdded and OnRemoteVoiceRemoved handlers run on it, and so does every LocalVoice method except PushAudio(). The audio thread calls PushAudio() with the captured samples, always from the same thread, and PlaybackBuffer::Read() for playback.

Voice Core runs threads of its own for encoding and decoding. Each local voice has one encoder thread, started by its first PushAudio(). Each remote voice has one decoder thread, started when the voice is announced. Both end with their voice.

Sharing Remote Voices With the Audio Thread

Reading a single voice is safe: PlaybackBuffer::Read() and the decoder's writes share an internal mutex that is held only while samples are copied. Copying or releasing a std::shared_ptr is thread-safe as well.

The audio thread needs no pacing of its own: it calls Read() from the device callback with whatever number of samples the device asks for. Reading too early underruns into silence while the buffer refills, and reading too rarely builds a backlog that is dropped above VoiceClientOptions::MaxPlaybackDelayMs. Read from a steady real-time thread, not from an irregular game loop.

What needs care is the list of voices. The application owns it and changes it on the service thread, and the audio thread must never iterate a list while it changes.

The service thread publishes an immutable copy of the list behind a std::shared_ptr<const ...> and replaces it on every change. The audio callback copies only that pointer under a short lock and never holds the lock while mixing:

C++

using RemoteVoiceList = std::vector<std::shared_ptr<RemoteVoice>>;

std::mutex                             snapshotMutex;
std::shared_ptr<const RemoteVoiceList> snapshot = std::make_shared<const RemoteVoiceList>();

// Service thread: call from the OnRemoteVoiceAdded and OnRemoteVoiceRemoved handlers,
// after changing the application's own list of voices.
void PublishSnapshot(const RemoteVoiceList& remoteVoices)
{
    auto copy = std::make_shared<const RemoteVoiceList>(remoteVoices);
    const std::lock_guard<std::mutex> lock(snapshotMutex);
    snapshot = std::move(copy);
}

// Audio thread: the device callback. scratch is preallocated to the largest callback size.
void OnAudio(std::span<float> output, std::span<float> scratch)
{
    std::shared_ptr<const RemoteVoiceList> voices;
    {
        const std::lock_guard<std::mutex> lock(snapshotMutex);
        voices = snapshot;
    }

    std::fill(output.begin(), output.end(), 0.0f);
    for (const auto& remote : *voices)
    {
        remote->GetPlaybackBuffer().Read(scratch.first(output.size()));
        for (std::size_t i = 0; i < output.size(); ++i)
        {
            output[i] += scratch[i];
        }
    }
}

A removed voice lives on while a snapshot still holds it. If the audio thread drops the last reference, the voice is destroyed there and frees its buffer inside the callback. Strict real-time code hands that release back to the service thread.

Latency

The delay from one player's microphone to another player's speaker adds up along the way. The audio library sets the capture and output buffer sizes. LocalVoiceOptions::FrameDurationMs sets the length of one frame, and the interval between VoiceClient::Service() calls adds to it. The network delay depends on the region the clients connect to. VoiceClientOptions::PlaybackDelayMs sets the delay of the playback buffer.

Start with a service tick of 10 ms or less, 20 ms frames and the default playback delay of 100 ms. A stable network allows a lower playback delay, and a jittery one needs a higher one.

Echo and Feedback

Without echo cancellation, the speaker output reaches the microphone and is sent back to the player who spoke. Headphones avoid it, and testing with debug echo or two instances on one machine needs them.

Where a platform offers voice processing, such as the voice-communication audio modes on mobile, it can help. Echo cancellation in the SDK is planned.

Additional Resources

Last updated on

Back to top