Speech-to-Speech and Expressive Voice › A minimal cascaded speech-to-speech loop
Language AI HandbookAll chapters This cascade converts a synthetic waveform to integer level summaries, computes alternating response values, and renders them as synthetic tones. The input values summarize frame energy and the response values set tone frequency.
The SyntheticASR groups complete analysis frames, discards low-level groups and returns rounded level values. A clip shorter than the 320-sample analysis frame returns an empty sequence. ToyDialog uses the count of input values. SyntheticTTS then renders one constant-frequency tone segment per response value.
PythonIn [20]Expand
class SyntheticASR:
"""Summarize rounded RMS levels for groups of frames above the threshold."""
def __init__(self, sr=SR, frame_len=frame_len, hop=hop):
self.sr, self.frame_len, self.hop = sr, frame_len, hop
def transcribe(self, waveform):
if len(waveform) < self.frame_len:
return []
n_f = 1 + (len(waveform) - self.frame_len) // self.hop
frames = np.stack(
[
waveform[i * self.hop : i * self.hop + self.frame_len]
for i in range(n_f)
]
)
rms = np.sqrt(np.mean(frames**2, axis=1))
# Start one group every 100 ms and keep only groups above the threshold.
tokens = []
for i in range(0, n_f, 10):
chunk_rms = rms[i : i + 10].mean()
if chunk_rms > 0.05:
tokens.append(int(round(chunk_rms * 100)))
return tokens
class ToyDialog:
"""Produces a response 'token' sequence from the input tokens."""
def respond(self, input_tokens):
# Append two positions to the input length.
n = len(input_tokens) + 2
# Alternating high/low values to look like varied intonation
return [80 + 40 * (i % 2) for i in range(n)]
class SyntheticTTS:
"""Render one constant-frequency tone per response value."""
def __init__(self, sr=SR):
self.sr = sr
def synthesize(self, token_seq, token_dur_s=0.10):
# Each token renders to `token_dur_s` of audio; value drives center freq
out = []
for v in token_seq:
n = int(self.sr * token_dur_s)
tt = np.arange(n) / self.sr
f = 200 + 3 * v # token value to center frequency
out.append(0.3 * np.sin(2 * np.pi * f * tt))
return np.concatenate(out) if out else np.zeros(0)
asr = SyntheticASR()
dialog = ToyDialog()
tts = SyntheticTTS()
in_tokens = asr.transcribe(waveform)
out_tokens = dialog.respond(in_tokens)
response_token_duration_s = 0.10
response_waveform = tts.synthesize(out_tokens, response_token_duration_s)ConsoleOut [21]Expand
input 'transcript' tokens: 10 response 'transcript' tokens: 12 response audio duration: 1.20 s
Ten input values give twelve response values under the toy dialog rule. Each response value renders 0.10 s of audio, giving 1.20 s in all. The stages run locally in sequence. The earlier chunk example shows how a deployment can schedule work and playback.
The top row shows the input waveform and its rounded level values. The lower-left panel gives the full response schedule. The lower-right panel zooms in around 0.10 s so you can see the two successive tone frequencies.
VisualizationOut [22]Expand

The dialog explicitly creates alternating high and low symbols. The synthesizer maps them to alternating tone frequencies. In the real text cascade the synthesizer derives its delivery from the response text and any additional controls the interface carries.
Comments
Add to the conversation
A question, a correction, or an example from your own work.
No comments yet. Be the first to share your thoughts!