Skip to content

Commit d4aa7e3

Browse files
committed
Make ready
1 parent d391ef8 commit d4aa7e3

6 files changed

Lines changed: 283 additions & 165 deletions

File tree

README.md

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,14 +16,12 @@ const buf = await encode.wav(channelData, { sampleRate: 44100 });
1616

1717
| Format | Package | Engine |
1818
|--------|---------|--------|
19-
| WAV | built-in | JS |
20-
| MP3 | [wasm-media-encoders](https://github.com/nicodemus26/wasm-media-encoders) | WASM |
21-
| OGG Vorbis | [wasm-media-encoders](https://github.com/nicodemus26/wasm-media-encoders) | WASM |
22-
| Opus | TBD | WASM |
23-
| FLAC | [libflac.js](https://github.com/nicodemus26/libflac.js) | WASM |
24-
| AAC / M4A | TBD | WASM |
25-
| AIFF | built-in | JS |
26-
| WebM | TBD | WASM |
19+
| WAV | [@audio/wav-encode](https://github.com/audiojs/wav-encode) | JS |
20+
| MP3 | [@audio/mp3-encode](https://github.com/audiojs/mp3-encode) | WASM |
21+
| OGG Vorbis | [@audio/ogg-encode](https://github.com/audiojs/ogg-encode) | WASM |
22+
| Opus | [@audio/opus-encode](https://github.com/audiojs/opus-encode) | WASM |
23+
| FLAC | [@audio/flac-encode](https://github.com/audiojs/flac-encode) | WASM |
24+
| AIFF | [@audio/aiff-encode](https://github.com/audiojs/aiff-encode) | JS |
2725

2826
### Whole-file encode
2927

@@ -32,9 +30,12 @@ Specify the format as method name. Input is _Float32Array[]_ (one per channel) o
3230
```js
3331
import encode from 'audio-encode';
3432

35-
const wav = await encode.wav(channelData, { sampleRate: 44100 });
36-
const mp3 = await encode.mp3(channelData, { sampleRate: 44100, bitrate: 128 });
37-
const ogg = await encode.ogg(channelData, { sampleRate: 44100, quality: 5 });
33+
const wav = await encode.wav(channelData, { sampleRate: 44100 });
34+
const aiff = await encode.aiff(channelData, { sampleRate: 44100 });
35+
const mp3 = await encode.mp3(channelData, { sampleRate: 44100, bitrate: 128 });
36+
const ogg = await encode.ogg(channelData, { sampleRate: 44100, quality: 5 });
37+
const flac = await encode.flac(channelData, { sampleRate: 44100 });
38+
const opus = await encode.opus(channelData, { sampleRate: 48000, bitrate: 96 });
3839
```
3940

4041
### Stream encoding
@@ -59,9 +60,12 @@ const c = await encoder.encode(); // end of stream — flush + free
5960
| Option | Description | Applies to |
6061
|--------|-------------|------------|
6162
| `sampleRate` | Output sample rate (required) | all |
62-
| `bitrate` | Target bitrate in kbps | mp3, aac, opus |
63+
| `bitrate` | Target bitrate in kbps | mp3, opus |
6364
| `quality` | Quality 0–10 (VBR) | ogg, mp3 |
6465
| `channels` | Output channel count | all |
66+
| `bitDepth` | Bit depth: 16 or 32 (wav), 16 or 24 (aiff, flac) | wav, aiff, flac |
67+
| `compression` | FLAC compression level 0–8 | flac |
68+
| `application` | `'audio'`, `'voip'`, or `'lowdelay'` | opus |
6569

6670
### Custom encoders
6771

audio-encode.d.ts

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,12 @@ export interface EncodeOptions {
1818
bitrate?: number;
1919
/** Quality 0-10 (VBR, format-specific). */
2020
quality?: number;
21+
/** Bit depth: 16|32 for wav, 16|24 for aiff/flac. */
22+
bitDepth?: number;
23+
/** FLAC compression level 0-8. */
24+
compression?: number;
25+
/** Opus application: 'audio', 'voip', 'lowdelay'. */
26+
application?: string;
2127
[key: string]: any;
2228
}
2329

@@ -26,8 +32,13 @@ export interface FormatEncoder {
2632
stream(opts: EncodeOptions): Promise<StreamEncoder>;
2733
}
2834

29-
/** Encoder registry. Formats attached as encode.wav, encode.mp3, etc. */
3035
declare const encode: {
36+
wav: FormatEncoder;
37+
aiff: FormatEncoder;
38+
mp3: FormatEncoder;
39+
ogg: FormatEncoder;
40+
flac: FormatEncoder;
41+
opus: FormatEncoder;
3142
[format: string]: FormatEncoder;
3243
};
3344

@@ -39,17 +50,3 @@ export function streamEncoder(
3950
onFlush?: (() => Uint8Array | Promise<Uint8Array>) | null,
4051
onFree?: (() => void) | null
4152
): StreamEncoder;
42-
43-
/** Wrap a stream factory into whole-file encoder + .stream property. */
44-
export function fmt(
45-
init: (opts: EncodeOptions) => Promise<StreamEncoder>
46-
): FormatEncoder;
47-
48-
/** Normalize input to Float32Array[]. */
49-
export function channels(data: Float32Array[] | Float32Array | null): Float32Array[];
50-
51-
/** Ensure result is Uint8Array. */
52-
export function norm(r: any): Uint8Array;
53-
54-
/** Concatenate two Uint8Arrays. */
55-
export function merge(a: Uint8Array, b: Uint8Array): Uint8Array;

audio-encode.js

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,20 @@ export default encode
1616

1717
// --- format registration ---
1818

19-
// encode.wav = fmt(async (opts) => streamEncoder(...))
20-
// encode.mp3 = fmt(async (opts) => streamEncoder(...))
19+
function reg(name, load) {
20+
encode[name] = fmt(async (opts) => {
21+
let init = (await load()).default
22+
let codec = await init(opts)
23+
return streamEncoder(ch => codec.encode(ch), () => codec.flush(), () => codec.free())
24+
})
25+
}
26+
27+
reg('wav', () => import('@audio/wav-encode'))
28+
reg('aiff', () => import('@audio/aiff-encode'))
29+
reg('mp3', () => import('@audio/mp3-encode'))
30+
reg('ogg', () => import('@audio/ogg-encode'))
31+
reg('flac', () => import('@audio/flac-encode'))
32+
reg('opus', () => import('@audio/opus-encode'))
2133

2234
/**
2335
* Wrap a stream factory into whole-file encoder + .stream
@@ -107,3 +119,4 @@ function merge(a, b) {
107119
}
108120

109121
export { fmt, channels, norm, merge }
122+

package-lock.json

Lines changed: 130 additions & 40 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)