<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<title>Chinese Sounds Synth</title>
<style>
button {
margin:4px;
padding:6px 12px;
}
</style>
</head>
<body>
<button id="guzheng">Guzheng</button>
<button id="erhu">Erhu</button>
<button id="dizi">Dizi</button>
<script src="https://cdn.jsdelivr.net/npm/tone@14.7.77/build/Tone.min.js"></script>
<script>
// Common gain for all instruments at max volume
const masterGain = new Tone.Gain(1).toDestination();
// Guzheng-like pluck model
// Using PluckSynth with customized filter and envelope
class GuzhengSynth {
constructor() {
// Base synth: PluckSynth (Karplus-Strong)
this.pluck = new Tone.PluckSynth({
attackNoise: 1,
dampening: 4000,
resonance: 0.8
});
// Add a little resonant lowpass filter to emulate body resonance
this.filter = new Tone.Filter(1200, "lowpass", -12);
// Small reverb for string body ambiance
this.reverb = new Tone.Reverb({
decay: 2,
preDelay: 0.01,
wet: 0.15
}).generate();
// Connect chain
this.pluck.chain(this.filter, this.reverb, masterGain);
}
trigger(note) {
// Slight detune for authentic shimmer
this.pluck.triggerAttack(note, 0.001, 1);
}
}
// Erhu-like bowed string model
// Using Synth with sawtooth, slow filter envelope plus noise for bow noise
class ErhuSynth {
constructor() {
// Main oscillator: Sawtooth (nasal tone)
this.osc = new Tone.Oscillator({
type: "sawtooth",
volume: -6
});
// VCA envelope with slow attack and medium release to mimic bow pressure
this.ampEnv = new Tone.AmplitudeEnvelope({
attack: 0.2,
decay: 0.05,
sustain: 0.8,
release: 0.5
});
// Filter (lowpass) with slow envelope for timbre shaping (like bowing changes)
this.filter = new Tone.Filter({
type: "lowpass",
frequency: 1500,
rolloff: -12,
Q: 2
});
this.filterEnv = new Tone.FrequencyEnvelope({
attack: 0.15,
decay: 0.1,
sustain: 0.5,
release: 0.5,
baseFrequency: 600,
octaves: 2.5
});
// Bow noise component (brown noise filtered)
this.noise = new Tone.Noise("brown");
this.noiseFilter = new Tone.Filter(500, "highpass");
this.noiseGainEnv = new Tone.AmplitudeEnvelope({
attack: 0.15,
decay: 0.1,
sustain: 0.2,
release: 0.5
});
// Connections
this.osc.chain(this.filter, this.ampEnv, masterGain);
this.noise.chain(this.noiseFilter, this.noiseGainEnv, masterGain);
}
trigger(note) {
// Set frequency and envelopes
this.osc.frequency.value = Tone.Frequency(note).toFrequency();
this.filterEnv.triggerAttack(Tone.now()); // Will modulate freq automatically
// Modulate filter cutoff with freqEnv
this.filter.frequency.cancelAndHoldAtTime(Tone.now());
this.filter.frequency.setValueAtTime(this.filterEnv.baseFrequency, Tone.now());
this.filter.frequency.linearRampToValueAtTime(this.filterEnv.baseFrequency * Math.pow(2, this.filterEnv.octaves), Tone.now() + this.filterEnv.attack);
this.filterEnv.connect(this.filter.frequency);
this.ampEnv.triggerAttackRelease("1.5");
this.noise.start();
this.noiseGainEnv.triggerAttackRelease("1.5");
setTimeout(() => this.noise.stop(), 1600);
this.osc.start();
setTimeout(() => this.osc.stop(), 1600);
}
}
// Dizi-like flute model
// Using a breathy synth: shaped triangle oscillator modulated by noise and fast envelope
class DiziSynth {
constructor() {
this.osc = new Tone.Oscillator({
type: "triangle",
volume: -8
});
this.noise = new Tone.Noise("white");
this.noiseFilter = new Tone.Filter(1000, "bandpass");
// Breath envelope modulating noise amplitude
this.breathEnv = new Tone.AmplitudeEnvelope({
attack: 0.005,
decay: 0.12,
sustain: 0,
release: 0.1
});
// Main amplitude envelope for note shape (quick, but smooth)
this.ampEnv = new Tone.AmplitudeEnvelope({
attack: 0.03,
decay: 0.3,
sustain: 0,
release: 0.15
});
// Filter to soften tone
this.filter = new Tone.Filter({
type: "lowpass",
frequency: 1900,
rolloff: -12,
Q: 1
});
this.noise.chain(this.noiseFilter, this.breathEnv, this.filter);
this.osc.chain(this.ampEnv, this.filter, masterGain);
}
trigger(note) {
this.osc.frequency.value = Tone.Frequency(note).toFrequency();
this.ampEnv.triggerAttackRelease("0.6");
this.breathEnv.triggerAttackRelease("0.6");
this.noise.start();
this.osc.start();
setTimeout(() => {
this.noise.stop();
this.osc.stop();
}, 650);
}
}
// Create instances
const guzheng = new GuzhengSynth();
const erhu = new ErhuSynth();
const dizi = new DiziSynth();
// Note chosen for demo Chinese pentatonic style: E4 (mi), G4 (so), A4 (la)
// Common pentatonic notes could be: E4, G4, A4
const notes = {
guzheng: "E4",
erhu: "G4",
dizi: "A4"
};
document.getElementById("guzheng").onclick = () => guzheng.trigger(notes.guzheng);
document.getElementById("erhu").onclick = () => erhu.trigger(notes.erhu);
document.getElementById("dizi").onclick = () => dizi.trigger(notes.dizi);
</script>
</body>
</html>