canvas-recorder-lib 0.6.0 โ 0.7.1
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/BREAKING-CHANGES.md +6 -0
- package/README.md +37 -23
- package/dev/audio-builder.ts +69 -163
- package/dev/canvas-recorder.ts +350 -169
- package/dev/player-body.ts +8 -0
- package/dev/sound-clips.ts +3 -2
- package/dev/soundtrack.ts +442 -0
- package/dev/transcripts.ts +55 -4
- package/dev/video-clip-panel.ts +1 -1
- package/dev/voice/chirp.ts +129 -0
- package/dev/voice/folder.ts +84 -0
- package/dev/voice/voice-panel.ts +523 -0
- package/dev/voice/voice-recorder.ts +603 -0
- package/dist/dev/audio-builder.d.ts +14 -31
- package/dist/dev/audio-builder.d.ts.map +1 -1
- package/dist/dev/audio-builder.js +62 -135
- package/dist/dev/audio-builder.js.map +1 -1
- package/dist/dev/canvas-recorder.js +336 -156
- package/dist/dev/canvas-recorder.js.map +1 -1
- package/dist/dev/player-body.d.ts +1 -1
- package/dist/dev/player-body.d.ts.map +1 -1
- package/dist/dev/player-body.js +8 -0
- package/dist/dev/player-body.js.map +1 -1
- package/dist/dev/sound-clips.d.ts +3 -2
- package/dist/dev/sound-clips.d.ts.map +1 -1
- package/dist/dev/sound-clips.js +3 -2
- package/dist/dev/sound-clips.js.map +1 -1
- package/dist/dev/soundtrack.d.ts +115 -0
- package/dist/dev/soundtrack.d.ts.map +1 -0
- package/dist/dev/soundtrack.js +376 -0
- package/dist/dev/soundtrack.js.map +1 -0
- package/dist/dev/transcripts.d.ts +5 -0
- package/dist/dev/transcripts.d.ts.map +1 -1
- package/dist/dev/transcripts.js +50 -4
- package/dist/dev/transcripts.js.map +1 -1
- package/dist/dev/video-clip-panel.js +1 -1
- package/dist/dev/video-clip-panel.js.map +1 -1
- package/dist/dev/voice/chirp.d.ts +27 -0
- package/dist/dev/voice/chirp.d.ts.map +1 -0
- package/dist/dev/voice/chirp.js +107 -0
- package/dist/dev/voice/chirp.js.map +1 -0
- package/dist/dev/voice/folder.d.ts +18 -0
- package/dist/dev/voice/folder.d.ts.map +1 -0
- package/dist/dev/voice/folder.js +68 -0
- package/dist/dev/voice/folder.js.map +1 -0
- package/dist/dev/voice/voice-panel.d.ts +44 -0
- package/dist/dev/voice/voice-panel.d.ts.map +1 -0
- package/dist/dev/voice/voice-panel.js +428 -0
- package/dist/dev/voice/voice-panel.js.map +1 -0
- package/dist/dev/voice/voice-recorder.d.ts +141 -0
- package/dist/dev/voice/voice-recorder.d.ts.map +1 -0
- package/dist/dev/voice/voice-recorder.js +506 -0
- package/dist/dev/voice/voice-recorder.js.map +1 -0
- package/dist/src/showable.d.ts +9 -0
- package/dist/src/showable.d.ts.map +1 -1
- package/dist/src/showable.js.map +1 -1
- package/dist/src/snapshot.d.ts.map +1 -1
- package/dist/src/snapshot.js +27 -11
- package/dist/src/snapshot.js.map +1 -1
- package/package.json +1 -1
- package/src/showable.ts +9 -0
- package/src/snapshot.ts +30 -15
|
@@ -0,0 +1,523 @@
|
|
|
1
|
+
import { canWrite, chooseFolder, rememberedFolder } from "./folder.ts";
|
|
2
|
+
import {
|
|
3
|
+
DEFAULT_PROCESSING,
|
|
4
|
+
supportedProcessing,
|
|
5
|
+
VoiceRecorder,
|
|
6
|
+
type HeardResult,
|
|
7
|
+
type ProcessingChoices,
|
|
8
|
+
type ProcessingName,
|
|
9
|
+
} from "./voice-recorder.ts";
|
|
10
|
+
|
|
11
|
+
/**
|
|
12
|
+
* The voice recorder's controls: a level meter, the voice processing checkboxes, the folder, and
|
|
13
|
+
* โ Record. ๐ Record mode in the player shows and hides it. It brings its own styles.
|
|
14
|
+
*
|
|
15
|
+
* The panel owns the mic and the folder. The player owns everything about the video: โ Record calls
|
|
16
|
+
* {@link VoicePanelOptions.onRecord}, and the player says when recording starts and stops with
|
|
17
|
+
* {@link VoicePanel.setRecording}.
|
|
18
|
+
*
|
|
19
|
+
* Copied from riemann-surfaces-3d's src/recording/record-panel.ts (9bbe2f9), without the Marker,
|
|
20
|
+
* and in the player's left column instead of floating. See development-plans/voice-recording.md.
|
|
21
|
+
* @module
|
|
22
|
+
*/
|
|
23
|
+
|
|
24
|
+
export type VoicePanelOptions = {
|
|
25
|
+
/** The panel goes at the end of this element. */
|
|
26
|
+
readonly parent: HTMLElement;
|
|
27
|
+
/** The player's AudioContext, so the mic and the video share one clock. */
|
|
28
|
+
readonly context: AudioContext;
|
|
29
|
+
/** โ Record, with a folder we can write to. Start recording, and the video. */
|
|
30
|
+
readonly onRecord: (recorder: VoiceRecorder, folder: FileSystemDirectoryHandle) => Promise<void>;
|
|
31
|
+
/** โ Stop. */
|
|
32
|
+
readonly onStop: () => void;
|
|
33
|
+
/** Called after the panel closes, by ร or by {@link VoicePanel.close}. */
|
|
34
|
+
readonly onClose?: () => void;
|
|
35
|
+
};
|
|
36
|
+
|
|
37
|
+
export type VoicePanel = {
|
|
38
|
+
readonly recorder: VoiceRecorder;
|
|
39
|
+
/** The player started or stopped recording. โ Record becomes โ Stop, and back. */
|
|
40
|
+
setRecording(on: boolean): void;
|
|
41
|
+
/** How far past the end of the play limits the recording is, in ms. Undefined when it isn't. */
|
|
42
|
+
setPastEnd(ms: number | undefined): void;
|
|
43
|
+
/** A line in the status area. */
|
|
44
|
+
say(text: string, warning?: boolean): void;
|
|
45
|
+
/** Use this folder from now on, without asking. For tests. */
|
|
46
|
+
setFolder(folder: FileSystemDirectoryHandle): void;
|
|
47
|
+
/** Stop recording, turn the mic off, and remove the panel. */
|
|
48
|
+
close(): Promise<void>;
|
|
49
|
+
};
|
|
50
|
+
|
|
51
|
+
/** The meter shows โ60 to 0 dBFS. */
|
|
52
|
+
const METER_FLOOR = -60;
|
|
53
|
+
/** Where your voice's peaks should land. The usual target for speech. */
|
|
54
|
+
const VOICE_TARGET_DB = -12;
|
|
55
|
+
/** Peaks louder than this count as clipping. */
|
|
56
|
+
const CLIP_DB = -1;
|
|
57
|
+
/** The bar averages the level over about this long, in seconds. Meters usually do. */
|
|
58
|
+
const METER_SECONDS = 0.3;
|
|
59
|
+
const STORAGE = {
|
|
60
|
+
processing: "voice-recorder.processing",
|
|
61
|
+
chirpLevel: "voice-recorder.chirp-level",
|
|
62
|
+
};
|
|
63
|
+
|
|
64
|
+
const CSS = /* css */ `
|
|
65
|
+
.vr-panel {
|
|
66
|
+
margin-top: 0.5em;
|
|
67
|
+
padding: 10px 12px;
|
|
68
|
+
border-radius: 10px;
|
|
69
|
+
border: 1px solid rgba(160, 180, 230, 0.25);
|
|
70
|
+
background: rgb(14, 18, 32);
|
|
71
|
+
color: #e6ecfa;
|
|
72
|
+
font: 13px/1.35 system-ui, sans-serif;
|
|
73
|
+
font-variant-numeric: tabular-nums;
|
|
74
|
+
display: flex;
|
|
75
|
+
flex-direction: column;
|
|
76
|
+
gap: 7px;
|
|
77
|
+
}
|
|
78
|
+
.vr-panel.vr-recording {
|
|
79
|
+
border-color: #ff5a5f;
|
|
80
|
+
}
|
|
81
|
+
.vr-head {
|
|
82
|
+
display: flex;
|
|
83
|
+
align-items: center;
|
|
84
|
+
justify-content: space-between;
|
|
85
|
+
font-weight: 650;
|
|
86
|
+
}
|
|
87
|
+
.vr-panel button {
|
|
88
|
+
font: inherit;
|
|
89
|
+
color: inherit;
|
|
90
|
+
background: rgba(255, 255, 255, 0.08);
|
|
91
|
+
border: 1px solid rgba(160, 180, 230, 0.25);
|
|
92
|
+
border-radius: 6px;
|
|
93
|
+
padding: 4px 10px;
|
|
94
|
+
cursor: pointer;
|
|
95
|
+
}
|
|
96
|
+
.vr-panel button:hover {
|
|
97
|
+
background: rgba(255, 255, 255, 0.16);
|
|
98
|
+
}
|
|
99
|
+
.vr-panel button:disabled {
|
|
100
|
+
opacity: 0.5;
|
|
101
|
+
cursor: default;
|
|
102
|
+
}
|
|
103
|
+
.vr-close {
|
|
104
|
+
padding: 0 7px !important;
|
|
105
|
+
font-size: 16px !important;
|
|
106
|
+
}
|
|
107
|
+
.vr-meter {
|
|
108
|
+
display: flex;
|
|
109
|
+
align-items: center;
|
|
110
|
+
gap: 8px;
|
|
111
|
+
}
|
|
112
|
+
.vr-track {
|
|
113
|
+
position: relative;
|
|
114
|
+
flex: 1;
|
|
115
|
+
height: 14px;
|
|
116
|
+
border-radius: 3px;
|
|
117
|
+
background: linear-gradient(to right, #1f6f3d 0%, #1f6f3d 75%, #7a6a10 75%, #7a6a10 93%, #7a1f22 93%);
|
|
118
|
+
overflow: hidden;
|
|
119
|
+
}
|
|
120
|
+
.vr-level {
|
|
121
|
+
position: absolute;
|
|
122
|
+
inset: 0;
|
|
123
|
+
background: linear-gradient(to right, #3ddc84 0%, #3ddc84 75%, #f2d14c 75%, #f2d14c 93%, #ff5a5f 93%);
|
|
124
|
+
}
|
|
125
|
+
.vr-peak {
|
|
126
|
+
position: absolute;
|
|
127
|
+
top: 0;
|
|
128
|
+
bottom: 0;
|
|
129
|
+
width: 2px;
|
|
130
|
+
background: #fff;
|
|
131
|
+
}
|
|
132
|
+
.vr-mark {
|
|
133
|
+
position: absolute;
|
|
134
|
+
top: -2px;
|
|
135
|
+
bottom: -2px;
|
|
136
|
+
width: 0;
|
|
137
|
+
border-left: 2px dashed;
|
|
138
|
+
}
|
|
139
|
+
.vr-mark.vr-voice {
|
|
140
|
+
border-color: #7fd0ff;
|
|
141
|
+
}
|
|
142
|
+
.vr-mark.vr-sound {
|
|
143
|
+
border-color: #ff9df2;
|
|
144
|
+
}
|
|
145
|
+
.vr-db {
|
|
146
|
+
width: 62px;
|
|
147
|
+
text-align: right;
|
|
148
|
+
}
|
|
149
|
+
.vr-clipping .vr-db {
|
|
150
|
+
color: #ff5a5f;
|
|
151
|
+
font-weight: 700;
|
|
152
|
+
}
|
|
153
|
+
.vr-legend {
|
|
154
|
+
display: flex;
|
|
155
|
+
gap: 12px;
|
|
156
|
+
font-size: 11px;
|
|
157
|
+
color: #9aa7c4;
|
|
158
|
+
}
|
|
159
|
+
.vr-legend span::before {
|
|
160
|
+
content: "";
|
|
161
|
+
display: inline-block;
|
|
162
|
+
width: 10px;
|
|
163
|
+
margin-right: 4px;
|
|
164
|
+
vertical-align: middle;
|
|
165
|
+
border-top: 2px dashed;
|
|
166
|
+
}
|
|
167
|
+
.vr-legend .vr-voice::before {
|
|
168
|
+
border-color: #7fd0ff;
|
|
169
|
+
}
|
|
170
|
+
.vr-legend .vr-sound::before {
|
|
171
|
+
border-color: #ff9df2;
|
|
172
|
+
}
|
|
173
|
+
.vr-choice {
|
|
174
|
+
display: flex;
|
|
175
|
+
align-items: center;
|
|
176
|
+
gap: 6px;
|
|
177
|
+
cursor: pointer;
|
|
178
|
+
}
|
|
179
|
+
.vr-got {
|
|
180
|
+
margin-left: auto;
|
|
181
|
+
font-size: 11px;
|
|
182
|
+
color: #9aa7c4;
|
|
183
|
+
}
|
|
184
|
+
.vr-got.vr-mismatch {
|
|
185
|
+
color: #ff9a5f;
|
|
186
|
+
}
|
|
187
|
+
.vr-small {
|
|
188
|
+
font-size: 11px;
|
|
189
|
+
color: #9aa7c4;
|
|
190
|
+
overflow: hidden;
|
|
191
|
+
text-overflow: ellipsis;
|
|
192
|
+
white-space: nowrap;
|
|
193
|
+
}
|
|
194
|
+
.vr-buttons {
|
|
195
|
+
display: flex;
|
|
196
|
+
align-items: center;
|
|
197
|
+
gap: 8px;
|
|
198
|
+
}
|
|
199
|
+
.vr-record {
|
|
200
|
+
min-width: 92px;
|
|
201
|
+
}
|
|
202
|
+
.vr-recording .vr-record {
|
|
203
|
+
background: rgba(255, 90, 95, 0.3) !important;
|
|
204
|
+
border-color: #ff5a5f !important;
|
|
205
|
+
}
|
|
206
|
+
.vr-past-end {
|
|
207
|
+
color: #ff9a5f;
|
|
208
|
+
}
|
|
209
|
+
.vr-time {
|
|
210
|
+
margin-left: auto;
|
|
211
|
+
font-size: 15px;
|
|
212
|
+
}
|
|
213
|
+
.vr-recording .vr-time::before {
|
|
214
|
+
content: "โ ";
|
|
215
|
+
color: #ff5a5f;
|
|
216
|
+
}
|
|
217
|
+
.vr-status:empty {
|
|
218
|
+
display: none;
|
|
219
|
+
}
|
|
220
|
+
.vr-status.vr-warning {
|
|
221
|
+
color: #ff9a5f;
|
|
222
|
+
}
|
|
223
|
+
`;
|
|
224
|
+
|
|
225
|
+
function addStyles() {
|
|
226
|
+
if (!document.getElementById("vr-styles")) {
|
|
227
|
+
const style = document.createElement("style");
|
|
228
|
+
style.id = "vr-styles";
|
|
229
|
+
style.textContent = CSS;
|
|
230
|
+
document.head.append(style);
|
|
231
|
+
}
|
|
232
|
+
}
|
|
233
|
+
|
|
234
|
+
/** Make an element, with a class and some content. */
|
|
235
|
+
function element<K extends keyof HTMLElementTagNameMap>(
|
|
236
|
+
tag: K,
|
|
237
|
+
className = "",
|
|
238
|
+
...content: (Node | string)[]
|
|
239
|
+
) {
|
|
240
|
+
const result = document.createElement(tag);
|
|
241
|
+
result.className = className;
|
|
242
|
+
result.append(...content);
|
|
243
|
+
return result;
|
|
244
|
+
}
|
|
245
|
+
|
|
246
|
+
const toDb = (level: number) => 20 * Math.log10(Math.max(level, 1e-6));
|
|
247
|
+
/** Where on the meter, 0 to 1. */
|
|
248
|
+
const meterFraction = (db: number) =>
|
|
249
|
+
Math.min(1, Math.max(0, (db - METER_FLOOR) / -METER_FLOOR));
|
|
250
|
+
const formatDb = (db: number) =>
|
|
251
|
+
db <= METER_FLOOR ? "โโ dB" : `${db.toFixed(1).replace("-", "โ")} dB`;
|
|
252
|
+
|
|
253
|
+
function loadChoices(): ProcessingChoices {
|
|
254
|
+
try {
|
|
255
|
+
const saved = localStorage.getItem(STORAGE.processing);
|
|
256
|
+
return saved ? JSON.parse(saved) : { ...DEFAULT_PROCESSING };
|
|
257
|
+
} catch {
|
|
258
|
+
return { ...DEFAULT_PROCESSING };
|
|
259
|
+
}
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* Open the panel and turn on the mic. Call this from a click, so the browser lets audio start.
|
|
264
|
+
* The browser asks for permission to use the mic the first time.
|
|
265
|
+
*/
|
|
266
|
+
export async function openVoicePanel(options: VoicePanelOptions): Promise<VoicePanel> {
|
|
267
|
+
addStyles();
|
|
268
|
+
const panel = element("div", "vr-panel");
|
|
269
|
+
const close = element("button", "vr-close", "ร");
|
|
270
|
+
close.title = "Close record mode, and turn the mic off";
|
|
271
|
+
const level = element("div", "vr-level");
|
|
272
|
+
const peak = element("div", "vr-peak");
|
|
273
|
+
const voiceMark = element("div", "vr-mark vr-voice");
|
|
274
|
+
const soundMark = element("div", "vr-mark vr-sound");
|
|
275
|
+
const db = element("span", "vr-db", formatDb(-Infinity));
|
|
276
|
+
const meter = element(
|
|
277
|
+
"div",
|
|
278
|
+
"vr-meter",
|
|
279
|
+
element("div", "vr-track", level, peak, voiceMark, soundMark),
|
|
280
|
+
db,
|
|
281
|
+
);
|
|
282
|
+
const legend = element(
|
|
283
|
+
"div",
|
|
284
|
+
"vr-legend",
|
|
285
|
+
element("span", "vr-voice", "voice target"),
|
|
286
|
+
element("span", "vr-sound", "chirp, last time"),
|
|
287
|
+
);
|
|
288
|
+
const choicesBox = element("div");
|
|
289
|
+
const micLine = element("div", "vr-small", "Turning on the micโฆ");
|
|
290
|
+
const folderName = element("b", "", "โฆ");
|
|
291
|
+
const changeFolder = element("button", "", "Changeโฆ");
|
|
292
|
+
changeFolder.style.padding = "0 6px";
|
|
293
|
+
const folderLine = element("div", "vr-small", "Folder: ", folderName, " ", changeFolder);
|
|
294
|
+
const record = element("button", "vr-record", "โ Record");
|
|
295
|
+
record.title =
|
|
296
|
+
"Play the chirp, then record your voice while the video plays. Pause the video to stop. " +
|
|
297
|
+
"Each seek starts a new sound clip.";
|
|
298
|
+
const pastEnd = element("span", "vr-past-end");
|
|
299
|
+
const time = element("span", "vr-time", "0:00");
|
|
300
|
+
const status = element("div", "vr-status");
|
|
301
|
+
panel.append(
|
|
302
|
+
element("div", "vr-head", element("span", "", "๐ Record mode"), close),
|
|
303
|
+
meter,
|
|
304
|
+
legend,
|
|
305
|
+
choicesBox,
|
|
306
|
+
micLine,
|
|
307
|
+
folderLine,
|
|
308
|
+
element("div", "vr-buttons", record, pastEnd, time),
|
|
309
|
+
status,
|
|
310
|
+
);
|
|
311
|
+
options.parent.append(panel);
|
|
312
|
+
|
|
313
|
+
const say = (text: string, warning = false) => {
|
|
314
|
+
status.textContent = text;
|
|
315
|
+
status.classList.toggle("vr-warning", warning);
|
|
316
|
+
};
|
|
317
|
+
const placeMark = (mark: HTMLElement, value: number | undefined) => {
|
|
318
|
+
mark.hidden = value === undefined;
|
|
319
|
+
mark.style.left = `${meterFraction(value ?? 0) * 100}%`;
|
|
320
|
+
};
|
|
321
|
+
placeMark(voiceMark, VOICE_TARGET_DB);
|
|
322
|
+
const savedChirpLevel = Number(localStorage.getItem(STORAGE.chirpLevel));
|
|
323
|
+
placeMark(
|
|
324
|
+
soundMark,
|
|
325
|
+
Number.isFinite(savedChirpLevel) && savedChirpLevel < 0 ? savedChirpLevel : undefined,
|
|
326
|
+
);
|
|
327
|
+
const closed = () => {
|
|
328
|
+
panel.remove();
|
|
329
|
+
options.onClose?.();
|
|
330
|
+
};
|
|
331
|
+
|
|
332
|
+
// The folder. Independent of the mic, so it shows even if the mic is refused.
|
|
333
|
+
// Not awaited: the mic should start while this click still counts as a click.
|
|
334
|
+
let folder: FileSystemDirectoryHandle | undefined;
|
|
335
|
+
const showFolder = () => {
|
|
336
|
+
folderName.textContent = folder ? folder.name || "browser storage" : "not chosen yet";
|
|
337
|
+
};
|
|
338
|
+
void rememberedFolder().then((remembered) => {
|
|
339
|
+
// ??=, in case Changeโฆ or setFolder() got here first.
|
|
340
|
+
folder ??= remembered;
|
|
341
|
+
showFolder();
|
|
342
|
+
});
|
|
343
|
+
changeFolder.addEventListener("click", async () => {
|
|
344
|
+
changeFolder.blur();
|
|
345
|
+
try {
|
|
346
|
+
folder = (await chooseFolder()) ?? folder;
|
|
347
|
+
showFolder();
|
|
348
|
+
} catch (error) {
|
|
349
|
+
say(error instanceof Error ? error.message : String(error), true);
|
|
350
|
+
}
|
|
351
|
+
});
|
|
352
|
+
|
|
353
|
+
let recorder: VoiceRecorder;
|
|
354
|
+
try {
|
|
355
|
+
recorder = await VoiceRecorder.open(options.context, loadChoices());
|
|
356
|
+
} catch (error) {
|
|
357
|
+
micLine.textContent = "No mic.";
|
|
358
|
+
say(`Couldn't turn on the mic: ${error instanceof Error ? error.message : error}`, true);
|
|
359
|
+
// Leave the panel up, so you can read why.
|
|
360
|
+
close.addEventListener("click", closed);
|
|
361
|
+
throw error;
|
|
362
|
+
}
|
|
363
|
+
|
|
364
|
+
// The level meter. The bar is the average level, and the line is the peak, held for a moment.
|
|
365
|
+
let held = -Infinity;
|
|
366
|
+
let heldAt = 0;
|
|
367
|
+
let lastLevel = performance.now();
|
|
368
|
+
let clippedAt = -Infinity;
|
|
369
|
+
let meanSquare = 0;
|
|
370
|
+
recorder.onLevel = (peakLevel, rms) => {
|
|
371
|
+
const now = performance.now();
|
|
372
|
+
const seconds = (now - lastLevel) / 1000;
|
|
373
|
+
lastLevel = now;
|
|
374
|
+
meanSquare += (rms * rms - meanSquare) * (1 - Math.exp(-seconds / METER_SECONDS));
|
|
375
|
+
const peakDb = toDb(peakLevel);
|
|
376
|
+
if (peakDb >= held) {
|
|
377
|
+
held = peakDb;
|
|
378
|
+
heldAt = now;
|
|
379
|
+
} else if (now - heldAt > 1000) {
|
|
380
|
+
// Hold the peak for a second, then let it fall 20 dB a second.
|
|
381
|
+
held = Math.max(peakDb, held - 20 * seconds);
|
|
382
|
+
}
|
|
383
|
+
if (peakDb >= CLIP_DB) {
|
|
384
|
+
clippedAt = now;
|
|
385
|
+
}
|
|
386
|
+
level.style.clipPath = `inset(0 ${(1 - meterFraction(toDb(Math.sqrt(meanSquare)))) * 100}% 0 0)`;
|
|
387
|
+
peak.style.left = `calc(${meterFraction(held) * 100}% - 1px)`;
|
|
388
|
+
db.textContent = formatDb(held);
|
|
389
|
+
panel.classList.toggle("vr-clipping", now - clippedAt < 2000);
|
|
390
|
+
};
|
|
391
|
+
|
|
392
|
+
// The voice processing checkboxes, and what the mic actually got.
|
|
393
|
+
const gotLabels = new Map<ProcessingName, HTMLElement>();
|
|
394
|
+
const showSettings = () => {
|
|
395
|
+
const actual = recorder.actualSettings();
|
|
396
|
+
const requested = loadChoices();
|
|
397
|
+
for (const [name, got] of gotLabels) {
|
|
398
|
+
const value = actual[name];
|
|
399
|
+
got.textContent = value === undefined ? "got: ?" : `got: ${value ? "on" : "off"}`;
|
|
400
|
+
got.classList.toggle(
|
|
401
|
+
"vr-mismatch",
|
|
402
|
+
value !== undefined && value !== (requested[name] ?? false),
|
|
403
|
+
);
|
|
404
|
+
}
|
|
405
|
+
const rate = actual.sampleRate ? ` ยท ${Number(actual.sampleRate) / 1000} kHz` : "";
|
|
406
|
+
micLine.textContent = `Mic: ${recorder.micName || "unknown"}${rate}`;
|
|
407
|
+
};
|
|
408
|
+
for (const { name, label, help } of supportedProcessing()) {
|
|
409
|
+
const box = element("input");
|
|
410
|
+
box.type = "checkbox";
|
|
411
|
+
box.checked = loadChoices()[name] ?? false;
|
|
412
|
+
box.setAttribute("aria-label", label);
|
|
413
|
+
const got = element("span", "vr-got");
|
|
414
|
+
gotLabels.set(name, got);
|
|
415
|
+
const row = element("label", "vr-choice", box, label, got);
|
|
416
|
+
row.title = help;
|
|
417
|
+
// "change", not "input": each one reopens the mic, so once per click is right.
|
|
418
|
+
box.addEventListener("change", async () => {
|
|
419
|
+
box.blur();
|
|
420
|
+
localStorage.setItem(
|
|
421
|
+
STORAGE.processing,
|
|
422
|
+
JSON.stringify({ ...loadChoices(), [name]: box.checked }),
|
|
423
|
+
);
|
|
424
|
+
box.disabled = true;
|
|
425
|
+
try {
|
|
426
|
+
await recorder.setProcessing(name, box.checked);
|
|
427
|
+
} catch (error) {
|
|
428
|
+
say(
|
|
429
|
+
`Couldn't change ${label.toLowerCase()}: ${error instanceof Error ? error.message : error}`,
|
|
430
|
+
true,
|
|
431
|
+
);
|
|
432
|
+
}
|
|
433
|
+
box.disabled = false;
|
|
434
|
+
showSettings();
|
|
435
|
+
});
|
|
436
|
+
choicesBox.append(row);
|
|
437
|
+
}
|
|
438
|
+
showSettings();
|
|
439
|
+
|
|
440
|
+
// What we heard of the chirp.
|
|
441
|
+
recorder.onHeard = (result: HeardResult) => {
|
|
442
|
+
if (result.roundTripMs === undefined) {
|
|
443
|
+
say(
|
|
444
|
+
`Didn't hear the chirp. Are the speakers off, or are you using headphones? ` +
|
|
445
|
+
`Using the last delay measured, ${recorder.micDelayMs.toFixed(0)} ms.`,
|
|
446
|
+
true,
|
|
447
|
+
);
|
|
448
|
+
return;
|
|
449
|
+
}
|
|
450
|
+
localStorage.setItem(STORAGE.chirpLevel, String(result.peakDb));
|
|
451
|
+
placeMark(soundMark, result.peakDb);
|
|
452
|
+
say(
|
|
453
|
+
`Heard the chirp. Round trip ${result.roundTripMs.toFixed(1)} ms, at ${formatDb(result.peakDb)}. ` +
|
|
454
|
+
`Mic delay ${recorder.micDelayMs.toFixed(0)} ms.`,
|
|
455
|
+
);
|
|
456
|
+
};
|
|
457
|
+
|
|
458
|
+
// โ Record and โ Stop.
|
|
459
|
+
let started = 0;
|
|
460
|
+
let clock = 0;
|
|
461
|
+
const showTime = () => {
|
|
462
|
+
const seconds = Math.floor((performance.now() - started) / 1000);
|
|
463
|
+
time.textContent = `${Math.floor(seconds / 60)}:${String(seconds % 60).padStart(2, "0")}`;
|
|
464
|
+
};
|
|
465
|
+
/** What the button says: โ Stop. That's what a click does, even if the two ever disagree. */
|
|
466
|
+
let showingStop = false;
|
|
467
|
+
record.addEventListener("click", async () => {
|
|
468
|
+
record.blur();
|
|
469
|
+
if (showingStop) {
|
|
470
|
+
options.onStop();
|
|
471
|
+
return;
|
|
472
|
+
}
|
|
473
|
+
record.disabled = true;
|
|
474
|
+
try {
|
|
475
|
+
if (!folder || !(await canWrite(folder, true))) {
|
|
476
|
+
folder = await chooseFolder();
|
|
477
|
+
showFolder();
|
|
478
|
+
}
|
|
479
|
+
if (!folder) {
|
|
480
|
+
say("Choose a folder for the recordings first.", true);
|
|
481
|
+
return;
|
|
482
|
+
}
|
|
483
|
+
say("");
|
|
484
|
+
await options.onRecord(recorder, folder);
|
|
485
|
+
} catch (error) {
|
|
486
|
+
say(error instanceof Error ? error.message : String(error), true);
|
|
487
|
+
} finally {
|
|
488
|
+
record.disabled = false;
|
|
489
|
+
}
|
|
490
|
+
});
|
|
491
|
+
|
|
492
|
+
const result: VoicePanel = {
|
|
493
|
+
recorder,
|
|
494
|
+
setRecording(on) {
|
|
495
|
+
showingStop = on;
|
|
496
|
+
panel.classList.toggle("vr-recording", on);
|
|
497
|
+
record.textContent = on ? "โ Stop" : "โ Record";
|
|
498
|
+
window.clearInterval(clock);
|
|
499
|
+
if (on) {
|
|
500
|
+
started = performance.now();
|
|
501
|
+
clock = window.setInterval(showTime, 250);
|
|
502
|
+
showTime();
|
|
503
|
+
} else {
|
|
504
|
+
pastEnd.textContent = "";
|
|
505
|
+
}
|
|
506
|
+
},
|
|
507
|
+
setPastEnd(ms) {
|
|
508
|
+
pastEnd.textContent = ms === undefined ? "" : `+${(ms / 1000).toFixed(1)} s past the end`;
|
|
509
|
+
},
|
|
510
|
+
say,
|
|
511
|
+
setFolder(newFolder) {
|
|
512
|
+
folder = newFolder;
|
|
513
|
+
showFolder();
|
|
514
|
+
},
|
|
515
|
+
async close() {
|
|
516
|
+
window.clearInterval(clock);
|
|
517
|
+
await recorder.close();
|
|
518
|
+
closed();
|
|
519
|
+
},
|
|
520
|
+
};
|
|
521
|
+
close.addEventListener("click", () => void result.close());
|
|
522
|
+
return result;
|
|
523
|
+
}
|