This is part 6 of my series on a jack-o'-lantern that talks to trick-or-treaters. If you haven't read part 1, start there. In part 5 the pumpkin learned who's standing in front of it, but it still sounds like a stock text-to-speech voice coming out of a speaker. In this part I turn that voice into the pumpkin's own with audio effects that run live in the browser. I also keep the pumpkin from hearing itself through its own microphone.
The agent's voice reaches the page as ordinary call audio over WebRTC. Everything in this post happens after that, on the computer hidden in the prop. The effects take no time on the call, and I can adjust them while I listen.
Get the call's audio into Web Audio
The Browser SDK hands the page the call's incoming audio as a media stream. I route it into Web Audio, the browser's audio processing API, where the effects live. Chrome only passes remote WebRTC audio to Web Audio when a media element also holds the stream, so the page uses a muted <audio> element. The speaker plays the processed voice from Web Audio, and the muted element only keeps Chrome delivering the stream.
const streamSub = call.remoteStream$.subscribe((stream) => {
source?.disconnect();
source = undefined;
voice.srcObject = stream;
if (stream?.getAudioTracks().length) {
source = Tone.getContext().createMediaStreamSource(stream);
Tone.connect(source, chain.input);
}
});const streamSub = call.remoteStream$.subscribe((stream) => {
source?.disconnect();
source = undefined;
voice.srcObject = stream;
if (stream?.getAudioTracks().length) {
source = Tone.getContext().createMediaStreamSource(stream);
Tone.connect(source, chain.input);
}
});const streamSub = call.remoteStream$.subscribe((stream) => {
source?.disconnect();
source = undefined;
voice.srcObject = stream;
if (stream?.getAudioTracks().length) {
source = Tone.getContext().createMediaStreamSource(stream);
Tone.connect(source, chain.input);
}
});const streamSub = call.remoteStream$.subscribe((stream) => {
source?.disconnect();
source = undefined;
voice.srcObject = stream;
if (stream?.getAudioTracks().length) {
source = Tone.getContext().createMediaStreamSource(stream);
Tone.connect(source, chain.input);
}
});voice is that muted element, and chain.input is the start of the effects chain.
Drop the pitch
A lower voice sounds bigger and older. At a quarter octave lower, a friendly narrator sounds like something that's been burning in a yard for a few centuries. Much lower and the words turn muddy and hart to understand.
After the pitch shift, the voice goes through chorus, a low-pass filter, and reverb, all from Tone.js. Chorus mixes in copies of the voice, each delayed by a few milliseconds. The delay keeps wobbling, so each copy's pitch drifts a little, and one voice sounds like several speaking almost together. I mix in 30% of the copies, which makes the voice slightly uncanny without blurring the words.
Finally a low-pass filter that cuts everything above 6 kHz to further deepen the voice. Text-to-speech voices carry a lot of hiss and sharp "s" sounds up there, and taking them out makes the voice duller and rounder, as if it came from deep inside something.
It's one heck of an effect and makes a stock TTS voice sound like custom recordings.
const pitch = context.createAudioWorkletNode("pitch-shifter", { outputChannelCount: [1] });
pitch.parameters.get("semitones").value = s.pitchSemitones;
const chorus = new Tone.Chorus({ frequency: 0.6, delayTime: 20, depth: 0.5, wet: s.chorusMix }).start();
const filter = new Tone.Filter(s.lowpassHz, "lowpass");
const reverb = new Tone.Reverb({ decay: s.reverbSeconds, wet: s.reverbMix });
await reverb.ready;
const output = new Tone.Gain();
Tone.connect(pitch, chorus);
chorus.chain(filter, reverb, output);const pitch = context.createAudioWorkletNode("pitch-shifter", { outputChannelCount: [1] });
pitch.parameters.get("semitones").value = s.pitchSemitones;
const chorus = new Tone.Chorus({ frequency: 0.6, delayTime: 20, depth: 0.5, wet: s.chorusMix }).start();
const filter = new Tone.Filter(s.lowpassHz, "lowpass");
const reverb = new Tone.Reverb({ decay: s.reverbSeconds, wet: s.reverbMix });
await reverb.ready;
const output = new Tone.Gain();
Tone.connect(pitch, chorus);
chorus.chain(filter, reverb, output);const pitch = context.createAudioWorkletNode("pitch-shifter", { outputChannelCount: [1] });
pitch.parameters.get("semitones").value = s.pitchSemitones;
const chorus = new Tone.Chorus({ frequency: 0.6, delayTime: 20, depth: 0.5, wet: s.chorusMix }).start();
const filter = new Tone.Filter(s.lowpassHz, "lowpass");
const reverb = new Tone.Reverb({ decay: s.reverbSeconds, wet: s.reverbMix });
await reverb.ready;
const output = new Tone.Gain();
Tone.connect(pitch, chorus);
chorus.chain(filter, reverb, output);const pitch = context.createAudioWorkletNode("pitch-shifter", { outputChannelCount: [1] });
pitch.parameters.get("semitones").value = s.pitchSemitones;
const chorus = new Tone.Chorus({ frequency: 0.6, delayTime: 20, depth: 0.5, wet: s.chorusMix }).start();
const filter = new Tone.Filter(s.lowpassHz, "lowpass");
const reverb = new Tone.Reverb({ decay: s.reverbSeconds, wet: s.reverbMix });
await reverb.ready;
const output = new Tone.Gain();
Tone.connect(pitch, chorus);
chorus.chain(filter, reverb, output);Each of these settings has a slider on the page and on the phone remote from part 8, and changes apply mid-sentence so I can tune the voice.
Keep the pumpkin from hearing itself
The pumpkin's microphone sits a couple feet below the speaker, so everything the pumpkin says comes straight back into the mic. The speech recognizer hears it as a visitor talking, and the pumpkin starts answering itself.
Browsers have echo cancellation for this, and the page turns it on, but it doesn't help here. The sound from the pumpkin's speaker has been pitched down and given echoes on purpose. What's in the room no longer matches what the browser is listening for.
So the mic goes through a noise gate I wrote, another AudioWorklet. A noise gate lets sound through only when it's loud enough to be speech, and mine has a second input. That input is the pumpkin's own voice, tapped from the end of the effects chain. While the voice is playing, and for 0.3 seconds afterward to cover the reverb's tail, the gate keeps the mic muted.
this.voiceEnvelope += (Math.abs(voice ? voice[n] : 0) - this.voiceEnvelope) * this.envelopeCoef;
if (this.voiceEnvelope >= duckThreshold) this.duckLeft = duckHoldSamples;
else if (this.duckLeft > 0) this.duckLeft--;
if (this.duckLeft > 0) {
this.holdLeft = 0;
} else if (this.envelope >= threshold) {
this.holdLeft = holdSamples;
} else if (this.holdLeft > 0) {
this.holdLeft--;
}this.voiceEnvelope += (Math.abs(voice ? voice[n] : 0) - this.voiceEnvelope) * this.envelopeCoef;
if (this.voiceEnvelope >= duckThreshold) this.duckLeft = duckHoldSamples;
else if (this.duckLeft > 0) this.duckLeft--;
if (this.duckLeft > 0) {
this.holdLeft = 0;
} else if (this.envelope >= threshold) {
this.holdLeft = holdSamples;
} else if (this.holdLeft > 0) {
this.holdLeft--;
}this.voiceEnvelope += (Math.abs(voice ? voice[n] : 0) - this.voiceEnvelope) * this.envelopeCoef;
if (this.voiceEnvelope >= duckThreshold) this.duckLeft = duckHoldSamples;
else if (this.duckLeft > 0) this.duckLeft--;
if (this.duckLeft > 0) {
this.holdLeft = 0;
} else if (this.envelope >= threshold) {
this.holdLeft = holdSamples;
} else if (this.holdLeft > 0) {
this.holdLeft--;
}this.voiceEnvelope += (Math.abs(voice ? voice[n] : 0) - this.voiceEnvelope) * this.envelopeCoef;
if (this.voiceEnvelope >= duckThreshold) this.duckLeft = duckHoldSamples;
else if (this.duckLeft > 0) this.duckLeft--;
if (this.duckLeft > 0) {
this.holdLeft = 0;
} else if (this.envelope >= threshold) {
this.holdLeft = holdSamples;
} else if (this.holdLeft > 0) {
this.holdLeft--;
}The wiring connects the voice tap to the gate's second input:
const node = context.createAudioWorkletNode("noise-gate", { numberOfInputs: 2, outputChannelCount: [1] });
const voiceTap = context.createGain();
chain.output.connect(voiceTap);
voiceTap.connect(node, 0, 1);
source.connect(node);
node.connect(destination);const node = context.createAudioWorkletNode("noise-gate", { numberOfInputs: 2, outputChannelCount: [1] });
const voiceTap = context.createGain();
chain.output.connect(voiceTap);
voiceTap.connect(node, 0, 1);
source.connect(node);
node.connect(destination);const node = context.createAudioWorkletNode("noise-gate", { numberOfInputs: 2, outputChannelCount: [1] });
const voiceTap = context.createGain();
chain.output.connect(voiceTap);
voiceTap.connect(node, 0, 1);
source.connect(node);
node.connect(destination);const node = context.createAudioWorkletNode("noise-gate", { numberOfInputs: 2, outputChannelCount: [1] });
const voiceTap = context.createGain();
chain.output.connect(voiceTap);
voiceTap.connect(node, 0, 1);
source.connect(node);
node.connect(destination);Muting the mic means visitors can't interrupt the pumpkin mid-sentence. I can live with that, since the character doesn't think anyone should interrupt it anyway. Between sentences the gate keeps doing its ordinary job, and street noise below its threshold never reaches the recognizer. It also reports when it's muting, which is how the stage from part 5 knows to wait before sending a message into the call.
Traps to avoid
Don't count on browser echo cancellation when the voice plays through Web Audio. Once you've changed the voice with effects, the sound from the speaker no longer matches the call audio the browser subtracts. Mute the mic while the agent speaks, and use the agent's own voice as the signal.
Next
The pumpkin looks, listens, and sounds like itself. What it can't do is keep count of how long a group has stayed, or which mode it should be in. In part 7 the server takes over both jobs.
The series
The system and the call.
The agent: a personality and three modes, as contexts and steps.
Tools: what the AI calls on my server, and why its material lives in code.
Vision: letting the agent see, and checking the camera before any decision that matters.
The stage: detecting who is standing in front of the pumpkin, in the browser.
The voice: audio effects in the browser (this post).
Pacing and state: the server keeps score.
The operator: a person with a phone who can overrule the agent.
Testing it, and running it for a whole night.