mmpSearch/assets/js/audio/plugins/Nes.js

45 lines
1.4 KiB
JavaScript

import * as Tone from "https://esm.sh/tone";
export class Nes {
constructor(toneContext, data = {}) {
this.params = data.nes || data || {};
// Mapear tipo de onda do LMMS para Tone.js
// 0=Pulse12.5, 1=Pulse25, 2=Pulse50, 3=Triangle, 4=Noise
let oscType = "pulse";
let width = 0.5;
const mode = parseInt(this.params.mode || 0);
if (mode === 0) { oscType = "pulse"; width = 0.125; }
else if (mode === 1) { oscType = "pulse"; width = 0.25; }
else if (mode === 2) { oscType = "pulse"; width = 0.5; }
else if (mode === 3) { oscType = "triangle"; }
else if (mode === 4) { oscType = "square"; } // Aproximação para noise tonal
// Chiptunes geralmente não têm polifonia, mas vamos permitir PolySynth para acordes
this.synth = new Tone.PolySynth(Tone.Synth, {
oscillator: { type: oscType, width: width },
envelope: { attack: 0.001, decay: 0.1, sustain: 0.1, release: 0.01 } // Envelope rápido "clicky"
});
// BitCrusher para dar a sujeira 8-bit
this.crusher = new Tone.BitCrusher(4).toDestination(); // 4 bits
this.synth.connect(this.crusher);
this.output = this.crusher;
}
triggerAttackRelease(note, duration, time) {
this.synth.triggerAttackRelease(note, duration, time);
}
connect(dest) {
this.output.connect(dest);
}
dispose() {
this.synth.dispose();
this.crusher.dispose();
}
}