wavify-audiobuffer 1.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/LICENSE.md +21 -0
- package/README.md +55 -0
- package/index.d.ts +15 -0
- package/index.js +103 -0
- package/package.json +55 -0
- package/utils.d.ts +1 -0
- package/utils.js +30 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
The MIT License (MIT)
|
|
2
|
+
Copyright (c) 2015 Jam3
|
|
3
|
+
|
|
4
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
5
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
6
|
+
in the Software without restriction, including without limitation the rights
|
|
7
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
8
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
9
|
+
furnished to do so, subject to the following conditions:
|
|
10
|
+
|
|
11
|
+
The above copyright notice and this permission notice shall be included in all
|
|
12
|
+
copies or substantial portions of the Software.
|
|
13
|
+
|
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
17
|
+
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
|
|
18
|
+
DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
19
|
+
OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE
|
|
20
|
+
OR OTHER DEALINGS IN THE SOFTWARE.
|
|
21
|
+
|
package/README.md
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
# wavify-audiobuffer
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/wavify-audiobuffer) [](https://github.com/tucsok007/wavify-audiobuffer)
|
|
4
|
+
|
|
5
|
+
Encodes the contents of an [AudioBuffer](https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer) from the WebAudio API as WAVE. Supports 16-bit PCM, 24bit PCM and 32-bit float data.
|
|
6
|
+
|
|
7
|
+
This project is a fork and extension/rewrite of the [audiobuffer-to-wav](https://www.npmjs.com/audiobuffer-to-wav) npm library.
|
|
8
|
+
|
|
9
|
+
The package is designed to work in both web, Node.js, and Embedded environments, considering that the web audio api is available or polyfilled.
|
|
10
|
+
|
|
11
|
+
## Installation:
|
|
12
|
+
|
|
13
|
+
```sh
|
|
14
|
+
npm i wavify-audiobuffer
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
## Usage:
|
|
18
|
+
|
|
19
|
+
#### `arrayBuffer = wavifyBuffer(audioBuffer, {...options})`
|
|
20
|
+
|
|
21
|
+
Encodes the [AudioBuffer](https://developer.mozilla.org/en-US/docs/Web/API/AudioBuffer) instance as WAV, returning a new array buffer. Interleaves multi-channel data, if necessary.
|
|
22
|
+
|
|
23
|
+
By default, the function exports with 16-bit PCM.
|
|
24
|
+
|
|
25
|
+
You can optionally specify 24 or 32-bit bit depth.
|
|
26
|
+
|
|
27
|
+
## Example(s):
|
|
28
|
+
|
|
29
|
+
```typescript
|
|
30
|
+
import { wavifyBuffer } from "wavify-audiobuffer";
|
|
31
|
+
|
|
32
|
+
fetch("https://www.example.audio/track.mp3")
|
|
33
|
+
.then((response) => {
|
|
34
|
+
if (!response.ok) {
|
|
35
|
+
throw new Error("Couldn't fetch the track data.");
|
|
36
|
+
} else {
|
|
37
|
+
return response.arrayBuffer();
|
|
38
|
+
}
|
|
39
|
+
})
|
|
40
|
+
.then((arrayBuffer) => {
|
|
41
|
+
audioContext.decodeAudioData(arrayBuffer, (audioBuffer) => {
|
|
42
|
+
const waveData = wavifyBuffer(audioBuffer, { bitDepth: 24 });
|
|
43
|
+
//consume the waveData buffer
|
|
44
|
+
});
|
|
45
|
+
})
|
|
46
|
+
.catch((error) => {
|
|
47
|
+
//handle errors
|
|
48
|
+
});
|
|
49
|
+
```
|
|
50
|
+
|
|
51
|
+
See [this example](./examples/example.ts) for an example of loading an MP3, decoding it, and re-encoding it as a WAV file (in Node.js).
|
|
52
|
+
|
|
53
|
+
## License:
|
|
54
|
+
|
|
55
|
+
This project is licensed under the terms of MIT, please see the [LICENSE.md](./LICENSE.md) file for details.
|
package/index.d.ts
ADDED
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
declare enum WaveFormat {
|
|
2
|
+
WAVE_FORMAT_PCM = 1,
|
|
3
|
+
WAVE_FORMAT_EXTENSIBLE = 65279,
|
|
4
|
+
WAVE_FORMAT_IEEE_FLOAT = 3
|
|
5
|
+
}
|
|
6
|
+
declare const waveFormatTagsByBitDepth: {
|
|
7
|
+
16: WaveFormat;
|
|
8
|
+
24: WaveFormat;
|
|
9
|
+
32: WaveFormat;
|
|
10
|
+
};
|
|
11
|
+
interface IWavEncodeOptions {
|
|
12
|
+
bitDepth: keyof typeof waveFormatTagsByBitDepth;
|
|
13
|
+
}
|
|
14
|
+
export default function wavifyBuffer(audioBuffer: AudioBuffer, options?: IWavEncodeOptions): ArrayBuffer;
|
|
15
|
+
export {};
|
package/index.js
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.default = wavifyBuffer;
|
|
4
|
+
const utils_1 = require("./utils");
|
|
5
|
+
const PCM_RIFF_HEADER_SIZE = 36;
|
|
6
|
+
const PCM_FMT_SUBCHUNK_SIZE_EXCLUDING_SUBCHUNK_HEADER = 16;
|
|
7
|
+
var WaveFormat;
|
|
8
|
+
(function (WaveFormat) {
|
|
9
|
+
WaveFormat[WaveFormat["WAVE_FORMAT_PCM"] = 1] = "WAVE_FORMAT_PCM";
|
|
10
|
+
WaveFormat[WaveFormat["WAVE_FORMAT_EXTENSIBLE"] = 65279] = "WAVE_FORMAT_EXTENSIBLE";
|
|
11
|
+
WaveFormat[WaveFormat["WAVE_FORMAT_IEEE_FLOAT"] = 3] = "WAVE_FORMAT_IEEE_FLOAT";
|
|
12
|
+
})(WaveFormat || (WaveFormat = {}));
|
|
13
|
+
const waveFormatTagsByBitDepth = {
|
|
14
|
+
16: WaveFormat.WAVE_FORMAT_PCM,
|
|
15
|
+
24: WaveFormat.WAVE_FORMAT_PCM,
|
|
16
|
+
32: WaveFormat.WAVE_FORMAT_IEEE_FLOAT,
|
|
17
|
+
};
|
|
18
|
+
const defaultOptions = {
|
|
19
|
+
bitDepth: 16,
|
|
20
|
+
};
|
|
21
|
+
function wavifyBuffer(audioBuffer, options = defaultOptions) {
|
|
22
|
+
if (!audioBuffer) {
|
|
23
|
+
throw new Error("Please provide an AudioBuffer object.");
|
|
24
|
+
}
|
|
25
|
+
const samples = (0, utils_1.getInterleavedChannelDataByAudioBuffer)(audioBuffer);
|
|
26
|
+
return encodeWave(samples, audioBuffer.sampleRate, audioBuffer.numberOfChannels, options.bitDepth);
|
|
27
|
+
}
|
|
28
|
+
function getWaveFormatTagByBitDepth(bitDepth) {
|
|
29
|
+
return waveFormatTagsByBitDepth[bitDepth];
|
|
30
|
+
}
|
|
31
|
+
function encodeWave(samples, sampleRate, numberOfChannels, bitDepth) {
|
|
32
|
+
const bytesPerSample = bitDepth / 8;
|
|
33
|
+
const blockAlign = numberOfChannels * bytesPerSample;
|
|
34
|
+
const buffer = new ArrayBuffer(44 + samples.length * bytesPerSample);
|
|
35
|
+
const view = new DataView(buffer);
|
|
36
|
+
/* RIFF identifier */
|
|
37
|
+
writeStringAsUint8Chunks(view, 0, "RIFF");
|
|
38
|
+
/* RIFF chunk length */
|
|
39
|
+
view.setUint32(4, PCM_RIFF_HEADER_SIZE + samples.length * bytesPerSample, true);
|
|
40
|
+
/* RIFF type */
|
|
41
|
+
writeStringAsUint8Chunks(view, 8, "WAVE");
|
|
42
|
+
/* format chunk identifier */
|
|
43
|
+
writeStringAsUint8Chunks(view, 12, "fmt ");
|
|
44
|
+
/* format chunk length */
|
|
45
|
+
view.setUint32(16, PCM_FMT_SUBCHUNK_SIZE_EXCLUDING_SUBCHUNK_HEADER, true);
|
|
46
|
+
/* sample format (raw) */
|
|
47
|
+
view.setUint16(20, getWaveFormatTagByBitDepth(bitDepth), true);
|
|
48
|
+
/* channel count */
|
|
49
|
+
view.setUint16(22, numberOfChannels, true);
|
|
50
|
+
/* sample rate */
|
|
51
|
+
view.setUint32(24, sampleRate, true);
|
|
52
|
+
/* byte rate (sample rate * block align) */
|
|
53
|
+
view.setUint32(28, sampleRate * blockAlign, true);
|
|
54
|
+
/* block align (channel count * bytes per sample) */
|
|
55
|
+
view.setUint16(32, blockAlign, true);
|
|
56
|
+
/* bits per sample */
|
|
57
|
+
view.setUint16(34, bitDepth, true);
|
|
58
|
+
/* data chunk identifier */
|
|
59
|
+
writeStringAsUint8Chunks(view, 36, "data");
|
|
60
|
+
/* data chunk length */
|
|
61
|
+
view.setUint32(40, samples.length * bytesPerSample, true);
|
|
62
|
+
switch (bitDepth) {
|
|
63
|
+
case 16:
|
|
64
|
+
writeFloat32To16BitPCM(view, 44, samples);
|
|
65
|
+
break;
|
|
66
|
+
case 24:
|
|
67
|
+
writeFloat32To24BitPCM(view, 44, samples);
|
|
68
|
+
break;
|
|
69
|
+
case 32:
|
|
70
|
+
writeFloat32(view, 44, samples);
|
|
71
|
+
break;
|
|
72
|
+
}
|
|
73
|
+
return buffer;
|
|
74
|
+
}
|
|
75
|
+
function setUint24LittleEndian(view) {
|
|
76
|
+
return function (offset, value) {
|
|
77
|
+
view.setUint8(offset, value & ~0xffffff00);
|
|
78
|
+
view.setUint16(offset + 1, value >> 8, true);
|
|
79
|
+
};
|
|
80
|
+
}
|
|
81
|
+
function writeStringAsUint8Chunks(view, offset, value) {
|
|
82
|
+
for (let index = 0; index < value.length; index++) {
|
|
83
|
+
view.setUint8(offset + index, value.charCodeAt(index));
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
function writeFloat32(output, offset, input) {
|
|
87
|
+
for (let index = 0; index < input.length; index++, offset += 4) {
|
|
88
|
+
output.setFloat32(offset, input[index], true);
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
function writeFloat32To24BitPCM(output, offset, input) {
|
|
92
|
+
const writeToView = setUint24LittleEndian(output);
|
|
93
|
+
for (let index = 0; index < input.length; index++, offset += 3) {
|
|
94
|
+
let s = Math.max(-1, Math.min(1, input[index]));
|
|
95
|
+
writeToView(offset, s < 0 ? s * 0x800000 : s * 0x7fffff);
|
|
96
|
+
}
|
|
97
|
+
}
|
|
98
|
+
function writeFloat32To16BitPCM(output, offset, input) {
|
|
99
|
+
for (let index = 0; index < input.length; index++, offset += 2) {
|
|
100
|
+
let s = Math.max(-1, Math.min(1, input[index]));
|
|
101
|
+
output.setInt16(offset, s < 0 ? s * 0x8000 : s * 0x7fff, true);
|
|
102
|
+
}
|
|
103
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "wavify-audiobuffer",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "convert an AudioBuffer to .wav format",
|
|
5
|
+
"main": "index.js",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": {
|
|
8
|
+
"name": "Clio Orgyán",
|
|
9
|
+
"url": "https://github.com/tucsok007"
|
|
10
|
+
},
|
|
11
|
+
"devDependencies": {
|
|
12
|
+
"@types/jest": "^30.0.0",
|
|
13
|
+
"node-web-audio-api": "^2.0.0",
|
|
14
|
+
"ts-jest": "^29.4.11",
|
|
15
|
+
"typescript": "^6.0.3"
|
|
16
|
+
},
|
|
17
|
+
"scripts": {
|
|
18
|
+
"rebuild": "sh cleanup.sh && sh build.sh",
|
|
19
|
+
"test": "set NODE_OPTIONS=--experimental-vm-modules && jest --coverage"
|
|
20
|
+
},
|
|
21
|
+
"keywords": [
|
|
22
|
+
"convert",
|
|
23
|
+
"AudioBuffer",
|
|
24
|
+
"to",
|
|
25
|
+
"wav",
|
|
26
|
+
"format",
|
|
27
|
+
"for",
|
|
28
|
+
"browser",
|
|
29
|
+
"webaudio",
|
|
30
|
+
"web audio",
|
|
31
|
+
"audio",
|
|
32
|
+
"buffer",
|
|
33
|
+
"conversion",
|
|
34
|
+
"formats",
|
|
35
|
+
"fmt",
|
|
36
|
+
"riff",
|
|
37
|
+
"wave",
|
|
38
|
+
"sound"
|
|
39
|
+
],
|
|
40
|
+
"files": [
|
|
41
|
+
"*.d.ts",
|
|
42
|
+
"*.js",
|
|
43
|
+
"LICENSE.md",
|
|
44
|
+
"README.md",
|
|
45
|
+
"package.json"
|
|
46
|
+
],
|
|
47
|
+
"repository": {
|
|
48
|
+
"type": "git",
|
|
49
|
+
"url": "git://github.com/tucsok007/wavify-audiobuffer.git"
|
|
50
|
+
},
|
|
51
|
+
"homepage": "https://github.com/tucsok007/wavify-audiobuffer",
|
|
52
|
+
"bugs": {
|
|
53
|
+
"url": "https://github.com/tucsok007/wavify-audiobuffer/issues"
|
|
54
|
+
}
|
|
55
|
+
}
|
package/utils.d.ts
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const getInterleavedChannelDataByAudioBuffer: (audioBuffer: AudioBuffer) => Float32Array<ArrayBuffer>;
|
package/utils.js
ADDED
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getInterleavedChannelDataByAudioBuffer = void 0;
|
|
4
|
+
const getInterleavedChannelDataByAudioBuffer = (audioBuffer) => {
|
|
5
|
+
if (!audioBuffer ||
|
|
6
|
+
(!!audioBuffer.numberOfChannels && audioBuffer.numberOfChannels == 0)) {
|
|
7
|
+
throw new Error("Please provide an AudioBuffer object with at least 1 channel.");
|
|
8
|
+
}
|
|
9
|
+
const channels = [];
|
|
10
|
+
for (let channelIndex = 0; channelIndex < audioBuffer.numberOfChannels; channelIndex++) {
|
|
11
|
+
channels.push(audioBuffer.getChannelData(channelIndex));
|
|
12
|
+
}
|
|
13
|
+
return interleaveChannelData(...channels);
|
|
14
|
+
};
|
|
15
|
+
exports.getInterleavedChannelDataByAudioBuffer = getInterleavedChannelDataByAudioBuffer;
|
|
16
|
+
function interleaveChannelData(...channels) {
|
|
17
|
+
if (channels.length < 2) {
|
|
18
|
+
return channels[0];
|
|
19
|
+
}
|
|
20
|
+
const numberOfChannels = channels.length;
|
|
21
|
+
const channelLength = channels[0].length;
|
|
22
|
+
const result = new Float32Array(channelLength * numberOfChannels);
|
|
23
|
+
for (let frame = 0; frame < channelLength; frame++) {
|
|
24
|
+
for (let channelIndex = 0; channelIndex < numberOfChannels; channelIndex++) {
|
|
25
|
+
result[frame * numberOfChannels + channelIndex] =
|
|
26
|
+
channels[channelIndex][frame];
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
return result;
|
|
30
|
+
}
|