-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsequencer.ts
More file actions
92 lines (75 loc) · 2.26 KB
/
sequencer.ts
File metadata and controls
92 lines (75 loc) · 2.26 KB
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
import * as Tone from 'tone';
import { randomIndex } from '../utils/array';
export interface SequencerNoteEvent {
detail: {
note: Tone.Unit.Frequency;
};
}
const sequenceSynth = new Tone.Synth().toDestination();
const SEQ_NOTE_LENGTH = '8n';
class Sequencer extends EventTarget {
private _transport = Tone.getTransport();
NOTE_EVENT = 'sequencerNoteEvent';
get noteDuration() {
return {
s: Tone.Time(SEQ_NOTE_LENGTH).toSeconds(),
ms: Tone.Time(SEQ_NOTE_LENGTH).toMilliseconds(),
};
}
constructor() {
super();
this._sequence.loop = false;
// we start our sequence at 0,
// and use transport start/stop for playback
this._sequence.start(0);
}
private _sequence = new Tone.Sequence((time, note) => {
sequenceSynth.triggerAttackRelease(note, SEQ_NOTE_LENGTH, time);
Tone.getDraw().schedule(() => {
this.dispatchEvent(
new CustomEvent(this.NOTE_EVENT, { detail: { note } }),
);
}, time);
}, []);
get length() {
return this._sequence.events.length;
}
valueAt(index: number) {
return this._sequence.events[index];
}
addRandomNote(notes: Tone.Unit.Frequency[]) {
const index = randomIndex(notes);
const note = notes[index];
this._sequence.events.push(note);
}
private _sequenceCompleteId = 0;
/** plays the sequence and resolves when complete */
async play() {
this.stop();
return new Promise((res) => {
const sequenceDuration = this.length * this.noteDuration.s;
this._sequenceCompleteId = this._transport.schedule(
() => {
this.stop();
res(undefined);
},
// resolve after the sequence duration, but take a little off since
// we'll consider the sequence done while the last note is releasing
sequenceDuration - this.noteDuration.s / 2,
);
// best to start the transport a little late
// https://github.com/Tonejs/Tone.js/wiki/Performance#scheduling-in-advance
this._transport.start('+0.1');
});
}
stop() {
this._transport.stop(Tone.now());
this._transport.clear(this._sequenceCompleteId);
this._transport.position = 0;
}
reset() {
this._sequence.clear();
this._sequence.events = [];
}
}
export const sequencer = new Sequencer();