Skip to content

Commit 79a2405

Browse files
committed
Move live input playback into own class
1 parent 32041cd commit 79a2405

File tree

3 files changed

+38
-8
lines changed

3 files changed

+38
-8
lines changed

editor/LiveInput.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import {SongDocument} from "./SongDocument";
2+
3+
export class LiveInput {
4+
constructor(private _doc: SongDocument) {
5+
this._doc.notifier.watch(this._documentChanged);
6+
this._documentChanged();
7+
}
8+
private _playingPitches: number[] = [];
9+
public addNote(pitch: number) {
10+
this._playingPitches = [...this._playingPitches, pitch];
11+
this.updateSound();
12+
}
13+
public removeNote(pitch: number) {
14+
this._playingPitches = this._playingPitches.filter(p => p !== pitch);
15+
this.updateSound();
16+
}
17+
private updateSound() {
18+
if(this._playingPitches.length == 0) {
19+
this._doc.synth.liveInputDuration = 0;
20+
} else {
21+
this._doc.synth.liveInputDuration = Number.MAX_SAFE_INTEGER;
22+
this._doc.synth.liveInputPitches = this._playingPitches;
23+
this._doc.synth.liveInputStarted = true;
24+
}
25+
}
26+
private _documentChanged = (): void => {
27+
this._doc.synth.liveInputChannel = this._doc.channel;
28+
this._doc.synth.liveInputInstruments = this._doc.recentPatternInstruments[this._doc.channel];
29+
}
30+
}

editor/Piano.ts

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import {Config} from "../synth/SynthConfig";
44
import {SongDocument} from "./SongDocument";
55
import {HTML} from "imperative-html/dist/esm/elements-strict";
66
import {ColorConfig} from "./ColorConfig";
7+
import {LiveInput} from "./LiveInput";
78

89
export class Piano {
910
private readonly _pianoContainer: HTMLDivElement = HTML.div({style: "width: 100%; height: 100%; display: flex; flex-direction: column-reverse; align-items: stretch;"});
@@ -31,7 +32,7 @@ export class Piano {
3132
private _renderedKey: number = -1;
3233
private _renderedPitchCount: number = -1;
3334

34-
constructor(private _doc: SongDocument) {
35+
constructor(private _doc: SongDocument, private _liveInput: LiveInput) {
3536
for (let i: number = 0; i < Config.drumCount; i++) {
3637
const scale: number = (1.0 - (i / Config.drumCount) * 0.35) * 100;
3738
const brightness: number = 1.0 + ((i - Config.drumCount / 2.0) / Config.drumCount) * 0.5;
@@ -83,15 +84,14 @@ export class Piano {
8384
const octaveOffset: number = this._doc.getBaseVisibleOctave(this._doc.channel) * Config.pitchesPerOctave;
8485
const currentPitch: number = this._cursorPitch + octaveOffset;
8586
if (this._playedPitch == currentPitch) return;
87+
this._liveInput.removeNote(this._playedPitch);
8688
this._playedPitch = currentPitch;
87-
this._doc.synth.liveInputDuration = Number.MAX_SAFE_INTEGER;
88-
this._doc.synth.liveInputPitches = [this._playedPitch];
89-
this._doc.synth.liveInputStarted = true;
89+
this._liveInput.addNote(currentPitch);
9090
}
9191

9292
private _releaseLiveInput(): void {
93+
this._liveInput.removeNote(this._playedPitch);
9394
this._playedPitch = -1;
94-
this._doc.synth.liveInputDuration = 0;
9595
}
9696

9797
private _whenMouseOver = (event: MouseEvent): void => {
@@ -181,8 +181,6 @@ export class Piano {
181181
this._pitchHeight = this._editorHeight / this._pitchCount;
182182
this._updateCursorPitch();
183183
if (this._mouseDown) this._playLiveInput();
184-
this._doc.synth.liveInputChannel = this._doc.channel;
185-
this._doc.synth.liveInputInstruments = this._doc.recentPatternInstruments[this._doc.channel];
186184

187185
if (!this._doc.showLetters) return;
188186
if (this._renderedScale == this._doc.song.scale && this._renderedKey == this._doc.song.key && this._renderedDrums == isDrum && this._renderedPitchCount == this._pitchCount) return;

editor/SongEditor.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {SpectrumEditor} from "./SpectrumEditor";
2121
import {HarmonicsEditor} from "./HarmonicsEditor";
2222
import {BarScrollBar} from "./BarScrollBar";
2323
import {OctaveScrollBar} from "./OctaveScrollBar";
24+
import {LiveInput} from "./LiveInput";
2425
import {Piano} from "./Piano";
2526
import {BeatsPerBarPrompt} from "./BeatsPerBarPrompt";
2627
import {MoveNotesSidewaysPrompt} from "./MoveNotesSidewaysPrompt";
@@ -126,7 +127,8 @@ export class SongEditor {
126127
private readonly _trackEditor: TrackEditor = new TrackEditor(this._doc);
127128
private readonly _loopEditor: LoopEditor = new LoopEditor(this._doc);
128129
private readonly _octaveScrollBar: OctaveScrollBar = new OctaveScrollBar(this._doc);
129-
private readonly _piano: Piano = new Piano(this._doc);
130+
private readonly _liveInput: LiveInput = new LiveInput(this._doc);
131+
private readonly _piano: Piano = new Piano(this._doc, this._liveInput);
130132
private readonly _playButton: HTMLButtonElement = button({style: "width: 80px;", type: "button"});
131133
private readonly _prevBarButton: HTMLButtonElement = button({class: "prevBarButton", style: "width: 40px;", type: "button", title: "Previous Bar (left bracket)"});
132134
private readonly _nextBarButton: HTMLButtonElement = button({class: "nextBarButton", style: "width: 40px;", type: "button", title: "Next Bar (right bracket)"});

0 commit comments

Comments
 (0)