Skip to content
/a/ FaceCue Performance Studio

Other Audio Systems

Following a voice that Unity's audio does not play.

In most games, one of your scripts plays a line through the character's Audio Source, and FaceCue follows the line by watching that Audio Source. A game that plays its voice through an audio system of its own, such as FMOD, Wwise or an engine you wrote, keeps that audio out of Unity, so there is nothing to watch. The character's Voice Source takes its place. It tells the character which line is playing and how far into it the voice is, and FaceCue does the rest, as it does for a clip.

The voice then drives the whole face, as a clip would. The face plays the line's bake, the mouth, the head and the brows alike, holds through a pause and stops when the voice ends.

A Voice Source is one script, and it can implement up to three interfaces:

  • IVoiceSource, the Voice Source itself, which every one needs. It tells the character which line is playing and how far into it the voice is. See Implementing a Voice Source.
  • IVoiceSamples, optional. It gives FaceCue the line's audio, so Live Viseme and Live Amplitude can follow it. See Implementing Voice Samples.
  • IVoiceOutputDelay, optional. It says how far your engine's reported position runs ahead of what is heard, so the mouth keeps to the sound. See Implementing an Output Delay.

FMOD

FMOD has a Voice Source ready made, the FaceCue FMOD Bridge. Everything about it is on the FMOD page.

The Voice Source Row

A character's Voice Source sits under its Audio Source, in the FaceCue Brain's Core References and in the Speaker Context's references. The row shows once the project has something that can be one: FMOD for Unity with the bridge, or a voice source of your own.

A character can have an Audio Source and a Voice Source at once. Whichever starts a line last has the mouth, and the other gets it back when that line ends.

To change it while the game runs:

public void SetVoiceSource(IVoiceSource source)
Parameter What it does
IVoiceSourcesource
The voice source to follow, or null to follow none

It is on the character's Speaker Context, brain.Speaker. A line playing through the old voice source ends there, and whatever the new one plays is picked up from the next frame. A line on the Audio Source carries on.

Implementing a Voice Source

A Voice Source is one script that you write. It is a component, so it sits on an object in the scene, and it implements an interface named IVoiceSource. An interface is a list of members a class promises to have. This one lists eight properties and no methods, and every one of them is read-only: FaceCue reads them, and your script decides what each returns.

public interface IVoiceSource
Member What it does
string read-onlyClipId
Which line is playing: the name its cues were baked under
int read-onlyLineNumber
A number that changes each time a new line starts
bool read-onlyIsPlaying
Whether the line's voice is going, a pause included
bool read-onlyIsPaused
Whether the line is paused part of the way through
float read-onlyLinePositionSeconds
How far into the line the voice is, in seconds
float read-onlyPitch
How fast the voice plays. 1 is as recorded
float read-onlyLineLengthSeconds
The line's length in seconds, or 0 when it is not known
float read-onlyPositionStepSeconds
How far LinePositionSeconds moves each time it changes, in seconds, or 0 when it moves smoothly

The rest of this section goes through what that means for the script you write: who does what, when each property is read, what each should return, and a script to start from.

Your Game Still Plays the Sound

The script plays nothing, and FaceCue plays nothing either. Your game plays the line through your audio engine exactly as it does now. FaceCue never starts, stops, pauses or moves the sound, and it never calls a method on your script. The properties only describe what your engine is doing, so the character can follow it.

How FaceCue Knows a Line Has Started

Nothing is sent. There is no method to call and no event to raise when a line starts or ends.

Every frame, FaceCue reads IsPlaying, and compares what it reads with what it read the frame before. A line begins in one of two ways, and either one is enough on its own:

  • IsPlaying turns true. It read false on the last frame and reads true on this one. LineNumber plays no part here: a line begins whether the number changed or not.
  • LineNumber changes while IsPlaying stays true. No frame of false is needed between the two lines. This is the only way FaceCue can tell one line from the next when your engine starts the second as the first ends.

Each time a line begins, FaceCue reads ClipId, finds that line's cues and starts the character on them, at the point LinePositionSeconds reads. A line ends on the frame IsPlaying reads false.

Frame by frame:

Last frame This frame What FaceCue does
IsPlaying false IsPlaying true Begins a line, whatever LineNumber reads
IsPlaying true IsPlaying true, LineNumber changed Begins a new line in place of the one that was playing
IsPlaying true IsPlaying true, LineNumber the same Carries on with the same line
IsPlaying true IsPlaying false Ends the line
IsPlaying false IsPlaying false Nothing, whatever LineNumber reads

A change in LineNumber does nothing on its own. It only counts on a frame where IsPlaying is true. And a line never begins while IsPaused is true: it begins on the first frame the voice is going.

So from your script's side, starting a line means making IsPlaying return true, or changing LineNumber if it already is, with ClipId already describing the new line when you do.

When Each Property Is Read

FaceCue reads the properties on the main thread, on every frame the character is updated.

Property When it is read
IsPlaying Every frame, whether a line is playing or not
IsPaused Every frame while IsPlaying is true
LineNumber Every frame while the voice is going
ClipId Once, on the frame a line begins
LinePositionSeconds, Pitch, LineLengthSeconds, PositionStepSeconds While a line plays, often several times a frame

Because they are read this often, each should return a value your script already holds, with no searching, no allocating and no side effects. If your engine reports from another thread, copy what it reports into fields and return those.

What to Return for Each

ClipId

The name the line's cues were baked under. That is the name of the line's audio file without its extension, the name it would carry as an audio clip in Unity, and it has to match the line's entry in the Cue Registry. See The Naming Rule.

It is read when a line begins, so set it before IsPlaying turns true or LineNumber changes. Return null or an empty string for a line with no baked cues.

LineNumber

A counter. Start it anywhere, and add one each time your engine starts a line. FaceCue does not care what the number is, only that it changed.

It matters only when you start a line without IsPlaying reading false for a frame first, whether that is the same line again or a different one:

  • Change LineNumber and keep ClipId, and the same line's cues start again.
  • Change LineNumber and ClipId, and the new line's cues play.
  • Change ClipId alone, and nothing happens. The character carries on with the cues it had.

When IsPlaying reads false on one frame and true on a later one, a line begins whatever its number is. Adding one every time is still the advice, even from a plain counter. The rule is simple, and the number itself never matters, only that it changed.

IsPlaying

True from the moment the line's voice starts until it ends. Keep it true through a pause.

Report the voice, not whatever plays it. If the line sits inside an event that carries on with music or a tail after the last word, return false when the voice finishes.

IsPaused

True while the line is stopped part of the way through and will carry on. The character holds where it is until it does. IsPlaying stays true through the pause, and LinePositionSeconds stays where it was.

If your game never pauses a line, return false.

LinePositionSeconds

How many seconds into the line's own audio the voice is: 0 at the voice's first moment, and the line's length at its last. If the voice is one sound inside a longer event, count from where the voice starts, not from where the event starts.

Most engines report a playback position in seconds, milliseconds or samples. Convert it to seconds.

Pitch

How fast the voice plays, as a multiple of the speed it was recorded at: 1 as recorded, 2 at double speed. LinePositionSeconds is still the position in the line, so at 2 it rises twice as fast.

If your game never changes a voice's speed, return 1.

LineLengthSeconds

The line's length in seconds. Return 0 when your engine does not know it.

PositionStepSeconds

Many engines move their playback position only when they mix a block of audio, so LinePositionSeconds holds still for a few frames and then jumps. If yours does, return the size of one jump in seconds, and the character moves smoothly between jumps. It is usually the engine's block length divided by its sample rate: 1,024 samples at 48,000 Hz come to about 0.021.

If LinePositionSeconds moves a little every frame, return 0.

A Starting Point

A voice source for an engine of your own. Your audio code calls the four methods at the top as things happen, and FaceCue reads the properties at the bottom:

using Firecrest.Systems.FaceCue;
using UnityEngine;

public class MyEngineVoice : MonoBehaviour, IVoiceSource
{
    // What your engine has told this script about the line it is playing
    string _lineName;
    int _lineNumber;
    bool _playing, _paused;
    float _time, _length;

    // Call this where your engine starts a line
    public void LineStarted(string fileName, float length)
    {
        _lineName = fileName;   // the name its cues were baked under
        _length = length;
        _lineNumber++;          // a new line, even when it is the same one again
        _time = 0f;
        _playing = true;
        _paused = false;
    }

    // Call these as your engine reports them
    public void VoiceAt(float seconds) => _time = seconds;
    public void Paused(bool paused) => _paused = paused;
    public void LineEnded() => _playing = false;

    // What FaceCue reads
    public string ClipId => _lineName;
    public int LineNumber => _lineNumber;
    public bool IsPlaying => _playing;
    public bool IsPaused => _paused;
    public float LinePositionSeconds => _time;
    public float Pitch => 1f;                  // never changed in this game
    public float LineLengthSeconds => _length;
    public float PositionStepSeconds => 0f;    // VoiceAt is called every frame, so the position already moves smoothly
}

And in your own audio code, wherever a line is played:

// As the line starts
voice.LineStarted("guard_greeting_01", lengthInSeconds);

// Every frame while it plays
voice.VoiceAt(positionInSeconds);

// When the voice finishes
voice.LineEnded();

Connecting It to the Character

Add the script to the character, or to whatever object plays its voice, and drag that object into the character's Voice Source row. To assign it from code, call SetVoiceSource on the character's Speaker Context.

A Line From Start to End

What the properties should read at each moment:

Moment IsPlaying IsPaused LinePositionSeconds LineNumber
Nothing is playing false false anything as it was
Your engine starts a line true false 0, then rising one more than before
The line is paused true true holding unchanged
The line carries on true false rising again unchanged
The voice ends false false anything unchanged
The next line starts straight away true false 0, then rising one more again

In the last row IsPlaying never reads false between the two lines, so the change in LineNumber is the only sign that a new line began.

Implementing Voice Samples

A Voice Source alone is enough for a baked line. Live Viseme and Live Amplitude follow the line's sound itself, so for them, implement a second interface, IVoiceSamples, on the same script, and FaceCue reads the line's audio through it.

public interface IVoiceSamples
Member What it does
VoiceSamplesState read-onlySamplesState
Whether this line's audio can be read: Unavailable, Preparing or Ready
int read-onlySampleRate
The rate of the samples in Hz
int read-onlyChannels
How many channels the samples are interleaved over: 1 for mono, 2 for stereo
int read-onlySampleCount
The line's length in samples, counted for one channel, or 0 when it is not known
int read-onlyFirstReadableSample
The first sample that can be read now
int read-onlyLastReadableSample
The last sample that can be read now
boolReadSamples(float[] destination, int startSample)
Copy samples into destination, starting startSample samples into the line
bool read-onlyIsLiveVoice
Whether the line is a live voice, such as a microphone or another player's voice in voice chat, rather than a recording

Line Position in Samples

FaceCue asks for the line's audio by sample number. Every member here that takes or returns a position uses the same numbers: FirstReadableSample, LastReadableSample, SampleCount, and the startSample of ReadSamples.

A sample's number is its place in the line. The line's first sample is 0, and the numbers count up from there, one per sample of each channel, so a stereo line's left and right samples at the same moment share one number. A number and a time are two ways of saying the same thing:

// The sample a moment of the line falls on, from its time in seconds
int sample = (int)(seconds * SampleRate);

// The sample the line has reached: the line's current sample
int currentSample = (int)(LinePositionSeconds * SampleRate);

At 48,000 Hz, sample 48,000 is one second into the line, and sample 24,000 is half a second in. The rest of this page calls the second value the line's current sample: where the line has got to, counted in samples.

The numbers belong to the line as it was recorded, not to how fast it plays. A line played at double speed still reaches sample 48,000 one second into its recording, which is half a second after it started.

How Samples Are Read

FirstReadableSample and LastReadableSample say which samples FaceCue can read. Where they sit against the line's current sample depends on what your engine can give, and FaceCue tells which it is by what they say, so there is nothing else to set.

Reading Ahead

Your engine gives you all of the line's samples, decoded from its file for example. FirstReadableSample is 0 and LastReadableSample is the line's last sample, so FaceCue can read ahead of the line's current sample, and the mouth leads each sound as it does on an audio clip. The starting point at the end of this section is this case.

Reading as It Plays

Some engines can't give you a copy of the line. They only let you capture the audio as they play it: they call a callback, a DSP or a filter of yours with each block, a short slice, typically a few thousandths to a few hundredths of a second. FaceCue can still follow the line this way. But it can only read what has already played, so the mouth moves with each sound rather than just ahead of it, as it does when it can read ahead.

What the two members say. Keep what you capture in a buffer, with each sample numbered by its place in the line (see Line Position in Samples):

  • LastReadableSample is the newest sample you have captured.
  • FirstReadableSample is the oldest sample you still have.

For example, three seconds into a line at 48,000 Hz, the line's current sample is 144,000. If you keep the last two seconds, LastReadableSample is about 144,000 and FirstReadableSample about 48,000.

LastReadableSample is usually close to the line's current sample. It can sit a little behind it, by part of a block, because your engine may count a block in the position it reports a moment before it hands that block to you. That's expected. FaceCue never reads past LastReadableSample. It reads what is there and picks up the rest when your next block arrives.

Numbering what you capture. FaceCue asks for audio by number: "samples 143,520 to 144,000", say. Each sample you keep needs the number of its place in the line.

If your engine tells you where each block sits in the line, use that. A time in seconds becomes a number as Line Position in Samples shows. A position counted at another rate, such as the audio file's own, goes through seconds first: divide by that rate, then multiply by SampleRate.

If your engine doesn't tell you, count the numbers yourself:

  1. When you start capturing, take the line's current sample. That's the number of the first sample you capture. If you start just as the line begins to play, it's 0.
  2. Each block carries on the count. A block of 480 samples per channel that starts at 144,000 holds samples 144,000 to 144,479, and the next block starts at 144,480.
  3. If the line jumps, after a seek or a loop, take the line's current sample again and count on from there.

How much to keep. About two seconds, so FaceCue can catch up after a slow frame. A ring buffer, one that writes over its oldest samples as new ones arrive, keeps it at that. Move FirstReadableSample along as the buffer moves on. FaceCue never asks for a sample older than that.

The audio thread. Your engine most likely calls your capture code on its own audio thread, while FaceCue reads on the main thread. Keep the buffer safe to read while the next block is being written. A lock around the buffer is the simplest way.

A line played at another speed. If your engine plays the line faster or slower than it was recorded, what you capture is sped up or slowed down. At double speed, a block of 480 samples covers 960 samples of the recording. Stretch what you capture back to its recorded speed before you keep it, so the numbers still match the line. If you can't, return Unavailable while the line plays at another speed.

The project setting. A line read as it plays is treated like an audio clip FaceCue can't read ahead in, so the project's Unreadable Clips setting decides whether the live modes follow it. They do, unless it's set to Skip Live Modes. A live voice, such as a microphone, is followed whatever it is set to (see IsLiveVoice).

SamplesState

public enum VoiceSamplesState
Value When to return it
Ready
The samples from FirstReadableSample to LastReadableSample can be read. When the line is read as it plays, return it from the line's first moment, before anything has been captured, with LastReadableSample below FirstReadableSample until something has
Preparing
The line's audio is on its way, a copy still opening for example. FaceCue waits for it
Unavailable
This line has no audio to give. The live modes leave it alone, and a baked line still plays in full

It is read for each line, so one line can be Ready and the next Unavailable.

ReadSamples

Fill the whole array, with the channels interleaved the way an audio clip stores them, and with silence for any part past the end of the line. Return true when it was filled. Return false when the samples cannot be read just now, and FaceCue asks again on a later frame. FaceCue only asks for samples from FirstReadableSample to LastReadableSample.

Give the line's audio as it was recorded, before the volume, distance and effects it is played with, so a character far from the listener does not read as a quiet one. These members are read several times a frame while a line plays, so return them cheaply too.

IsLiveVoice

True when the line is a live voice, heard as it is made, such as a microphone or another player's voice in voice chat. A live voice has no recording of the whole line to give and no set end, so its audio is given as it plays, as Reading as It Plays describes.

FaceCue then treats the line as it treats a microphone on the character's Audio Source. Its Console messages speak of a microphone and never suggest giving FaceCue the whole line, and the live modes follow it whatever Unreadable Clips is set to.

Return false for a recorded line, including one your engine can only give as it plays.

A Starting Point

The Voice Source's starting point above, given the line's whole audio as your engine decodes it:

public class MyEngineVoice : MonoBehaviour, IVoiceSource, IVoiceSamples
{
    // The line's whole audio, as your engine decoded it. Clear it in LineStarted
    // (_lineAudio = null), so a new line never reads the last one's audio
    float[] _lineAudio;
    int _lineRate, _lineChannels;

    // Call this once your engine has decoded the line
    public void LineDecoded(float[] samples, int sampleRate, int channels)
    {
        _lineAudio = samples;
        _lineRate = sampleRate;
        _lineChannels = channels;
    }

    // What FaceCue reads
    public VoiceSamplesState SamplesState =>
        _lineAudio != null ? VoiceSamplesState.Ready      // decoded
        : _playing ? VoiceSamplesState.Preparing          // started, still decoding
        : VoiceSamplesState.Unavailable;
    public int SampleRate => _lineRate;
    public int Channels => _lineChannels;
    public int SampleCount => _lineAudio != null ? _lineAudio.Length / _lineChannels : 0;
    public int FirstReadableSample => 0;
    public int LastReadableSample => SampleCount - 1;
    public bool IsLiveVoice => false;   // a recorded line, not a microphone or voice chat

    public bool ReadSamples(float[] destination, int startSample)
    {
        int from = startSample * _lineChannels;
        for (int i = 0; i < destination.Length; i++)
        {
            int at = from + i;
            destination[i] = at < _lineAudio.Length ? _lineAudio[at] : 0f;   // silence past the end
        }
        return true;
    }
}

For an engine that can only give the audio as it plays, keep what you capture in a ring instead, as Reading as It Plays describes, and return its oldest and newest samples from FirstReadableSample and LastReadableSample. For a microphone or voice chat, return true from IsLiveVoice as well.

Implementing an Output Delay

Most audio engines report the position they have mixed up to, while the speakers are still playing what was mixed a moment earlier. So LinePositionSeconds usually runs a little ahead of what the player hears, and a character that follows it moves a little early. Implement a third interface, IVoiceOutputDelay, on the same script to say how far ahead, and the character follows the sound instead, with the mouth running ahead of it as Anticipation describes, the way it does on an Audio Source line. Leave it out and the character follows LinePositionSeconds as reported.

public interface IVoiceOutputDelay
Member What it does
float read-onlyOutputDelaySeconds
How much audio your engine holds between mixing it and playing it, in seconds. Usually its buffer length times its buffer count, divided by its sample rate: four buffers of 512 samples at 48,000 Hz come to about 0.043. Return 0 when LinePositionSeconds is already the position being heard

A Starting Point

The Voice Source's starting point above, given its engine's own buffer settings:

public class MyEngineVoice : MonoBehaviour, IVoiceSource, IVoiceOutputDelay
{
    // your engine's buffer settings: four buffers of 512 samples at 48,000 Hz
    int _bufferLength = 512, _bufferCount = 4, _sampleRate = 48000;

    public float OutputDelaySeconds => _bufferLength * _bufferCount / (float)_sampleRate;
}

An engine can hold less than its buffer settings say, in a browser above all, so trust a measurement over the settings. What happens to the sound after the engine, in the operating system and the output device, is not part of it. The value is read every frame while a line plays, so return it cheaply.

When Something Looks Wrong

What you see What to check
The character does nothing when a line plays That the script is assigned as the character's Voice Source, that IsPlaying turns true, and that ClipId is the name the line was baked and registered under
The mouth moves, but on another line's words That ClipId was already the new line's when IsPlaying turned true, and that LineNumber changed if the line followed another with no gap
The same line played twice in a row does not start over That LineNumber changes each time a line starts
The character stays in the line after the voice has finished That IsPlaying reports the voice, not the event or track that plays it
The mouth is on the wrong part of the line That LinePositionSeconds is in seconds and counts from the start of the voice
The mouth moves in small steps That PositionStepSeconds gives the size of the jumps LinePositionSeconds moves in
The mouth runs a little early against the sound Implementing an Output Delay
Live Viseme or Live Amplitude gives way to another mode on a line read as it plays That SamplesState is Ready from the line's first moment, and that Unreadable Clips is not on Skip Live Modes