Speech-to-Speech Translation › Worked Example: A Full Cascaded Pipeline

Michael BrenndoerferNovember 25, 20265 min read
Language AI HandbookAll chapters
Part 27 of 67Machine Translation and Speech Translation
Chapter 196Speech-to-Speech Translation › Worked Example: A Full Cascaded Pipeline
Reading section 10 of 14 · Worked Example: A Full Cascaded Pipeline · 5 minSection 10 of 14 · 5 min

Section 10 of 14 · 5 min read

Reading settings

Definitions

Choose how many terms have tooltips.

Article links

Make inline references clickable

Notes & comments

We will run each stage of an audio-to-text, text-to-text, and text-to-audio cascade on a small test signal. The input represents four English words and the output uses German tone identifiers. The lookup uses lowercase symbols, such as “guten morgen liebe freunde”. In ordinary written German the phrase is “Guten Morgen, liebe Freunde”. To translate speech, replace the tone recognizer, dictionary, and synthesizer with models for those tasks.

The English source sample rate is 16 kHz. The pipeline will synthesize the German test signal at 24 kHz. Each word is encoded as a quarter-second segment. The fundamental frequency identifies the word, while the relative amplitudes of its harmonics determine the tone's timbre.

PythonIn [15]
CollapseExpand
from math import gcd

import numpy as np
from scipy.signal import resample_poly

word_seconds = 0.25
recognizer_rate = 16_000
source_rate = 16_000
target_rate = 24_000
english_tones = {"good": 120, "morning": 136, "dear": 152, "friends": 168}
german_tones = {"guten": 196, "morgen": 208, "liebe": 220, "freunde": 232}
reference_en = "good morning dear friends"
reference_de = "guten morgen liebe freunde"


def synthesize_tones(text, rate, tones, harmonic_weights):
    samples = round(word_seconds * rate)
    time = np.arange(samples) / rate
    envelope = np.hanning(samples)
    frames = []
    for word in text.split():
        fundamental = tones[word]
        frame = np.zeros(samples)
        for harmonic, weight in enumerate(harmonic_weights, start=1):
            frame += weight * np.sin(2 * np.pi * harmonic * fundamental * time)
        frames.append(envelope * frame / sum(harmonic_weights))
    return np.concatenate(frames)


def resample_waveform(waveform, original_rate, output_rate):
    if original_rate == output_rate:
        return waveform
    factor = gcd(original_rate, output_rate)
    return resample_poly(
        waveform, output_rate // factor, original_rate // factor
    )


source_audio = synthesize_tones(
    reference_en, source_rate, english_tones, (1.0, 0.35, 0.12)
)
ConsoleOut [16]
CollapseExpand
Source samples: 16,000 at 16,000 Hz
Source audio duration: 1.00 s
Source reference: good morning dear friends

The source contains one second of audio. The reference text stays outside the recognizer, so we can compare its output to the expected words.

Stage 1: Recognize the Source Tones

The tone recognizer splits the waveform into known-duration frames, finds the strongest frequency in each, and returns the nearest word identifier. A production recognizer such as Whisper shares the audio-to-text task, but infers words and their boundaries from natural speech.

PythonIn [17]
CollapseExpand
def frame_spectra(waveform, rate):
    frame_samples = round(word_seconds * rate)
    frames = waveform.reshape(-1, frame_samples)
    frequencies = np.fft.rfftfreq(frame_samples, d=1 / rate)
    magnitude = np.abs(np.fft.rfft(frames, axis=1))
    return frequencies, magnitude


def recognize_tones(waveform, rate, tones):
    waveform = resample_waveform(waveform, rate, recognizer_rate)
    frequencies, magnitude = frame_spectra(waveform, recognizer_rate)
    strongest_hz = frequencies[magnitude.argmax(axis=1)]
    words = [
        min(tones, key=lambda word: abs(tones[word] - peak))
        for peak in strongest_hz
    ]
    return " ".join(words)


transcript_en = recognize_tones(source_audio, source_rate, english_tones)
ConsoleOut [18]
CollapseExpand
Recovered source words: good morning dear friends
Match to source reference: True

The FFT recovers the four expected word identifiers. The output string contains no indication of which harmonics accompanied the fundamental.

Stage 2: Machine Translation

A direction-specific model such as English-to-German Opus-MT consumes the transcript and generates a translation. The notebook uses a four-word bilingual dictionary in place of the model. The example phrase was chosen because the word order agrees across the two languages. A real translator needs to select among meanings in context and may reorder or insert words. English-to-German Opus-MT model card

PythonIn [19]
CollapseExpand
word_translation = {
    "good": "guten",
    "morning": "morgen",
    "dear": "liebe",
    "friends": "freunde",
}
translation_de = " ".join(
    word_translation[word] for word in transcript_en.split()
)
ConsoleOut [20]
CollapseExpand
Translator input:  good morning dear friends
Translator output: guten morgen liebe freunde

If a source word is misrecognized, the translation uses the recovered word. It has only the transcript to work with.

Stage 3: Synthesize the Target Tones

A production synthesizer needs to accept the translated text and emit audio at its documented sample rate. The public SpeechT5 checkpoint was fine-tuned on English LibriTTS. For this German example, choose a checkpoint trained or fine-tuned for German. Changing the spelling of the input leaves the model’s training data and learned parameters unchanged.

For the offline example, the German tone inventory supplies the frequencies and a fixed set of harmonic weights gives the target signal its own timbre. The source signal and its harmonic weights are absent from the synthesis call.

PythonIn [21]
CollapseExpand
target_speech = synthesize_tones(
    translation_de, target_rate, german_tones, (1.0, 0.10, 0.42)
)
ConsoleOut [22]
CollapseExpand
Synthesis input: guten morgen liebe freunde
Target samples: 24,000 at 24,000 Hz
Target audio duration: 1.00 s

The generated audio has the same duration as the source because the phrase has four equal-length segments in both tone inventories. The sample counts differ because the signals use different sample rates.

Measuring End-to-End Fidelity

ASR-BLEU scores text recovered from the generated target audio against an independently specified reference. The target recognizer receives the audio and its sample rate. Here, it decodes lowercase German tone identifiers and we compute smoothed sentence BLEU on whitespace-delimited IDs. The printed scores use the fractional 0–1 scale. The later FLEURS benchmark uses the 0–100 scale and a different evaluation protocol.

PythonIn [23]
CollapseExpand
from nltk.translate.bleu_score import SmoothingFunction, sentence_bleu


def evaluate_target_audio(waveform, output_rate, reference, target_recognizer):
    transcript = target_recognizer(waveform, output_rate)
    score = sentence_bleu(
        [reference.split()],
        transcript.split(),
        smoothing_function=SmoothingFunction().method1,
    )
    return transcript, score


def recognize_german(waveform, rate):
    return recognize_tones(waveform, rate, german_tones)


target_transcript, target_bleu = evaluate_target_audio(
    target_speech, target_rate, reference_de, recognize_german
)

altered_audio = synthesize_tones(
    "guten morgen guten freunde", target_rate, german_tones, (1.0, 0.10, 0.42)
)
altered_transcript, altered_bleu = evaluate_target_audio(
    altered_audio, target_rate, reference_de, recognize_german
)
ConsoleOut [24]
CollapseExpand
Decoded target:  guten morgen liebe freunde (1.000 BLEU)
Altered target:  guten morgen guten freunde (0.188 BLEU)
Reference:       guten morgen liebe freunde

Changing the third target tone changes the recovered German transcript and lowers its score. This checks that the metric reads the synthesized audio. With real recognizers, a low ASR-BLEU score can reflect either the translation and synthesis or errors made when recognizing the generated audio.

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!

Newsletter

Stay up to date

Get new articles, book updates, and notes on AI and technology.

Unsubscribe at any time. Privacy policy