Erste Schritte

Lerne ChiptuneSynth Schritt für Schritt kennen – klicke auf „Ausführen“, um jedes Beispiel live anzuhören

1

Hallo, Chiptune!

Erstelle einen Synthesizer, initialisiere ihn und spiele deinen ersten Soundeffekt ab – alles in nur 3 Zeilen.

const synth = new ChiptuneSynth();
await synth.init();
synth.playPreset('coin');  // ding!
Der AudioContext benötigt eine Benutzeraktion (Klick), um zu starten – deshalb verwenden wir eine Schaltfläche.
2

Eine Note nach Namen spielen

Spiele das mittlere C für eine halbe Sekunde auf der Lead-Spur (Spur 0).

synth.playNoteByName('C', 4, 0, 0.5);
// note, octave, track, duration (seconds)
3

Eine Melodie spielen

Plane mehrere Noten mit Timing, um eine einfache Melodie zu erstellen.

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
  )
);
4

Ein Instrument laden

Wechsle von der standardmäßigen Rechteckwelle zu einer Violine und spiele dann eine Note.

synth.loadInstrument('violin', 0);
synth.playNoteByName('A', 4, 0, 1.5);
// Available: piano, violin, cello, flute, organ,
// brass, harmonica, synthLead, synthPad, synthBass,
// marimba, electricGuitar
5

Mehrspur-Überlagerung

Spiele verschiedene Instrumente gleichzeitig auf verschiedenen Spuren.

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);
6

Sound anpassen

Passen Sie Hüllkurve, Vibrato und Wellenform an, um Ihren Sound zu gestalten.

// 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);
7

Spiel-SFX-Kombination

Verkette mehrere Soundeffekte für eine Spielsequenz – Münze sammeln, Power-Up, schießen!

synth.playPreset('coin');
setTimeout(() => synth.playPreset('powerup'), 400);
setTimeout(() => synth.playPreset('laser'), 1200);
setTimeout(() => synth.playPreset('explosion'), 1500);
setTimeout(() => synth.playPreset('1up'), 2200);
8

Wellenform-Visualisierung

Zeichne die Live-Audio-Wellenform auf eine Leinwand – ideal für Spiel-Benutzeroberflächen!

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();