-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathnote-processor.ts
More file actions
67 lines (59 loc) · 2.23 KB
/
Copy pathnote-processor.ts
File metadata and controls
67 lines (59 loc) · 2.23 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
import { NoteEventStage, type TNoteEventStage } from "./event-types.ts";
import { uuid, type TUniqueIdentifier } from "./uuid.ts";
/* There are some common tasks which can be handled
by this utility/helper:
* Every time a note is played, it needs to be assigned a
new unique note identifier. This is because each played note
is something like a new instantiation of a note that exists
in some data source like a piano roll. If a
piano roll doesn't loop, this isn't so important,
but it's crucially important if you consider something like a
looping section of a piano roll that might eventually be
recorded: Every note in each loop iteration needs to be unique,
even if it's coming from the same "source". To avoid needing to
write this note-assignment logic ad-hoc, this object
can be used to trivially assign ids properly.
* It's handy to know which notes in a source are currently
playing/scheduled.
*/
export type NoteMetaData = Partial<{ noteNumber: number }> | undefined;
export interface IScheduledNoteInfo {
idForScheduling: TUniqueIdentifier;
metadata?: NoteMetaData;
}
export class NoteProcessor {
private map: Map<TUniqueIdentifier, IScheduledNoteInfo> = new Map();
public process(
noteId: TUniqueIdentifier,
stage: TNoteEventStage,
metadata?: NoteMetaData,
): IScheduledNoteInfo | undefined {
switch (stage) {
case NoteEventStage.InstantaneousStartEnd:
return { idForScheduling: uuid() };
case NoteEventStage.Start: {
const mapping = { idForScheduling: uuid(), metadata };
this.map.set(noteId, mapping);
return mapping;
}
case NoteEventStage.Update: {
const mapping = this.map.get(noteId);
return mapping;
}
case NoteEventStage.End: {
const mapping = this.map.get(noteId);
if (mapping === undefined) {
throw new Error(`processing end before start of ${noteId}`);
}
this.map.delete(noteId);
return mapping;
}
}
}
public getInfo(noteId: TUniqueIdentifier): IScheduledNoteInfo | undefined {
return this.map.get(noteId);
}
public playingNoteIds(): Set<TUniqueIdentifier> {
return new Set(this.map.keys());
}
}