Spaces:
Sleeping
Sleeping
Download sim/audio_synth.py from adisha-education-lab/Mus_Sim_1-beta: direct link, hf CLI and curl.
- Browser
- Download file 3.48 kB
-
https://huggingface.co/spaces/adisha-education-lab/Mus_Sim_1-beta/resolve/main/sim/audio_synth.py
- Command line
-
hf download hf://spaces/adisha-education-lab/Mus_Sim_1-beta/sim/audio_synth.py
-
curl -L -o audio_synth.py https://huggingface.co/spaces/adisha-education-lab/Mus_Sim_1-beta/resolve/main/sim/audio_synth.py
3.48 kB
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from typing import List, Tuple | |
| import numpy as np | |
| import io | |
| import wave | |
| def midi_to_freq(m: int) -> float: | |
| return float(440.0 * (2.0 ** ((m - 69) / 12.0))) | |
| class Timbre: | |
| name: str | |
| harmonics: List[Tuple[int, float]] | |
| attack: float | |
| decay: float | |
| sustain: float | |
| release: float | |
| lp_hz: float | None = None | |
| TIMBRES = { | |
| "Piano (synth)": Timbre("Piano (synth)", [(1,1.0),(2,0.35),(3,0.18),(4,0.10),(5,0.06),(6,0.04)], 0.005, 0.20, 0.35, 0.60, 5000.0), | |
| "Organ": Timbre("Organ", [(1,1.0),(2,0.75),(3,0.55),(4,0.35),(5,0.25),(6,0.18)], 0.01, 0.05, 0.90, 0.20, 7000.0), | |
| "Nylon Guitar": Timbre("Nylon Guitar", [(1,1.0),(2,0.25),(3,0.18),(4,0.12),(5,0.08)], 0.002, 0.15, 0.20, 0.40, 4200.0), | |
| "Choir": Timbre("Choir", [(1,1.0),(2,0.20),(3,0.15),(4,0.08)], 0.10, 0.25, 0.70, 1.10, 3200.0), | |
| } | |
| def _adsr_env(n: int, sr: int, a: float, d: float, s: float, r: float) -> np.ndarray: | |
| env = np.zeros(n, dtype=np.float32) | |
| a_n = int(a * sr); d_n = int(d * sr); r_n = int(r * sr) | |
| s_n = max(0, n - (a_n + d_n + r_n)) | |
| if a_n > 0: | |
| env[:a_n] = np.linspace(0, 1, a_n, endpoint=False) | |
| start = a_n | |
| if d_n > 0: | |
| env[start:start+d_n] = np.linspace(1, s, d_n, endpoint=False) | |
| start = a_n + d_n | |
| if s_n > 0: | |
| env[start:start+s_n] = s | |
| start = a_n + d_n + s_n | |
| if r_n > 0: | |
| env[start:start+r_n] = np.linspace(env[start-1] if start>0 else s, 0, r_n, endpoint=True) | |
| if start + r_n < n: | |
| env[start+r_n:] = 0 | |
| return env | |
| def _lowpass(x: np.ndarray, sr: int, cutoff: float) -> np.ndarray: | |
| rc = 1.0 / (2*np.pi*cutoff) | |
| dt = 1.0/sr | |
| alpha = dt / (rc + dt) | |
| y = np.zeros_like(x) | |
| y[0] = x[0] | |
| for i in range(1, len(x)): | |
| y[i] = y[i-1] + alpha*(x[i]-y[i-1]) | |
| return y | |
| def _to_wav_bytes(signal: np.ndarray, sr: int) -> bytes: | |
| sig = np.clip(signal, -1.0, 1.0) | |
| pcm = (sig * 32767.0).astype(np.int16) | |
| buf = io.BytesIO() | |
| with wave.open(buf, "wb") as wf: | |
| wf.setnchannels(1) | |
| wf.setsampwidth(2) | |
| wf.setframerate(sr) | |
| wf.writeframes(pcm.tobytes()) | |
| return buf.getvalue() | |
| def render_test_beep(seconds: float = 0.4, sr: int = 22050) -> bytes: | |
| n = int(seconds * sr) | |
| t = np.arange(n, dtype=np.float32)/sr | |
| sig = 0.35*np.sin(2*np.pi*880.0*t) | |
| # quick fade | |
| fade = np.linspace(0, 1, int(0.01*sr), endpoint=False) | |
| sig[:len(fade)] *= fade | |
| sig[-len(fade):] *= fade[::-1] | |
| return _to_wav_bytes(sig, sr) | |
| def render_chord_wav(midi_notes: List[int], timbre_name: str = "Piano (synth)", seconds: float = 1.6, sr: int = 22050) -> bytes: | |
| timbre = TIMBRES.get(timbre_name, TIMBRES["Piano (synth)"]) | |
| n = int(seconds * sr) | |
| mix = np.zeros(n, dtype=np.float32) | |
| rng = np.random.default_rng(7) | |
| t = (np.arange(n, dtype=np.float32) / sr) | |
| for m in midi_notes: | |
| f0 = midi_to_freq(int(m)) | |
| detune = 2 ** (rng.uniform(-2, 2) / 1200.0) | |
| f0 *= detune | |
| sig = np.zeros(n, dtype=np.float32) | |
| for mult, amp in timbre.harmonics: | |
| sig += amp * np.sin(2*np.pi*(f0*mult)*t) | |
| sig *= _adsr_env(n, sr, timbre.attack, timbre.decay, timbre.sustain, timbre.release) | |
| mix += sig | |
| if timbre.lp_hz: | |
| mix = _lowpass(mix, sr, timbre.lp_hz) | |
| # normalise | |
| maxv = float(np.max(np.abs(mix)) + 1e-9) | |
| mix = (mix / maxv) * 0.8 | |
| return _to_wav_bytes(mix, sr) | |