Code
# The sentence, read by each talker, at its native 16 kHz. Sources: docs/speech/SOURCES.md.
SPEAKERS = {"Male talker": "bdl", "Female talker": "slt"}
talkers = {
label: finish(so.load(fetch(f"docs/speech/{speaker}_arctic_a0131.flac")))
for label, speaker in SPEAKERS.items()
}
tracks = {label: so.f0_track(snd) for label, snd in talkers.items()}
fs = talkers["Male talker"].fs
F_LO, F_HI = 80, 7600
# Every spectrogram: a wideband STFT (Hann 5 ms), dB re its own maximum over 60 dB, 0 to 8 kHz.
wide = so.GaborFrame(0.005, 0.001, n_fft=1024)
FMAX, DB = 8000, 60
def bands(snd, n_bands, env_lowpass=50.0):
"""The envelopes the vocoder keeps: ERB bands, Hilbert envelopes, lowpassed."""
fb = so.cosine_filterbank(n_bands, F_LO, F_HI)
return fb.analyze(snd).envelopes(lowpass=env_lowpass, fs=1000)
def draw(axes, snd, envelopes=None, title=""):
"""Waveform, spectrogram and (if given) the band envelopes, down one column of axes."""
snd.plot(axes[0], color="k", lw=0.4)
axes[0].set_title(title)
wide.analyze(snd).plot(axes[1], db_range=DB, colorbar=False, fmax=FMAX)
axes[1].set_title("Spectrogram (Hann 5 ms)")
if envelopes is not None and envelopes.data.shape[1] == 3: # one band (and the two dropped edges)
axes[2].plot(envelopes.t, envelopes.data[:, 1, 0], color="k", lw=0.6)
axes[2].set(title="The one envelope the vocoder keeps", ylabel="Envelope")
elif envelopes is not None:
envelopes.plot(axes[2], db_range=DB, colorbar=False, fscale="linear", fmax=FMAX)
axes[2].set_title(f"The {envelopes.data.shape[1] - 2} band envelopes the vocoder keeps")
for ax in axes:
ax.set_xlim(0, snd.duration)
ax.set_xlabel("")
axes[-1].set_xlabel("Time [s]")
def show(snd, envelopes=None, title=""):
"""One sound: waveform, spectrogram and (if given) band envelopes on one time axis.
Returns the figure and the panels the playhead follows."""
n = 3 if envelopes is not None else 2
fig = plt.figure(figsize=(10, 2.0 + 2.2 * (n - 1)), layout="constrained")
axes = fig.subplots(n, 1, sharex=True, height_ratios=[0.55] + [1] * (n - 1))
draw(axes, snd, envelopes, f"Waveform{': ' + title if title else ''}")
return fig, list(axes)
def show_pair(sounds, n_bands=None, env_lowpass=50.0, title=""):
"""One column per talker, as `show`; the band envelopes are drawn from each talker's
original recording. Returns the figure and, for each talker, the panels the playhead follows."""
n = 2 if n_bands is None else 3
fig = plt.figure(figsize=(10, 1.6 + 2.0 * (n - 1)), layout="constrained")
columns = fig.subfigures(1, 2)
playhead = {}
for column, (label, snd) in zip(columns, sounds.items(), strict=True):
axes = column.subplots(n, 1, sharex=True, height_ratios=[0.55] + [1] * (n - 1))
envelopes = None if n_bands is None else bands(talkers[label], n_bands, env_lowpass)
draw(axes, snd, envelopes, f"{label}{': ' + title if title else ''}")
playhead[label] = list(axes)
return fig, playhead
def vocode_both(n_bands, **options):
"""Both talkers through the same vocoder."""
return {
label: finish(so.channel_vocode(snd, n_bands, F_LO, F_HI, **options))
for label, snd in talkers.items()
}