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.
@@ -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
+
@@ -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": "1.3.1",
3
+ "version": "2.0.0",
4
4
  "description": "Decode audio data in node or browser",
5
- "main": "index.js",
5
+ "main": "audio-decode.js",
6
+ "type": "module",
6
7
  "dependencies": {
7
- "audio-buffer-utils": "^5.0.1",
8
- "audio-context": "^1.0.1",
9
- "audio-type": "^1.0.2",
10
- "av": "^0.4.9",
11
- "is-buffer": "^1.1.4",
12
- "mp3": "^0.1.0",
13
- "to-array-buffer": "^2.0.0",
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-play": "^2.1.0",
21
- "audio-speaker": "^1.4.3",
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 [![Build Status](https://travis-ci.org/audiojs/audio-decode.svg?branch=master)](https://travis-ci.org/audiojs/audio-decode) [![unstable](https://img.shields.io/badge/stability-unstable-green.svg)](http://github.com/badges/stability-badges) [![Greenkeeper badge](https://badges.greenkeeper.io/audiojs/audio-decode.svg)](https://greenkeeper.io/)
2
-
3
- Convert `mp3`/`wav` audio data to [AudioBuffer](https://github.com/audiojs/audio-buffer).
4
-
5
- [![npm install audio-decode](https://nodei.co/npm/audio-decode.png?mini=true)](https://npmjs.org/package/audio-decode/)
6
-
7
- ```js
8
- const decode = require('audio-decode');
9
- const buffer = require('audio-lena/mp3');
10
-
11
- //as a callback
12
- decode(buffer, (err, audioBuffer) => {});
13
-
14
- //as a promise
15
- decode(buffer).then(audioBuffer => {}, err => {});
16
- ```
17
-
18
- ## API
19
-
20
- **`let promise = decode(source, {context}?, (err, audioBuffer)=>{}?)`**
21
-
22
- Decode `source`, based on `options` and fire `callback` when done, or resolve a promise if no callback passed. Source type can be: _ArrayBuffer_, _ArrayBufferView_, _Buffer_, _Blob_, _File_ or data-uri string.
23
-
24
- `options` may include `context` property for web-audio-api context (browser-only). By default new [audio-context](https://npmjs.org/package/audio-context) is created.
25
-
26
- ## Supported formats
27
-
28
- Browser version uses [decodeAudioData](https://developer.mozilla.org/en-US/docs/Web/API/AudioContext/decodeAudioData) method, in node the [aurora](https://npmjs.org/package/av) is used.
29
-
30
- Shipped by default:
31
-
32
- * _wav_ via [wav-decoder](https://github.com/mohayonao/wav-decoder)
33
- * _mp3_ via [aurora mp3](https://github.com/audiocogs/mp3.js)
34
-
35
- To enable additional format, install it as a dependency `npm install --save flac.js` and require once `require('flac.js')`.
36
-
37
- Additional formats available:
38
-
39
- * _flac_ via [flac.js](https://github.com/audiocogs/flac.js)
40
- * _alac_ via [alac.js](https://github.com/audiocogs/alac.js)
41
- * _aac_ via [aac.js](https://github.com/audiocogs/aac.js)
42
- * _vorbis_ via [vorbis.js](https://github.com/audiocogs/vorbis.js)
43
- * _ogg_ via [ogg.js](https://github.com/audiocogs/ogg.js)
44
- * _opus_ via [opus.js](https://github.com/audiocogs/opus.js)
45
-
46
-
47
- ## Credits
48
-
49
- * [@mohayonao](https://github.com/mohayonao/) 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) &copy; <a href="https://github.com/audiojs">audiojs</a>.
1
+ # audio-decode [![test](https://github.com/audiojs/audio-decode/actions/workflows/test.js.yml/badge.svg)](https://github.com/audiojs/audio-decode/actions/workflows/test.js.yml) [![stable](https://img.shields.io/badge/stability-unstable-green.svg)](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
+ [![npm install audio-decode](https://nodei.co/npm/audio-decode.png?mini=true)](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)&nbsp;&nbsp;•&nbsp;&nbsp;<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
- 'use strict';
2
-
3
- const decode = require('./');
4
- const wav = require('audio-lena/wav');
5
- const mp3 = require('audio-lena/mp3');
6
- const raw = require('audio-lena/raw');
7
- const context = require('audio-context')();
8
- const play = require('audio-play');
9
- const t = require('tape');
10
- const isBrowser = require('is-browser')
11
-
12
-
13
- //as a callback
14
- t('wav buffer', function (t) {
15
- decode(wav, (err, audioBuffer) => {
16
- try {
17
- play(audioBuffer, {end: 1}, () => t.end());
18
- } catch (e) {
19
- throw e;
20
- }
21
- });
22
- });
23
-
24
- t('mp3 buffer', function (t) {
25
- decode(mp3, (err, audioBuffer) => {
26
- try {
27
- play(audioBuffer, {end: 1}, () => {
28
- t.end()
29
- });
30
- } catch (e) {
31
- throw e;
32
- }
33
- });
34
- });
35
-
36
- t('mp3 base64', t => {
37
- decode(require('audio-lena/mp3-base64'), (err, buf) => {
38
- if (err) t.fail(err)
39
- t.end()
40
- })
41
- })
42
-
43
- t('mp3 datauri', t => {
44
- decode(require('audio-lena/mp3-datauri'), (err, buf) => {
45
- if (err) t.fail(err)
46
- t.end()
47
- })
48
- })
49
-
50
- isBrowser && t('File', t => {
51
- decode(new File([mp3], 'lena.mp3'), (err, audioBuffer) => {
52
- try {
53
- play(audioBuffer, {end: 1}, () => {
54
- t.end()
55
- });
56
- } catch (e) {
57
- throw e;
58
- }
59
- });
60
- })
61
-
62
- isBrowser && t('Blob', t => {
63
- decode(new Blob([mp3]), (err, audioBuffer) => {
64
- try {
65
- play(audioBuffer, {end: 1}, () => {
66
- t.end()
67
- });
68
- } catch (e) {
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
@@ -1,2 +0,0 @@
1
- node_modules
2
- *.log
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
- };