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,129 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The chirp that starts a voice recording, and the code that finds it in the recording.
|
|
3
|
+
*
|
|
4
|
+
* The chirp is a sweep: a tone that slides smoothly from one pitch to another. Lining a sweep up
|
|
5
|
+
* with its recording only works at one exact position, so it can be found to a small fraction of a
|
|
6
|
+
* millisecond, even with a train going by. A plain beep's start is fuzzy.
|
|
7
|
+
*
|
|
8
|
+
* Copied from riemann-surfaces-3d's src/recording/calibration-sounds.ts (9bbe2f9), with only the
|
|
9
|
+
* Start sound, and quieter. See development-plans/voice-recording.md.
|
|
10
|
+
* @module
|
|
11
|
+
*/
|
|
12
|
+
|
|
13
|
+
/** One sweep, from one frequency to another, in Hz. */
|
|
14
|
+
type Sweep = { from: number; to: number; seconds: number };
|
|
15
|
+
|
|
16
|
+
/** Riemann's Start sound: a rising sweep. */
|
|
17
|
+
const CHIRP: Sweep = { from: 400, to: 4000, seconds: 0.2 };
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* About −28 dBFS. Riemann's was 0.3, about −10 dBFS, and it reached a MacBook Air's mic at almost 5
|
|
21
|
+
* times full scale. It was found easily (matches 0.37–0.75), so 17 dB quieter should still work.
|
|
22
|
+
*/
|
|
23
|
+
const AMPLITUDE = 0.04;
|
|
24
|
+
/** Fade in and out over this long, so there are no clicks. */
|
|
25
|
+
const FADE_SECONDS = 0.006;
|
|
26
|
+
|
|
27
|
+
/** The chirp's samples, at this sample rate. */
|
|
28
|
+
export function chirpSamples(sampleRate: number): Float32Array<ArrayBuffer> {
|
|
29
|
+
return sweepSamples(CHIRP, sampleRate);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/** An exponential sweep: every octave gets the same amount of time, so it sounds even. */
|
|
33
|
+
function sweepSamples({ from, to, seconds }: Sweep, sampleRate: number) {
|
|
34
|
+
const count = Math.round(seconds * sampleRate);
|
|
35
|
+
const samples = new Float32Array(count);
|
|
36
|
+
const k = Math.log(to / from);
|
|
37
|
+
for (let i = 0; i < count; i++) {
|
|
38
|
+
const t = i / sampleRate;
|
|
39
|
+
const phase = (2 * Math.PI * from * seconds * (Math.exp((t * k) / seconds) - 1)) / k;
|
|
40
|
+
samples[i] = AMPLITUDE * Math.sin(phase);
|
|
41
|
+
}
|
|
42
|
+
const fade = Math.min(count / 2, Math.round(FADE_SECONDS * sampleRate));
|
|
43
|
+
for (let i = 0; i < fade; i++) {
|
|
44
|
+
const gain = 0.5 - 0.5 * Math.cos((Math.PI * i) / fade);
|
|
45
|
+
samples[i] *= gain;
|
|
46
|
+
samples[count - 1 - i] *= gain;
|
|
47
|
+
}
|
|
48
|
+
return samples;
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export type Found = {
|
|
52
|
+
/** Where the sound starts in the recording, in samples. Not a whole number: it's interpolated. */
|
|
53
|
+
position: number;
|
|
54
|
+
/** How well it matched, 0 to 1. Above about 0.1 means it really is there. Noise alone scores about 0.03. */
|
|
55
|
+
match: number;
|
|
56
|
+
/** The loudest sample in the match, as dBFS. */
|
|
57
|
+
peakDb: number;
|
|
58
|
+
};
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* Find a sound in a recording, starting somewhere from `from` to `to` (sample indexes). It searches
|
|
62
|
+
* coarsely first, at a quarter of the sample rate, then exactly around the best spot.
|
|
63
|
+
*/
|
|
64
|
+
export function findSound(
|
|
65
|
+
recording: Float32Array,
|
|
66
|
+
sound: Float32Array,
|
|
67
|
+
from: number,
|
|
68
|
+
to: number,
|
|
69
|
+
): Found {
|
|
70
|
+
const first = Math.max(0, Math.floor(from));
|
|
71
|
+
const last = Math.min(recording.length - sound.length, Math.ceil(to));
|
|
72
|
+
if (last < first) {
|
|
73
|
+
return { position: NaN, match: 0, peakDb: -Infinity };
|
|
74
|
+
}
|
|
75
|
+
const soundEnergy = energy(sound, 0, sound.length, 1);
|
|
76
|
+
const score = (lag: number, step: number) => {
|
|
77
|
+
let sum = 0;
|
|
78
|
+
for (let i = 0; i < sound.length; i += step) {
|
|
79
|
+
sum += sound[i] * recording[lag + i];
|
|
80
|
+
}
|
|
81
|
+
const norm = Math.sqrt(energy(recording, lag, sound.length, step) * (soundEnergy / step));
|
|
82
|
+
return norm > 0 ? sum / norm : 0;
|
|
83
|
+
};
|
|
84
|
+
// Coarse: every 4th lag, every 4th sample.
|
|
85
|
+
let best = first;
|
|
86
|
+
let bestScore = -Infinity;
|
|
87
|
+
for (let lag = first; lag <= last; lag += 4) {
|
|
88
|
+
const s = score(lag, 4);
|
|
89
|
+
if (s > bestScore) {
|
|
90
|
+
bestScore = s;
|
|
91
|
+
best = lag;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
// Exact: every lag near the best one, every sample.
|
|
95
|
+
const exact = new Map<number, number>();
|
|
96
|
+
for (let lag = Math.max(first, best - 6); lag <= Math.min(last, best + 6); lag++) {
|
|
97
|
+
exact.set(lag, score(lag, 1));
|
|
98
|
+
}
|
|
99
|
+
for (const [lag, s] of exact) {
|
|
100
|
+
if (s > (exact.get(best) ?? -Infinity)) {
|
|
101
|
+
best = lag;
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
// Between samples: fit a parabola through the best score and its neighbors.
|
|
105
|
+
const here = exact.get(best) ?? 0;
|
|
106
|
+
const before = exact.get(best - 1);
|
|
107
|
+
const after = exact.get(best + 1);
|
|
108
|
+
let position = best;
|
|
109
|
+
if (before !== undefined && after !== undefined) {
|
|
110
|
+
const curve = before - 2 * here + after;
|
|
111
|
+
if (curve < 0) {
|
|
112
|
+
position += (0.5 * (before - after)) / curve;
|
|
113
|
+
}
|
|
114
|
+
}
|
|
115
|
+
let peak = 0;
|
|
116
|
+
for (let i = best; i < best + sound.length; i++) {
|
|
117
|
+
peak = Math.max(peak, Math.abs(recording[i]));
|
|
118
|
+
}
|
|
119
|
+
return { position, match: Math.max(0, here), peakDb: 20 * Math.log10(peak || 1e-12) };
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
function energy(samples: Float32Array, start: number, length: number, step: number) {
|
|
123
|
+
let sum = 0;
|
|
124
|
+
for (let i = 0; i < length; i += step) {
|
|
125
|
+
const v = samples[start + i];
|
|
126
|
+
sum += v * v;
|
|
127
|
+
}
|
|
128
|
+
return sum;
|
|
129
|
+
}
|
|
@@ -0,0 +1,84 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The folder where voice recordings are saved, remembered between visits. The browser can keep the
|
|
3
|
+
* folder's handle in IndexedDB. In each new session it asks for permission again, and that has to
|
|
4
|
+
* happen during a click.
|
|
5
|
+
*
|
|
6
|
+
* Copied from riemann-surfaces-3d's src/recording/folder.ts (9bbe2f9), with its own database name.
|
|
7
|
+
* See development-plans/voice-recording.md.
|
|
8
|
+
* @module
|
|
9
|
+
*/
|
|
10
|
+
|
|
11
|
+
/** Permission methods that Chrome has but TypeScript's DOM types don't include yet. */
|
|
12
|
+
type WithPermission = FileSystemDirectoryHandle & {
|
|
13
|
+
queryPermission?(options: { mode: "readwrite" }): Promise<PermissionState>;
|
|
14
|
+
requestPermission?(options: { mode: "readwrite" }): Promise<PermissionState>;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const DATABASE = "canvas-recorder-voice";
|
|
18
|
+
const STORE = "folders";
|
|
19
|
+
const KEY = "takes";
|
|
20
|
+
|
|
21
|
+
function inStore<T>(
|
|
22
|
+
mode: IDBTransactionMode,
|
|
23
|
+
run: (store: IDBObjectStore) => IDBRequest<T>,
|
|
24
|
+
): Promise<T> {
|
|
25
|
+
return new Promise((resolve, reject) => {
|
|
26
|
+
const open = indexedDB.open(DATABASE, 1);
|
|
27
|
+
open.onupgradeneeded = () => open.result.createObjectStore(STORE);
|
|
28
|
+
open.onerror = () => reject(open.error);
|
|
29
|
+
open.onsuccess = () => {
|
|
30
|
+
const request = run(open.result.transaction(STORE, mode).objectStore(STORE));
|
|
31
|
+
request.onsuccess = () => resolve(request.result);
|
|
32
|
+
request.onerror = () => reject(request.error);
|
|
33
|
+
};
|
|
34
|
+
});
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/** The folder chosen last time, if there is one. */
|
|
38
|
+
export async function rememberedFolder(): Promise<FileSystemDirectoryHandle | undefined> {
|
|
39
|
+
try {
|
|
40
|
+
return await inStore("readonly", (store) => store.get(KEY));
|
|
41
|
+
} catch {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
}
|
|
45
|
+
|
|
46
|
+
/** Save recordings here from now on. */
|
|
47
|
+
export async function rememberFolder(folder: FileSystemDirectoryHandle): Promise<void> {
|
|
48
|
+
await inStore("readwrite", (store) => store.put(folder, KEY));
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
/** Ask the user for a folder. Undefined if they cancel. Call this from a click. */
|
|
52
|
+
export async function chooseFolder(): Promise<FileSystemDirectoryHandle | undefined> {
|
|
53
|
+
const picker = (
|
|
54
|
+
window as unknown as {
|
|
55
|
+
showDirectoryPicker?: (options: object) => Promise<FileSystemDirectoryHandle>;
|
|
56
|
+
}
|
|
57
|
+
).showDirectoryPicker;
|
|
58
|
+
if (!picker) {
|
|
59
|
+
throw new Error("This browser can't save into a folder. Try Chrome.");
|
|
60
|
+
}
|
|
61
|
+
try {
|
|
62
|
+
const folder = await picker({ id: "voice-recordings", mode: "readwrite" });
|
|
63
|
+
await rememberFolder(folder);
|
|
64
|
+
return folder;
|
|
65
|
+
} catch (error) {
|
|
66
|
+
if (error instanceof DOMException && error.name == "AbortError") {
|
|
67
|
+
return undefined;
|
|
68
|
+
}
|
|
69
|
+
throw error;
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/** Can we write to this folder? If ask is true, ask for permission if we need it. That needs a click. */
|
|
74
|
+
export async function canWrite(folder: FileSystemDirectoryHandle, ask: boolean): Promise<boolean> {
|
|
75
|
+
const handle = folder as WithPermission;
|
|
76
|
+
// Folders that don't need permission, like the browser's private storage, don't have these.
|
|
77
|
+
if (!handle.queryPermission || !handle.requestPermission) {
|
|
78
|
+
return true;
|
|
79
|
+
}
|
|
80
|
+
if ((await handle.queryPermission({ mode: "readwrite" })) == "granted") {
|
|
81
|
+
return true;
|
|
82
|
+
}
|
|
83
|
+
return ask && (await handle.requestPermission({ mode: "readwrite" })) == "granted";
|
|
84
|
+
}
|