-
Notifications
You must be signed in to change notification settings - Fork 0
/
note-tracking.js
139 lines (121 loc) · 4.47 KB
/
note-tracking.js
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
// This dict only contains notes that are currently being sounded.
import { NOTE_ON_HAPPENINGNESS, RESET_TIME_SECS, addHappeningness } from "./configs.js";
import { HarmonicContext } from "./harmonic-context.js";
import { ScaffoldingManager, BallsManager } from "./drawn-objects.js";
import { HarmonicCoordinates } from "./just-intonation.js";
import { IS_RECORDING, RECORDED_NOTES, RecNote } from "./recording.js";
/**
* KVP of [stepsFromA, {@linkcode KeyState}]
*
* @type {Object.<number, KeyState>}
*/
export let KEYS_STATE = {};
window.keysState = KEYS_STATE;
export let SUSTAIN_STATE = false;
export class KeyState {
constructor(stepsFromA, vel) {
this.stepsFromA = stepsFromA;
this.vel = vel;
this.fromSustainPedal = false;
}
}
let clearHarmonicContextTimeoutID = null;
export function countExistingKeysState() {
let held = 0, sustained = 0;
for (let [_, v] of Object.entries(KEYS_STATE)) {
if (v.fromSustainPedal)
sustained++;
else
held++;
}
return [held, sustained];
}
/**
* Stores the epoch time (ms) of the first note of the recording.
*
* @type {number}
*/
let recStartTime = 0;
/**
*
* @param {number} stepsFromA
* @param {number} vel
* @param {HarmonicContext} harmonicContext
* @param {BallsManager} ballManager
* @param {ScaffoldingManager} scaffoldingManager
* @param {number[]?} explicitCoords When {@linkcode HARMONIC_CONTEXT_METHOD} is `12ji`, this array is used as the
* explicit JI coordinates for the new ball. This should be `null`/unspecified if not in `12ji` mode.
*/
export function noteOn(stepsFromA, vel, harmonicContext, ballManager, scaffoldingManager, explicitCoords = null) {
// console.log('received note on: ', stepsFromA, vel);
addHappeningness(NOTE_ON_HAPPENINGNESS * Math.pow(vel/127, 1.5) + 0.002);
if (clearHarmonicContextTimeoutID !== null) {
clearTimeout(clearHarmonicContextTimeoutID);
clearHarmonicContextTimeoutID = null;
}
KEYS_STATE[stepsFromA] = new KeyState(stepsFromA, vel);
let [fromPitch, relativeRatio] = harmonicContext.registerNote(stepsFromA, explicitCoords);
if (fromPitch === null) {
// the harmonic context is fresh.
ballManager.noteOn(relativeRatio, stepsFromA, vel);
if (IS_RECORDING) {
if (RECORDED_NOTES.length === 0) {
recStartTime = Date.now();
}
RECORDED_NOTES.push(new RecNote(Date.now() - recStartTime, stepsFromA, relativeRatio));
}
} else {
let absoluteRatio = fromPitch.absoluteRatio.add(relativeRatio); // the absolute interval of new ball
let newBall = ballManager.noteOn(absoluteRatio, stepsFromA, vel);
scaffoldingManager.create(fromPitch.absoluteRatio, newBall);
if (IS_RECORDING) {
if (RECORDED_NOTES.length === 0) {
recStartTime = Date.now();
}
RECORDED_NOTES.push(new RecNote(Date.now() - recStartTime, stepsFromA, absoluteRatio));
}
}
}
export function startOriginResetTimer() {
if (Object.keys(KEYS_STATE).length === 0 && clearHarmonicContextTimeoutID === null) {
clearHarmonicContextTimeoutID = setTimeout(() => {
harmonicContext.reset();
clearHarmonicContextTimeoutID = null;
}, RESET_TIME_SECS * 1000);
}
}
export function noteOff(stepsFromA, vel) {
// console.log('received note off: ', stepsFromA, vel);
if (SUSTAIN_STATE) {
KEYS_STATE[stepsFromA].fromSustainPedal = true;
} else {
delete KEYS_STATE[stepsFromA];
}
startOriginResetTimer();
}
export function offAll() {
if (clearHarmonicContextTimeoutID !== null) {
clearTimeout(clearHarmonicContextTimeoutID);
clearHarmonicContextTimeoutID = null;
}
KEYS_STATE = {};
harmonicContext.reset();
}
window.offAll = offAll;
export function cc(cc, value) {
// console.log('received cc: ', cc, value);
if (cc === 64) {
// for expensive sustain pedals, sustain ranges from 0 to 127 (half pedalling is a thing)
// make sure that the threshold below is appropriate.
SUSTAIN_STATE = value >= 70;
if (!SUSTAIN_STATE) {
let sustainedNotes = Object.entries(KEYS_STATE).filter(
([_,keyState]) => keyState.fromSustainPedal
).map(([stepsFromA,_]) => stepsFromA);
for (let x of sustainedNotes) {
delete KEYS_STATE[x];
}
startOriginResetTimer();
}
}
}