Aprenda a usar o ChiptuneSynth passo a passo — clique em “Executar” para ouvir cada exemplo ao vivo
Crie um sintetizador, inicialize-o e reproduza seu primeiro efeito sonoro — tudo em 3 linhas.
const synth = new ChiptuneSynth(); await synth.init(); synth.playPreset('coin'); // ding!
Toque o Dó central por meio segundo na faixa Lead (faixa 0).
synth.playNoteByName('C', 4, 0, 0.5); // note, octave, track, duration (seconds)
Programe várias notas com sincronização para criar uma melodia simples.
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 ) );
Mude da onda quadrada padrão para um violino e, em seguida, toque uma nota.
synth.loadInstrument('violin', 0); synth.playNoteByName('A', 4, 0, 1.5); // Available: piano, violin, cello, flute, organ, // brass, harmonica, synthLead, synthPad, synthBass, // marimba, electricGuitar
Toque instrumentos diferentes em faixas diferentes ao mesmo tempo.
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);
Personalize o envelope, o vibrato e a forma de onda para moldar seu som.
// 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);
Encadeie vários efeitos sonoros para uma sequência de jogo — coletar moedas, ativar poder, atirar!
synth.playPreset('coin'); setTimeout(() => synth.playPreset('powerup'), 400); setTimeout(() => synth.playPreset('laser'), 1200); setTimeout(() => synth.playPreset('explosion'), 1500); setTimeout(() => synth.playPreset('1up'), 2200);
Desenhe a forma de onda de áudio em tempo real em uma tela — ótimo para interfaces de jogos!
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();