逐步学习ChiptuneSynth — 点击"运行"实时聆听每个示例
创建合成器,初始化它,并播放您的第一个音效 — 全部只需3行代码。
const synth = new ChiptuneSynth(); await synth.init(); synth.playPreset('coin'); // ding!
在Lead音轨(音轨0)上播放中央C半秒钟。
synth.playNoteByName('C', 4, 0, 0.5); // note, octave, track, duration (seconds)
按时间安排多个音符以创建简单的曲调。
const melody = [ { note:'C', oct:4, time:0 }, { note:'E', oct:4, time:0.25 }, { note:'G', oct:4, time:0.5 }, { note:'C', oct:5, time:0.75 } ]; melody.forEach(n => setTimeout(() => synth.playNoteByName(n.note, n.oct, 0, 0.3), n.time * 1000 ) );
从默认方波切换到小提琴,然后播放一个音符。
synth.loadInstrument('violin', 0); synth.playNoteByName('A', 4, 0, 1.5); // Available: piano, violin, cello, flute, organ, // brass, harmonica, synthLead, synthPad, synthBass, // marimba, electricGuitar
同时在不同音轨上播放不同乐器。
synth.loadInstrument('synthPad', 0); // Lead synth.loadInstrument('synthBass', 1); // Bass // Play a chord on the pad synth.playNoteByName('C', 4, 0, 2); synth.playNoteByName('E', 4, 0, 2); synth.playNoteByName('G', 4, 0, 2); // Bass note underneath synth.playNoteByName('C', 2, 1, 2);
自定义包络、颤音和波形来塑造您的音色。
// Slow attack pad synth.updateEnvelope(0, { attack: 0.5, decay: 0.3, sustain: 0.6, release: 1.0 }); // Add vibrato synth.updateVibrato(0, { rate: 5, depth: 8 }); // Change waveform to sawtooth synth.updateTrack(0, { type: 'sawtooth' }); synth.playNoteByName('D', 4, 0, 2.5);
为游戏序列连接多个音效 — 收集金币、增强能量、射击!
synth.playPreset('coin'); setTimeout(() => synth.playPreset('powerup'), 400); setTimeout(() => synth.playPreset('laser'), 1200); setTimeout(() => synth.playPreset('explosion'), 1500); setTimeout(() => synth.playPreset('1up'), 2200);
在画布上绘制实时音频波形 — 非常适合游戏界面!
function draw() { const data = synth.getWaveformData(); ctx.clearRect(0, 0, canvas.width, canvas.height); ctx.strokeStyle = '#00f0ff'; ctx.beginPath(); data.forEach((v, i) => { const x = (i / data.length) * canvas.width; const y = (v / 255) * canvas.height; i === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y); }); ctx.stroke(); requestAnimationFrame(draw); } draw();