audio-decode 1.3.1 → 2.0.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/.github/workflows/test.js.yml +31 -0
- package/.travis.yml +13 -13
- package/LICENSE +22 -0
- package/audio-decode.js +63 -0
- package/lena.opus +0 -0
- package/package.json +19 -19
- package/readme.md +47 -59
- package/test.html +15 -0
- package/test.js +68 -109
- package/.npmignore +0 -2
- package/browser.js +0 -48
- package/index.js +0 -67
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
# This workflow will do a clean installation of node dependencies, cache/restore them, build the source code and run tests across different versions of node
|
|
2
|
+
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-nodejs
|
|
3
|
+
|
|
4
|
+
name: test
|
|
5
|
+
|
|
6
|
+
on:
|
|
7
|
+
push:
|
|
8
|
+
branches: [ "master" ]
|
|
9
|
+
pull_request:
|
|
10
|
+
branches: [ "master" ]
|
|
11
|
+
|
|
12
|
+
jobs:
|
|
13
|
+
build:
|
|
14
|
+
|
|
15
|
+
runs-on: ubuntu-latest
|
|
16
|
+
|
|
17
|
+
strategy:
|
|
18
|
+
matrix:
|
|
19
|
+
node-version: [14.x, 16.x, 18.x]
|
|
20
|
+
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
|
|
21
|
+
|
|
22
|
+
steps:
|
|
23
|
+
- uses: actions/checkout@v3
|
|
24
|
+
- name: Use Node.js ${{ matrix.node-version }}
|
|
25
|
+
uses: actions/setup-node@v3
|
|
26
|
+
with:
|
|
27
|
+
node-version: ${{ matrix.node-version }}
|
|
28
|
+
cache: 'npm'
|
|
29
|
+
- run: npm ci
|
|
30
|
+
- run: npm run build --if-present
|
|
31
|
+
- run: npm test
|
package/.travis.yml
CHANGED
|
@@ -1,13 +1,13 @@
|
|
|
1
|
-
sudo: false
|
|
2
|
-
language: node_js
|
|
3
|
-
node_js:
|
|
4
|
-
- 'node'
|
|
5
|
-
- '6'
|
|
6
|
-
- '5'
|
|
7
|
-
- '4'
|
|
8
|
-
matrix:
|
|
9
|
-
fast_finish: true
|
|
10
|
-
allow_failures:
|
|
11
|
-
- node_js: "4"
|
|
12
|
-
- node_js: "5"
|
|
13
|
-
|
|
1
|
+
sudo: false
|
|
2
|
+
language: node_js
|
|
3
|
+
node_js:
|
|
4
|
+
- 'node'
|
|
5
|
+
- '6'
|
|
6
|
+
- '5'
|
|
7
|
+
- '4'
|
|
8
|
+
matrix:
|
|
9
|
+
fast_finish: true
|
|
10
|
+
allow_failures:
|
|
11
|
+
- node_js: "4"
|
|
12
|
+
- node_js: "5"
|
|
13
|
+
|
package/LICENSE
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright (c) 2018 Dmitry Ivanov <df.creative@gmail.com>
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
16
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
17
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
18
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
19
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
20
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
|
21
|
+
OR OTHER DEALINGS IN THE SOFTWARE.
|
|
22
|
+
|
package/audio-decode.js
ADDED
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Web-Audio-API decoder
|
|
3
|
+
* @module audio-decode
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import getType from 'audio-type';
|
|
7
|
+
import AudioBufferShim from 'audio-buffer';
|
|
8
|
+
|
|
9
|
+
const AudioBuffer = globalThis.AudioBuffer || AudioBufferShim
|
|
10
|
+
|
|
11
|
+
export default async function audioDecode (buf) {
|
|
12
|
+
if (!buf) throw Error('No decode target')
|
|
13
|
+
buf = new Uint8Array(buf.buffer || buf)
|
|
14
|
+
|
|
15
|
+
let type = getType(buf);
|
|
16
|
+
|
|
17
|
+
if (!type) throw Error('Cannot detect audio format');
|
|
18
|
+
|
|
19
|
+
if (!decoders[type]) throw Error('Missing decoder for ' + type + ' format')
|
|
20
|
+
|
|
21
|
+
return decoders[type](buf)
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const decoders = {
|
|
25
|
+
async oga(buf) {
|
|
26
|
+
let { OggVorbisDecoder } = await import('@wasm-audio-decoders/ogg-vorbis')
|
|
27
|
+
const decoder = new OggVorbisDecoder()
|
|
28
|
+
await decoder.ready;
|
|
29
|
+
return (decoders.oga = async buf => buf && createBuffer(await decoder.decodeFile(buf)))(buf)
|
|
30
|
+
},
|
|
31
|
+
async mp3(buf) {
|
|
32
|
+
const { MPEGDecoder } = await import('mpg123-decoder')
|
|
33
|
+
const decoder = new MPEGDecoder()
|
|
34
|
+
await decoder.ready;
|
|
35
|
+
return (decoders.mp3 = buf => buf && createBuffer(decoder.decode(buf)))(buf)
|
|
36
|
+
},
|
|
37
|
+
async flac(buf) {
|
|
38
|
+
const { FLACDecoder } = await import('@wasm-audio-decoders/flac')
|
|
39
|
+
const decoder = new FLACDecoder()
|
|
40
|
+
await decoder.ready;
|
|
41
|
+
return (decoders.mp3 = async buf => buf && createBuffer(await decoder.decode(buf)))(buf)
|
|
42
|
+
},
|
|
43
|
+
async opus(buf) {
|
|
44
|
+
const { OggOpusDecoder } = await import('ogg-opus-decoder')
|
|
45
|
+
const decoder = new OggOpusDecoder()
|
|
46
|
+
await decoder.ready;
|
|
47
|
+
return (decoders.opus = async buf => buf && createBuffer(await decoder.decodeFile(buf)))(buf)
|
|
48
|
+
},
|
|
49
|
+
async wav(buf) {
|
|
50
|
+
let {decode} = await import('node-wav')
|
|
51
|
+
return (decoders.wav = buf => buf && createBuffer(decode(buf)) )(buf)
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
function createBuffer({channelData, sampleRate}) {
|
|
56
|
+
let audioBuffer = new AudioBuffer({
|
|
57
|
+
sampleRate,
|
|
58
|
+
length: channelData[0].length,
|
|
59
|
+
numberOfChannels: channelData.length
|
|
60
|
+
})
|
|
61
|
+
for (let ch = 0; ch < channelData.length; ch++) audioBuffer.getChannelData(ch).set(channelData[ch])
|
|
62
|
+
return audioBuffer
|
|
63
|
+
}
|
package/lena.opus
ADDED
|
Binary file
|
package/package.json
CHANGED
|
@@ -1,32 +1,26 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "audio-decode",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "2.0.0",
|
|
4
4
|
"description": "Decode audio data in node or browser",
|
|
5
|
-
"main": "
|
|
5
|
+
"main": "audio-decode.js",
|
|
6
|
+
"type": "module",
|
|
6
7
|
"dependencies": {
|
|
7
|
-
"audio-
|
|
8
|
-
"audio-
|
|
9
|
-
"audio-
|
|
10
|
-
"
|
|
11
|
-
"
|
|
12
|
-
"
|
|
13
|
-
"
|
|
14
|
-
"typedarray-to-buffer": "^3.1.2",
|
|
15
|
-
"wav-decoder": "^1.1.0"
|
|
8
|
+
"@wasm-audio-decoders/flac": "^0.1.3",
|
|
9
|
+
"@wasm-audio-decoders/ogg-vorbis": "^0.0.1",
|
|
10
|
+
"audio-buffer": "^5.0.0",
|
|
11
|
+
"audio-type": "^2.0.0",
|
|
12
|
+
"mpg123-decoder": "^0.4.3",
|
|
13
|
+
"node-wav": "^0.0.2",
|
|
14
|
+
"ogg-opus-decoder": "^1.5.3"
|
|
16
15
|
},
|
|
17
|
-
"optionalDependencies": {},
|
|
18
16
|
"devDependencies": {
|
|
19
17
|
"audio-lena": "^2.0.0",
|
|
20
|
-
"audio-
|
|
21
|
-
"
|
|
22
|
-
"tape": "^4.6.3"
|
|
18
|
+
"audio-speaker": "^1.5.0",
|
|
19
|
+
"tst": "^7.1.1"
|
|
23
20
|
},
|
|
24
21
|
"browser": "./browser.js",
|
|
25
22
|
"scripts": {
|
|
26
|
-
"test": "node test.js"
|
|
27
|
-
"test:browser": "budo test.js",
|
|
28
|
-
"preversion": "npm run lint && npm run test",
|
|
29
|
-
"lint": "eslint *.js --ignore-pattern test*"
|
|
23
|
+
"test": "node test.js"
|
|
30
24
|
},
|
|
31
25
|
"repository": {
|
|
32
26
|
"type": "git",
|
|
@@ -37,9 +31,15 @@
|
|
|
37
31
|
"audio",
|
|
38
32
|
"dsp",
|
|
39
33
|
"decode",
|
|
34
|
+
"audio decode",
|
|
35
|
+
"audio decoder",
|
|
36
|
+
"web audio decoder",
|
|
40
37
|
"codec",
|
|
41
38
|
"mp3",
|
|
42
39
|
"wav",
|
|
40
|
+
"ogg",
|
|
41
|
+
"vorbis",
|
|
42
|
+
"opus",
|
|
43
43
|
"web-audio"
|
|
44
44
|
],
|
|
45
45
|
"author": "ΔY <dfcreative@gmail.com>",
|
package/readme.md
CHANGED
|
@@ -1,59 +1,47 @@
|
|
|
1
|
-
# audio-decode [ for [wav-decoder](https://github.com/mohayonao/wav-decoder).
|
|
50
|
-
* [@devongovett](https://github.com/devongovett) and [@jensnockert](https://github.com/jensnockert) for [aurora.js](https://github.com/audiocogs/aurora.js).
|
|
51
|
-
* [@jamen](https://github.com/jamen) as originator of this package.
|
|
52
|
-
|
|
53
|
-
## Related
|
|
54
|
-
|
|
55
|
-
* [context.decodeAudioData](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/decodeAudioData) web-audio-api
|
|
56
|
-
|
|
57
|
-
## License
|
|
58
|
-
|
|
59
|
-
[MIT](LICENSE) © <a href="https://github.com/audiojs">audiojs</a>.
|
|
1
|
+
# audio-decode [](https://github.com/audiojs/audio-decode/actions/workflows/test.js.yml) [](http://github.com/badges/stability-badges)
|
|
2
|
+
|
|
3
|
+
Decode audio data from supported format to [AudioBuffer](https://github.com/audiojs/audio-buffer).
|
|
4
|
+
|
|
5
|
+
Supported formats:
|
|
6
|
+
|
|
7
|
+
* [x] `wav`
|
|
8
|
+
* [x] `mp3`
|
|
9
|
+
* [x] `ogg vorbis`
|
|
10
|
+
* [x] `flac`
|
|
11
|
+
* [x] `opus`
|
|
12
|
+
* [ ] `alac`
|
|
13
|
+
* [ ] `aac`
|
|
14
|
+
* [ ] `m4a`
|
|
15
|
+
|
|
16
|
+
[](https://npmjs.org/package/audio-decode/)
|
|
17
|
+
|
|
18
|
+
```js
|
|
19
|
+
import decodeAudio from 'audio-decode';
|
|
20
|
+
import buffer from 'audio-lena/mp3';
|
|
21
|
+
|
|
22
|
+
let audioBuffer = await decode(buffer);
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
`buffer` type can be: _ArrayBuffer_, _Uint8Array_ or _Buffer_.
|
|
26
|
+
|
|
27
|
+
Decoder's code is lazy, first run loads decoder's sources and compiles module, subsequent runs are fast.
|
|
28
|
+
|
|
29
|
+
To get more granular control over individual decoders, use `decoders`:
|
|
30
|
+
|
|
31
|
+
```js
|
|
32
|
+
import decode, {decoders} from 'audio-decode';
|
|
33
|
+
|
|
34
|
+
await decoders.mp3(); // load & compile decoder
|
|
35
|
+
const audioBuffer = await decoders.mp3(mp3buf); // decode
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
## See also
|
|
39
|
+
|
|
40
|
+
* [Wasm-audio-decoders](https://github.com/eshaz/wasm-audio-decoders) – best in class compact & fast WASM audio decoders.
|
|
41
|
+
* [AudioDecoder](https://developer.mozilla.org/en-US/docs/Web/API/AudioDecoder) – native decoders API, hope one day will be fixed or alternatively polyfilled.
|
|
42
|
+
* [decodeAudioData](https://github.com/eshaz/wasm-audio-decoders) – default in-browser decoding method.
|
|
43
|
+
|
|
44
|
+
## License
|
|
45
|
+
|
|
46
|
+
[MIT](LICENSE) • <a href="https://github.com/krishnized/license/">🕉</a>
|
|
47
|
+
|
package/test.html
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
<!DOCTYPE html>
|
|
2
|
+
<script type="importmap">
|
|
3
|
+
{
|
|
4
|
+
"imports": {
|
|
5
|
+
"tst": "./node_modules/tst/tst.js",
|
|
6
|
+
"audio-type": "./node_modules/audio-type/audio-type.js",
|
|
7
|
+
"audio-buffer": "./node_modules/audio-buffer/index.js",
|
|
8
|
+
"audio-lena/wav.js": "./node_modules/audio-lena/wav.js",
|
|
9
|
+
"audio-lena/mp3.js": "./node_modules/audio-lena/mp3.js",
|
|
10
|
+
"audio-lena/ogg.js": "./node_modules/audio-lena/ogg.js",
|
|
11
|
+
"audio-lena/flac.js": "./node_modules/audio-lena/flac.js"
|
|
12
|
+
}
|
|
13
|
+
}
|
|
14
|
+
</script>
|
|
15
|
+
<script src="./test.js" type="module"></script>
|
package/test.js
CHANGED
|
@@ -1,109 +1,68 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
throw e;
|
|
70
|
-
}
|
|
71
|
-
});
|
|
72
|
-
})
|
|
73
|
-
|
|
74
|
-
t.skip('raw floats', function (t) {
|
|
75
|
-
decode(raw, (err, audioBuffer) => {
|
|
76
|
-
play(audioBuffer, {end: 1}, () => {
|
|
77
|
-
t.end()
|
|
78
|
-
});
|
|
79
|
-
})
|
|
80
|
-
})
|
|
81
|
-
|
|
82
|
-
t('promise', t => {
|
|
83
|
-
decode(wav).then(audioBuffer => {
|
|
84
|
-
play(audioBuffer, {end: 1}, () => {
|
|
85
|
-
t.end()
|
|
86
|
-
});
|
|
87
|
-
}, err => {
|
|
88
|
-
t.fail(err)
|
|
89
|
-
});
|
|
90
|
-
})
|
|
91
|
-
|
|
92
|
-
t('decode error', t => {
|
|
93
|
-
decode(new Float32Array(10)).then(data => {
|
|
94
|
-
t.fail(data)
|
|
95
|
-
}, err => {
|
|
96
|
-
t.ok(err)
|
|
97
|
-
})
|
|
98
|
-
|
|
99
|
-
decode(null).then(data => {
|
|
100
|
-
t.fail(data)
|
|
101
|
-
}, err => {
|
|
102
|
-
t.ok(err)
|
|
103
|
-
})
|
|
104
|
-
|
|
105
|
-
decode(require('audio-lena/mp3-base64') + 'xxx', (err, buf) => {
|
|
106
|
-
t.ok(err)
|
|
107
|
-
t.end()
|
|
108
|
-
})
|
|
109
|
-
})
|
|
1
|
+
|
|
2
|
+
import decode, {decoders} from './audio-decode.js';
|
|
3
|
+
import wav from 'audio-lena/wav.js';
|
|
4
|
+
import mp3 from 'audio-lena/mp3.js';
|
|
5
|
+
import ogg from 'audio-lena/ogg.js';
|
|
6
|
+
import flac from 'audio-lena/flac.js';
|
|
7
|
+
import t, { is, throws } from 'tst';
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
//as a callback
|
|
11
|
+
t('wav buffer', async function (t) {
|
|
12
|
+
console.time('wav first')
|
|
13
|
+
await decoders.wav()
|
|
14
|
+
console.timeEnd('wav first')
|
|
15
|
+
|
|
16
|
+
console.time('wav second')
|
|
17
|
+
let audioBuffer = await decode(wav)
|
|
18
|
+
console.timeEnd('wav second')
|
|
19
|
+
is(audioBuffer.duration | 0, 12, 'wav duration')
|
|
20
|
+
});
|
|
21
|
+
|
|
22
|
+
t('mp3 buffer', async function (t) {
|
|
23
|
+
console.time('mp3 first')
|
|
24
|
+
await decoders.mp3()
|
|
25
|
+
console.timeEnd('mp3 first')
|
|
26
|
+
|
|
27
|
+
console.time('mp3 second')
|
|
28
|
+
let audioBuffer = await decode(mp3)
|
|
29
|
+
console.timeEnd('mp3 second')
|
|
30
|
+
is(audioBuffer.duration | 0, 12, 'mp3 duration')
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
t('ogg buffer', async function (t) {
|
|
34
|
+
console.time('ogg first')
|
|
35
|
+
let audioBuffer = await decode(ogg)
|
|
36
|
+
console.timeEnd('ogg first')
|
|
37
|
+
is(audioBuffer.duration | 0, 12, 'ogg duration')
|
|
38
|
+
|
|
39
|
+
console.time('ogg second')
|
|
40
|
+
audioBuffer = await decode(ogg)
|
|
41
|
+
console.timeEnd('ogg second')
|
|
42
|
+
is(audioBuffer.duration | 0, 12, 'ogg duration')
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
t('flac buffer', async function (t) {
|
|
46
|
+
console.time('flac first')
|
|
47
|
+
let audioBuffer = await decode(flac)
|
|
48
|
+
console.timeEnd('flac first')
|
|
49
|
+
is(audioBuffer.duration | 0, 12, 'flac duration')
|
|
50
|
+
|
|
51
|
+
console.time('flac second')
|
|
52
|
+
audioBuffer = await decode(flac)
|
|
53
|
+
console.timeEnd('flac second')
|
|
54
|
+
is(audioBuffer.duration | 0, 12, 'flac duration')
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
t('malformed data', async t => {
|
|
58
|
+
let log = []
|
|
59
|
+
try {
|
|
60
|
+
let x = await decode(new Float32Array(10))
|
|
61
|
+
} catch (e) { log.push('arr')}
|
|
62
|
+
|
|
63
|
+
try {
|
|
64
|
+
let x = await decode(null)
|
|
65
|
+
} catch (e) { log.push('null')}
|
|
66
|
+
|
|
67
|
+
is(log, ['arr', 'null'])
|
|
68
|
+
})
|
package/.npmignore
DELETED
package/browser.js
DELETED
|
@@ -1,48 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Web-Audio-API decoder fn style
|
|
3
|
-
*
|
|
4
|
-
* @module audio-decode
|
|
5
|
-
*/
|
|
6
|
-
'use strict';
|
|
7
|
-
|
|
8
|
-
const getContext = require('audio-context')
|
|
9
|
-
const toAB = require('to-array-buffer')
|
|
10
|
-
|
|
11
|
-
module.exports = decode;
|
|
12
|
-
|
|
13
|
-
function decode (buffer, opts, cb) {
|
|
14
|
-
if (opts instanceof Function) {
|
|
15
|
-
cb = opts;
|
|
16
|
-
opts = {};
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
if (!opts) opts = {};
|
|
20
|
-
|
|
21
|
-
let ctx = opts.context || getContext();
|
|
22
|
-
|
|
23
|
-
//blob/file cases
|
|
24
|
-
if (buffer instanceof Blob) buffer = new File([buffer], 'decode')
|
|
25
|
-
if (buffer instanceof File) {
|
|
26
|
-
return new Promise((resolve, reject) => {
|
|
27
|
-
try {
|
|
28
|
-
let reader = new FileReader()
|
|
29
|
-
reader.readAsArrayBuffer(buffer)
|
|
30
|
-
reader.onload = () => {
|
|
31
|
-
return resolve(decode(reader.result, opts, cb))
|
|
32
|
-
}
|
|
33
|
-
} catch (e) {
|
|
34
|
-
reject(e)
|
|
35
|
-
}
|
|
36
|
-
})
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
if (!(buffer instanceof ArrayBuffer)) {
|
|
40
|
-
buffer = toAB(buffer)
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return ctx.decodeAudioData(buffer, (buf) => {
|
|
44
|
-
cb && cb(null, buf);
|
|
45
|
-
}, (err) => {
|
|
46
|
-
cb && cb(err);
|
|
47
|
-
});
|
|
48
|
-
}
|
package/index.js
DELETED
|
@@ -1,67 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Web-Audio-API decoder
|
|
3
|
-
*
|
|
4
|
-
* @module audio-decode
|
|
5
|
-
*/
|
|
6
|
-
'use strict';
|
|
7
|
-
|
|
8
|
-
const getType = require('audio-type');
|
|
9
|
-
const WavDecoder = require('wav-decoder');
|
|
10
|
-
const util = require('audio-buffer-utils');
|
|
11
|
-
const toArrayBuffer = require('to-array-buffer')
|
|
12
|
-
const toBuffer = require('typedarray-to-buffer')
|
|
13
|
-
const isBuffer = require('is-buffer');
|
|
14
|
-
const AV = require('av');
|
|
15
|
-
require('mp3');
|
|
16
|
-
|
|
17
|
-
module.exports = (buffer, opts, cb) => {
|
|
18
|
-
if (opts instanceof Function) {
|
|
19
|
-
cb = opts;
|
|
20
|
-
opts = {};
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
if (!opts) opts = {};
|
|
24
|
-
if (!cb) cb = (() => {});
|
|
25
|
-
|
|
26
|
-
if (!isBuffer(buffer)) {
|
|
27
|
-
if (ArrayBuffer.isView(buffer)) buffer = toBuffer(buffer)
|
|
28
|
-
else {
|
|
29
|
-
buffer = Buffer.from(toArrayBuffer(buffer));
|
|
30
|
-
}
|
|
31
|
-
}
|
|
32
|
-
|
|
33
|
-
let type = getType(buffer);
|
|
34
|
-
|
|
35
|
-
if (!type) {
|
|
36
|
-
let err = Error('Cannot detect audio format of buffer');
|
|
37
|
-
cb(err);
|
|
38
|
-
return Promise.reject(err);
|
|
39
|
-
}
|
|
40
|
-
|
|
41
|
-
if (type === 'wav') {
|
|
42
|
-
return WavDecoder.decode(buffer).then(audioData => {
|
|
43
|
-
let audioBuffer = util.create(audioData.channelData, audioData.numberOfChannels, audioData.sampleRate);
|
|
44
|
-
cb(null, audioBuffer);
|
|
45
|
-
return Promise.resolve(audioBuffer);
|
|
46
|
-
}, err => {
|
|
47
|
-
cb(err);
|
|
48
|
-
return Promise.reject(err);
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
return new Promise((resolve, reject) => {
|
|
53
|
-
//handle other codecs by AV
|
|
54
|
-
let asset = AV.Asset.fromBuffer(buffer);
|
|
55
|
-
|
|
56
|
-
asset.on('error', err => {
|
|
57
|
-
cb(err)
|
|
58
|
-
reject(err)
|
|
59
|
-
})
|
|
60
|
-
|
|
61
|
-
asset.decodeToBuffer((buffer) => {
|
|
62
|
-
let data = util.create(buffer, asset.format.channelsPerFrame, asset.format.sampleRate)
|
|
63
|
-
cb(null, data);
|
|
64
|
-
resolve(data)
|
|
65
|
-
});
|
|
66
|
-
});
|
|
67
|
-
};
|