Completed
Push — master ( fddde1...99a577 )
by Rafael S.
01:33
created

index.js (2 issues)

1
/*!
2
 * wavefile
3
 * Read & write wave files with 8, 16, 24, 32 PCM, 32 IEEE & 64-bit data.
4
 * Copyright (c) 2017 Rafael da Silva Rocha.
5
 * https://github.com/rochars/wavefile
6
 *
7
 */
8
9
const bitDepthLib = require("bitdepth");
10
const waveFileReaderWriter = require("./src/wavefile-reader-writer");
11
12
/**
13
 * WaveFile
14
 */
15
class WaveFile extends waveFileReaderWriter.WaveFileReaderWriter {
16
17
    /**
18
     * @param {Uint8Array} bytes A wave file buffer.
19
     * @param {boolean} enforceFact True if it should throw a error
0 ignored issues
show
The parameter enforceFact does not exist. Did you maybe forget to remove this comment?
Loading history...
20
     *      if no "fact" chunk is found.
21
     * @param {boolean} enforceBext True if it should throw a error
0 ignored issues
show
The parameter enforceBext does not exist. Did you maybe forget to remove this comment?
Loading history...
22
     *      if no "bext" chunk is found.
23
     */
24
    constructor(bytes) {
25
        super();
26
        if(bytes) {
27
            this.fromBuffer(bytes);
28
        }
29
    }
30
31
    /**
32
     * Create a WaveFile object based on the arguments passed.
33
     * @param {number} numChannels The number of channels
34
     *     (Ints like 1 for mono, 2 stereo and so on).
35
     * @param {number} sampleRate The sample rate.
36
     *     Integer numbers like 8000, 44100, 48000, 96000, 192000.
37
     * @param {string} bitDepth The audio bit depth.
38
     *     One of "8", "16", "24", "32", "32f", "64".
39
     * @param {!Array<number>} samples Array of samples to be written.
40
     *     Samples must be in the correct range according to the bit depth.
41
     *     Samples of multi-channel data .
42
     */
43
    fromScratch(numChannels, sampleRate, bitDepth, samples, options={}) {
44
        if (!options.container) {
45
            options.container = "RIFF";
46
        }
47
        let bytes = parseInt(bitDepth, 10) / 8;
48
        this.chunkSize = 36 + samples.length * bytes;
49
        this.fmtChunkSize = 16;
50
        this.byteRate = (numChannels * bytes) * sampleRate;
51
        this.blockAlign = numChannels * bytes;
52
        this.chunkId = options.container;
53
        this.format = "WAVE";
54
        this.fmtChunkId = "fmt ";
55
        this.audioFormat = this.headerFormats_[bitDepth];
56
        this.numChannels = numChannels;
57
        this.sampleRate = sampleRate;
58
        this.bitsPerSample = parseInt(bitDepth, 10);
59
        this.dataChunkId = "data";
60
        this.dataChunkSize = samples.length * bytes;
61
        this.samples_ = samples;
62
        this.bitDepth_ = bitDepth;
63
    }
64
    
65
    /**
66
     * Turn the file to RIFF.
67
     * All values will be little-endian when writing.
68
     */
69
    toRIFF() {
70
        this.chunkId = "RIFF";
71
    }
72
73
    /**
74
     * Turn the file to RIFX.
75
     * All values but FourCCs will be big-endian when writing.
76
     */
77
    toRIFX() {
78
        this.chunkId = "RIFX";
79
    }
80
81
    /**
82
     * Change the bit depth of the data.
83
     * @param {string} bitDepth The new bit depth of the data.
84
     *      One of "8", "16", "24", "32", "32f", "64"
85
     */
86
    toBitDepth(bitDepth) {
87
        bitDepthLib.toBitDepth(this.samples_, this.bitDepth_, bitDepth);
88
        this.fromScratch(
89
            this.numChannels,
90
            this.sampleRate,
91
            bitDepth,
92
            this.samples_,
93
            {"container": this.chunkId}
94
        );
95
    }
96
97
    /**
98
     * Interleave multi-channel samples.
99
     */
100
    interleave() {
101
        let finalSamples = [];
102
        let i;
103
        let j;
104
        let numChannels = this.samples_[0].length;
105
        for (i = 0; i < numChannels; i++) {
106
            for (j = 0; j < this.samples_.length; j++) {
107
                finalSamples.push(this.samples_[j][i]);
108
            }
109
        }
110
        this.samples_ = finalSamples;
111
    }
112
113
    /**
114
     * De-interleave samples into multiple channels.
115
     */
116
    deInterleave() {
117
        let finalSamples = [];
118
        let i;
119
        for (i = 0; i < this.numChannels; i++) {
120
            finalSamples[i] = [];
121
        }
122
        i = 0;
123
        let j;
124
        while (i < this.samples_.length) {
125
            for (j = 0; j < this.numChannels; j++) {
126
                finalSamples[j].push(this.samples_[i+j]);
127
            }
128
            i += j;
129
        }
130
        this.samples_ = finalSamples;
131
    }
132
}
133
134
module.exports.WaveFile = WaveFile;
135
136