torchsynth developer on retainer: SineVCO pitch parameter expects normalized [0, 1] — raw MIDI note values [36, 72] clamped to 1.0 — all outputs at 12,543 Hz (above musical range) — 0/50 generated sounds usable — $240,000 — 8 months — torchsynth developer on monthly retainer

torchsynth 1.3.0 • PyTorch 2.0.1 • Python 3.10; SineVCO pitch ModuleParameter expects normalized [0, 1] mapped to MIDI [0, 127] internally; ML model head outputs raw MIDI note values [36, 72]; torch.clamp(pitch, 0.0, 1.0) clamps all values to 1.0; SineVCO outputs MIDI 127 = 12,543.85 Hz for every generated sound; multi-scale STFT loss decreases but generated audio is useless; human evaluation: 0/50 usable sounds; 8 months of GAN architecture experiments; $240,000; fix: pitch_normalized = midi_note / 127.0 • Eastfield TorchSynth Ltd. (4-person AI drum synthesis; 8 months; 1.5h)

Eastfield TorchSynth Ltd. is a 4-person startup building an AI drum synthesis product: users type a text description (“heavy kick with long decay,” “tight snare,” “hi-hat sizzle”) and the system generates a drum sound by predicting torchsynth synthesizer parameters with a transformer ML model, then synthesizing audio on-device with torchsynth. The parameter prediction code in the first training configuration was:

# drum_synth_model.py (torchsynth 1.3.0)
import torch
import torch.nn as nn
from torchsynth.synth import Voice
from torchsynth.config import SynthConfig

synth_config = SynthConfig(batch_size=64, sample_rate=44100, buffer_size_seconds=1.0)
voice = Voice(synth_config).cuda()

class ParameterPredictor(nn.Module):
    def __init__(self):
        super().__init__()
        self.encoder = TransformerTextEncoder()
        self.pitch_head = nn.Linear(512, 1)
        # Other parameter heads (attack, decay, etc.) omitted for brevity

    def forward(self, text_tokens):
        features = self.encoder(text_tokens)
        # Predict MIDI note in musical bass drum range [36 (C2) to 72 (C5)]
        pitch_midi = 36.0 + 36.0 * torch.sigmoid(self.pitch_head(features))
        # pitch_midi range: [36.0, 72.0]  — MIDI note values
        return pitch_midi   # <-- BUG: Voice expects [0,1], not raw MIDI

def synthesize(text_tokens):
    pitch_midi = model(text_tokens)      # shape: (batch, 1), range [36, 72]
    audio = voice({'pitch': pitch_midi}) # torch.clamp(pitch_midi, 0, 1)
    # torch.clamp(36.0, 0.0, 1.0) = 1.0 for ALL values in [36, 72]
    # SineVCO pitch=1.0 → MIDI 127 → 12,543.85 Hz
    return audio

The engineer designed the pitch head to output MIDI note values because the drum samples in the training corpus were labeled with MIDI notes (the drum machine interface labels each pad with a MIDI note number). The torchsynth documentation describes SineVCO.pitch as a “pitch in MIDI note number” in the human-readable parameter description, but specifies in the ModuleParameter API that the passed value must be normalized to [0, 1]. The normalization requirement is listed in the ModuleParameter docstring but not in the SineVCO usage example at the top of the torchsynth README. torch.clamp() did not raise an error on MIDI values in [36, 72] — it silently clamped them all to 1.0.

How the MIDI-to-normalized mapping works in torchsynth

torchsynth’s ModuleParameter stores a ParameterRange that defines the mapping between the normalized [0, 1] input and the human-meaningful value. For SineVCO.pitch:

# torchsynth SineVCO pitch parameter (torchsynth 1.3.0)
# ModuleParameter name: 'pitch'
# ParameterRange: MidiFrequencyParameter
#   minimum_value = 0.0  (normalized) → MIDI 0  → 8.18 Hz
#   maximum_value = 1.0  (normalized) → MIDI 127 → 12,543.85 Hz
#   mapping: normalized → MIDI note = normalized × 127
#            MIDI note → frequency (Hz) = 440 × 2^((midi - 69) / 12)

# Correct normalization:
# MIDI 36 (C2,  65.4 Hz) → 36 / 127 = 0.283
# MIDI 48 (C3, 130.8 Hz) → 48 / 127 = 0.378
# MIDI 60 (C4, 261.6 Hz) → 60 / 127 = 0.472
# MIDI 69 (A4, 440.0 Hz) → 69 / 127 = 0.543
# MIDI 72 (C5, 523.3 Hz) → 72 / 127 = 0.567

# Bug: torch.clamp(value, 0.0, 1.0) on raw MIDI values:
# torch.clamp(36.0, 0.0, 1.0) = 1.0  → MIDI 127 → 12,543.85 Hz
# torch.clamp(60.0, 0.0, 1.0) = 1.0  → MIDI 127 → 12,543.85 Hz
# torch.clamp(72.0, 0.0, 1.0) = 1.0  → MIDI 127 → 12,543.85 Hz
# EVERY predicted MIDI note → clamped to 1.0 → 12,543.85 Hz

# 12,543.85 Hz is audible but not musical:
# piano top key (A8) = 7,040 Hz; piano is 27.5 Hz – 4,186 Hz
# human hearing range: ~20 Hz – ~20,000 Hz (age-dependent)
# 12,543 Hz: a harsh, high-pitched hiss; no percussive character

Why the loss decreased despite generating unusable sounds

The training loop minimized a multi-scale STFT loss between the generated audio (a 12.5 kHz sinusoidal hum) and the target drum sample (a kick drum with energy primarily from 50 Hz to 5 kHz). The loss used FFT sizes of 512, 1024, and 2048 at a 44100 Hz sample rate:

# Multi-scale STFT loss computation
# (using auraloss.freq.MultiResolutionSTFTLoss)

# Kick drum target: energy in 50 Hz – 5000 Hz
# Generated audio: energy at 12,543 Hz (single peak)

# STFT with n_fft=512 at 44100 Hz: bin width = 44100/512 = 86.1 Hz
# bin 145 = 12,500 Hz ← generated audio peak here
# bins 1–58 = 50–5000 Hz ← target drum energy here
# mismatch: very high loss at all scales (epoch 0 loss: 1.84)

# Generator learns to minimize loss by shaping the high-frequency output:
# It cannot change pitch (always 1.0 → 12,543 Hz due to clamping)
# But it CAN adjust envelope, filter, LFO (other parameters are also wrong
# but vary because those heads produce values < 1.0 after sigmoid)
# The 12.5 kHz signal is modulated with envelopes that roughly match
# the amplitude envelope of the kick drum

# Epoch 100: loss 1.21  (generator learns better amplitude matching)
# Epoch 500: loss 0.89  (generator tunes all non-pitch parameters)
# Epoch 1500: loss 0.76 (ceiling — cannot match spectral content without
#                         correct pitch)
# Loss decreased 58% from epoch 0 to 1500 — appears to be "learning"
# Human evaluation at epoch 1500: 0/50 generated sounds usable
# All 50 sounds are variations of the same 12.5 kHz hum with different
# amplitude envelopes; zero percussive character; zero musical utility

Eight months of GAN experiments against a fixed ceiling

The team monitored training loss curves and saw consistent improvement. Human listening evaluations during each milestone showed 0/50 usable sounds, but the team attributed this to the model “not yet converging” and tried a series of architectural improvements:

# Eight months of architecture experiments (pitch still clamped to 1.0)

# Month 1: add GAN discriminator (MelGAN-style)
# Generator loss improved; discriminator learns to reject 12.5 kHz hum
# Loss: 0.76 → 0.71  Human eval: 0/50

# Month 2: conditioning on mel spectrogram of reference drum
# Instead of text → params, use mel-spec of example drum → params
# Loss: 0.71 → 0.69  Human eval: 0/50

# Month 3: multi-scale discriminator (3 timescales)
# Sub-band discriminator + full-band discriminator
# Loss: 0.69 → 0.67  Human eval: 0/50

# Month 4: phase-aware STFT loss
# Add loss on STFT phase difference
# Loss: 0.67 → 0.65  Human eval: 0/50

# Month 5: larger transformer encoder (6 layers → 12 layers)
# Loss: 0.65 → 0.64  Human eval: 0/50

# Month 6: separate pitch vs timbre parameter heads
# (pitch head still outputs MIDI [36,72] → clamped to 1.0)
# Loss: 0.64 → 0.63  Human eval: 0/50

# Month 7–8: reinforcement learning fine-tuning
# Reward = 1.0 if human rater approves, 0.0 otherwise
# After 8 months: 0/50 approval rate
# Ceiling: loss 0.63  (all improvements cosmetic — pitch is always wrong)

# DIAGNOSIS (retainer triage, 0.5h):
# Generate 1 batch → mean(torch.abs(torch.fft.rfft(audio)).argmax(-1))
# Peak frequency bin: 145 × (44100 / 512) ≈ 12,500 Hz for all 64 sounds
# → pitch parameter is always 1.0 → check pitch_midi range → [36, 72]
# → check torch.clamp(pitch_midi, 0, 1) → all 1.0  FOUND

The retainer consultant identified the normalization error in 1.5 hours: 0.5h to generate a batch of audio, listen to the output (all 12.5 kHz hums), and FFT-analyze the peak frequency; 0.25h to trace the pitch value through the prediction head and confirm all values were clamped to 1.0 by torch.clamp; 0.25h to read the torchsynth ModuleParameter documentation and confirm the [0, 1] normalization requirement; 0.5h to change the pitch head output to pitch_normalized = midi_note / 127.0, retrain for 100 epochs, and confirm the peak frequency was now in the 65–524 Hz range (C2–C5). Hours logged to HourTab with per-session breakdown.

# Fixed drum_synth_model.py

import torch
import torch.nn as nn
from torchsynth.synth import Voice
from torchsynth.config import SynthConfig

synth_config = SynthConfig(batch_size=64, sample_rate=44100, buffer_size_seconds=1.0)
voice = Voice(synth_config).cuda()

MIDI_MIN = 0.0
MIDI_MAX = 127.0

def normalize_midi(midi_note: torch.Tensor) -> torch.Tensor:
    """Convert MIDI note [0, 127] to torchsynth normalized [0, 1]."""
    return midi_note / MIDI_MAX   # was: return midi_note (passed raw)

class ParameterPredictor(nn.Module):
    def forward(self, text_tokens):
        features = self.encoder(text_tokens)
        pitch_midi = 36.0 + 36.0 * torch.sigmoid(self.pitch_head(features))
        # Normalize to [0, 1] for torchsynth
        pitch_norm = normalize_midi(pitch_midi)   # range: [0.283, 0.567]
        return pitch_norm

# Assertion for parameter bounds (add to training loop):
def validate_params(params: dict):
    for name, value in params.items():
        assert value.min() >= 0.0 and value.max() <= 1.0, (
            f"Parameter '{name}' out of [0,1] range: "
            f"min={value.min():.3f}, max={value.max():.3f}. "
            f"Normalize with param_value / param_range_max before passing to Voice."
        )

# After fix and 100 epochs of retraining:
# Mean output pitch: 261 Hz (C4 region, correct for kick drum range)
# Multi-scale STFT loss epoch 100: 0.61  (better than 8 months of GAN work)
# Human evaluation (50 sounds): 41/50 usable
# 9/50 unusable: other parameters (attack, decay) still need normalization audit

Related retainer posts


Track retainer hours without a client portal

HourTab turns your time-tracker CSV into a public URL your client bookmarks. No login. No portal. CSV in, URL out.

See pricing →