|
| 1 | +import "core-js/actual/array/includes"; |
| 2 | +import type { State, StateProp, CalculationInput } from "../types"; |
| 3 | +import { ApiManager, mt, log } from "../../../util"; |
| 4 | +import { loggedStateProps, numberOfLamps, lampColours } from "../config"; |
| 5 | + |
| 6 | +const observedLiveSetProps: StateProp[] = [ |
| 7 | + "clip_trigger_quantization", |
| 8 | + "signature_numerator", |
| 9 | + "signature_denominator", |
| 10 | + "is_playing", |
| 11 | + "current_song_time", |
| 12 | +]; |
| 13 | + |
| 14 | +export default class BeatLampManager { |
| 15 | + private _state: State = { |
| 16 | + clip_trigger_quantization: null, |
| 17 | + signature_numerator: null, |
| 18 | + signature_denominator: null, |
| 19 | + is_playing: null, |
| 20 | + is_active: null, |
| 21 | + current_song_time: null, |
| 22 | + ctq_beats: null, |
| 23 | + current_lamp: null, |
| 24 | + }; |
| 25 | + |
| 26 | + private _apiMan = new ApiManager(); |
| 27 | + |
| 28 | + private _outlet; |
| 29 | + |
| 30 | + constructor(outlet: (outlet_number: number, ...args: unknown[]) => void) { |
| 31 | + this._outlet = outlet; |
| 32 | + } |
| 33 | + |
| 34 | + start() { |
| 35 | + if (this._apiMan.hasNoApis) { |
| 36 | + observedLiveSetProps.forEach(this._observeLiveSet.bind(this)); |
| 37 | + } |
| 38 | + this._apiMan.start(); |
| 39 | + } |
| 40 | + |
| 41 | + stop() { |
| 42 | + this._apiMan.stop(); |
| 43 | + } |
| 44 | + |
| 45 | + private _observeLiveSet(prop: StateProp) { |
| 46 | + this._observeState("live_set", prop); |
| 47 | + } |
| 48 | + |
| 49 | + private _observeState(path: string, prop: StateProp) { |
| 50 | + this._apiMan.make(`${path} ${prop}`, (nextValue) => { |
| 51 | + this._doStateUpdateIfChanged(prop, nextValue); |
| 52 | + this._updateDerivedState(); |
| 53 | + }); |
| 54 | + } |
| 55 | + |
| 56 | + private _updateDerivedState() { |
| 57 | + const isActiveHasChanged = this._updateIsActive(); |
| 58 | + |
| 59 | + if (isActiveHasChanged && this._state.is_active === 0) { |
| 60 | + // if quantisation is “None”, it makes no sense to have beat lamps on |
| 61 | + this._sendAllLampsOffMidiMessage(); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + this._updateCtqBeats(); |
| 66 | + const currentLampHasChanged = this._updateCurrentLamp(); |
| 67 | + |
| 68 | + if (currentLampHasChanged) { |
| 69 | + this._sendLampMidiMessage(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + private _updateDerivedStateProp( |
| 74 | + prop: StateProp, |
| 75 | + requiredProps: StateProp[], |
| 76 | + calculate: (props: CalculationInput) => number, |
| 77 | + ) { |
| 78 | + if (this._isMissingProps(...requiredProps)) { |
| 79 | + return false; |
| 80 | + } |
| 81 | + const propsForCalculation = requiredProps.reduce( |
| 82 | + (acc, curr) => ({ |
| 83 | + ...acc, |
| 84 | + [curr]: this._state[curr]!, |
| 85 | + }), |
| 86 | + {} as CalculationInput, |
| 87 | + ); |
| 88 | + const nextValue = calculate(propsForCalculation); |
| 89 | + return this._doStateUpdateIfChanged(prop, nextValue); |
| 90 | + } |
| 91 | + |
| 92 | + private _updateIsActive() { |
| 93 | + return this._updateDerivedStateProp( |
| 94 | + "is_active", |
| 95 | + ["clip_trigger_quantization"], |
| 96 | + ({ clip_trigger_quantization }) => |
| 97 | + clip_trigger_quantization > 0 ? 1 : 0, |
| 98 | + ); |
| 99 | + } |
| 100 | + |
| 101 | + private _updateCtqBeats() { |
| 102 | + return this._updateDerivedStateProp( |
| 103 | + "ctq_beats", |
| 104 | + [ |
| 105 | + "clip_trigger_quantization", |
| 106 | + "signature_denominator", |
| 107 | + "signature_numerator", |
| 108 | + ], |
| 109 | + ({ |
| 110 | + clip_trigger_quantization, |
| 111 | + signature_denominator, |
| 112 | + signature_numerator, |
| 113 | + }: CalculationInput) => |
| 114 | + mt.beatsForCTQ( |
| 115 | + clip_trigger_quantization, |
| 116 | + signature_numerator, |
| 117 | + signature_denominator, |
| 118 | + )!, // we know this can't be null because clip_trigger_quantization is validated previously |
| 119 | + ); |
| 120 | + } |
| 121 | + |
| 122 | + private _updateCurrentLamp() { |
| 123 | + return this._updateDerivedStateProp( |
| 124 | + "current_lamp", |
| 125 | + ["ctq_beats", "current_song_time"], |
| 126 | + ({ ctq_beats, current_song_time }) => { |
| 127 | + const elapsedQuantisationCycles = Math.floor( |
| 128 | + current_song_time / ctq_beats, |
| 129 | + ); |
| 130 | + const currentBeatInSpan = |
| 131 | + current_song_time - elapsedQuantisationCycles * ctq_beats; |
| 132 | + const beatsPerLamp = ctq_beats / numberOfLamps; |
| 133 | + return Math.floor(currentBeatInSpan / beatsPerLamp); |
| 134 | + }, |
| 135 | + ); |
| 136 | + } |
| 137 | + |
| 138 | + private _isMissingProps(...props: StateProp[]) { |
| 139 | + return props.some((prop) => this._state[prop] === null); |
| 140 | + } |
| 141 | + |
| 142 | + private _doStateUpdateIfChanged(prop: StateProp, nextValue: number) { |
| 143 | + if (nextValue !== this._state[prop]) { |
| 144 | + this._state[prop] = nextValue; |
| 145 | + this._maybeLogStateChange(prop); |
| 146 | + return true; |
| 147 | + } |
| 148 | + return false; |
| 149 | + } |
| 150 | + |
| 151 | + private _maybeLogStateChange(prop: StateProp) { |
| 152 | + if (!loggedStateProps.includes(prop)) { |
| 153 | + return; |
| 154 | + } |
| 155 | + log(`state.${prop} = ${this._state[prop]}`); |
| 156 | + } |
| 157 | + |
| 158 | + private _sendAllLampsOffMidiMessage() { |
| 159 | + for (let lampIndex = 0; lampIndex < 8; lampIndex++) { |
| 160 | + const lampPosition = lampIndex * 10 + 19; |
| 161 | + outlet(0, [176, lampPosition, 0]); |
| 162 | + outlet(lampIndex + 1, 0); |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + // send MIDI message to Launchpad Pro MK3 to light the lamps |
| 167 | + // on the scene trigger buttons on the right |
| 168 | + private _sendLampMidiMessage() { |
| 169 | + for (let lampIndex = 0; lampIndex < 8; lampIndex++) { |
| 170 | + const isOn = lampIndex <= this._state.current_lamp!; |
| 171 | + const colourIndex = isOn ? lampColours[lampIndex] : 0; |
| 172 | + const lampPosition = lampIndex * 10 + 19; |
| 173 | + outlet(0, [176, lampPosition, colourIndex]); |
| 174 | + outlet(lampIndex + 1, isOn ? 1 : 0); |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments