-
Notifications
You must be signed in to change notification settings - Fork 4
/
Pianolatron.svelte
339 lines (306 loc) · 9.25 KB
/
Pianolatron.svelte
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
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
<style lang="scss">
#app {
height: 100vh;
display: flex;
flex-direction: column;
overflow: hidden;
> div:first-child {
flex: 1 0 auto;
position: relative;
display: grid;
grid-template-rows: 1fr;
grid-template-columns: auto 1fr auto;
grid-template-areas: "left center right";
}
}
:global(#left-sidebar) {
grid-area: left;
p {
opacity: 0.5;
padding: 0.5em 1em;
}
}
#roll {
position: relative;
flex: 1 0 auto;
grid-area: center;
}
:global(#right-sidebar) {
grid-area: right;
}
#keyboard-container {
flex: 0 1 auto;
user-select: none;
}
#keyboard-overlay {
position: absolute;
bottom: 0;
width: calc(100% - var(--navigator-width));
opacity: 0.5;
z-index: z($main-context, keyboard-overlay);
}
</style>
<script>
import { onMount } from "svelte";
import { quartInOut } from "svelte/easing";
import { fade } from "svelte/transition";
import IntervalTree from "node-interval-tree";
import {
bassVolumeCoefficient,
trebleVolumeCoefficient,
tempoCoefficient,
playbackProgress,
currentTick,
rollMetadata,
isReproducingRoll,
scrollDownwards,
playExpressionsOnOff,
rollPedalingOnOff,
userSettings,
} from "./stores";
import { annotateHoleData, clamp } from "./lib/utils";
import SamplePlayer from "./components/SamplePlayer.svelte";
import RollSelector from "./components/RollSelector.svelte";
import RollDetails from "./components/RollDetails.svelte";
import RollViewer from "./components/RollViewer.svelte";
import Keyboard from "./components/Keyboard.svelte";
import KeyboardControls from "./components/KeyboardControls.svelte";
import KeyboardShortcuts from "./components/KeyboardShortcuts.svelte";
import KeyboardShortcutEditor from "./components/KeyboardShortcutEditor.svelte";
import TabbedPanel from "./components/TabbedPanel.svelte";
import Welcome, { showWelcomeScreen } from "./components/Welcome.svelte";
import Notification, {
notify,
clearNotification,
} from "./ui-components/Notification.svelte";
import FlexCollapsible from "./ui-components/FlexCollapsible.svelte";
import LoadingSpinner from "./ui-components/LoadingSpinner.svelte";
import catalog from "./config/catalog.json";
let firstLoad = true;
let appReady = false;
let appWaiting = true;
let mididataReady;
let metadataReady;
let currentRoll;
let previousRoll;
let metadata;
let holeData;
let holesByTickInterval = new IntervalTree();
let samplePlayer;
let midiSamplePlayer;
let pianoReady;
let updatePlayer;
let startNote;
let stopNote;
let pausePlayback;
let startPlayback;
let resetPlayback;
let rollViewer;
let updateTickByViewportIncrement;
let panHorizontal;
const rollListItems = catalog.map((item) => {
const [number, label] = item.label.split(" ");
return {
...item,
_label: `${number} ${item.title}${label ? ` [${label}]` : ""}`,
};
});
const slide = (node, { delay = 0, duration = 300 }) => {
const o = parseInt(getComputedStyle(node).height, 10);
return {
delay,
duration,
css: (t) => `height: ${quartInOut(t) * o}px`,
};
};
const buildHolesIntervalTree = () => {
const { FIRST_HOLE } = $rollMetadata;
const firstHolePx = parseInt(FIRST_HOLE, 10);
holeData.forEach((hole) => {
const { y: offsetY, h: height } = hole;
const tickOn = offsetY - firstHolePx;
const tickOff = offsetY + height - firstHolePx;
holesByTickInterval.insert(tickOn, tickOff, hole);
});
};
const skipToTick = (tick) => {
if (tick < 0) pausePlayback();
$currentTick = tick;
updatePlayer(() => midiSamplePlayer.skipToTick($currentTick));
};
const skipToPercentage = (percentage) =>
skipToTick(midiSamplePlayer.totalTicks * percentage);
const playPauseApp = () => {
if (midiSamplePlayer.isPlaying()) {
pausePlayback();
} else {
startPlayback();
}
};
const stopApp = () => {
pausePlayback();
resetPlayback();
};
const resetApp = () => {
rollViewer?.$destroy();
mididataReady = false;
if (!firstLoad) clearNotification();
appReady = false;
pausePlayback();
resetPlayback();
playbackProgress.reset();
tempoCoefficient.reset();
bassVolumeCoefficient.reset();
trebleVolumeCoefficient.reset();
holesByTickInterval = new IntervalTree();
};
const loadRoll = (roll) => {
mididataReady = fetch(`./midi/${roll.druid}.mid`)
.then((mididataResponse) => {
if (mididataResponse.status === 200)
return mididataResponse.arrayBuffer();
throw new Error("Error fetching MIDI file! (Operation cancelled)");
})
.then((mididataArrayBuffer) => {
resetApp();
midiSamplePlayer.loadArrayBuffer(mididataArrayBuffer);
})
.catch((err) => {
notify({ title: "Error!", message: err, type: "error" });
currentRoll = previousRoll;
});
metadataReady = fetch(`./json/${roll.druid}.json`)
.then((metadataResponse) => {
if (metadataResponse.status === 200) return metadataResponse.json();
throw new Error("Error fetching metadata file! (Operation cancelled)");
})
.catch((err) => {
notify({ title: "Error!", message: err, type: "error" });
currentRoll = previousRoll;
});
Promise.all([mididataReady, metadataReady, pianoReady]).then(
([, metadataJson]) => {
metadata = (({ holeData: _, ...obj }) => obj)(metadataJson);
holeData = metadataJson.holeData;
annotateHoleData(holeData, $rollMetadata, $scrollDownwards);
buildHolesIntervalTree();
$playExpressionsOnOff = $isReproducingRoll;
$rollPedalingOnOff = $isReproducingRoll;
appReady = true;
appWaiting = false;
firstLoad = false;
previousRoll = currentRoll;
const params = new URLSearchParams(window.location.search);
if (params.has("druid") && params.get("druid") !== currentRoll.druid) {
const url = new URL(window.location);
url.searchParams.set("druid", currentRoll.druid);
window.history.pushState({ roll: currentRoll }, "", url);
}
},
);
};
const setCurrentRollFromUrl = () => {
const params = new URLSearchParams(window.location.search);
if (params.has("druid")) {
const druid = params.get("druid");
const roll = rollListItems.find((r) => r.druid === druid);
if (roll !== undefined) {
currentRoll = roll;
} else {
notify({
title: "DRUID not found!",
message:
"Please check the specified DRUID, or select a roll to continue.",
type: "error",
closable: false,
});
}
} else {
currentRoll =
rollListItems[Math.floor(Math.random() * rollListItems.length)];
}
};
onMount(async () => {
document.getElementById("loading").remove();
({
midiSamplePlayer,
pianoReady,
updatePlayer,
startNote,
stopNote,
pausePlayback,
startPlayback,
resetPlayback,
} = samplePlayer);
setCurrentRollFromUrl();
});
$: if (currentRoll !== previousRoll) loadRoll(currentRoll);
$: playbackProgress.update(() =>
clamp($currentTick / midiSamplePlayer?.totalTicks, 0, 1),
);
$: if (rollViewer)
({ updateTickByViewportIncrement, panHorizontal } = rollViewer);
</script>
<div id="app">
<div>
<FlexCollapsible id="left-sidebar" width="20vw">
<RollSelector bind:currentRoll {rollListItems} />
{#if appReady}
<RollDetails {metadata} />
{#if !holesByTickInterval.count}
<p>
Note:<br />Hole visualization data is not available for this roll at
this time. Hole highlighting will not be enabled.
</p>
{/if}
{/if}
</FlexCollapsible>
<div id="roll">
{#if appReady}
<RollViewer
bind:this={rollViewer}
imageUrl={currentRoll.image_url}
{holeData}
{holesByTickInterval}
{skipToTick}
/>
{/if}
{#if $userSettings.showKeyboard && $userSettings.overlayKeyboard}
<div id="keyboard-overlay" transition:fade>
<Keyboard keyCount="88" {startNote} {stopNote} />
</div>
{/if}
</div>
<FlexCollapsible id="right-sidebar" width="20vw" position="left">
<TabbedPanel {playPauseApp} {stopApp} {skipToPercentage} />
</FlexCollapsible>
</div>
{#if $userSettings.showKeyboard && !$userSettings.overlayKeyboard}
<div id="keyboard-container" transition:slide>
<Keyboard keyCount="88" {startNote} {stopNote} />
</div>
{:else if !$userSettings.showKeyboard}
<KeyboardControls outside />
{/if}
<LoadingSpinner showLoadingSpinner={appWaiting} />
</div>
<SamplePlayer
bind:this={samplePlayer}
on:loading={({ detail: loadingSamples }) => {
appWaiting = true;
loadingSamples.then(() => (appWaiting = false)).catch(() => {});
}}
/>
<KeyboardShortcuts
{playPauseApp}
{stopApp}
{updateTickByViewportIncrement}
{panHorizontal}
/>
<KeyboardShortcutEditor />
<Notification />
{#if $showWelcomeScreen}<Welcome />{/if}
<svelte:window
on:popstate={({ state }) =>
state?.roll ? (currentRoll = state.roll) : setCurrentRollFromUrl()}
/>