Passed
Push — master ( 6e4ba7...34794c )
by Rafael S.
02:21
created

lib/validate-wav-header.js   A

Complexity

Total Complexity 11
Complexity/F 2.75

Size

Lines of Code 60
Function Count 4

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 0
wmc 11
eloc 23
c 1
b 0
f 0
nc 1
mnd 2
bc 8
fnc 4
dl 0
loc 60
bpm 2
cpm 2.75
noi 0
rs 10

4 Functions

Rating   Name   Duplication   Size   Complexity  
A validate-wav-header.js ➔ validateSampleRate_ 0 9 3
A validate-wav-header.js ➔ validateWavHeader_ 0 5 1
A validate-wav-header.js ➔ validateNumChannels_ 0 8 3
A validate-wav-header.js ➔ validateBitDepth_ 0 10 4
1
/*
2
 * Copyright (c) 2018 Rafael da Silva Rocha.
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining
5
 * a copy of this software and associated documentation files (the
6
 * "Software"), to deal in the Software without restriction, including
7
 * without limitation the rights to use, copy, modify, merge, publish,
8
 * distribute, sublicense, and/or sell copies of the Software, and to
9
 * permit persons to whom the Software is furnished to do so, subject to
10
 * the following conditions:
11
 *
12
 * The above copyright notice and this permission notice shall be
13
 * included in all 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
18
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
19
 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
20
 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
21
 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 */
24
25
/**
26
 * @fileoverview A tool to validate wav file headers.
27
 * @see https://github.com/rochars/wavefile
28
 */
29
30
import WAV_AUDIO_FORMATS from './wav-audio-formats.js';
31
32
/**
33
 * Validate the header of the file.
34
 * @throws {Error} If any property of the object appears invalid.
35
 * @private
36
 */
37
export default function validateWavHeader_(header) {
38
  validateBitDepth_(header);
39
  validateNumChannels_(header);
40
  validateSampleRate_(header);
41
}
42
43
/**
44
 * Validate the bit depth.
45
 * @return {boolean} True is the bit depth is valid.
46
 * @throws {Error} If bit depth is invalid.
47
 * @private
48
 */
49
function validateBitDepth_(header) {
50
  if (!WAV_AUDIO_FORMATS[header.bitDepth]) {
51
    if (parseInt(header.bitDepth, 10) > 8 &&
52
        parseInt(header.bitDepth, 10) < 54) {
53
      return true;
54
    }
55
    throw new Error('Invalid bit depth.');
56
  }
57
  return true;
58
}
59
60
/**
61
 * Validate the number of channels.
62
 * @return {boolean} True is the number of channels is valid.
63
 * @throws {Error} If the number of channels is invalid.
64
 * @private
65
 */
66
function validateNumChannels_(header) {
67
  /** @type {number} */
68
  let blockAlign = header.fmt.numChannels * header.fmt.bitsPerSample / 8;
69
  if (header.fmt.numChannels < 1 || blockAlign > 65535) {
70
    throw new Error('Invalid number of channels.');
71
  }
72
  return true;
73
}
74
75
/**
76
 * Validate the sample rate value.
77
 * @return {boolean} True is the sample rate is valid.
78
 * @throws {Error} If the sample rate is invalid.
79
 * @private
80
 */
81
function validateSampleRate_(header) {
82
  /** @type {number} */
83
  let byteRate = header.fmt.numChannels *
84
    (header.fmt.bitsPerSample / 8) * header.fmt.sampleRate;
85
  if (header.fmt.sampleRate < 1 || byteRate > 4294967295) {
86
    throw new Error('Invalid sample rate.');
87
  }
88
  return true;
89
}