Passed
Push — master ( 13193c...29a9de )
by Rafael S.
02:59
created

RIFFFile.readUInt16   A

Complexity

Conditions 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 4
dl 0
loc 6
rs 10
c 0
b 0
f 0
cc 1
1
/*
2
 * Copyright (c) 2017-2019 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 The RIFFFile class.
27
 * @see https://github.com/rochars/wavefile
28
 */
29
30
/** @module wavefile */
31
32
import {unpackString, unpack} from 'byte-data';
33
34
/**
35
 * A class to perform low-level reading of RIFF/RIFX files.
36
 */
37
export default class RIFFFile {
38
39
  constructor() {
40
    
41
    /** @type {number} */
42
    this.head_ = 0;
43
    /** @type {!Object} */
44
    this.uInt32_ = {bits: 32, be: false};
45
    /** @type {!Object} */
46
    this.uInt16_ = {bits: 16, be: false};
47
    /**
48
     * The list of supported containers.
49
     * Any format different from RIFX will be treated as RIFF.
50
     * @type {!Array<string>}
51
     * @protected
52
     */
53
    this.supported_containers = ['RIFF', 'RIFX'];
54
    /**
55
     * The container identifier.
56
     * 'RIFF', 'RIFX' and 'RF64' are supported.
57
     * @type {string}
58
     */
59
    this.container = '';
60
    /**
61
     * @type {number}
62
     */
63
    this.chunkSize = 0;
64
    /**
65
     * The format.
66
     * @type {string}
67
     */
68
    this.format = '';
69
    /**
70
     * A object defining the start and end of all chunks in a wav buffer.
71
     * @type {!Object}
72
     */
73
    this.signature = {};
74
  }
75
76
  /**
77
   * Read the signature of the chunks in a RIFF/RIFX file.
78
   * @param {!Uint8Array} buffer The file bytes.
79
   * @protected
80
   */
81
  setSignature(buffer) {
82
      this.head_ = 0;
83
84
      // TODO the container should always come from this.signature
85
      this.container = this.readString(buffer, 4);
86
      if (this.supported_containers.indexOf(this.container) === -1) {
87
        throw Error('Not a supported format.');
88
      }
89
90
      // If its RIFX data should be BE
91
      this.uInt16_.be = this.container === 'RIFX';
92
      this.uInt32_.be = this.uInt16_.be;
93
94
      // TODO chunkSize and format should always come from this.signature
95
      this.chunkSize = this.readUInt32(buffer);
96
      this.format = this.readString(buffer, 4);
97
98
      // The RIFF file signature
99
      this.signature = {
100
          chunkId: this.container,
101
          chunkSize: this.chunkSize,
102
          format: this.format,
103
          subChunks: this.getSubChunksIndex_(buffer)
104
      };
105
  }
106
107
  /**
108
    * Find a chunk by its fourCC_ in a array of RIFF chunks.
109
    * @param {string} chunkId The chunk fourCC_.
110
    * @param {boolean} multiple True if there may be multiple chunks
111
    *    with the same chunkId.
112
    * @return {Object}
113
    * @protected
114
    */
115
  findChunk(chunkId, multiple=false) {
116
    /** @type {!Array<!Object>} */
117
    let chunks = this.signature.subChunks;
118
    /** @type {!Array<!Object>} */
119
    let chunk = [];
120
    for (let i=0; i<chunks.length; i++) {
121
      if (chunks[i].chunkId == chunkId) {
122
        if (multiple) {
123
          chunk.push(chunks[i]);
124
        } else {
125
          return chunks[i];
126
        }
127
      }
128
    }
129
    if (chunkId == 'LIST') {
130
      return chunk.length ? chunk : null;
131
    }
132
    return null;
133
  }
134
135
  /**
136
   * Read bytes as a string from a RIFF chunk.
137
   * @param {!Uint8Array} bytes The bytes.
138
   * @param {number} maxSize the max size of the string.
139
   * @return {string} The string.
140
   * @protected
141
   */
142
  readString(bytes, maxSize) {
143
    /** @type {string} */
144
    let str = '';
145
    str = unpackString(bytes, this.head_, this.head_ + maxSize);
146
    this.head_ += maxSize;
147
    return str;
148
  }
149
150
  /**
151
   * Read a number from a chunk.
152
   * @param {!Uint8Array} bytes The chunk bytes.
153
   * @return {number} The number.
154
   * @protected
155
   */
156
  readUInt16(bytes) {
157
    /** @type {number} */
158
    let value = unpack(bytes, this.uInt16_, this.head_);
159
    this.head_ += 2;
160
    return value;
161
  }
162
163
  /**
164
   * Read a number from a chunk.
165
   * @param {!Uint8Array} bytes The chunk bytes.
166
   * @return {number} The number.
167
   * @protected
168
   */
169
  readUInt32(bytes) {
170
    /** @type {number} */
171
    let value = unpack(bytes, this.uInt32_, this.head_);
172
    this.head_ += 4;
173
    return value;
174
  }
175
176
  /**
177
   * Return the sub chunks of a RIFF file.
178
   * @param {!Uint8Array} buffer the RIFF file bytes.
179
   * @return {!Array<Object>} The subchunks of a RIFF/RIFX or LIST chunk.
180
   * @private
181
   */
182
  getSubChunksIndex_(buffer) {
183
      /** @type {!Array<!Object>} */
184
      let chunks = [];
185
      /** @type {number} */
186
      let i = this.head_;
187
      while(i <= buffer.length - 8) {
188
          chunks.push(this.getSubChunkIndex_(buffer, i));
189
          i += 8 + chunks[chunks.length - 1].chunkSize;
190
          i = i % 2 ? i + 1 : i;
191
      }
192
      return chunks;
193
  }
194
195
  /**
196
   * Return a sub chunk from a RIFF file.
197
   * @param {!Uint8Array} buffer the RIFF file bytes.
198
   * @param {number} index The start index of the chunk.
199
   * @return {!Object} A subchunk of a RIFF/RIFX or LIST chunk.
200
   * @private
201
   */
202
  getSubChunkIndex_(buffer, index) {
203
      /** @type {!Object} */
204
      let chunk = {
205
          chunkId: this.getChunkId_(buffer, index),
206
          chunkSize: this.getChunkSize_(buffer, index),
207
      };
208
      if (chunk.chunkId == 'LIST') {
209
          chunk.format = unpackString(buffer, index + 8, index + 12);
210
          this.head_ += 4;
211
          chunk.subChunks = this.getSubChunksIndex_(buffer);
212
      } else {
213
          /** @type {number} */
214
          let realChunkSize = chunk.chunkSize % 2 ?
215
              chunk.chunkSize + 1 : chunk.chunkSize;
216
          this.head_ = index + 8 + realChunkSize;
217
          chunk.chunkData = {
218
              start: index + 8,
219
              end: this.head_
220
          };
221
      }
222
      return chunk;
223
  }
224
225
  /**
226
   * Return the fourCC_ of a chunk.
227
   * @param {!Uint8Array} buffer the RIFF file bytes.
228
   * @param {number} index The start index of the chunk.
229
   * @return {string} The id of the chunk.
230
   * @private
231
   */
232
  getChunkId_(buffer, index) {
233
      this.head_ += 4;
234
      return unpackString(buffer, index, index + 4);
235
  }
236
237
  /**
238
   * Return the size of a chunk.
239
   * @param {!Uint8Array} buffer the RIFF file bytes.
240
   * @param {number} index The start index of the chunk.
241
   * @return {number} The size of the chunk without the id and size fields.
242
   * @private
243
   */
244
  getChunkSize_(buffer, index) {
245
      this.head_ += 4;
246
      return unpack(buffer, this.uInt32_, index + 4);
247
  }
248
}
249