@rocket.chat/mp3-encoder 0.31.26 → 0.31.27
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/README.md +63 -55
- package/dist/index.js +3719 -4354
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +3719 -4354
- package/dist/index.mjs.map +1 -1
- package/dist/lame/ABRPresets.d.ts.map +1 -1
- package/dist/lame/ATH.d.ts +7 -7
- package/dist/lame/ATH.d.ts.map +1 -1
- package/dist/lame/BitStream.d.ts.map +1 -1
- package/dist/lame/Bits.d.ts.map +1 -1
- package/dist/lame/CBRNewIterationLoop.d.ts.map +1 -1
- package/dist/lame/CalcNoiseData.d.ts +3 -3
- package/dist/lame/CalcNoiseData.d.ts.map +1 -1
- package/dist/lame/CalcNoiseResult.d.ts.map +1 -1
- package/dist/lame/Encoder.d.ts.map +1 -1
- package/dist/lame/FFT.d.ts.map +1 -1
- package/dist/lame/GainAnalysis.d.ts.map +1 -1
- package/dist/lame/GrInfo.d.ts +8 -8
- package/dist/lame/GrInfo.d.ts.map +1 -1
- package/dist/lame/Header.d.ts +1 -1
- package/dist/lame/Header.d.ts.map +1 -1
- package/dist/lame/HuffCodeTab.d.ts.map +1 -1
- package/dist/lame/IIISideInfo.d.ts +1 -1
- package/dist/lame/IIISideInfo.d.ts.map +1 -1
- package/dist/lame/III_psy_ratio.d.ts.map +1 -1
- package/dist/lame/III_psy_xmin.d.ts +2 -2
- package/dist/lame/III_psy_xmin.d.ts.map +1 -1
- package/dist/lame/InOut.d.ts.map +1 -1
- package/dist/lame/Lame.d.ts.map +1 -1
- package/dist/lame/LameGlobalFlags.d.ts.map +1 -1
- package/dist/lame/LameInternalFlags.d.ts +35 -35
- package/dist/lame/LameInternalFlags.d.ts.map +1 -1
- package/dist/lame/MPEGMode.d.ts.map +1 -1
- package/dist/lame/MeanBits.d.ts.map +1 -1
- package/dist/lame/Mp3Encoder.d.ts +2 -2
- package/dist/lame/Mp3Encoder.d.ts.map +1 -1
- package/dist/lame/NewMDCT.d.ts.map +1 -1
- package/dist/lame/NsPsy.d.ts +5 -5
- package/dist/lame/NsPsy.d.ts.map +1 -1
- package/dist/lame/NumUsed.d.ts.map +1 -1
- package/dist/lame/PSY.d.ts +2 -2
- package/dist/lame/PSY.d.ts.map +1 -1
- package/dist/lame/Presets.d.ts.map +1 -1
- package/dist/lame/PsyModel.d.ts.map +1 -1
- package/dist/lame/Quantize.d.ts.map +1 -1
- package/dist/lame/QuantizePVT.d.ts.map +1 -1
- package/dist/lame/ReplayGain.d.ts +8 -8
- package/dist/lame/ReplayGain.d.ts.map +1 -1
- package/dist/lame/Reservoir.d.ts.map +1 -1
- package/dist/lame/ScaleFac.d.ts.map +1 -1
- package/dist/lame/ShortBlock.d.ts.map +1 -1
- package/dist/lame/StartLine.d.ts.map +1 -1
- package/dist/lame/Tables.d.ts.map +1 -1
- package/dist/lame/Takehiro.d.ts.map +1 -1
- package/dist/lame/TotalBytes.d.ts.map +1 -1
- package/dist/lame/VBRPresets.d.ts.map +1 -1
- package/dist/lame/VBRTag.d.ts.map +1 -1
- package/dist/lame/VbrMode.d.ts.map +1 -1
- package/dist/lame/WavHeader.d.ts.map +1 -1
- package/dist/lame/arrays.d.ts.map +1 -1
- package/dist/lame/assert.d.ts +2 -1
- package/dist/lame/assert.d.ts.map +1 -1
- package/dist/lame/bitrates.d.ts.map +1 -1
- package/dist/lame/math.d.ts.map +1 -1
- package/dist/lame/sampleRates.d.ts.map +1 -1
- package/package.json +27 -39
package/README.md
CHANGED
|
@@ -1,91 +1,99 @@
|
|
|
1
|
-
<!--header-->
|
|
2
|
-
|
|
3
|
-
<p align="center">
|
|
4
|
-
<a href="https://rocket.chat" title="Rocket.Chat">
|
|
5
|
-
<img src="https://github.com/RocketChat/Rocket.Chat.Artwork/raw/master/Logos/2020/png/logo-horizontal-red.png" alt="Rocket.Chat" />
|
|
6
|
-
</a>
|
|
7
|
-
</p>
|
|
8
|
-
|
|
9
1
|
# `@rocket.chat/mp3-encoder`
|
|
10
2
|
|
|
11
|
-
> A LAME encoder
|
|
3
|
+
> A LAME-based MP3 encoder that runs as a web worker.
|
|
12
4
|
|
|
13
|
-
|
|
5
|
+
[](https://www.npmjs.com/package/@rocket.chat/mp3-encoder) [](https://www.npmjs.com/package/@rocket.chat/mp3-encoder)
|
|
14
6
|
|
|
15
|
-
|
|
7
|
+
This package is a web worker script that encodes raw audio samples into an MP3 stream. It is a TypeScript port of the LAME encoder, so it needs no native code or WebAssembly. Rocket.Chat uses it to encode voice messages recorded in the browser.
|
|
16
8
|
|
|
17
|
-
|
|
9
|
+
The package has no exports. You load its built file as a worker and talk to it with `postMessage`.
|
|
18
10
|
|
|
19
|
-
|
|
11
|
+
## Installation
|
|
20
12
|
|
|
21
|
-
|
|
13
|
+
```sh
|
|
14
|
+
npm install @rocket.chat/mp3-encoder
|
|
15
|
+
```
|
|
22
16
|
|
|
23
|
-
|
|
17
|
+
There are no dependencies. The worker script is published in two builds: `dist/index.js` (a classic script, suitable for `new Worker(url)`) and `dist/index.mjs` (an ES module).
|
|
24
18
|
|
|
25
|
-
|
|
19
|
+
## Usage
|
|
26
20
|
|
|
27
|
-
|
|
28
|
-
npm i @rocket.chat/mp3-encoder
|
|
21
|
+
### Serving the worker
|
|
29
22
|
|
|
30
|
-
|
|
23
|
+
Copy or link `node_modules/@rocket.chat/mp3-encoder/dist/index.js` to a path your app serves as a static file, then start a worker from it:
|
|
31
24
|
|
|
32
|
-
|
|
25
|
+
```ts
|
|
26
|
+
const worker = new Worker('/workers/mp3-encoder/index.js');
|
|
33
27
|
```
|
|
34
28
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
## Contributing
|
|
29
|
+
### Encoding audio
|
|
38
30
|
|
|
39
|
-
|
|
31
|
+
The worker understands three commands:
|
|
40
32
|
|
|
41
|
-
|
|
42
|
-
|
|
33
|
+
1. `init` sets up a new encoder.
|
|
34
|
+
2. `encode` sends a chunk of samples. Send as many as you need.
|
|
35
|
+
3. `finish` flushes the encoder. The worker answers with an `end` message holding the encoded MP3.
|
|
43
36
|
|
|
44
|
-
|
|
37
|
+
```ts
|
|
38
|
+
const worker = new Worker('/workers/mp3-encoder/index.js');
|
|
45
39
|
|
|
46
|
-
|
|
40
|
+
worker.onmessage = (event: MessageEvent) => {
|
|
41
|
+
if (event.data.command === 'end') {
|
|
42
|
+
const mp3 = new Blob(event.data.buffer, { type: 'audio/mpeg' });
|
|
43
|
+
worker.terminate();
|
|
44
|
+
// upload or play `mp3`
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
47
|
|
|
48
|
-
|
|
48
|
+
worker.postMessage({
|
|
49
|
+
command: 'init',
|
|
50
|
+
config: { sampleRate: 48000, bitRate: 32 },
|
|
51
|
+
});
|
|
49
52
|
|
|
50
|
-
|
|
53
|
+
// For each chunk of mono audio, e.g. from an AudioWorklet or ScriptProcessorNode:
|
|
54
|
+
worker.postMessage({ command: 'encode', buffer: samples }); // Float32Array in [-1, 1]
|
|
51
55
|
|
|
52
|
-
|
|
53
|
-
|
|
56
|
+
// When recording stops:
|
|
57
|
+
worker.postMessage({ command: 'finish' });
|
|
54
58
|
```
|
|
55
59
|
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
### Linting
|
|
60
|
+
The encoded output does not depend on how you split the samples into chunks. After `finish`, send `init` again to start a new stream.
|
|
59
61
|
|
|
60
|
-
|
|
61
|
-
Before commiting, check if your code fits our style by running:
|
|
62
|
+
## API
|
|
62
63
|
|
|
63
|
-
|
|
64
|
+
### Messages to the worker
|
|
64
65
|
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
Some linter warnings and errors can be automatically fixed:
|
|
66
|
+
| Message | Description |
|
|
67
|
+
| ----------------------------------------------------------------------- | ----------------------------------------------------------------------------------------------------------------------------------------- |
|
|
68
|
+
| `{ command: 'init', config?: { numChannels?, sampleRate?, bitRate? } }` | Creates the encoder. Defaults: `numChannels` 1, `sampleRate` 44100 (Hz), `bitRate` 32 (kbps). `sampleRate` must match the audio you send. |
|
|
69
|
+
| `{ command: 'encode', buffer: Float32Array }` | Encodes a chunk of samples. Values are clamped to [-1, 1] and converted to 16-bit PCM. |
|
|
70
|
+
| `{ command: 'finish' }` | Flushes the encoder and posts the result. |
|
|
72
71
|
|
|
73
|
-
|
|
72
|
+
### Messages from the worker
|
|
74
73
|
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
74
|
+
| Message | Description |
|
|
75
|
+
| ----------------------------------------- | ------------------------------------------------------------------------------------------------- |
|
|
76
|
+
| `{ command: 'end', buffer: Int8Array[] }` | The MP3 stream as a list of chunks, in order. Pass the list to `new Blob()` to get a single file. |
|
|
78
77
|
|
|
79
|
-
|
|
78
|
+
### Mono only
|
|
80
79
|
|
|
81
|
-
|
|
80
|
+
Each `encode` message is treated as a single channel. Mix multi-channel audio down to one channel before sending it.
|
|
82
81
|
|
|
83
|
-
|
|
82
|
+
## Development
|
|
84
83
|
|
|
85
|
-
|
|
84
|
+
This package lives in the [Rocket.Chat monorepo](https://github.com/RocketChat/Rocket.Chat/tree/develop/packages/mp3-encoder). From the repository root:
|
|
86
85
|
|
|
87
86
|
```sh
|
|
88
|
-
yarn
|
|
87
|
+
yarn workspace @rocket.chat/mp3-encoder build
|
|
88
|
+
yarn workspace @rocket.chat/mp3-encoder start
|
|
89
|
+
yarn workspace @rocket.chat/mp3-encoder testunit
|
|
90
|
+
yarn workspace @rocket.chat/mp3-encoder lint
|
|
91
|
+
yarn workspace @rocket.chat/mp3-encoder lint:fix
|
|
92
|
+
yarn workspace @rocket.chat/mp3-encoder typecheck
|
|
89
93
|
```
|
|
90
94
|
|
|
91
|
-
|
|
95
|
+
`build` bundles the worker with Rollup; `start` rebuilds on change. The tests encode the WAV files in `testdata/` and compare the result with a known hash, so a change to the encoder that alters its output fails them.
|
|
96
|
+
|
|
97
|
+
## License
|
|
98
|
+
|
|
99
|
+
MIT — see [LICENSE](https://github.com/RocketChat/Rocket.Chat/blob/develop/LICENSE).
|