@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.
Files changed (66) hide show
  1. package/README.md +63 -55
  2. package/dist/index.js +3719 -4354
  3. package/dist/index.js.map +1 -1
  4. package/dist/index.mjs +3719 -4354
  5. package/dist/index.mjs.map +1 -1
  6. package/dist/lame/ABRPresets.d.ts.map +1 -1
  7. package/dist/lame/ATH.d.ts +7 -7
  8. package/dist/lame/ATH.d.ts.map +1 -1
  9. package/dist/lame/BitStream.d.ts.map +1 -1
  10. package/dist/lame/Bits.d.ts.map +1 -1
  11. package/dist/lame/CBRNewIterationLoop.d.ts.map +1 -1
  12. package/dist/lame/CalcNoiseData.d.ts +3 -3
  13. package/dist/lame/CalcNoiseData.d.ts.map +1 -1
  14. package/dist/lame/CalcNoiseResult.d.ts.map +1 -1
  15. package/dist/lame/Encoder.d.ts.map +1 -1
  16. package/dist/lame/FFT.d.ts.map +1 -1
  17. package/dist/lame/GainAnalysis.d.ts.map +1 -1
  18. package/dist/lame/GrInfo.d.ts +8 -8
  19. package/dist/lame/GrInfo.d.ts.map +1 -1
  20. package/dist/lame/Header.d.ts +1 -1
  21. package/dist/lame/Header.d.ts.map +1 -1
  22. package/dist/lame/HuffCodeTab.d.ts.map +1 -1
  23. package/dist/lame/IIISideInfo.d.ts +1 -1
  24. package/dist/lame/IIISideInfo.d.ts.map +1 -1
  25. package/dist/lame/III_psy_ratio.d.ts.map +1 -1
  26. package/dist/lame/III_psy_xmin.d.ts +2 -2
  27. package/dist/lame/III_psy_xmin.d.ts.map +1 -1
  28. package/dist/lame/InOut.d.ts.map +1 -1
  29. package/dist/lame/Lame.d.ts.map +1 -1
  30. package/dist/lame/LameGlobalFlags.d.ts.map +1 -1
  31. package/dist/lame/LameInternalFlags.d.ts +35 -35
  32. package/dist/lame/LameInternalFlags.d.ts.map +1 -1
  33. package/dist/lame/MPEGMode.d.ts.map +1 -1
  34. package/dist/lame/MeanBits.d.ts.map +1 -1
  35. package/dist/lame/Mp3Encoder.d.ts +2 -2
  36. package/dist/lame/Mp3Encoder.d.ts.map +1 -1
  37. package/dist/lame/NewMDCT.d.ts.map +1 -1
  38. package/dist/lame/NsPsy.d.ts +5 -5
  39. package/dist/lame/NsPsy.d.ts.map +1 -1
  40. package/dist/lame/NumUsed.d.ts.map +1 -1
  41. package/dist/lame/PSY.d.ts +2 -2
  42. package/dist/lame/PSY.d.ts.map +1 -1
  43. package/dist/lame/Presets.d.ts.map +1 -1
  44. package/dist/lame/PsyModel.d.ts.map +1 -1
  45. package/dist/lame/Quantize.d.ts.map +1 -1
  46. package/dist/lame/QuantizePVT.d.ts.map +1 -1
  47. package/dist/lame/ReplayGain.d.ts +8 -8
  48. package/dist/lame/ReplayGain.d.ts.map +1 -1
  49. package/dist/lame/Reservoir.d.ts.map +1 -1
  50. package/dist/lame/ScaleFac.d.ts.map +1 -1
  51. package/dist/lame/ShortBlock.d.ts.map +1 -1
  52. package/dist/lame/StartLine.d.ts.map +1 -1
  53. package/dist/lame/Tables.d.ts.map +1 -1
  54. package/dist/lame/Takehiro.d.ts.map +1 -1
  55. package/dist/lame/TotalBytes.d.ts.map +1 -1
  56. package/dist/lame/VBRPresets.d.ts.map +1 -1
  57. package/dist/lame/VBRTag.d.ts.map +1 -1
  58. package/dist/lame/VbrMode.d.ts.map +1 -1
  59. package/dist/lame/WavHeader.d.ts.map +1 -1
  60. package/dist/lame/arrays.d.ts.map +1 -1
  61. package/dist/lame/assert.d.ts +2 -1
  62. package/dist/lame/assert.d.ts.map +1 -1
  63. package/dist/lame/bitrates.d.ts.map +1 -1
  64. package/dist/lame/math.d.ts.map +1 -1
  65. package/dist/lame/sampleRates.d.ts.map +1 -1
  66. 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 to be used in web workers
3
+ > A LAME-based MP3 encoder that runs as a web worker.
12
4
 
13
- ---
5
+ [![npm](https://img.shields.io/npm/v/@rocket.chat/mp3-encoder?style=flat-square)](https://www.npmjs.com/package/@rocket.chat/mp3-encoder) [![downloads](https://img.shields.io/npm/dw/@rocket.chat/mp3-encoder?style=flat-square)](https://www.npmjs.com/package/@rocket.chat/mp3-encoder)
14
6
 
15
- [![npm@latest](https://img.shields.io/npm/v/@rocket.chat/mp3-encoder/latest?style=flat-square)](https://www.npmjs.com/package/@rocket.chat/mp3-encoder/v/latest) [![npm@next](https://img.shields.io/npm/v/@rocket.chat/mp3-encoder/next?style=flat-square)](https://www.npmjs.com/package/@rocket.chat/mp3-encoder/v/next) ![npm downloads](https://img.shields.io/npm/dw/@rocket.chat/mp3-encoder?style=flat-square) ![License: MIT](https://img.shields.io/npm/l/@rocket.chat/mp3-encoder?style=flat-square)
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
- ![deps](https://img.shields.io/librariesio/release/npm/@rocket.chat/mp3-encoder?style=flat-square) ![npm bundle size](https://img.shields.io/bundlephobia/min/@rocket.chat/mp3-encoder?style=flat-square)
9
+ The package has no exports. You load its built file as a worker and talk to it with `postMessage`.
18
10
 
19
- <!--/header-->
11
+ ## Installation
20
12
 
21
- ## Install
13
+ ```sh
14
+ npm install @rocket.chat/mp3-encoder
15
+ ```
22
16
 
23
- <!--install-->
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
- Add `@rocket.chat/mp3-encoder` as a dependency:
19
+ ## Usage
26
20
 
27
- ```sh
28
- npm i @rocket.chat/mp3-encoder
21
+ ### Serving the worker
29
22
 
30
- # or, if you are using yarn:
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
- yarn add @rocket.chat/mp3-encoder
25
+ ```ts
26
+ const worker = new Worker('/workers/mp3-encoder/index.js');
33
27
  ```
34
28
 
35
- <!--/install-->
36
-
37
- ## Contributing
29
+ ### Encoding audio
38
30
 
39
- <!--contributing(msg)-->
31
+ The worker understands three commands:
40
32
 
41
- Contributions, issues, and feature requests are welcome!<br />
42
- Feel free to check the [issues](https://github.com/RocketChat/fuselage/issues).
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
- <!--/contributing(msg)-->
37
+ ```ts
38
+ const worker = new Worker('/workers/mp3-encoder/index.js');
45
39
 
46
- ### Building
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
- As this package dependends on others in this monorepo, before anything run the following at the root directory:
48
+ worker.postMessage({
49
+ command: 'init',
50
+ config: { sampleRate: 48000, bitRate: 32 },
51
+ });
49
52
 
50
- <!--yarn(build)-->
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
- ```sh
53
- yarn build
56
+ // When recording stops:
57
+ worker.postMessage({ command: 'finish' });
54
58
  ```
55
59
 
56
- <!--/yarn(build)-->
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
- To ensure the source is matching our coding style, we perform [linting](<https://en.wikipedia.org/wiki/Lint_(software)>).
61
- Before commiting, check if your code fits our style by running:
62
+ ## API
62
63
 
63
- <!--yarn(lint)-->
64
+ ### Messages to the worker
64
65
 
65
- ```sh
66
- yarn lint
67
- ```
68
-
69
- <!--/yarn(lint)-->
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
- <!--yarn(lint-and-fix)-->
72
+ ### Messages from the worker
74
73
 
75
- ```sh
76
- yarn lint-and-fix
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
- <!--/yarn(lint-and-fix)-->
78
+ ### Mono only
80
79
 
81
- ### Running tests
80
+ Each `encode` message is treated as a single channel. Mix multi-channel audio down to one channel before sending it.
82
81
 
83
- Whenever possible, add tests to describe exactly what your code do. You can run them by yourself:
82
+ ## Development
84
83
 
85
- <!--yarn(test)-->
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 test
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
- <!--/yarn(test)-->
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).