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

src/riff.js   A

Complexity

Total Complexity 2
Complexity/F 2

Size

Lines of Code 43
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 2
c 1
b 0
f 0
nc 1
mnd 1
bc 2
fnc 1
dl 0
loc 43
rs 10
bpm 2
cpm 2
noi 0

1 Function

Rating   Name   Duplication   Size   Complexity  
B riff.js ➔ getChunks 0 33 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