Completed
Push — master ( d13d03...0b358a )
by Rafael S.
01:38
created

riff.js ➔ getChunks   B

Complexity

Conditions 2
Paths 2

Size

Total Lines 33

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 2
c 1
b 0
f 0
nc 2
dl 0
loc 33
rs 8.8571
nop 2
1
/*
2
 * riff
3
 * Get the chunks of a RIFF file.
4
 * TODO: This should be a npm package on its own.
5
 * Copyright (c) 2017 Rafael da Silva Rocha. MIT License.
6
 * https://github.com/rochars/wavefile
7
 *
8
 */
9
10
const byteData = require("byte-data");
11
12
/**
13
 * Get the chunks of a RIFF file.
14
 * @param {Uint8Array|!Array<number>} buffer the RIFF file bytes.
15
 * @param {boolean} bigEndian true if its RIFX.
16
 * @return {object}
17
 */
18
function getChunks(buffer, bigEndian) {
19
    
20
    // RIFF container
21
    let chunkId = byteData.fromBytes(
22
            buffer.slice(0, 4), 8, {"char": true}
23
        );
24
    let chunkSize = byteData.fromBytes(
25
            buffer.slice(4, 8), 32, {'be': bigEndian}
26
        )[0];
27
    let format = byteData.fromBytes(
28
            buffer.slice(8, 12), 8, {"char": true}
29
        );
30
31
    let chunks = [];
32
    let len = buffer.length;
33
    let i = 12;
34
    while(i < len) {
35
        let subChunkSize = byteData.fromBytes(
36
            buffer.slice(i + 4, i + 8), 32, {'be': bigEndian})[0];
37
        chunks.push({
38
                "subChunkId": byteData.fromBytes(buffer.slice(i, i + 4), 8, {"char": true}),
39
                "subChunkSize": subChunkSize,
40
                "subChunkData": buffer.slice(i + 8, i + 8 + subChunkSize)
41
            });
42
        i = i + 8 + subChunkSize;
43
    }
44
    return {
45
        "chunkId": chunkId,
46
        "chunkSize": chunkSize,
47
        "format": format,
48
        "subChunks": chunks
49
    }
50
}
51
52
module.exports.getChunks = getChunks;
53