@sound2score/player 0.0.0-stage → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +21 -0
- package/README.md +92 -2
- package/index.d.ts +9 -0
- package/index.js +561 -0
- package/package.json +30 -5
- package/src/embed-frame.d.ts +13 -0
- package/src/embed-message-reader.d.ts +20 -0
- package/src/embed-options-codec.d.ts +21 -0
- package/src/embed-options.d.ts +77 -0
- package/src/embed-url-builder.d.ts +13 -0
- package/src/fake-embed-frame.d.ts +19 -0
- package/src/iframe-embed-frame.d.ts +23 -0
- package/src/player.d.ts +90 -0
- package/src/protocol.d.ts +92 -0
- package/vendor/core/LICENSE +21 -0
- package/vendor/core/disposable/async-disposer.d.ts +15 -0
- package/vendor/core/disposable/async-resource.d.ts +4 -0
- package/vendor/core/disposable/disposables.d.ts +8 -0
- package/vendor/core/disposable/disposer.d.ts +13 -0
- package/vendor/core/disposable/index.d.ts +5 -0
- package/vendor/core/disposable/index.js +96 -0
- package/vendor/core/disposable/resource.d.ts +4 -0
- package/vendor/core/events/dispatcher.d.ts +18 -0
- package/vendor/core/events/eventful.d.ts +7 -0
- package/vendor/core/events/events.d.ts +14 -0
- package/vendor/core/events/index.d.ts +4 -0
- package/vendor/core/events/index.js +75 -0
- package/vendor/core/events/noop-events.d.ts +6 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 sound2score
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
CHANGED
|
@@ -1,3 +1,93 @@
|
|
|
1
|
-
#
|
|
1
|
+
# @sound2score/player
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Put [sound2score](https://sound2score.com) sheet music on your site and
|
|
4
|
+
control it from your page: play, pause, seek, loop bars, change the speed,
|
|
5
|
+
transpose and click along, and hear back what the Player is doing.
|
|
6
|
+
|
|
7
|
+
```sh
|
|
8
|
+
npm install @sound2score/player
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
Building with an AI coding agent? Give it the Player's skill:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npx skills add sound2score/player
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Quick start
|
|
18
|
+
|
|
19
|
+
```ts
|
|
20
|
+
import { Player } from "@sound2score/player";
|
|
21
|
+
|
|
22
|
+
const container = document.getElementById("score");
|
|
23
|
+
if (!container) {
|
|
24
|
+
throw new Error("No #score element");
|
|
25
|
+
}
|
|
26
|
+
const player = Player.create(container, {
|
|
27
|
+
scoreId: "abc123",
|
|
28
|
+
userId: "student-42",
|
|
29
|
+
options: { start: { speed: 80 } },
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
player.events.on("ready", () => {
|
|
33
|
+
player.setLoop({ from: 9, to: 16 });
|
|
34
|
+
player.play();
|
|
35
|
+
});
|
|
36
|
+
player.events.on("change", (state) => {
|
|
37
|
+
console.log(`bar ${state.bar} of ${state.bars}`);
|
|
38
|
+
});
|
|
39
|
+
```
|
|
40
|
+
|
|
41
|
+
The score's owner has to add your site to the sites in their Embeds settings
|
|
42
|
+
on sound2score first. The score ID is the last part of the score's address,
|
|
43
|
+
`sound2score.com/s/<scoreId>`.
|
|
44
|
+
|
|
45
|
+
Already have an iframe pasted from sound2score? Take control of it:
|
|
46
|
+
|
|
47
|
+
```ts
|
|
48
|
+
import { Player } from "@sound2score/player";
|
|
49
|
+
|
|
50
|
+
const iframe = document.querySelector<HTMLIFrameElement>("iframe");
|
|
51
|
+
if (iframe) {
|
|
52
|
+
Player.attach(iframe).play();
|
|
53
|
+
}
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
## Commands
|
|
57
|
+
|
|
58
|
+
| Command | What it does |
|
|
59
|
+
| --- | --- |
|
|
60
|
+
| `play()` / `pause()` | Starts and stops playback. Phones need one tap inside the embed before sound plays. |
|
|
61
|
+
| `seek(seconds)` | Moves the playhead, staying inside a loop. |
|
|
62
|
+
| `goToBar(bar)` | Moves to the start of a bar, counted from 1 in playback order. |
|
|
63
|
+
| `setLoop({ from, to })` / `setLoop(null)` | Loops bars, both included, or stops looping. |
|
|
64
|
+
| `setSpeed(percent)` | 25 to 200 percent of the written tempo. |
|
|
65
|
+
| `setTranspose(semitones)` | -12 to 12 from as written. |
|
|
66
|
+
| `setMetronome(on)` | Turns the click on or off. |
|
|
67
|
+
|
|
68
|
+
Commands given before the score is ready wait, then run in order. They
|
|
69
|
+
work even when the embed's options hide that control.
|
|
70
|
+
|
|
71
|
+
## Events
|
|
72
|
+
|
|
73
|
+
- `ready(state)`: the score loaded and takes commands.
|
|
74
|
+
- `change(state)`: what the Player shows changed, at most 4 times a second.
|
|
75
|
+
- `unavailable({ reason, resetsAt })`: the embed can't play. `reason` is
|
|
76
|
+
`not-found`, `site` (your site isn't allowed), `capped` (the owner's
|
|
77
|
+
spending limit, until `resetsAt`) or `busy` (too many new viewers from one
|
|
78
|
+
network, until `resetsAt`).
|
|
79
|
+
|
|
80
|
+
## Students
|
|
81
|
+
|
|
82
|
+
Each viewer of an embed counts as one of the score owner's students. Pass
|
|
83
|
+
`userId`, the ID your site knows the viewer by, so a student counts once
|
|
84
|
+
however many browsers they use. It is read once, when the embed loads.
|
|
85
|
+
|
|
86
|
+
## What it can't do
|
|
87
|
+
|
|
88
|
+
No loading another score into the same Player (create a new one), no
|
|
89
|
+
printing or exporting, and no preloading recordings.
|
|
90
|
+
|
|
91
|
+
## License
|
|
92
|
+
|
|
93
|
+
MIT
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export type { EmbedFrame, EmbedFrameEvents } from "./src/embed-frame.js";
|
|
2
|
+
export { EmbedMessageReader } from "./src/embed-message-reader.js";
|
|
3
|
+
export { APPEARANCES, type Appearance, type BarRange, type ControlMode, type CreditPlacement, DEFAULT_EMBED_OPTIONS, type EmbedControls, type EmbedLayout, type EmbedLook, type EmbedOptions, type EmbedStart, type InitialLayout, MAX_SEMITONES, MAX_SPEED, MIN_SPEED, THEMES, type Theme, type VideoPlacement, } from "./src/embed-options.js";
|
|
4
|
+
export { EmbedOptionsCodec } from "./src/embed-options-codec.js";
|
|
5
|
+
export { EmbedUrlBuilder } from "./src/embed-url-builder.js";
|
|
6
|
+
export { FakeEmbedFrame } from "./src/fake-embed-frame.js";
|
|
7
|
+
export { IframeEmbedFrame } from "./src/iframe-embed-frame.js";
|
|
8
|
+
export { type CreateOptions, Player, type PlayerEvents, type PlayerOptions, } from "./src/player.js";
|
|
9
|
+
export { type Command, type EmbedEvent, type EmbedMessage, type HostMessage, type PlayerState, PROTOCOL_TAG, PROTOCOL_VERSION, type Unavailable, type UnavailableReason, } from "./src/protocol.js";
|
package/index.js
ADDED
|
@@ -0,0 +1,561 @@
|
|
|
1
|
+
// src/protocol.ts
|
|
2
|
+
var PROTOCOL_VERSION = 1;
|
|
3
|
+
var PROTOCOL_TAG = "sound2score-player";
|
|
4
|
+
|
|
5
|
+
// src/embed-message-reader.ts
|
|
6
|
+
var REASONS = [
|
|
7
|
+
"not-found",
|
|
8
|
+
"site",
|
|
9
|
+
"capped",
|
|
10
|
+
"busy"
|
|
11
|
+
];
|
|
12
|
+
|
|
13
|
+
class EmbedMessageReader {
|
|
14
|
+
host(data) {
|
|
15
|
+
const envelope = this.envelope(data);
|
|
16
|
+
const command = envelope && this.command(this.fields(envelope.command));
|
|
17
|
+
return envelope && command ? { tag: PROTOCOL_TAG, version: envelope.version, command } : null;
|
|
18
|
+
}
|
|
19
|
+
embed(data) {
|
|
20
|
+
const envelope = this.envelope(data);
|
|
21
|
+
const event = envelope && this.event(this.fields(envelope.event));
|
|
22
|
+
return envelope && event ? { tag: PROTOCOL_TAG, version: envelope.version, event } : null;
|
|
23
|
+
}
|
|
24
|
+
envelope(data) {
|
|
25
|
+
const fields = this.fields(data);
|
|
26
|
+
if (fields?.tag !== PROTOCOL_TAG) {
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
const version = fields.version;
|
|
30
|
+
return typeof version === "number" && Number.isInteger(version) && version >= 1 ? { ...fields, version } : null;
|
|
31
|
+
}
|
|
32
|
+
command(fields) {
|
|
33
|
+
switch (fields?.kind) {
|
|
34
|
+
case "hello":
|
|
35
|
+
case "play":
|
|
36
|
+
case "pause":
|
|
37
|
+
return { kind: fields.kind };
|
|
38
|
+
case "seek": {
|
|
39
|
+
const seconds = this.number(fields.seconds);
|
|
40
|
+
return seconds !== null && seconds >= 0 ? { kind: "seek", seconds } : null;
|
|
41
|
+
}
|
|
42
|
+
case "goToBar": {
|
|
43
|
+
const bar = this.count(fields.bar);
|
|
44
|
+
return bar !== null ? { kind: "goToBar", bar } : null;
|
|
45
|
+
}
|
|
46
|
+
case "setLoop": {
|
|
47
|
+
if (fields.bars === null) {
|
|
48
|
+
return { kind: "setLoop", bars: null };
|
|
49
|
+
}
|
|
50
|
+
const bars = this.bars(fields.bars);
|
|
51
|
+
return bars ? { kind: "setLoop", bars } : null;
|
|
52
|
+
}
|
|
53
|
+
case "setSpeed": {
|
|
54
|
+
const percent = this.number(fields.percent);
|
|
55
|
+
return percent !== null ? { kind: "setSpeed", percent } : null;
|
|
56
|
+
}
|
|
57
|
+
case "setTranspose": {
|
|
58
|
+
const semitones = this.number(fields.semitones);
|
|
59
|
+
return semitones !== null && Number.isInteger(semitones) ? { kind: "setTranspose", semitones } : null;
|
|
60
|
+
}
|
|
61
|
+
case "setMetronome":
|
|
62
|
+
return typeof fields.on === "boolean" ? { kind: "setMetronome", on: fields.on } : null;
|
|
63
|
+
default:
|
|
64
|
+
return null;
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
event(fields) {
|
|
68
|
+
switch (fields?.kind) {
|
|
69
|
+
case "ready":
|
|
70
|
+
case "change": {
|
|
71
|
+
const state = this.state(this.fields(fields.state));
|
|
72
|
+
return state ? { kind: fields.kind, state } : null;
|
|
73
|
+
}
|
|
74
|
+
case "unavailable": {
|
|
75
|
+
const unavailable = this.unavailable(this.fields(fields.unavailable));
|
|
76
|
+
return unavailable ? { kind: "unavailable", unavailable } : null;
|
|
77
|
+
}
|
|
78
|
+
default:
|
|
79
|
+
return null;
|
|
80
|
+
}
|
|
81
|
+
}
|
|
82
|
+
state(fields) {
|
|
83
|
+
if (!fields) {
|
|
84
|
+
return null;
|
|
85
|
+
}
|
|
86
|
+
const seconds = this.number(fields.seconds);
|
|
87
|
+
const duration = this.number(fields.duration);
|
|
88
|
+
const bar = this.count(fields.bar);
|
|
89
|
+
const bars = this.number(fields.bars);
|
|
90
|
+
const speed = this.number(fields.speed);
|
|
91
|
+
const semitones = this.number(fields.semitones);
|
|
92
|
+
const loop = fields.loop === null ? null : this.bars(fields.loop);
|
|
93
|
+
if (typeof fields.playing !== "boolean" || typeof fields.metronome !== "boolean" || seconds === null || duration === null || bar === null || bars === null || speed === null || semitones === null || fields.loop !== null && loop === null) {
|
|
94
|
+
return null;
|
|
95
|
+
}
|
|
96
|
+
return {
|
|
97
|
+
playing: fields.playing,
|
|
98
|
+
seconds,
|
|
99
|
+
duration,
|
|
100
|
+
bar,
|
|
101
|
+
bars,
|
|
102
|
+
loop,
|
|
103
|
+
speed,
|
|
104
|
+
semitones,
|
|
105
|
+
metronome: fields.metronome
|
|
106
|
+
};
|
|
107
|
+
}
|
|
108
|
+
unavailable(fields) {
|
|
109
|
+
const reason = REASONS.find((reason) => reason === fields?.reason);
|
|
110
|
+
const resetsAt = fields?.resetsAt === null ? null : this.number(fields?.resetsAt);
|
|
111
|
+
if (!reason || fields?.resetsAt !== null && resetsAt === null) {
|
|
112
|
+
return null;
|
|
113
|
+
}
|
|
114
|
+
return { reason, resetsAt };
|
|
115
|
+
}
|
|
116
|
+
bars(value) {
|
|
117
|
+
const fields = this.fields(value);
|
|
118
|
+
const from = this.count(fields?.from);
|
|
119
|
+
const to = this.count(fields?.to);
|
|
120
|
+
return from !== null && to !== null && to >= from ? { from, to } : null;
|
|
121
|
+
}
|
|
122
|
+
count(value) {
|
|
123
|
+
return typeof value === "number" && Number.isInteger(value) && value >= 1 ? value : null;
|
|
124
|
+
}
|
|
125
|
+
number(value) {
|
|
126
|
+
return typeof value === "number" && Number.isFinite(value) ? value : null;
|
|
127
|
+
}
|
|
128
|
+
fields(value) {
|
|
129
|
+
return typeof value === "object" && value !== null && !Array.isArray(value) ? value : null;
|
|
130
|
+
}
|
|
131
|
+
}
|
|
132
|
+
// src/embed-options.ts
|
|
133
|
+
var APPEARANCES = ["auto", "light", "dark"];
|
|
134
|
+
var THEMES = ["neon", "volt", "electric", "plasma"];
|
|
135
|
+
var MIN_SPEED = 25;
|
|
136
|
+
var MAX_SPEED = 200;
|
|
137
|
+
var MAX_SEMITONES = 12;
|
|
138
|
+
var DEFAULT_EMBED_OPTIONS = {
|
|
139
|
+
controls: {
|
|
140
|
+
tempo: "on",
|
|
141
|
+
metronome: "on",
|
|
142
|
+
loop: "on",
|
|
143
|
+
transpose: "on",
|
|
144
|
+
fullscreen: "on",
|
|
145
|
+
mixer: "on",
|
|
146
|
+
fretboard: "on",
|
|
147
|
+
piano: "on"
|
|
148
|
+
},
|
|
149
|
+
start: { speed: 100, semitones: 0, metronome: false, bars: null, bar: null },
|
|
150
|
+
layout: { title: true, video: "beside", initial: "video" },
|
|
151
|
+
look: {}
|
|
152
|
+
};
|
|
153
|
+
// src/embed-options-codec.ts
|
|
154
|
+
var HEX = /^[0-9a-f]{6}$/;
|
|
155
|
+
var CONTROLS = Object.keys(DEFAULT_EMBED_OPTIONS.controls);
|
|
156
|
+
var MODES = {
|
|
157
|
+
tempo: ["off", "on"],
|
|
158
|
+
metronome: ["off", "on"],
|
|
159
|
+
loop: ["off", "on"],
|
|
160
|
+
transpose: ["off", "on"],
|
|
161
|
+
fullscreen: ["off", "on"],
|
|
162
|
+
mixer: ["off", "on", "open"],
|
|
163
|
+
fretboard: ["off", "on", "open"],
|
|
164
|
+
piano: ["off", "on", "open"]
|
|
165
|
+
};
|
|
166
|
+
var PLACEMENTS = ["beside", "above"];
|
|
167
|
+
var INITIALS = {
|
|
168
|
+
video: "video",
|
|
169
|
+
line: "firstLine",
|
|
170
|
+
score: "score"
|
|
171
|
+
};
|
|
172
|
+
var INITIAL_VALUES = Object.keys(INITIALS);
|
|
173
|
+
var CREDITS = ["header", "corner", "hidden"];
|
|
174
|
+
|
|
175
|
+
class EmbedOptionsCodec {
|
|
176
|
+
encode(opts) {
|
|
177
|
+
const params = new URLSearchParams;
|
|
178
|
+
const defaults = DEFAULT_EMBED_OPTIONS;
|
|
179
|
+
for (const control of CONTROLS) {
|
|
180
|
+
if (opts.controls[control] !== defaults.controls[control]) {
|
|
181
|
+
params.set(control, opts.controls[control]);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
const { start, layout, look } = opts;
|
|
185
|
+
if (start.speed !== defaults.start.speed) {
|
|
186
|
+
params.set("speed", String(start.speed));
|
|
187
|
+
}
|
|
188
|
+
if (start.semitones !== defaults.start.semitones) {
|
|
189
|
+
params.set("semitones", String(start.semitones));
|
|
190
|
+
}
|
|
191
|
+
if (start.metronome) {
|
|
192
|
+
params.set("click", "1");
|
|
193
|
+
}
|
|
194
|
+
if (start.bars) {
|
|
195
|
+
params.set("bars", `${start.bars.from}-${start.bars.to}`);
|
|
196
|
+
} else if (start.bar !== null) {
|
|
197
|
+
params.set("bar", String(start.bar));
|
|
198
|
+
}
|
|
199
|
+
if (!layout.title) {
|
|
200
|
+
params.set("title", "0");
|
|
201
|
+
}
|
|
202
|
+
if (layout.video !== defaults.layout.video) {
|
|
203
|
+
params.set("video", layout.video);
|
|
204
|
+
}
|
|
205
|
+
if (layout.initial !== defaults.layout.initial) {
|
|
206
|
+
params.set("initial", layout.initial === "firstLine" ? "line" : "score");
|
|
207
|
+
}
|
|
208
|
+
if (look.appearance) {
|
|
209
|
+
params.set("appearance", look.appearance);
|
|
210
|
+
}
|
|
211
|
+
if (look.theme) {
|
|
212
|
+
params.set("theme", look.theme);
|
|
213
|
+
}
|
|
214
|
+
if (look.accent) {
|
|
215
|
+
params.set("accent", look.accent.slice(1));
|
|
216
|
+
}
|
|
217
|
+
if (look.background) {
|
|
218
|
+
params.set("bg", look.background === "transparent" ? "transparent" : look.background.slice(1));
|
|
219
|
+
}
|
|
220
|
+
if (look.credit) {
|
|
221
|
+
params.set("credit", look.credit);
|
|
222
|
+
}
|
|
223
|
+
return params;
|
|
224
|
+
}
|
|
225
|
+
decode(params) {
|
|
226
|
+
return {
|
|
227
|
+
controls: this.controls(params),
|
|
228
|
+
start: this.start(params),
|
|
229
|
+
layout: this.layout(params),
|
|
230
|
+
look: this.look(params)
|
|
231
|
+
};
|
|
232
|
+
}
|
|
233
|
+
controls(params) {
|
|
234
|
+
const controls = {
|
|
235
|
+
...DEFAULT_EMBED_OPTIONS.controls
|
|
236
|
+
};
|
|
237
|
+
for (const control of CONTROLS) {
|
|
238
|
+
const mode = this.oneOf(params.get(control), MODES[control]);
|
|
239
|
+
if (mode) {
|
|
240
|
+
controls[control] = mode;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return controls;
|
|
244
|
+
}
|
|
245
|
+
start(params) {
|
|
246
|
+
const bars = this.bars(params.get("bars"));
|
|
247
|
+
return {
|
|
248
|
+
speed: this.integer(params.get("speed"), MIN_SPEED, MAX_SPEED) ?? DEFAULT_EMBED_OPTIONS.start.speed,
|
|
249
|
+
semitones: this.integer(params.get("semitones"), -MAX_SEMITONES, MAX_SEMITONES) ?? 0,
|
|
250
|
+
metronome: params.get("click") === "1",
|
|
251
|
+
bars,
|
|
252
|
+
bar: bars ? null : this.integer(params.get("bar"), 1, Infinity)
|
|
253
|
+
};
|
|
254
|
+
}
|
|
255
|
+
layout(params) {
|
|
256
|
+
return {
|
|
257
|
+
title: params.get("title") !== "0",
|
|
258
|
+
video: this.oneOf(params.get("video"), PLACEMENTS) ?? DEFAULT_EMBED_OPTIONS.layout.video,
|
|
259
|
+
initial: this.initial(params)
|
|
260
|
+
};
|
|
261
|
+
}
|
|
262
|
+
initial(params) {
|
|
263
|
+
const initial = this.oneOf(params.get("initial"), INITIAL_VALUES);
|
|
264
|
+
if (initial) {
|
|
265
|
+
return INITIALS[initial];
|
|
266
|
+
}
|
|
267
|
+
return params.get("collapse") === "1" ? "score" : DEFAULT_EMBED_OPTIONS.layout.initial;
|
|
268
|
+
}
|
|
269
|
+
look(params) {
|
|
270
|
+
const look = {};
|
|
271
|
+
const appearance = this.oneOf(params.get("appearance"), APPEARANCES);
|
|
272
|
+
if (appearance) {
|
|
273
|
+
look.appearance = appearance;
|
|
274
|
+
}
|
|
275
|
+
const theme = this.oneOf(params.get("theme"), THEMES);
|
|
276
|
+
if (theme) {
|
|
277
|
+
look.theme = theme;
|
|
278
|
+
}
|
|
279
|
+
const accent = this.hex(params.get("accent"));
|
|
280
|
+
if (accent) {
|
|
281
|
+
look.accent = accent;
|
|
282
|
+
}
|
|
283
|
+
const background = params.get("bg");
|
|
284
|
+
if (background === "transparent") {
|
|
285
|
+
look.background = "transparent";
|
|
286
|
+
} else if (this.hex(background)) {
|
|
287
|
+
look.background = this.hex(background);
|
|
288
|
+
}
|
|
289
|
+
const credit = this.oneOf(params.get("credit"), CREDITS);
|
|
290
|
+
if (credit) {
|
|
291
|
+
look.credit = credit;
|
|
292
|
+
}
|
|
293
|
+
return look;
|
|
294
|
+
}
|
|
295
|
+
bars(value) {
|
|
296
|
+
const match = value?.match(/^(\d+)-(\d+)$/);
|
|
297
|
+
if (!match) {
|
|
298
|
+
return null;
|
|
299
|
+
}
|
|
300
|
+
const from = Number(match[1]);
|
|
301
|
+
const to = Number(match[2]);
|
|
302
|
+
return from >= 1 && to >= from ? { from, to } : null;
|
|
303
|
+
}
|
|
304
|
+
integer(value, min, max) {
|
|
305
|
+
if (value === null || !/^-?\d+$/.test(value)) {
|
|
306
|
+
return null;
|
|
307
|
+
}
|
|
308
|
+
const number = Number(value);
|
|
309
|
+
return number >= min && number <= max ? number : null;
|
|
310
|
+
}
|
|
311
|
+
hex(value) {
|
|
312
|
+
const lower = value?.toLowerCase() ?? "";
|
|
313
|
+
return HEX.test(lower) ? `#${lower}` : null;
|
|
314
|
+
}
|
|
315
|
+
oneOf(value, options) {
|
|
316
|
+
return options.find((option) => option === value) ?? null;
|
|
317
|
+
}
|
|
318
|
+
}
|
|
319
|
+
// src/embed-url-builder.ts
|
|
320
|
+
class EmbedUrlBuilder {
|
|
321
|
+
codec;
|
|
322
|
+
origin;
|
|
323
|
+
constructor(origin, codec) {
|
|
324
|
+
this.codec = codec;
|
|
325
|
+
this.origin = origin.replace(/\/+$/, "");
|
|
326
|
+
}
|
|
327
|
+
build(scoreId, opts, userId) {
|
|
328
|
+
const params = this.codec.encode(opts);
|
|
329
|
+
if (userId !== null && userId !== "") {
|
|
330
|
+
params.set("u", userId);
|
|
331
|
+
}
|
|
332
|
+
const query = params.toString();
|
|
333
|
+
const path = `${this.origin}/s/${encodeURIComponent(scoreId)}/embed`;
|
|
334
|
+
return query ? `${path}?${query}` : path;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
// src/fake-embed-frame.ts
|
|
338
|
+
import { Dispatcher } from "./vendor/core/events/index.js";
|
|
339
|
+
|
|
340
|
+
class FakeEmbedFrame {
|
|
341
|
+
dispatcher = new Dispatcher;
|
|
342
|
+
posted = [];
|
|
343
|
+
disposed = false;
|
|
344
|
+
get events() {
|
|
345
|
+
return this.dispatcher.events;
|
|
346
|
+
}
|
|
347
|
+
post(message) {
|
|
348
|
+
this.posted.push(message);
|
|
349
|
+
}
|
|
350
|
+
dispose() {
|
|
351
|
+
this.disposed = true;
|
|
352
|
+
this.dispatcher.dispose();
|
|
353
|
+
}
|
|
354
|
+
sent() {
|
|
355
|
+
return [...this.posted];
|
|
356
|
+
}
|
|
357
|
+
receive(message) {
|
|
358
|
+
this.dispatcher.dispatch("message", message);
|
|
359
|
+
}
|
|
360
|
+
load() {
|
|
361
|
+
this.dispatcher.dispatch("load");
|
|
362
|
+
}
|
|
363
|
+
isDisposed() {
|
|
364
|
+
return this.disposed;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
// src/iframe-embed-frame.ts
|
|
368
|
+
import { Dispatcher as Dispatcher2 } from "./vendor/core/events/index.js";
|
|
369
|
+
|
|
370
|
+
class IframeEmbedFrame {
|
|
371
|
+
iframe;
|
|
372
|
+
window;
|
|
373
|
+
origin;
|
|
374
|
+
reader;
|
|
375
|
+
dispatcher = new Dispatcher2;
|
|
376
|
+
constructor(iframe, window, origin, reader) {
|
|
377
|
+
this.iframe = iframe;
|
|
378
|
+
this.window = window;
|
|
379
|
+
this.origin = origin;
|
|
380
|
+
this.reader = reader;
|
|
381
|
+
this.window.addEventListener("message", this.receive);
|
|
382
|
+
this.iframe.addEventListener("load", this.loaded);
|
|
383
|
+
}
|
|
384
|
+
get events() {
|
|
385
|
+
return this.dispatcher.events;
|
|
386
|
+
}
|
|
387
|
+
post(message) {
|
|
388
|
+
this.iframe.contentWindow?.postMessage(message, this.origin);
|
|
389
|
+
}
|
|
390
|
+
dispose() {
|
|
391
|
+
this.window.removeEventListener("message", this.receive);
|
|
392
|
+
this.iframe.removeEventListener("load", this.loaded);
|
|
393
|
+
this.dispatcher.dispose();
|
|
394
|
+
}
|
|
395
|
+
receive = (event) => {
|
|
396
|
+
if (event.origin !== this.origin || event.source === null || event.source !== this.iframe.contentWindow) {
|
|
397
|
+
return;
|
|
398
|
+
}
|
|
399
|
+
const message = this.reader.embed(event.data);
|
|
400
|
+
if (message) {
|
|
401
|
+
this.dispatcher.dispatch("message", message);
|
|
402
|
+
}
|
|
403
|
+
};
|
|
404
|
+
loaded = () => {
|
|
405
|
+
this.dispatcher.dispatch("load");
|
|
406
|
+
};
|
|
407
|
+
}
|
|
408
|
+
// src/player.ts
|
|
409
|
+
import { Disposer } from "./vendor/core/disposable/index.js";
|
|
410
|
+
import { Dispatcher as Dispatcher3 } from "./vendor/core/events/index.js";
|
|
411
|
+
var DEFAULT_ORIGIN = "https://sound2score.com";
|
|
412
|
+
|
|
413
|
+
class Player {
|
|
414
|
+
frame;
|
|
415
|
+
dispatcher = new Dispatcher3;
|
|
416
|
+
disposer = new Disposer;
|
|
417
|
+
state = null;
|
|
418
|
+
ready = false;
|
|
419
|
+
unavailable = null;
|
|
420
|
+
waiting = [];
|
|
421
|
+
static create(container, opts) {
|
|
422
|
+
const origin = opts.origin ?? DEFAULT_ORIGIN;
|
|
423
|
+
const document = container.ownerDocument;
|
|
424
|
+
const iframe = document.createElement("iframe");
|
|
425
|
+
iframe.src = new EmbedUrlBuilder(origin, new EmbedOptionsCodec).build(opts.scoreId, Player.options(opts.options ?? {}), opts.userId ?? null);
|
|
426
|
+
iframe.width = "100%";
|
|
427
|
+
iframe.height = String(opts.height ?? 520);
|
|
428
|
+
iframe.style.border = "0";
|
|
429
|
+
iframe.style.colorScheme = "normal";
|
|
430
|
+
iframe.allow = "autoplay; fullscreen";
|
|
431
|
+
iframe.title = opts.title ?? "Score on sound2score";
|
|
432
|
+
container.append(iframe);
|
|
433
|
+
return Player.frame(iframe, new URL(origin).origin);
|
|
434
|
+
}
|
|
435
|
+
static attach(iframe) {
|
|
436
|
+
return Player.frame(iframe, new URL(iframe.src).origin);
|
|
437
|
+
}
|
|
438
|
+
static frame(iframe, origin) {
|
|
439
|
+
const window = iframe.ownerDocument.defaultView;
|
|
440
|
+
if (!window) {
|
|
441
|
+
throw new Error("The iframe must be in a document with a window.");
|
|
442
|
+
}
|
|
443
|
+
return new Player(new IframeEmbedFrame(iframe, window, origin, new EmbedMessageReader));
|
|
444
|
+
}
|
|
445
|
+
static options(opts) {
|
|
446
|
+
return {
|
|
447
|
+
controls: { ...DEFAULT_EMBED_OPTIONS.controls, ...opts.controls },
|
|
448
|
+
start: { ...DEFAULT_EMBED_OPTIONS.start, ...opts.start },
|
|
449
|
+
layout: { ...DEFAULT_EMBED_OPTIONS.layout, ...opts.layout },
|
|
450
|
+
look: { ...opts.look }
|
|
451
|
+
};
|
|
452
|
+
}
|
|
453
|
+
constructor(frame) {
|
|
454
|
+
this.frame = frame;
|
|
455
|
+
this.disposer.use(this.dispatcher);
|
|
456
|
+
this.disposer.use(frame);
|
|
457
|
+
this.disposer.defer(frame.events.on("message", (message) => this.receive(message)));
|
|
458
|
+
this.disposer.defer(frame.events.on("load", () => this.loaded()));
|
|
459
|
+
this.send({ kind: "hello" });
|
|
460
|
+
}
|
|
461
|
+
get events() {
|
|
462
|
+
return this.dispatcher.events;
|
|
463
|
+
}
|
|
464
|
+
getState() {
|
|
465
|
+
return this.state;
|
|
466
|
+
}
|
|
467
|
+
isReady() {
|
|
468
|
+
return this.ready;
|
|
469
|
+
}
|
|
470
|
+
getUnavailable() {
|
|
471
|
+
return this.unavailable;
|
|
472
|
+
}
|
|
473
|
+
play() {
|
|
474
|
+
this.command({ kind: "play" });
|
|
475
|
+
}
|
|
476
|
+
pause() {
|
|
477
|
+
this.command({ kind: "pause" });
|
|
478
|
+
}
|
|
479
|
+
seek(seconds) {
|
|
480
|
+
this.command({ kind: "seek", seconds: Math.max(0, seconds) });
|
|
481
|
+
}
|
|
482
|
+
goToBar(bar) {
|
|
483
|
+
this.command({ kind: "goToBar", bar: Math.max(1, Math.round(bar)) });
|
|
484
|
+
}
|
|
485
|
+
setLoop(bars) {
|
|
486
|
+
this.command({ kind: "setLoop", bars });
|
|
487
|
+
}
|
|
488
|
+
setSpeed(percent) {
|
|
489
|
+
this.command({ kind: "setSpeed", percent });
|
|
490
|
+
}
|
|
491
|
+
setTranspose(semitones) {
|
|
492
|
+
this.command({ kind: "setTranspose", semitones: Math.round(semitones) });
|
|
493
|
+
}
|
|
494
|
+
setMetronome(on) {
|
|
495
|
+
this.command({ kind: "setMetronome", on });
|
|
496
|
+
}
|
|
497
|
+
dispose() {
|
|
498
|
+
this.disposer.dispose();
|
|
499
|
+
this.waiting = [];
|
|
500
|
+
}
|
|
501
|
+
command(command) {
|
|
502
|
+
if (this.ready) {
|
|
503
|
+
this.send(command);
|
|
504
|
+
} else if (!this.unavailable) {
|
|
505
|
+
this.waiting.push(command);
|
|
506
|
+
}
|
|
507
|
+
}
|
|
508
|
+
send(command) {
|
|
509
|
+
this.frame.post({ tag: PROTOCOL_TAG, version: PROTOCOL_VERSION, command });
|
|
510
|
+
}
|
|
511
|
+
receive({ event }) {
|
|
512
|
+
switch (event.kind) {
|
|
513
|
+
case "ready": {
|
|
514
|
+
this.state = event.state;
|
|
515
|
+
this.unavailable = null;
|
|
516
|
+
const first = !this.ready;
|
|
517
|
+
this.ready = true;
|
|
518
|
+
if (first) {
|
|
519
|
+
const waiting = this.waiting;
|
|
520
|
+
this.waiting = [];
|
|
521
|
+
for (const command of waiting) {
|
|
522
|
+
this.send(command);
|
|
523
|
+
}
|
|
524
|
+
this.dispatcher.dispatch("ready", event.state);
|
|
525
|
+
}
|
|
526
|
+
return;
|
|
527
|
+
}
|
|
528
|
+
case "change":
|
|
529
|
+
this.state = event.state;
|
|
530
|
+
this.dispatcher.dispatch("change", event.state);
|
|
531
|
+
return;
|
|
532
|
+
case "unavailable":
|
|
533
|
+
this.ready = false;
|
|
534
|
+
this.waiting = [];
|
|
535
|
+
this.unavailable = event.unavailable;
|
|
536
|
+
this.dispatcher.dispatch("unavailable", event.unavailable);
|
|
537
|
+
return;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
loaded() {
|
|
541
|
+
this.ready = false;
|
|
542
|
+
this.unavailable = null;
|
|
543
|
+
this.send({ kind: "hello" });
|
|
544
|
+
}
|
|
545
|
+
}
|
|
546
|
+
export {
|
|
547
|
+
APPEARANCES,
|
|
548
|
+
DEFAULT_EMBED_OPTIONS,
|
|
549
|
+
EmbedMessageReader,
|
|
550
|
+
EmbedOptionsCodec,
|
|
551
|
+
EmbedUrlBuilder,
|
|
552
|
+
FakeEmbedFrame,
|
|
553
|
+
IframeEmbedFrame,
|
|
554
|
+
MAX_SEMITONES,
|
|
555
|
+
MAX_SPEED,
|
|
556
|
+
MIN_SPEED,
|
|
557
|
+
PROTOCOL_TAG,
|
|
558
|
+
PROTOCOL_VERSION,
|
|
559
|
+
Player,
|
|
560
|
+
THEMES
|
|
561
|
+
};
|