-
Notifications
You must be signed in to change notification settings - Fork 190
/
Copy pathSongPerformance.ts
300 lines (277 loc) · 12.2 KB
/
SongPerformance.ts
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
// Copyright (c) John Nesky and contributing authors, distributed under the MIT license, see accompanying the LICENSE.md file.
import {Config} from "../synth/SynthConfig.js";
import {Note, Pattern} from "../synth/synth.js";
import {SongDocument} from "./SongDocument.js";
import {ChangeGroup} from "./Change.js";
import {ChangeChannelBar, ChangePinTime, ChangeEnsurePatternExists, ChangeNoteAdded, ChangeInsertBars, ChangeDeleteBars, ChangeNoteLength} from "./changes.js";
export class SongPerformance {
private _channelIsDrum: boolean = false;
private _channelOctave: number = -1;
private _songKey: number = -1;
private _pitchesAreTemporary: boolean = false;
private readonly _recentlyAddedPitches: number[] = []; // Pitches that are rapidly added then removed within a minimum rhythm duration wouldn't get recorded until I explicitly track recently added notes and check if any are no longer held.
private _songLengthWhenRecordingStarted: number = -1;
private _playheadPart: number = -1;
private _playheadPattern: Pattern | null = null;
private _pitchesChanged: boolean = false;
private _lastNote: Note | null = null;
private _recordingChange: ChangeGroup | null = null;
constructor(private _doc: SongDocument) {
this._doc.notifier.watch(this._documentChanged);
this._documentChanged();
window.requestAnimationFrame(this._onAnimationFrame);
}
public play(): void {
this._doc.synth.play();
this._doc.synth.enableMetronome = false;
this._doc.synth.countInMetronome = false
this._doc.synth.maintainLiveInput();
}
public pause(): void {
this.clearAllPitches();
if (this._recordingChange != null) {
if (this._doc.song.barCount > this._songLengthWhenRecordingStarted && !this._lastBarHasPatterns()) {
// If an extra empty bar was added in case it was needed for recording, but it didn't end up getting used, delete it now.
new ChangeDeleteBars(this._doc, this._doc.song.barCount - 1, 1);
new ChangeChannelBar(this._doc, this._doc.channel, this._doc.song.barCount - 1);
}
if (!this._recordingChange.isNoop()) {
this._doc.record(this._recordingChange);
this._recordingChange = null;
}
this._lastNote = null;
}
this._doc.synth.pause();
this._doc.synth.resetEffects();
this._doc.synth.enableMetronome = false;
this._doc.synth.countInMetronome = false
if (this._doc.prefs.autoFollow) {
this._doc.synth.goToBar(this._doc.bar);
}
this._doc.synth.snapToBar();
}
public record(): void {
this._doc.synth.snapToBar();
const playheadBar: number = Math.floor(this._doc.synth.playhead);
if (playheadBar != this._doc.bar) {
new ChangeChannelBar(this._doc, this._doc.channel, playheadBar);
}
if (this._pitchesAreTemporary) {
this.clearAllPitches();
this._pitchesAreTemporary = false;
}
this._doc.synth.enableMetronome = this._doc.prefs.metronomeWhileRecording;
this._doc.synth.countInMetronome = this._doc.prefs.metronomeCountIn;
this._doc.synth.startRecording();
this._doc.synth.maintainLiveInput();
this._songLengthWhenRecordingStarted = this._doc.song.barCount;
this._playheadPart = this._getCurrentPlayheadPart();
this._playheadPattern = null;
this._pitchesChanged = false;
this._lastNote = null;
this._recentlyAddedPitches.length = 0;
this._recordingChange = new ChangeGroup();
this._doc.setProspectiveChange(this._recordingChange);
}
public abortRecording(): void {
this._recordingChange = null;
this.pause();
}
public pitchesAreTemporary(): boolean {
return this._pitchesAreTemporary;
}
private _getMinDivision(): number {
if (this._doc.prefs.snapRecordedNotesToRhythm) {
return Config.partsPerBeat / Config.rhythms[this._doc.song.rhythm].stepsPerBeat;
} else {
return 1;
}
}
private _getCurrentPlayheadPart(): number {
const currentPart: number = this._doc.synth.playhead * this._doc.song.beatsPerBar * Config.partsPerBeat;
if (this._doc.prefs.snapRecordedNotesToRhythm) {
const minDivision: number = this._getMinDivision();
return Math.round(currentPart / minDivision) * minDivision;
}
return Math.round(currentPart);
}
private _lastBarHasPatterns(): boolean {
for (let channelIndex: number = 0; channelIndex < this._doc.song.getChannelCount(); channelIndex++) {
if (this._doc.song.channels[channelIndex].bars[this._doc.song.barCount - 1] != 0) return true;
}
return false;
}
private _onAnimationFrame = (): void => {
window.requestAnimationFrame(this._onAnimationFrame);
if (this._doc.synth.recording) {
const dirty = this._updateRecordedNotes();
if (dirty) {
// The full interface is usually only rerendered in response to user input events, not animation events, but in this case go ahead and rerender everything.
this._doc.notifier.notifyWatchers();
}
}
}
// Returns true if the full interface needs to be rerendered.
private _updateRecordedNotes(): boolean {
if (this._recordingChange == null) return false;
if (!this._doc.lastChangeWas(this._recordingChange)) {
this.abortRecording();
return false;
}
if (this._doc.synth.countInMetronome) {
// If the synth is still counting in before recording, discard any recently added pitches.
this._recentlyAddedPitches.length = 0;
this._pitchesChanged = false;
return false;
}
const partsPerBar: number = this._doc.song.beatsPerBar * Config.partsPerBeat;
const oldPart: number = this._playheadPart % partsPerBar;
const oldBar: number = Math.floor(this._playheadPart / partsPerBar);
const oldPlayheadPart: number = this._playheadPart;
this._playheadPart = this._getCurrentPlayheadPart();
const newPart: number = this._playheadPart % partsPerBar;
const newBar: number = Math.floor(this._playheadPart / partsPerBar);
if (oldPart == newPart && oldBar == newBar) return false;
if (this._playheadPart < oldPlayheadPart) {
this._lastNote = null;
this._playheadPattern = null;
return false;
}
let dirty = false;
for (let bar: number = oldBar; bar <= newBar; bar++) {
if (bar != oldBar) this._playheadPattern = null;
const startPart: number = (bar == oldBar) ? oldPart : 0;
const endPart: number = (bar == newBar) ? newPart : partsPerBar;
if (startPart == endPart) break;
if (this._lastNote != null && !this._pitchesChanged && startPart > 0 && this._doc.synth.liveInputPitches.length > 0) {
this._recordingChange.append(new ChangePinTime(this._doc, this._lastNote, 1, endPart, this._lastNote.continuesLastPattern));
// Instead of updating the entire interface when extending the last note, just update the current pattern as a special case to avoid doing too much work every frame since performance is important while recording.
this._doc.currentPatternIsDirty = true;
} else {
if (this._lastNote != null) {
// End the last note.
this._lastNote = null;
}
// All current pitches will usually fill the time span from startPart to endPart, but
// if any recent pitches were released before being recorded, they'll get recorded here
// as short as possible and then any remaining time will be dedicated to pitches that
// haven't been released yet.
let noteStartPart: number = startPart;
let noteEndPart: number = endPart;
while (noteStartPart < endPart) {
let addedAlreadyReleasedPitch: boolean = false;
if (this._recentlyAddedPitches.length > 0 || this._doc.synth.liveInputPitches.length > 0) {
if (this._playheadPattern == null) {
this._doc.selection.erasePatternInBar(this._recordingChange, this._doc.synth.liveInputChannel, bar);
this._recordingChange.append(new ChangeEnsurePatternExists(this._doc, this._doc.synth.liveInputChannel, bar));
this._playheadPattern = this._doc.song.getPattern(this._doc.synth.liveInputChannel, bar);
}
if (this._playheadPattern == null) throw new Error();
this._lastNote = new Note(-1, noteStartPart, noteEndPart, Config.noteSizeMax, this._doc.song.getChannelIsNoise(this._doc.synth.liveInputChannel));
this._lastNote.continuesLastPattern = (noteStartPart == 0 && !this._pitchesChanged);
this._lastNote.pitches.length = 0;
while (this._recentlyAddedPitches.length > 0) {
if (this._lastNote.pitches.length >= Config.maxChordSize) break;
const recentPitch: number = this._recentlyAddedPitches.shift()!;
if (this._doc.synth.liveInputPitches.indexOf(recentPitch) == -1) {
this._lastNote.pitches.push(recentPitch);
addedAlreadyReleasedPitch = true;
}
}
for (let i: number = 0; i < this._doc.synth.liveInputPitches.length; i++) {
if (this._lastNote.pitches.length >= Config.maxChordSize) break;
this._lastNote.pitches.push(this._doc.synth.liveInputPitches[i]);
}
this._recordingChange.append(new ChangeNoteAdded(this._doc, this._playheadPattern, this._lastNote, this._playheadPattern.notes.length));
if (addedAlreadyReleasedPitch) {
// If this note contains pitches that were already released, shorten it and start a new note.
noteEndPart = noteStartPart + this._getMinDivision();
new ChangeNoteLength(this._doc, this._lastNote, this._lastNote.start, noteEndPart);
this._lastNote = null;
}
dirty = true;
}
this._pitchesChanged = addedAlreadyReleasedPitch;
noteStartPart = noteEndPart;
noteEndPart = endPart;
}
}
if (bar == this._doc.song.barCount - 1) {
if (this._lastBarHasPatterns()) {
new ChangeInsertBars(this._doc, this._doc.song.barCount, 1);
this._doc.bar--; // To counteract it increasing in ChangeInsertBars.
dirty = true;
}
}
}
return dirty;
}
public setTemporaryPitches(pitches: number[], duration: number): void {
this._updateRecordedNotes();
for (let i: number = 0; i < pitches.length; i++) {
this._doc.synth.liveInputPitches[i] = pitches[i];
}
this._doc.synth.liveInputPitches.length = Math.min(pitches.length, Config.maxChordSize);
this._doc.synth.liveInputDuration = duration;
this._doc.synth.liveInputStarted = true;
this._pitchesAreTemporary = true;
this._pitchesChanged = true;
}
public addPerformedPitch(pitch: number): void {
this._doc.synth.maintainLiveInput();
this._updateRecordedNotes();
if (this._pitchesAreTemporary) {
this.clearAllPitches();
this._pitchesAreTemporary = false;
}
if (this._doc.prefs.ignorePerformedNotesNotInScale && !Config.scales[this._doc.song.scale].flags[pitch % Config.pitchesPerOctave]) {
return;
}
if (this._doc.synth.liveInputPitches.indexOf(pitch) == -1) {
this._doc.synth.liveInputPitches.push(pitch);
this._pitchesChanged = true;
while (this._doc.synth.liveInputPitches.length > Config.maxChordSize) {
this._doc.synth.liveInputPitches.shift();
}
this._doc.synth.liveInputDuration = Number.MAX_SAFE_INTEGER;
if (this._recordingChange != null) {
const recentIndex: number = this._recentlyAddedPitches.indexOf(pitch);
if (recentIndex != -1) {
// If the latest pitch is already in _recentlyAddedPitches, remove it before adding it back at the end.
this._recentlyAddedPitches.splice(recentIndex, 1);
}
this._recentlyAddedPitches.push(pitch);
while (this._recentlyAddedPitches.length > Config.maxChordSize * 4) {
this._recentlyAddedPitches.shift();
}
}
}
}
public removePerformedPitch(pitch: number): void {
this._updateRecordedNotes();
for (let i: number = 0; i < this._doc.synth.liveInputPitches.length; i++) {
if (this._doc.synth.liveInputPitches[i] == pitch) {
this._doc.synth.liveInputPitches.splice(i, 1);
this._pitchesChanged = true;
i--;
}
}
}
public clearAllPitches(): void {
this._updateRecordedNotes();
this._doc.synth.liveInputPitches.length = 0;
this._pitchesChanged = true;
}
private _documentChanged = (): void => {
const isDrum: boolean = this._doc.song.getChannelIsNoise(this._doc.channel);
const octave: number = this._doc.song.channels[this._doc.channel].octave;
if (this._doc.synth.liveInputChannel != this._doc.channel || this._channelIsDrum != isDrum || this._channelOctave != octave || this._songKey != this._doc.song.key) {
this._doc.synth.liveInputChannel = this._doc.channel;
this._channelIsDrum = isDrum;
this._channelOctave = octave;
this._songKey = this._doc.song.key;
this.clearAllPitches();
}
this._doc.synth.liveInputInstruments = this._doc.recentPatternInstruments[this._doc.channel];
}
}