summaryrefslogtreecommitdiffstats
path: root/player.mjs
blob: 070a8b7eeb3280c35fd0147343593a146d2d073f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import { Note, toCents } from './scale.mjs';
import GuitarFuzz from './vend/guitar-fuzz.mjs';
import Trombone from './vend/trombone.mjs';

const audioCtx = new AudioContext();
const fuzzWave = new PeriodicWave(audioCtx, {
    real: GuitarFuzz.real,
    imag: GuitarFuzz.imag
});
const tromboneWave = new PeriodicWave(audioCtx, {
    real: Trombone.real,
    imag: Trombone.imag
});

const globalGain = new GainNode(audioCtx, { gain: 0.06 })

export default class {
    // oscillatornodes
    notes = [];

    // offsets is an array of offsets in cents from A4.
    constructor(offsets) {
        console.debug('Player#constructor', this, offsets);
        this.offsets = offsets;

        this.notes = offsets.map(o =>
            new OscillatorNode(audioCtx, {
                frequency: 440, // a above middle c (c4)
                detune: o,
                //                type: 'sine',
                type: 'custom',
                periodicWave: tromboneWave,
            }));

        this.notes.forEach(n => n.connect(globalGain).connect(audioCtx.destination));
    }

    start() {
        console.debug('Player#start', this);
        this.notes.forEach(n => n.start());
    }

    stop() {
        console.debug('Player#stop', this);
        this.notes.forEach(n => n.stop());
    }
}