Passed
Branch master (4064de)
by Rafael S.
02:04 queued 40s
created

test/from-scratch/32bitIEEE-from-scratch.js   A

Complexity

Total Complexity 15
Complexity/F 1

Size

Lines of Code 522
Function Count 15

Duplication

Duplicated Lines 522
Ratio 100 %

Importance

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

1 Function

Rating   Name   Duplication   Size   Complexity  
B 32bitIEEE-from-scratch.js ➔ describe(ꞌcreate 32-bit IEEE wave file from scratchꞌ) 520 520 1

How to fix   Duplicated Code   

Duplicated Code

Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.

Common duplication problems, and corresponding solutions are:

1
/*!
2
 * riffwave64
3
 * Read & write wave files with 8, 16, 24, 32 PCM, 32 IEEE & 64-bit data.
4
 * Copyright (c) 2017 Rafael da Silva Rocha.
5
 * https://github.com/rochars/riffwave64
6
 * https://tr2099.github.io
7
 *
8
 */
9
 
10 View Code Duplication
var assert = require('assert');
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated in your project.
Loading history...
11
12
describe('create 32-bit IEEE wave file from scratch', function() {
13
    
14
    let wavefile = require('../../index.js');
15
    let wav = new wavefile.WaveFile();
16
    wav.fromScratch(1, 44100, '32f', [0, 0.04029441, -0.04029440, 1]);
17
18
    let fs = require('fs');
19
    fs.writeFileSync("./test/files/out/32-bitIEEE-441kHz-mono-fromScratch.wav", wav.toBytes());
20
21
    it('chunkId should be "RIFF"', function() {
22
        assert.equal(wav.chunkId, "RIFF");
23
    });
24
25
    it('format should be "WAVE"', function() {
26
        assert.equal(wav.format, "WAVE");
27
    });
28
29
    it('subChunk1Id should be "fmt "', function() {
30
        assert.equal(wav.subChunk1Id, "fmt ");
31
    });
32
33
    it('subChunk1Size should be 16', function() {
34
        assert.equal(wav.subChunk1Size, 16);
35
    });
36
37
    it('audioFormat should be 3', function() {
38
        assert.equal(wav.audioFormat, 3);
39
    });
40
41
    it('numChannels should be 1', function() {
42
        assert.equal(wav.numChannels, 1);
43
    });
44
45
    it('sampleRate should be 44100', function() {
46
        assert.equal(wav.sampleRate, 44100);
47
    });
48
49
    it('byteRate should be 176400', function() {
50
        assert.equal(wav.byteRate, 176400);
51
    });
52
53
    it('blockAlign should be 4', function() {
54
        assert.equal(wav.blockAlign, 4);
55
    });
56
57
    it('bitsPerSample should be 32', function() {
58
        assert.equal(wav.bitsPerSample, 32);
59
    });
60
61
    it('subChunk2Id should be "data"', function() {
62
        assert.equal(wav.subChunk2Id, "data");
63
    });
64
65
    it('subChunk2Size should be 16', function() {
66
        assert.equal(wav.subChunk2Size, 16);
67
    });
68
69
    it('samples_ should be the same as the args', function() {
70
        assert.deepEqual(wav.samples_, [0, 0.04029441, -0.04029440, 1]);
71
    });
72
73
    it('bitDepth_ should be "24"', function() {
74
        assert.equal(wav.bitDepth_, "32f");
75
    });
76
77
    /*
78
    it('should return a 8-bit wave file with an odd number of samples',
79
            function() {
80
        let wav = rw64.writeWavBytes(1, 48000, '8', [0, 255, 2]);
81
        let wavRead = rw64.readWavBytes(wav);
82
83
        assert.equal(wavRead.audioFormat, 1);
84
        assert.equal(wavRead.numChannels, 1);
85
        assert.equal(wavRead.blockAlign, 1);
86
        assert.equal(wavRead.sampleRate, 48000);
87
        assert.equal(wavRead.bitsPerSample, 8);
88
        assert.deepEqual(wavRead.samples, [0, 255, 2]);
89
    });
90
91
    it('should return a 16-bit wave file', function() {
92
        let wav = rw64.writeWavBytes(1, 48000, '16',
93
            [0, 1, -32768, 32767]);
94
        let wavRead = rw64.readWavBytes(wav);
95
        
96
        assert.equal(wavRead.audioFormat, 1);
97
        assert.equal(wavRead.numChannels, 1);
98
        assert.equal(wavRead.blockAlign, 2);
99
        assert.equal(wavRead.sampleRate, 48000);
100
        assert.equal(wavRead.bitsPerSample, 16);
101
        assert.deepEqual(wavRead.samples, [0, 1, -32768, 32767]);
102
    });
103
104
    it('should return a 16-bit wave file with a odd number of samples',
105
            function() {
106
        let wav = rw64.writeWavBytes(1, 48000, '16',
107
            [0, 1, -32768, 32767, 4]);
108
        let wavRead = rw64.readWavBytes(wav);
109
        
110
        assert.equal(wavRead.audioFormat, 1);
111
        assert.equal(wavRead.numChannels, 1);
112
        assert.equal(wavRead.blockAlign, 2);
113
        assert.equal(wavRead.sampleRate, 48000);
114
        assert.equal(wavRead.bitsPerSample, 16);
115
        assert.deepEqual(wavRead.samples, [0, 1, -32768, 32767, 4]);
116
    });
117
118
    it('should return a 24-bit wave file', function() {
119
        let wav = rw64.writeWavBytes(1, 48000, '24',
120
            [0, 1, -8388608, 8388607]);
121
        let wavRead = rw64.readWavBytes(wav);
122
        
123
        assert.equal(wavRead.audioFormat, 1);
124
        assert.equal(wavRead.numChannels, 1);
125
        assert.equal(wavRead.blockAlign, 3);
126
        assert.equal(wavRead.sampleRate, 48000);
127
        assert.equal(wavRead.bitsPerSample, 24);
128
        assert.equal(wavRead.subChunk2Size, 12); // 4 x 3
129
        assert.deepEqual(wavRead.samples, [0, 1, -8388608, 8388607]);
130
    });
131
132
    it('should return a 24-bit wave file with a odd number of samples',
133
            function() {
134
        let wav = rw64.writeWavBytes(1, 48000, '24',
135
            [0, 1, -8388608, 8388607, 4]);
136
        let wavRead = rw64.readWavBytes(wav);
137
        
138
        assert.equal(wavRead.audioFormat, 1);
139
        assert.equal(wavRead.numChannels, 1);
140
        assert.equal(wavRead.sampleRate, 48000);
141
        assert.equal(wavRead.bitsPerSample, 24);
142
        assert.equal(wavRead.blockAlign, 3);
143
        assert.equal(wavRead.subChunk2Size, 15); // 5 x 3
144
        assert.deepEqual(wavRead.samples, [0, 1, -8388608, 8388607, 4]);
145
    });
146
147
    it('should return a 32-bit PCM wave file', function() {
148
        let wav = rw64.writeWavBytes(1, 44100, '32',
149
            [0, -2147483648, 2147483647, 4]);
150
        let wavRead = rw64.readWavBytes(wav);
151
        
152
        assert.equal(wavRead.audioFormat, 1);
153
        assert.equal(wavRead.numChannels, 1);
154
        assert.equal(wavRead.sampleRate, 44100);
155
        assert.equal(wavRead.bitsPerSample, 32);
156
        assert.equal(wavRead.blockAlign, 4);
157
        assert.deepEqual(wavRead.samples, [0, -2147483648, 2147483647, 4]);
158
    });
159
160
    it('should return a 32-bit PCM wave file with a odd number of samples',
161
            function() {
162
        let wav = rw64.writeWavBytes(1, 44100, '32',
163
            [0, -2147483648, 45, 0, 1]);
164
        let wavRead = rw64.readWavBytes(wav);
165
        
166
        assert.equal(wavRead.audioFormat, 1);
167
        assert.equal(wavRead.numChannels, 1);
168
        assert.equal(wavRead.sampleRate, 44100);
169
        assert.equal(wavRead.bitsPerSample, 32);
170
        assert.equal(wavRead.blockAlign, 4);
171
        assert.deepEqual(wavRead.samples, [0, -2147483648, 45, 0, 1]);
172
    });
173
174
    it('should return a 32-bit IEEE wave file', function() {
175
        let wav = rw64.writeWavBytes(1, 48000, '32f',
176
            [0, 0.04029441, -0.04029440, 1, -1, 0]);
177
        let wavRead = rw64.readWavBytes(wav);
178
        
179
        assert.equal(wavRead.audioFormat, 3);
180
        assert.equal(wavRead.numChannels, 1);
181
        assert.equal(wavRead.sampleRate, 48000);
182
        assert.equal(wavRead.blockAlign, 4);
183
        assert.equal(wavRead.bitsPerSample, 32);
184
        for (let i=0; i<wavRead.samples.length; i++) {
185
            wavRead.samples[i] = Number((wavRead.samples[i]).toFixed(8));
186
        }
187
        assert.deepEqual(wavRead.samples,
188
            [0, 0.04029441, -0.04029440, 1, -1, 0]);
189
    });
190
191
    it('should return a 32-bit IEEE wave file ' +
192
        'with a odd number of samples',
193
            function() {
194
        let wav = rw64.writeWavBytes(1, 48000, '32f',
195
            [0, 0.04029441, -0.04029440, 1, -1, 0, 1]);
196
        let wavRead = rw64.readWavBytes(wav);
197
        
198
        assert.equal(wavRead.audioFormat, 3);
199
        assert.equal(wavRead.numChannels, 1);
200
        assert.equal(wavRead.sampleRate, 48000);
201
        assert.equal(wavRead.blockAlign, 4);
202
        assert.equal(wavRead.bitsPerSample, 32);
203
        for (let i=0; i<wavRead.samples.length; i++) {
204
            wavRead.samples[i] = Number((wavRead.samples[i]).toFixed(8));
205
        }
206
        assert.deepEqual(wavRead.samples,
207
            [0, 0.04029441, -0.04029440, 1, -1, 0, 1]);
208
    });
209
210
    it('should return a wave file with 64-bit audio', function() {
211
        let wav = rw64.writeWavBytes(1, 48000, '64',
212
            [0.0, 0.04029440055111987, -0.04029440055111987, 1.0]);
213
        let wavRead = rw64.readWavBytes(wav);
214
215
        assert.equal(wavRead.audioFormat, 3);
216
        assert.equal(wavRead.numChannels, 1);
217
        assert.equal(wavRead.sampleRate, 48000);
218
        assert.equal(wavRead.blockAlign, 8);
219
        assert.equal(wavRead.bitsPerSample, 64);
220
        assert.deepEqual(wavRead.samples,
221
            [0.0, 0.04029440055111987, -0.04029440055111987, 1.0]);
222
    });
223
224
    it('should return a wave file with 64-bit ' +
225
        'audio and a odd number of samples',
226
            function() {
227
        let wav = rw64.writeWavBytes(1, 48000, '64',
228
            [0.0, 0.04029440055111987, -0.04029440055111987, 1.0, 1]);
229
        let wavRead = rw64.readWavBytes(wav);
230
231
        assert.equal(wavRead.audioFormat, 3);
232
        assert.equal(wavRead.numChannels, 1);
233
        assert.equal(wavRead.sampleRate, 48000);
234
        assert.equal(wavRead.blockAlign, 8);
235
        assert.equal(wavRead.bitsPerSample, 64);
236
        assert.deepEqual(wavRead.samples,
237
            [0.0, 0.04029440055111987, -0.04029440055111987, 1, 1]);
238
    });
239
240
    it('check rouding for double values on read/write',
241
            function() {
242
        let wav = rw64.writeWavBytes(1, 48000, '64',
243
            [3.141592653589793]);
244
        let wavRead = rw64.readWavBytes(wav);
245
        
246
        assert.ok(wavRead.samples[0] ==
247
            3.141592653589793);
248
249
    });
250
251
    it('check rouding for double values on read/write (forced change)',
252
        function() {
253
        let wav = rw64.writeWavBytes(1, 48000, '64',
254
            [3.141592653589793]);
255
        let wavRead = rw64.readWavBytes(wav);
256
        
257
        assert.ok(wavRead.samples[0] !=
258
            3.141592653589792);
259
    });
260
261
    it('should return a wave file with 64-bit audio on max and min range',
262
            function() {
263
        let wav = rw64.writeWavBytes(1, 48000, '64', [0,1,0,-1]);
264
        let wavRead = rw64.readWavBytes(wav);
265
266
        assert.equal(wavRead.audioFormat, 3);
267
        assert.equal(wavRead.numChannels, 1);
268
        assert.equal(wavRead.sampleRate, 48000);
269
        assert.equal(wavRead.blockAlign, 8);
270
        assert.equal(wavRead.bitsPerSample, 64);
271
        assert.deepEqual(wavRead.samples, [0,1,0,-1]);
272
    });
273
274
    it('should return another wave file with 64-bit audio', function() {
275
        let wav = rw64.writeWavBytes(1, 48000, '64',
276
            [0.0, 0.99999999999999999, -0.00000000000000001, 1]);
277
        let wavRead = rw64.readWavBytes(wav);
278
279
        assert.equal(wavRead.audioFormat, 3);
280
        assert.equal(wavRead.numChannels, 1);
281
        assert.equal(wavRead.sampleRate, 48000);
282
        assert.equal(wavRead.blockAlign, 8);
283
        assert.equal(wavRead.bitsPerSample, 64);
284
        assert.deepEqual(wavRead.samples,
285
            [0, 0.99999999999999999, -0.00000000000000001, 1]);
286
    });
287
288
    it('should return one more wave file with 64-bit audio', function() {
289
        let wav = rw64.writeWavBytes(1, 48000, '64',
290
            [0, 1, 1, -1, 0, -1, 0.99999999999999999,
291
            -0.00000000000000001, 1]);
292
        let wavRead = rw64.readWavBytes(wav);
293
294
        assert.equal(wavRead.audioFormat, 3);
295
        assert.equal(wavRead.numChannels, 1);
296
        assert.equal(wavRead.sampleRate, 48000);
297
        assert.equal(wavRead.blockAlign, 8);
298
        assert.equal(wavRead.bitsPerSample, 64);
299
        assert.deepEqual(wavRead.samples,
300
            [0, 1, 1, -1, 0, -1, 0.99999999999999999,
301
            -0.00000000000000001, 1]);
302
    });
303
304
    it('should return a 24-bit wave file with 8 channels', function() {
305
        let wav = rw64.writeWavBytes(8, 48000, '24',
306
            [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]);
307
        let read = rw64.readWavBytes(wav);
308
        
309
        assert.equal(read.subChunk1Size, 16);
310
        assert.equal(read.audioFormat, 1);
311
        assert.equal(read.numChannels, 8);
312
        assert.equal(read.sampleRate, 48000);
313
        assert.equal(read.blockAlign, 24);
314
        assert.equal(read.bitsPerSample, 24);
315
    });
316
317
    it('should return a 32-bit PCM wave file with 7 channels', function() {
318
        let wav = rw64.writeWavBytes(7, 48000, '32',
319
            [0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1]);
320
        let read = rw64.readWavBytes(wav);
321
        
322
        assert.equal(read.subChunk1Size, 16);
323
        assert.equal(read.audioFormat, 1);
324
        assert.equal(read.numChannels, 7);
325
        assert.equal(read.sampleRate, 48000);
326
        assert.equal(read.blockAlign, 28);
327
        assert.equal(read.bitsPerSample, 32);
328
    });
329
330
    it('should return a 24-bit wave file encoded as a base 64 string',
331
            function() {
332
        assert.ok(rw64.writeWavBase64(1, 48000, '24',
333
            [0, 0.5, -0.5]));
334
    });
335
336
    it('should return a 32-bit IEEE wave file encoded as a base 64 string',
337
            function() {
338
        assert.ok(rw64.writeWavBase64(1, 48000, '32f',
339
            [0, 0.5, -0.5]));
340
    });
341
342
    it('should return a 64-bit wave file encoded as a base 64 string',
343
            function() {
344
        assert.ok(rw64.writeWavBase64(1, 48000, '64',
345
            [0, 0.5, -0.5]));
346
    });
347
348
    it('should return a 24-bit wave file encoded as a Data URI',
349
            function() {
350
        assert.ok(rw64.writeWavDataURI(1, 48000, '24',
351
            [0, 0.5, -0.5]));
352
    });
353
354
    it('should return a 32-bit IEEE wave file encoded as a Data URI',
355
            function() {
356
        assert.ok(rw64.writeWavDataURI(1, 48000, '32f',
357
            [0, 0.5, -0.5]));
358
    });
359
360
    it('should return a 64-bit wave file encoded as a Data URI',
361
            function() {
362
        assert.ok(rw64.writeWavDataURI(1, 48000, '64',
363
            [0, 0.5, -0.5]));
364
    });
365
366
    it('should return another 64-bit wave file encoded as a Data URI',
367
            function() {
368
        assert.ok(rw64.writeWavDataURI(1, 48000, '64',
369
            [0, 1, -1, 0, 1]));
370
    });
371
372
    it('should write a 8-bit wav file with 1 Hz sample rate',
373
            function() {
374
        let samples = [0,1];
375
        let wav = rw64.writeWavBytes(1, 1, '8', samples);
376
        let read = rw64.readWavBytes(wav);
377
        
378
        assert.equal(read.subChunk1Size, 16);
379
        assert.equal(read.audioFormat, 1);
380
        assert.equal(read.numChannels, 1);
381
        assert.equal(read.sampleRate, 1);
382
        assert.equal(read.blockAlign, 1);
383
        assert.equal(read.bitsPerSample, 8);
384
    });
385
386
    it('should write a 8-bit stereo wav file', function() {
387
        let channels = [
388
            [0,1,2],
389
            [0,1,2]
390
        ];
391
        let samples = rw64.interleave(channels);
392
        let wav = rw64.writeWavBytes(2, 44100, '8', samples);
393
        let read = rw64.readWavBytes(wav);
394
        
395
        assert.equal(read.subChunk1Size, 16);
396
        assert.equal(read.audioFormat, 1);
397
        assert.equal(read.numChannels, 2);
398
        assert.equal(read.sampleRate, 44100);
399
        assert.equal(read.blockAlign, 2);
400
        assert.equal(read.bitsPerSample, 8);
401
    });
402
403
    it('should write a 16-bit stereo wav file', function() {
404
        let channels = [
405
            [0,1,2],
406
            [0,1,2]
407
        ];
408
        let samples = rw64.interleave(channels);
409
        let wav = rw64.writeWavBytes(2, 44100, '16', samples);
410
        let read = rw64.readWavBytes(wav);
411
        
412
        assert.equal(read.subChunk1Size, 16);
413
        assert.equal(read.audioFormat, 1);
414
        assert.equal(read.numChannels, 2);
415
        assert.equal(read.sampleRate, 44100);
416
        assert.equal(read.blockAlign, 4);
417
        assert.equal(read.bitsPerSample, 16);
418
    });
419
420
    it('should write a 24-bit stereo wav file', function() {
421
        let channels = [
422
            [0,1,2],
423
            [0,1,2]
424
        ];
425
        let samples = rw64.interleave(channels);
426
        let wav = rw64.writeWavBytes(2, 44100, '24', samples);
427
        let read = rw64.readWavBytes(wav);
428
        
429
        assert.equal(read.subChunk1Size, 16);
430
        assert.equal(read.audioFormat, 1);
431
        assert.equal(read.numChannels, 2);
432
        assert.equal(read.sampleRate, 44100);
433
        assert.equal(read.blockAlign, 6);
434
        assert.equal(read.bitsPerSample, 24);
435
    });
436
437
    it('should write a 24-bit stereo wav file', function() {
438
        let channels = [
439
            [0,1,2],
440
            [0,1,2]
441
        ];
442
        let samples = rw64.interleave(channels);
443
        let wav = rw64.writeWavBytes(2, 44100, '24', samples);
444
        let read = rw64.readWavBytes(wav);
445
        
446
        assert.equal(read.subChunk1Size, 16);
447
        assert.equal(read.audioFormat, 1);
448
        assert.equal(read.numChannels, 2);
449
        assert.equal(read.sampleRate, 44100);
450
        assert.equal(read.blockAlign, 6);
451
        assert.equal(read.bitsPerSample, 24);
452
    });
453
454
    it('should write a 32-bit PCM stereo wav file', function() {
455
        let channels = [
456
            [0,1,2],
457
            [0,1,2]
458
        ];
459
        let samples = rw64.interleave(channels);
460
        let wav = rw64.writeWavBytes(2, 44100, '32', samples);
461
        let read = rw64.readWavBytes(wav);
462
        
463
        assert.equal(read.subChunk1Size, 16);
464
        assert.equal(read.audioFormat, 1);
465
        assert.equal(read.numChannels, 2);
466
        assert.equal(read.sampleRate, 44100);
467
        assert.equal(read.blockAlign, 8);
468
        assert.equal(read.bitsPerSample, 32);
469
    });
470
471
    it('should write a 32-bit IEEE stereo wav file', function() {
472
        let channels = [
473
            [0,1,-1],
474
            [0,1,-1]
475
        ];
476
        let samples = rw64.interleave(channels);
477
        let wav = rw64.writeWavBytes(2, 44100, '32f', samples);
478
        let read = rw64.readWavBytes(wav);
479
        
480
        assert.equal(read.subChunk1Size, 16);
481
        assert.equal(read.audioFormat, 3);
482
        assert.equal(read.numChannels, 2);
483
        assert.equal(read.sampleRate, 44100);
484
        assert.equal(read.blockAlign, 8);
485
        assert.equal(read.bitsPerSample, 32);
486
    });
487
488
    it('should write a 64-bit stereo wav file', function() {
489
        let channels = [
490
            [0,1,-1],
491
            [0,1,-1]
492
        ];
493
        let samples = rw64.interleave(channels);
494
        let wav = rw64.writeWavBytes(2, 44100, '64', samples);
495
        let read = rw64.readWavBytes(wav);
496
        
497
        assert.equal(read.subChunk1Size, 16);
498
        assert.equal(read.audioFormat, 3);
499
        assert.equal(read.numChannels, 2);
500
        assert.equal(read.sampleRate, 44100);
501
        assert.equal(read.blockAlign, 16);
502
        assert.equal(read.bitsPerSample, 64);
503
    });
504
505
    it('should return a 64-bit wave file with non-negative 0',
506
            function() {
507
        let wav = rw64.writeWavBytes(1, 48000, '64', [0, 1, -1, -0, 1]);
508
        let wavRead = rw64.readWavBytes(wav);
509
        assert.equal(wavRead.audioFormat, 3);
510
        assert.equal(wavRead.numChannels, 1);
511
        assert.equal(wavRead.sampleRate, 48000);
512
        assert.equal(wavRead.blockAlign, 8);
513
        assert.equal(wavRead.bitsPerSample, 64);
514
        assert.deepEqual(wavRead.samples, [0, 1, -1, 0, 1]);
515
    });
516
517
    it('should return a 32-bit PCM file with max possible sample value',
518
            function() {
519
        let wav = rw64.writeWavBytes(1, 16000, '32',
520
            [-2147483648, 2147483647, -1, 1, 0]);
521
        let wavRead = rw64.readWavBytes(wav);
522
        assert.equal(wavRead.audioFormat, 1);
523
        assert.equal(wavRead.numChannels, 1);
524
        assert.equal(wavRead.sampleRate, 16000);
525
        assert.equal(wavRead.blockAlign, 4);
526
        assert.equal(wavRead.bitsPerSample, 32);
527
        assert.deepEqual(wavRead.samples,
528
            [-2147483648, 2147483647, -1, 1, 0]);
529
    });
530
    */
531
});
532