audio-decode 2.1.6 → 2.2.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/audio-decode.js +38 -21
- package/package.json +2 -1
- package/test.js +12 -4
package/audio-decode.js
CHANGED
|
@@ -30,37 +30,54 @@ export default async function audioDecode(buf) {
|
|
|
30
30
|
|
|
31
31
|
export const decoders = {
|
|
32
32
|
async oga(buf) {
|
|
33
|
-
let {
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
33
|
+
let { decoder } = decoders.oga
|
|
34
|
+
if (!decoder) {
|
|
35
|
+
let { OggVorbisDecoder } = await import('@wasm-audio-decoders/ogg-vorbis')
|
|
36
|
+
await (decoders.oga.decoder = decoder = new OggVorbisDecoder()).ready;
|
|
37
|
+
} else await decoder.reset()
|
|
38
|
+
return buf && createBuffer(await decoder.decodeFile(buf))
|
|
37
39
|
},
|
|
38
40
|
async mp3(buf) {
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
41
|
+
let { decoder } = decoders.mp3
|
|
42
|
+
if (!decoder) {
|
|
43
|
+
const { MPEGDecoder } = await import('mpg123-decoder')
|
|
44
|
+
await (decoders.mp3.decoder = decoder = new MPEGDecoder()).ready;
|
|
45
|
+
}
|
|
46
|
+
else await decoder.reset()
|
|
47
|
+
return buf && createBuffer(await decoder.decode(buf))
|
|
43
48
|
},
|
|
44
49
|
async flac(buf) {
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
50
|
+
let { decoder } = decoders.flac
|
|
51
|
+
if (!decoder) {
|
|
52
|
+
const { FLACDecoder } = await import('@wasm-audio-decoders/flac')
|
|
53
|
+
await (decoders.flac.decoder = decoder = new FLACDecoder()).ready
|
|
54
|
+
}
|
|
55
|
+
else await decoder.reset()
|
|
56
|
+
return buf && createBuffer(await decoder.decode(buf))
|
|
49
57
|
},
|
|
50
58
|
async opus(buf) {
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
59
|
+
let { decoder } = decoders.opus
|
|
60
|
+
if (!decoder) {
|
|
61
|
+
const { OggOpusDecoder } = await import('ogg-opus-decoder')
|
|
62
|
+
await (decoders.opus.decoder = decoder = new OggOpusDecoder()).ready
|
|
63
|
+
}
|
|
64
|
+
else await decoder.reset()
|
|
65
|
+
return buf && createBuffer(await decoder.decodeFile(buf))
|
|
55
66
|
},
|
|
56
67
|
async wav(buf) {
|
|
57
|
-
let
|
|
58
|
-
|
|
59
|
-
|
|
68
|
+
let { decode } = decoders.wav
|
|
69
|
+
if (!decode) {
|
|
70
|
+
let module = await import('node-wav')
|
|
71
|
+
decode = decoders.wav.decode = module.default.decode
|
|
72
|
+
}
|
|
73
|
+
return buf && createBuffer(await decode(buf))
|
|
60
74
|
},
|
|
61
75
|
async qoa(buf) {
|
|
62
|
-
let { decode } =
|
|
63
|
-
|
|
76
|
+
let { decode } = decoders.qoa
|
|
77
|
+
if (!decode) {
|
|
78
|
+
decoders.qoa.decode = decode = (await import('qoa-format')).decode
|
|
79
|
+
}
|
|
80
|
+
return buf && createBuffer(await decode(buf))
|
|
64
81
|
}
|
|
65
82
|
}
|
|
66
83
|
|
package/package.json
CHANGED
|
@@ -1,11 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "audio-decode",
|
|
3
|
-
"version": "2.
|
|
3
|
+
"version": "2.2.0",
|
|
4
4
|
"description": "Decode audio data in node or browser",
|
|
5
5
|
"main": "audio-decode.js",
|
|
6
6
|
"module": "audio-decode.js",
|
|
7
7
|
"browser": "audio-decode.js",
|
|
8
8
|
"type": "module",
|
|
9
|
+
"types": "audio-decode.d.ts",
|
|
9
10
|
"dependencies": {
|
|
10
11
|
"@wasm-audio-decoders/flac": "^0.2.1",
|
|
11
12
|
"@wasm-audio-decoders/ogg-vorbis": "^0.1.12",
|
package/test.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
|
|
2
|
-
import decodeAudio, {decoders} from './audio-decode.js';
|
|
2
|
+
import decodeAudio, { decoders } from './audio-decode.js';
|
|
3
3
|
import wav from 'audio-lena/wav.js';
|
|
4
4
|
import mp3 from 'audio-lena/mp3.js';
|
|
5
5
|
import ogg from 'audio-lena/ogg.js';
|
|
@@ -85,15 +85,23 @@ t('malformed data', async t => {
|
|
|
85
85
|
let log = []
|
|
86
86
|
try {
|
|
87
87
|
let x = await decodeAudio(new Float32Array(10))
|
|
88
|
-
} catch (e) { log.push('arr')}
|
|
88
|
+
} catch (e) { log.push('arr') }
|
|
89
89
|
|
|
90
90
|
try {
|
|
91
91
|
let x = await decodeAudio(null)
|
|
92
|
-
} catch (e) { log.push('null')}
|
|
92
|
+
} catch (e) { log.push('null') }
|
|
93
93
|
|
|
94
94
|
try {
|
|
95
95
|
let x = await decodeAudio(Promise.resolve())
|
|
96
|
-
} catch (e) { log.push('nonbuf')}
|
|
96
|
+
} catch (e) { log.push('nonbuf') }
|
|
97
97
|
|
|
98
98
|
is(log, ['arr', 'null', 'nonbuf'])
|
|
99
99
|
})
|
|
100
|
+
|
|
101
|
+
t.skip('sequence #38', async t => {
|
|
102
|
+
let flacBuffer = await decoders.flac(new Uint8Array(flac))
|
|
103
|
+
is(flacBuffer.duration | 0, 12, 'flac duration')
|
|
104
|
+
|
|
105
|
+
let mp3Buffer = await decoders.mp3(new Uint8Array(mp3))
|
|
106
|
+
is(mp3Buffer.duration | 0, 12, 'mp3 duration')
|
|
107
|
+
})
|