canvas-recorder-lib 0.6.0 → 0.7.0
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 +333 -167
- package/dev/player-body.ts +8 -0
- package/dev/sound-clips.ts +3 -2
- package/dev/soundtrack.ts +434 -0
- 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 +513 -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 +320 -154
- 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 +369 -0
- package/dist/dev/soundtrack.js.map +1 -0
- 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 +418 -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
package/BREAKING-CHANGES.md
CHANGED
|
@@ -6,6 +6,12 @@ This is not a full list of changes: new features and fixes aren't here.
|
|
|
6
6
|
While the version is 0.x, every breaking change comes with a new minor version (0.5 → 0.6), and nothing else does.
|
|
7
7
|
A project that depends on `"^0.5.0"` gets 0.5.x updates but never 0.6.0, so it only meets these changes when someone installs the new version on purpose.
|
|
8
8
|
|
|
9
|
+
## 0.7.0 (2026-11-07)
|
|
10
|
+
|
|
11
|
+
- **`VisualEditorAPI.getDecodedBuffer()` returns a mono buffer,** at 48 kHz, for every file.
|
|
12
|
+
It used to return the file's own channels, so a stereo file's channel 0 was its left channel; now it's the average of both.
|
|
13
|
+
A waveform drawn from channel 0 still works, and shows the whole sound. Code that reads channel 1 should read channel 0.
|
|
14
|
+
|
|
9
15
|
## 0.6.0 (2026-11-05)
|
|
10
16
|
|
|
11
17
|
- **`CANVAS_WIDTH_UNITS`, `CANVAS_HEIGHT_UNITS` and `PIXELS_PER_UNIT` are gone** from `src/slide-components/video-info`.
|
package/README.md
CHANGED
|
@@ -4,17 +4,17 @@ Preview, edit, annotate and record HTML canvas animations.
|
|
|
4
4
|
This is the tooling from [canvas-recorder](https://github.com/TradeIdeasPhilip/canvas-recorder), packaged as a library, so each video can be its own small project.
|
|
5
5
|
|
|
6
6
|
**Status:** in use for published videos.
|
|
7
|
-
It's still growing:
|
|
7
|
+
It's still growing: while the version is 0.x, a change that could break your project comes with a new minor version (0.5 → 0.6), and [BREAKING-CHANGES.md](BREAKING-CHANGES.md) says what changed and how to update.
|
|
8
8
|
|
|
9
9
|
**Source:** https://github.com/TradeIdeasPhilip/canvas-recorder-lib
|
|
10
10
|
|
|
11
11
|
**API docs:** https://tradeideasphilip.github.io/canvas-recorder-lib/
|
|
12
12
|
|
|
13
|
-
**Try it:** the [Simple Video Editor](https://tradeideasphilip.github.io/canvas-recorder-lib/simple-video-editor/) is this library's player and Visual Editor with an empty project:
|
|
13
|
+
**Try it:** the [Simple Video Editor](https://tradeideasphilip.github.io/canvas-recorder-lib/simple-video-editor/) is this library's player and Visual Editor with an empty project: open video clips from your computer, add text and callouts, and save an MP4.
|
|
14
14
|
Its source is in [simple-video-editor/](https://github.com/TradeIdeasPhilip/canvas-recorder-lib/tree/master/simple-video-editor).
|
|
15
15
|
|
|
16
16
|
**Showcase:** the [showcase](https://tradeideasphilip.github.io/canvas-recorder-lib/tests/showcase.html) is a series of slides that show what the library can do, one topic per slide.
|
|
17
|
-
Looking for code or ideas to copy?
|
|
17
|
+
Looking for code or ideas to copy? Start there.
|
|
18
18
|
Its source is in [test-project/src/showcase/](https://github.com/TradeIdeasPhilip/canvas-recorder-lib/tree/master/test-project/src/showcase), one file per slide.
|
|
19
19
|
|
|
20
20
|
## Use
|
|
@@ -51,6 +51,20 @@ Imports:
|
|
|
51
51
|
|
|
52
52
|
The package includes the TypeScript source next to the compiled JavaScript, and Go to Definition takes you to the source.
|
|
53
53
|
|
|
54
|
+
## Quick Examples to Copy
|
|
55
|
+
|
|
56
|
+
If you want to create a new video you need to start by creating a new project.
|
|
57
|
+
At the time of this writing the [7 pointed stars](https://github.com/TradeIdeasPhilip/7-pointed-star) video is the most up to date example.
|
|
58
|
+
You can start a new project using [degit](https://github.com/Rich-Harris/degit) to copy this.
|
|
59
|
+
|
|
60
|
+
Note: The embedded test projects show how to write code that uses this library.
|
|
61
|
+
But don't try to copy the entire project.
|
|
62
|
+
Files like .gitignore and package.json will be different for embedded projects like these and stand alone projects like yours.
|
|
63
|
+
This refers to simple-video-editor and showcase found in this repository.
|
|
64
|
+
|
|
65
|
+
Note: [Riemann](https://github.com/TradeIdeasPhilip/riemann-surfaces-3d) provides a different type of example.
|
|
66
|
+
This shows how to attach this library to a project that was not specifically developed for this library.
|
|
67
|
+
|
|
54
68
|
## Notes for a project that uses this library
|
|
55
69
|
|
|
56
70
|
Things the first project to use this library ([riemann-surfaces-3d](https://github.com/TradeIdeasPhilip/riemann-surfaces-3d)) ran into.
|
|
@@ -70,7 +84,7 @@ npm install-scripts ls # should say "No packages with unreviewed install scr
|
|
|
70
84
|
Both come from `@huggingface/transformers`, which the library uses for transcription, and neither is needed in a browser.
|
|
71
85
|
`deny` records that in your `package.json`'s `allowScripts`.
|
|
72
86
|
- That field replaces any `allow-scripts` setting in your `~/.npmrc`.
|
|
73
|
-
If your `~/.npmrc` approves `fsevents` (Vite's file watcher on macOS), approve it again here:
|
|
87
|
+
If your `~/.npmrc` approves `fsevents` (Vite's file watcher on macOS), approve it again here: `npm install-scripts approve fsevents --no-allow-scripts-pin`.
|
|
74
88
|
In a new project that fails with `ENOMATCH`, because the first `npm install` doesn't put `fsevents` in `node_modules`.
|
|
75
89
|
Run `npm install` again (it warns that `fsevents` isn't approved), then approve it.
|
|
76
90
|
- **Expect a big `package-lock.json`.** The library's dependencies are installed in your project, including some that only Node.js uses.
|
|
@@ -118,7 +132,7 @@ export default defineConfig({
|
|
|
118
132
|
```
|
|
119
133
|
|
|
120
134
|
- **`base: "./"`:** relative paths, so the build works on GitHub Pages, or in any subdirectory.
|
|
121
|
-
- **`dedupe`:** one copy of phil-lib.
|
|
135
|
+
- **`dedupe`:** one copy of phil-lib. With the library linked, Vite would otherwise load two: yours, and the one in the library's `node_modules`.
|
|
122
136
|
- **`fs.allow`:** needed while the library is linked.
|
|
123
137
|
Without it, the server console says the transcription worker is "outside of Vite serving allow list", and transcripts fail.
|
|
124
138
|
A linked library lives outside your project. Vite serves files that are imported, but the worker is loaded by URL.
|
|
@@ -137,14 +151,14 @@ Each project that uses this library gets its own port, forever.
|
|
|
137
151
|
Changing the port loses the editor's saved work, because IndexedDB belongs to the origin, and the port is part of the origin.
|
|
138
152
|
For a new project, take the next number and add it here.
|
|
139
153
|
|
|
140
|
-
| Port | Project
|
|
141
|
-
|
|
|
142
|
-
| 5173 | [canvas-recorder](https://github.com/TradeIdeasPhilip/canvas-recorder) (Vite's default; historical)
|
|
143
|
-
| 5174 | [riemann-surfaces-3d](https://github.com/TradeIdeasPhilip/riemann-surfaces-3d)
|
|
144
|
-
| 5175 | [affine-transforms](https://github.com/TradeIdeasPhilip/affine-transforms)
|
|
145
|
-
| 5176 | [`test-project/`](https://github.com/TradeIdeasPhilip/canvas-recorder-lib/tree/master/test-project) in this repo:
|
|
146
|
-
| 5177 | [`simple-video-editor/`](https://github.com/TradeIdeasPhilip/canvas-recorder-lib/tree/master/simple-video-editor) in this repo
|
|
147
|
-
| 5178 | 7-pointed-star
|
|
154
|
+
| Port | Project |
|
|
155
|
+
| ---- | ----------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
156
|
+
| 5173 | [canvas-recorder](https://github.com/TradeIdeasPhilip/canvas-recorder) (Vite's default; historical) |
|
|
157
|
+
| 5174 | [riemann-surfaces-3d](https://github.com/TradeIdeasPhilip/riemann-surfaces-3d) |
|
|
158
|
+
| 5175 | [affine-transforms](https://github.com/TradeIdeasPhilip/affine-transforms) |
|
|
159
|
+
| 5176 | [`test-project/`](https://github.com/TradeIdeasPhilip/canvas-recorder-lib/tree/master/test-project) in this repo: the showcase, tests and tools |
|
|
160
|
+
| 5177 | [`simple-video-editor/`](https://github.com/TradeIdeasPhilip/canvas-recorder-lib/tree/master/simple-video-editor) in this repo |
|
|
161
|
+
| 5178 | 7-pointed-star |
|
|
148
162
|
|
|
149
163
|
### Starting the editor
|
|
150
164
|
|
|
@@ -157,7 +171,7 @@ For a new project, take the next number and add it here.
|
|
|
157
171
|
- **Put sound files in your `public/` folder** and refer to them as `"./name.m4a"`.
|
|
158
172
|
- **Or use files on your computer.** The Video File and Image File panels have **📂 Open file…**, which stores a URL like `local:Screen Recording.mov`.
|
|
159
173
|
The browser remembers the file, but only in this browser on this computer.
|
|
160
|
-
After a reload it may want your OK again:
|
|
174
|
+
After a reload it may want your OK again: click **🔓 Allow local files**.
|
|
161
175
|
The Save dialog checks every local file before it starts, so a long recording can't stop halfway to ask.
|
|
162
176
|
In your own code, read media with `fetchMedia()`, which understands `local:` URLs.
|
|
163
177
|
- **Save the editor's state into your project, so it's in git.** The editor always saves inside the browser (IndexedDB), which git can't see.
|
|
@@ -167,9 +181,9 @@ For a new project, take the next number and add it here.
|
|
|
167
181
|
- **Register your own components** with `componentRegistry.set(key, { create })`.
|
|
168
182
|
Then the Visual Editor can add, save and load them, and "TypeScript defaults" can rebuild them.
|
|
169
183
|
- **A mistake in the Visual Editor is a state, not an error.** Your components should treat it that way:
|
|
170
|
-
- Show a warning, in the editor or on the canvas.
|
|
171
|
-
- Save it like any other state.
|
|
172
|
-
- Draw as much as still makes sense.
|
|
184
|
+
- Show a warning, in the editor or on the canvas. Never throw, and never a dialog box.
|
|
185
|
+
- Save it like any other state. Editing between two good states often passes through bad ones.
|
|
186
|
+
- Draw as much as still makes sense. Values in worse shape can come back from IndexedDB or a file, so fail gracefully and make them easy to fix.
|
|
173
187
|
- Throwing while saving a video is only for external failures, like an image or video that doesn't load.
|
|
174
188
|
- Assertions are still fine for the programmer's own mistakes, like a table of values checked when the program starts.
|
|
175
189
|
- **Name a replaceable child with `userEditableDescription`,** not the constructor's `description`, when your code adds one with `replaceableComponents.push()`.
|
|
@@ -199,7 +213,7 @@ It is only downloaded if someone uses transcription.
|
|
|
199
213
|
## How this library is made
|
|
200
214
|
|
|
201
215
|
The code came from [canvas-recorder](https://github.com/TradeIdeasPhilip/canvas-recorder), which is now historical.
|
|
202
|
-
Since 2026-10-14, this library is the master copy:
|
|
216
|
+
Since 2026-10-14, this library is the master copy: new features and fixes start here.
|
|
203
217
|
`imported-files.txt` lists what was copied, and from which canvas-recorder commit.
|
|
204
218
|
|
|
205
219
|
## Build
|
|
@@ -221,13 +235,13 @@ A deleted or renamed source file leaves its old output behind until `npm run cle
|
|
|
221
235
|
|
|
222
236
|
A project that has linked this library (see [Working on the library at the same time](#working-on-the-library-at-the-same-time)) sees a change as soon as `npm run watch` recompiles it.
|
|
223
237
|
|
|
224
|
-
The Simple Video Editor is a separate npm project inside this repo.
|
|
238
|
+
The Simple Video Editor is a separate npm project inside this repo. [simple-video-editor/README.md](simple-video-editor/README.md) says how to run it.
|
|
225
239
|
|
|
226
240
|
## Releasing
|
|
227
241
|
|
|
228
|
-
1. Bump the version.
|
|
229
|
-
Otherwise:
|
|
230
|
-
2. `npm publish`.
|
|
231
|
-
3. In each project:
|
|
242
|
+
1. Bump the version. If [BREAKING-CHANGES.md](BREAKING-CHANGES.md) has an "Unreleased" section: `npm version minor --no-git-tag-version`, and replace "Unreleased" with the new version and the date.
|
|
243
|
+
Otherwise: `npm version patch --no-git-tag-version`. Commit and push.
|
|
244
|
+
2. `npm publish`. `prepublishOnly` empties and rebuilds `dist/` first, so nothing stale is published.
|
|
245
|
+
3. In each project: `npm install canvas-recorder-lib@<version>`, `npx tsc --noEmit`, and commit `package.json` and `package-lock.json`.
|
|
232
246
|
|
|
233
247
|
The API docs publish themselves on every push to master.
|
package/dev/audio-builder.ts
CHANGED
|
@@ -1,24 +1,20 @@
|
|
|
1
|
-
import { fetchMedia } from "../src/local-files.ts";
|
|
2
|
-
|
|
3
1
|
/**
|
|
4
|
-
*
|
|
5
|
-
*
|
|
2
|
+
* The soundtrack mixed into one buffer, for the saved video and 🎤 Record Just Sound.
|
|
3
|
+
*
|
|
4
|
+
* Live playback doesn't use this: it gives each clip its own node, and the browser
|
|
5
|
+
* mixes them. See dev/soundtrack.ts.
|
|
6
6
|
*
|
|
7
|
+
* It starts with all silence and you can add the clips wherever you need them.
|
|
7
8
|
* Overlapping clips are mixed: each one is added to whatever is already
|
|
8
9
|
* there, so two sounds at the same time are both heard.
|
|
9
10
|
*
|
|
10
|
-
* The result is always **mono
|
|
11
|
-
* it
|
|
12
|
-
* screen recording, for example, writes its one
|
|
13
|
-
* channels, and CapCut's exports have done the same.
|
|
14
|
-
* test and can't put a sound in only one ear by accident.
|
|
11
|
+
* The result is always **mono**, and so are the sources: dev/soundtrack.ts mixes
|
|
12
|
+
* each file down to one channel when it decodes it. Nothing recorded for these
|
|
13
|
+
* videos is really stereo: a Mac screen recording, for example, writes its one
|
|
14
|
+
* microphone signal into both channels, and CapCut's exports have done the same.
|
|
15
|
+
* One channel is less to test and can't put a sound in only one ear by accident.
|
|
15
16
|
*/
|
|
16
17
|
export class AudioBuilder {
|
|
17
|
-
/**
|
|
18
|
-
* Like AudioContext, but this lets you process data at full speed.
|
|
19
|
-
* AudioContext doesn't allow you to process things faster than realtime.
|
|
20
|
-
*/
|
|
21
|
-
private audioContext: OfflineAudioContext;
|
|
22
18
|
/**
|
|
23
19
|
* This is the sound we are building.
|
|
24
20
|
*/
|
|
@@ -28,67 +24,18 @@ export class AudioBuilder {
|
|
|
28
24
|
return this.buffer;
|
|
29
25
|
}
|
|
30
26
|
|
|
31
|
-
constructor(totalDurationMs: number) {
|
|
32
|
-
|
|
33
|
-
//
|
|
34
|
-
// start out at duration 0 (e.g. an empty timeline before any clips have
|
|
35
|
-
// been added yet), so clamp rather than let the constructor throw.
|
|
36
|
-
this.audioContext = new OfflineAudioContext({
|
|
37
|
-
length: Math.max(1, totalDurationMs * 1000),
|
|
38
|
-
sampleRate,
|
|
39
|
-
});
|
|
27
|
+
constructor(totalDurationMs: number, sampleRate = 48000) {
|
|
28
|
+
// A project can legitimately be 0 ms long (an empty timeline), and an
|
|
29
|
+
// AudioBuffer needs at least one sample.
|
|
40
30
|
const totalSamples = Math.max(
|
|
41
31
|
1,
|
|
42
32
|
Math.ceil((totalDurationMs / 1000) * sampleRate),
|
|
43
33
|
);
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
const time1 = performance.now();
|
|
50
|
-
const response = await fetchMedia(url);
|
|
51
|
-
const time2 = performance.now();
|
|
52
|
-
const encodedSource = await response.arrayBuffer();
|
|
53
|
-
const time3 = performance.now();
|
|
54
|
-
// decodeAudioData() takes about ⅜ - ¾ of a second to decode 4 minutes and 18 seconds of audio.
|
|
55
|
-
const sourceBuffer = await this.audioContext.decodeAudioData(encodedSource);
|
|
56
|
-
const time4 = performance.now();
|
|
57
|
-
const fileSizeKB = encodedSource.byteLength / 1024;
|
|
58
|
-
false &&
|
|
59
|
-
console.log(
|
|
60
|
-
`[audio] fetch: ${(time2 - time1).toFixed(0)} ms (${fileSizeKB.toFixed(0)} KB, ` +
|
|
61
|
-
`${((time2 - time1) / fileSizeKB).toFixed(3)} ms/KB) | ` +
|
|
62
|
-
`arrayBuffer: ${(time3 - time2).toFixed(0)} ms | ` +
|
|
63
|
-
`decode: ${(time4 - time3).toFixed(0)} ms`,
|
|
64
|
-
);
|
|
65
|
-
return sourceBuffer;
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
readonly #cache = new Map<string, Promise<AudioBuffer>>();
|
|
69
|
-
readonly #resolved = new Map<string, AudioBuffer>();
|
|
70
|
-
|
|
71
|
-
clearCache() {
|
|
72
|
-
this.#cache.clear();
|
|
73
|
-
this.#resolved.clear();
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
warmCache(url: string) {
|
|
77
|
-
this.#findAudioBuffer(url);
|
|
78
|
-
}
|
|
79
|
-
|
|
80
|
-
/** Returns the decoded buffer synchronously if already cached, otherwise null. */
|
|
81
|
-
getDecodedBuffer(url: string): AudioBuffer | null {
|
|
82
|
-
return this.#resolved.get(url) ?? null;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
#findAudioBuffer(url: string): Promise<AudioBuffer> {
|
|
86
|
-
return this.#cache.getOrInsertComputed(url, (missingUrl) =>
|
|
87
|
-
this.#createNewAudioBuffer(missingUrl).then((buf) => {
|
|
88
|
-
this.#resolved.set(missingUrl, buf);
|
|
89
|
-
return buf;
|
|
90
|
-
}),
|
|
91
|
-
);
|
|
34
|
+
this.buffer = new AudioBuffer({
|
|
35
|
+
numberOfChannels: 1,
|
|
36
|
+
length: totalSamples,
|
|
37
|
+
sampleRate,
|
|
38
|
+
});
|
|
92
39
|
}
|
|
93
40
|
|
|
94
41
|
/**
|
|
@@ -98,14 +45,7 @@ export class AudioBuilder {
|
|
|
98
45
|
* The sum can go past ±1, which clips. That's fine for what this is used
|
|
99
46
|
* for, a voiceover with the occasional overlap; the WAV encoder clamps, and
|
|
100
47
|
* live playback clips the same way.
|
|
101
|
-
* @param
|
|
102
|
-
*
|
|
103
|
-
* I typically use vite dev mode to run my project.
|
|
104
|
-
* I put the files in the `/public` directory of the project.
|
|
105
|
-
* And the url will start with "./".
|
|
106
|
-
*
|
|
107
|
-
* CORS can be an issue depending on the server.
|
|
108
|
-
* A `local:` URL reads a file on the user's computer. See src/local-files.ts.
|
|
48
|
+
* @param sourceBuffer The decoded sound, mono, at this builder's sample rate.
|
|
109
49
|
* @param startMsInDestination 0 to play the new clip at the the beginning of the video.
|
|
110
50
|
* 1000 to start this clip 1 second after the video starts.
|
|
111
51
|
*
|
|
@@ -142,14 +82,13 @@ export class AudioBuilder {
|
|
|
142
82
|
* whichever comes first.
|
|
143
83
|
*
|
|
144
84
|
* The default is `Infinity`, to copy the entire clip or as much as will fit.
|
|
145
|
-
* @returns This promise will resolve (or reject) when the request is complete.
|
|
146
85
|
*/
|
|
147
|
-
|
|
148
|
-
|
|
86
|
+
add(
|
|
87
|
+
sourceBuffer: AudioBuffer,
|
|
149
88
|
startMsInDestination: number,
|
|
150
89
|
trimFromStartMs: number = 0,
|
|
151
90
|
length: number = Infinity,
|
|
152
|
-
):
|
|
91
|
+
): void {
|
|
153
92
|
if (startMsInDestination < 0) {
|
|
154
93
|
// Skip the part that would play before the video starts.
|
|
155
94
|
trimFromStartMs -= startMsInDestination;
|
|
@@ -163,8 +102,6 @@ export class AudioBuilder {
|
|
|
163
102
|
trimFromStartMs = 0;
|
|
164
103
|
}
|
|
165
104
|
|
|
166
|
-
const sourceBuffer = await this.#findAudioBuffer(url);
|
|
167
|
-
|
|
168
105
|
const samplesPerMs = this.buffer.sampleRate / 1000;
|
|
169
106
|
const destinationStart = Math.floor(startMsInDestination * samplesPerMs);
|
|
170
107
|
const sourceStart = Math.floor(trimFromStartMs * samplesPerMs);
|
|
@@ -185,89 +122,58 @@ export class AudioBuilder {
|
|
|
185
122
|
const destination = this.buffer
|
|
186
123
|
.getChannelData(0)
|
|
187
124
|
.subarray(destinationStart, destinationStart + count);
|
|
188
|
-
const
|
|
189
|
-
|
|
190
|
-
(
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
.subarray(sourceStart, sourceStart + count),
|
|
194
|
-
);
|
|
195
|
-
if (channels.length === 1) {
|
|
196
|
-
// The common case.
|
|
197
|
-
const source = channels[0];
|
|
198
|
-
for (let i = 0; i < count; i++) {
|
|
199
|
-
destination[i] += source[i];
|
|
200
|
-
}
|
|
201
|
-
} else {
|
|
202
|
-
// Average the channels, don't sum them: identical channels (the usual
|
|
203
|
-
// "stereo") come out exactly as they went in, where a sum would double
|
|
204
|
-
// the level and clip.
|
|
205
|
-
const scale = 1 / channels.length;
|
|
206
|
-
for (let i = 0; i < count; i++) {
|
|
207
|
-
let sum = 0;
|
|
208
|
-
for (const channel of channels) {
|
|
209
|
-
sum += channel[i];
|
|
210
|
-
}
|
|
211
|
-
destination[i] += sum * scale;
|
|
212
|
-
}
|
|
125
|
+
const source = sourceBuffer
|
|
126
|
+
.getChannelData(0)
|
|
127
|
+
.subarray(sourceStart, sourceStart + count);
|
|
128
|
+
for (let i = 0; i < count; i++) {
|
|
129
|
+
destination[i] += source[i];
|
|
213
130
|
}
|
|
214
131
|
}
|
|
215
132
|
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
return new Blob([wavArrayBuffer], { type: "audio/wav" });
|
|
133
|
+
toBlob(): Blob {
|
|
134
|
+
return new Blob([audioBufferToWav(this.buffer)], { type: "audio/wav" });
|
|
219
135
|
}
|
|
136
|
+
}
|
|
220
137
|
|
|
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
|
-
console.log("Wuz lots!");
|
|
259
|
-
const channel = buffer.getChannelData(ch);
|
|
260
|
-
for (let i = 0; i < buffer.length; i++) {
|
|
261
|
-
const sample = Math.max(-1, Math.min(1, channel[i]));
|
|
262
|
-
view.setInt16(
|
|
263
|
-
offset,
|
|
264
|
-
sample < 0 ? sample * 0x8000 : sample * 0x7fff,
|
|
265
|
-
true,
|
|
266
|
-
);
|
|
267
|
-
offset += 2;
|
|
268
|
-
}
|
|
138
|
+
/**
|
|
139
|
+
* Simple 16-bit PCM WAV encoder.
|
|
140
|
+
* This takes about 0.6 seconds to save 8½ minutes of data.
|
|
141
|
+
*/
|
|
142
|
+
function audioBufferToWav(buffer: AudioBuffer): ArrayBuffer {
|
|
143
|
+
const numChannels = buffer.numberOfChannels;
|
|
144
|
+
const sampleRate = buffer.sampleRate;
|
|
145
|
+
const length = buffer.length * numChannels * 2 + 44;
|
|
146
|
+
const arrayBuffer = new ArrayBuffer(length);
|
|
147
|
+
const view = new DataView(arrayBuffer);
|
|
148
|
+
|
|
149
|
+
const writeString = (offset: number, str: string) => {
|
|
150
|
+
for (let i = 0; i < str.length; i++)
|
|
151
|
+
view.setUint8(offset + i, str.charCodeAt(i));
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
writeString(0, "RIFF");
|
|
155
|
+
view.setUint32(4, 36 + buffer.length * numChannels * 2, true);
|
|
156
|
+
writeString(8, "WAVE");
|
|
157
|
+
writeString(12, "fmt ");
|
|
158
|
+
view.setUint32(16, 16, true);
|
|
159
|
+
view.setUint16(20, 1, true);
|
|
160
|
+
view.setUint16(22, numChannels, true);
|
|
161
|
+
view.setUint32(24, sampleRate, true);
|
|
162
|
+
view.setUint32(28, sampleRate * numChannels * 2, true);
|
|
163
|
+
view.setUint16(32, numChannels * 2, true);
|
|
164
|
+
view.setUint16(34, 16, true);
|
|
165
|
+
writeString(36, "data");
|
|
166
|
+
view.setUint32(40, buffer.length * numChannels * 2, true);
|
|
167
|
+
|
|
168
|
+
let offset = 44;
|
|
169
|
+
for (let ch = 0; ch < numChannels; ch++) {
|
|
170
|
+
const channel = buffer.getChannelData(ch);
|
|
171
|
+
for (let i = 0; i < buffer.length; i++) {
|
|
172
|
+
const sample = Math.max(-1, Math.min(1, channel[i]));
|
|
173
|
+
view.setInt16(offset, sample < 0 ? sample * 0x8000 : sample * 0x7fff, true);
|
|
174
|
+
offset += 2;
|
|
269
175
|
}
|
|
270
|
-
|
|
271
|
-
return arrayBuffer;
|
|
272
176
|
}
|
|
177
|
+
|
|
178
|
+
return arrayBuffer;
|
|
273
179
|
}
|