1 | /* |
||
2 | * riff-chunks: Read and write the chunks of RIFF and RIFX files. |
||
3 | * https://github.com/rochars/riff-chunks |
||
4 | * |
||
5 | * Copyright (c) 2017-2018 Rafael da Silva Rocha. |
||
6 | * |
||
7 | * Permission is hereby granted, free of charge, to any person obtaining |
||
8 | * a copy of this software and associated documentation files (the |
||
9 | * "Software"), to deal in the Software without restriction, including |
||
10 | * without limitation the rights to use, copy, modify, merge, publish, |
||
11 | * distribute, sublicense, and/or sell copies of the Software, and to |
||
12 | * permit persons to whom the Software is furnished to do so, subject to |
||
13 | * the following conditions: |
||
14 | * |
||
15 | * The above copyright notice and this permission notice shall be |
||
16 | * included in all copies or substantial portions of the Software. |
||
17 | * |
||
18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
||
19 | * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF |
||
20 | * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
||
21 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE |
||
22 | * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION |
||
23 | * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
||
24 | * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
||
25 | * |
||
26 | */ |
||
27 | |||
28 | /** |
||
29 | * @fileoverview The riff-chunks API and private methods. |
||
30 | */ |
||
31 | |||
32 | /** @module riffChunks */ |
||
33 | |||
34 | import {pack, unpack, unpackFrom} from 'byte-data'; |
||
35 | |||
36 | /** |
||
37 | * Return the indexes of the chunks in a RIFF/RIFX file. |
||
38 | * @param {!Uint8Array|!Array<number>} buffer The file bytes. |
||
39 | * @return {!Object} The RIFF chunks. |
||
40 | */ |
||
41 | export function riffIndex(buffer) { |
||
42 | head_ = 0; |
||
0 ignored issues
–
show
Bug
introduced
by
![]() |
|||
43 | let chunkId = getChunkId_(buffer, 0); |
||
44 | uInt32_['be'] = chunkId == 'RIFX'; |
||
0 ignored issues
–
show
|
|||
45 | let format = unpackFrom(buffer, fourCC_, 8); |
||
0 ignored issues
–
show
|
|||
46 | head_ += 4; |
||
47 | return { |
||
48 | 'chunkId': chunkId, |
||
49 | 'chunkSize': getChunkSize_(buffer, 0), |
||
50 | 'format': format, |
||
51 | 'subChunks': getSubChunksIndex_(buffer) |
||
52 | }; |
||
53 | } |
||
54 | |||
55 | /** |
||
56 | * Pack a RIFF/RIFX file. |
||
57 | * @param {!Object} chunks A object like the return of riffChunks.read(). |
||
58 | * @param {boolean} list An optional param indicating if the chunk is LIST. |
||
59 | * 'LIST' chunks should not be rendered as Uint8Array. |
||
60 | * @return {!Array<number>|!Uint8Array} The bytes as Uint8Array when chunkId is |
||
61 | * 'RIFF'/'RIFX' or as Array<number> when chunkId is 'LIST'. |
||
62 | */ |
||
63 | export function write(chunks, list=false) { |
||
64 | uInt32_['be'] = chunks['chunkId'] == 'RIFX'; |
||
0 ignored issues
–
show
|
|||
65 | let bytes = pack(chunks['chunkId'], fourCC_).concat( |
||
0 ignored issues
–
show
|
|||
66 | pack(chunks['chunkSize'], uInt32_), |
||
67 | pack(chunks['format'], fourCC_), |
||
68 | writeSubChunks_(chunks['subChunks'])); |
||
69 | if (!list) { |
||
70 | bytes = new Uint8Array(bytes); |
||
71 | } |
||
72 | return bytes; |
||
73 | } |
||
74 | |||
75 | /** |
||
76 | * Return the chunks of a RIFF/RIFX file. |
||
77 | * @param {!Uint8Array|!Array<number>} buffer The file bytes. |
||
78 | * @return {!Object} The RIFF chunks. |
||
79 | */ |
||
80 | export function read(buffer) { |
||
81 | buffer = [].slice.call(buffer); |
||
82 | let chunkId = getChunkId_(buffer, 0); |
||
83 | uInt32_['be'] = chunkId == 'RIFX'; |
||
0 ignored issues
–
show
|
|||
84 | let format = unpack(buffer.slice(8, 12), fourCC_); |
||
0 ignored issues
–
show
|
|||
85 | let chunkSize = getChunkSize_(buffer, 0); |
||
86 | let subChunks = getSubChunks_(buffer); |
||
87 | return { |
||
88 | 'chunkId': chunkId, |
||
89 | 'chunkSize': chunkSize, |
||
90 | 'format': format, |
||
91 | 'subChunks': subChunks |
||
92 | }; |
||
93 | } |
||
94 | |||
95 | /** |
||
96 | * Return the sub chunks of a RIFF file. |
||
97 | * @param {!Uint8Array|!Array<number>} buffer the RIFF file bytes. |
||
98 | * @return {!Array<Object>} The subchunks of a RIFF/RIFX or LIST chunk. |
||
99 | * @private |
||
100 | */ |
||
101 | function getSubChunksIndex_(buffer) { |
||
102 | let chunks = []; |
||
103 | let i = head_; |
||
0 ignored issues
–
show
|
|||
104 | while(i <= buffer.length - 8) { |
||
105 | chunks.push(getSubChunkIndex_(buffer, i)); |
||
106 | i += 8 + chunks[chunks.length - 1]['chunkSize']; |
||
107 | i = i % 2 ? i + 1 : i; |
||
108 | } |
||
109 | return chunks; |
||
110 | } |
||
111 | |||
112 | /** |
||
113 | * Return a sub chunk from a RIFF file. |
||
114 | * @param {!Uint8Array|!Array<number>} buffer the RIFF file bytes. |
||
115 | * @param {number} index The start index of the chunk. |
||
116 | * @return {!Object} A subchunk of a RIFF/RIFX or LIST chunk. |
||
117 | * @private |
||
118 | */ |
||
119 | function getSubChunkIndex_(buffer, index) { |
||
120 | let chunk = { |
||
121 | 'chunkId': getChunkId_(buffer, index), |
||
122 | 'chunkSize': getChunkSize_(buffer, index), |
||
123 | }; |
||
124 | if (chunk['chunkId'] == 'LIST') { |
||
125 | chunk['format'] = unpackFrom(buffer, fourCC_, index + 8); |
||
0 ignored issues
–
show
|
|||
126 | head_ += 4; |
||
0 ignored issues
–
show
|
|||
127 | chunk['subChunks'] = getSubChunksIndex_(buffer); |
||
128 | } else { |
||
129 | let realChunkSize = chunk['chunkSize'] % 2 ? |
||
130 | chunk['chunkSize'] + 1 : chunk['chunkSize']; |
||
131 | head_ = index + 8 + realChunkSize; |
||
132 | chunk['chunkData'] = { |
||
133 | 'start': index + 8, |
||
134 | 'end': head_ |
||
135 | }; |
||
136 | } |
||
137 | return chunk; |
||
138 | } |
||
139 | |||
140 | /** |
||
141 | * Pack the sub chunks of a RIFF file. |
||
142 | * @param {!Array<!Object>} chunks The chunks. |
||
143 | * @return {!Array<number>} The chunk bytes. |
||
144 | * @private |
||
145 | */ |
||
146 | function writeSubChunks_(chunks) { |
||
147 | let subChunks = []; |
||
148 | let i = 0; |
||
149 | while (i < chunks.length) { |
||
150 | if (chunks[i]['chunkId'] == 'LIST') { |
||
151 | subChunks = subChunks.concat(write(chunks[i], true)); |
||
152 | } else { |
||
153 | subChunks = subChunks.concat( |
||
154 | pack(chunks[i]['chunkId'], fourCC_), |
||
0 ignored issues
–
show
|
|||
155 | pack(chunks[i]['chunkSize'], uInt32_), |
||
0 ignored issues
–
show
|
|||
156 | chunks[i]['chunkData']); |
||
157 | } |
||
158 | i++; |
||
159 | } |
||
160 | return subChunks; |
||
161 | } |
||
162 | |||
163 | /** |
||
164 | * Return the sub chunks of a RIFF file. |
||
165 | * @param {!Uint8Array|!Array<number>} buffer the RIFF file bytes. |
||
166 | * @return {!Array<Object>} The subchunks of a RIFF/RIFX or LIST chunk. |
||
167 | * @private |
||
168 | */ |
||
169 | function getSubChunks_(buffer) { |
||
170 | let chunks = []; |
||
171 | let i = 12; |
||
172 | while(i <= buffer.length - 8) { |
||
173 | chunks.push(getSubChunk_(buffer, i)); |
||
174 | i += 8 + chunks[chunks.length - 1]['chunkSize']; |
||
175 | i = i % 2 ? i + 1 : i; |
||
176 | } |
||
177 | return chunks; |
||
178 | } |
||
179 | |||
180 | /** |
||
181 | * Return a sub chunk from a RIFF file. |
||
182 | * @param {!Uint8Array|!Array<number>} buffer the RIFF file bytes. |
||
183 | * @param {number} index The start index of the chunk. |
||
184 | * @return {!Object} A subchunk of a RIFF/RIFX or LIST chunk. |
||
185 | * @private |
||
186 | */ |
||
187 | function getSubChunk_(buffer, index) { |
||
188 | let chunk = { |
||
189 | 'chunkId': getChunkId_(buffer, index), |
||
190 | 'chunkSize': getChunkSize_(buffer, index), |
||
191 | }; |
||
192 | if (chunk['chunkId'] == 'LIST') { |
||
193 | chunk['format'] = unpack( |
||
194 | buffer.slice(index + 8, index + 12), fourCC_); |
||
0 ignored issues
–
show
|
|||
195 | chunk['subChunks'] = getSubChunks_(buffer.slice(index)); |
||
196 | } else { |
||
197 | let slc = chunk['chunkSize'] % 2 ? chunk['chunkSize'] + 1 : chunk['chunkSize']; |
||
198 | chunk['chunkData'] = buffer.slice( |
||
199 | index + 8, index + 8 + slc); |
||
200 | } |
||
201 | return chunk; |
||
202 | } |
||
203 | |||
204 | /** |
||
205 | * Return the fourCC_ of a chunk. |
||
206 | * @param {!Uint8Array|!Array<number>} buffer the RIFF file bytes. |
||
207 | * @param {number} index The start index of the chunk. |
||
208 | * @return {string|number} The id of the chunk. |
||
209 | * @private |
||
210 | */ |
||
211 | function getChunkId_(buffer, index) { |
||
212 | head_ += 4; |
||
0 ignored issues
–
show
|
|||
213 | return unpackFrom(buffer, fourCC_, index); |
||
0 ignored issues
–
show
|
|||
214 | } |
||
215 | |||
216 | /** |
||
217 | * Return the size of a chunk. |
||
218 | * @param {!Uint8Array|!Array<number>} buffer the RIFF file bytes. |
||
219 | * @param {number} index The start index of the chunk. |
||
220 | * @return {string|number} The size of the chunk without the id and size fields. |
||
221 | * @private |
||
222 | */ |
||
223 | function getChunkSize_(buffer, index) { |
||
224 | head_ += 4; |
||
0 ignored issues
–
show
|
|||
225 | return unpackFrom(buffer, uInt32_, index + 4); |
||
0 ignored issues
–
show
|
|||
226 | } |
||
227 | |||
228 | /** @private */ |
||
229 | const uInt32_ = {'bits': 32}; |
||
0 ignored issues
–
show
|
|||
230 | /** @private */ |
||
231 | const fourCC_ = {'bits': 32, 'char': true}; |
||
0 ignored issues
–
show
|
|||
232 | /** @type {number} */ |
||
233 | let head_ = 0; |
||
0 ignored issues
–
show
|
|||
234 |