"""Sing a short scale on a vowel with the physical model: no recordings, no learned parts. python examples/sing_vowel.py # -> sing_vowel.wav (+ the measured pitch per note) python examples/sing_vowel.py --voice physvox/voices/rick_n2A.json # -> sing_vowel_rick_n2A.wav: the same scale with the folds, walls and # windpipe fitted to Rick Astley (his per-frame articulation is not included) Air pressure drives the self-oscillating folds; the closed-loop controller tunes fold stiffness to each note target; the tract glides from /ɑ/ to /i/ halfway through. Needs an NVIDIA GPU (CuPy).""" import json, sys from pathlib import Path import numpy as np, cupy as cp, soundfile as sf sys.path.insert(0, str(Path(__file__).resolve().parents[1])) from physvox.sim import Sim, set_p from physvox import params as PR FS = 44100 sim = Sim() # area functions glottis -> lips (m2): a crude /ɑ/ (narrow pharynx, open mouth) and /i/ (wide pharynx, narrow palate) x = np.linspace(0, 1, 40) area_a = np.interp(x, [0, .08, .35, .6, .85, 1], [.5e-4, .8e-4, .7e-4, 4e-4, 6e-4, 4e-4]) area_i = np.interp(x, [0, .08, .35, .6, .85, 1], [.5e-4, 2.5e-4, 5e-4, 2.5e-4, .3e-4, 1.5e-4]) nv, hv, av_a = sim.tract_nodes(area_a, 0.17) _, _, av_i = sim.tract_nodes(area_i, 0.17) ns, hs, as_ = sim.subglottal_nodes() P = PR.defaults()[None].copy() set_p(P, "NV", nv); set_p(P, "HV", hv); set_p(P, "NS", ns); set_p(P, "HS", hs) vpath = Path(sys.argv[sys.argv.index("--voice") + 1]) if "--voice" in sys.argv else None voice = json.loads(vpath.read_text()) if vpath else None if voice: # fitted constants (and the subglottal areas that go with its NS / HS) for k, v in voice["params"].items(): set_p(P, k, v) a = np.asarray(voice["subglottal_areas_m2"], np.float32); as_ = as_.copy(); as_[:len(a)] = a; as_[len(a):] = a[-1] notes = [174.6, 196.0, 220.0, 246.9, 261.6] # F3 G3 A3 B3 C4, 0.5 s each nd, fdt = 0.5, 0.01 f0t = np.repeat(notes, int(nd / fdt)).astype(np.float32) dur = len(notes) * nd kdt = 0.01; t = np.arange(int(dur / kdt) + 2) * kdt pl_note = np.repeat(np.linspace(800.0, 1100.0, len(notes)), int(nd / kdt)) # singers raise lung pressure with pitch pl_note = np.r_[pl_note, pl_note[-1], pl_note[-1]][:len(t)] pl = (pl_note * np.clip(t / 0.06, 0, 1) * np.clip((dur - t) / 0.08, 0, 1)).astype(np.float32)[None] # lung pressure, Pa out, aux, tr = sim.run(P, av_i[None], as_[None], pl, f0t, dur, kdt, fdt, av2=av_a[None], tm=(1.1, 1.5), ntrace=1, bl=bool(voice and voice["run"]["bl"])) y = cp.asnumpy(out)[0]; ug = cp.asnumpy(tr)[0, 0] wav = f"sing_vowel_{vpath.with_suffix('').name}.wav" if voice else "sing_vowel.wav" sf.write(wav, 0.5 * y / np.abs(y).max(), FS) print("wrote", wav) for k, f in enumerate(notes): # pitch from zero crossings of the glottal flow, middle of each note seg = ug[int((k * nd + 0.2) * FS):int((k * nd + 0.45) * FS)]; seg = seg - seg.mean() up = np.flatnonzero((seg[:-1] <= 0) & (seg[1:] > 0)) sung = FS * (len(up) - 1) / (up[-1] - up[0]) if len(up) > 2 else float("nan") print(f"note {f:6.1f} Hz -> sung {sung:6.1f} Hz")