-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathtest.js
218 lines (178 loc) · 5.19 KB
/
test.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
var pcm = require('./');
var AudioBiquad = require('audio-biquad');
var AudioBuffer = require('audio-buffer');
var test = require('tape')
var util = require('audio-buffer-utils')
test('Max/min limits', function (t) {
var a = pcm.normalize({
float: false,
signed: true,
bitDepth: 16
});
t.equal(a.max, 32767);
t.equal(a.min, -32768);
var a = pcm.normalize({
float: false,
signed: false,
bitDepth: 16
});
t.equal(a.max, 65535);
t.equal(a.min, 0);
var a = pcm.normalize({
float: false,
signed: false,
bitDepth: 8
});
t.equal(a.max, 255);
t.equal(a.min, 0);
var a = pcm.normalize({
float: false,
signed: true,
bitDepth: 8
});
t.equal(a.max, 127);
t.equal(a.min, -128);
var a = pcm.normalize({
float: true
});
t.equal(a.max, 1);
t.equal(a.min, -1);
t.end()
});
// test.skip('Parse/stringify', function () {
// var format = pcm.format(pcm.defaults);
// var formatId = pcm.stringifyFormat(format);
// var result = pcm.parseFormat(formatId);
// t.deepEqual(format, result);
// });
test('Keep prototype values', function (t) {
var A = function(){};
A.prototype = Object.create({samplesPerFrame: 1});
var a = new A();
pcm.normalize(a);
t.equal(a.samplesPerFrame, 1);
t.end()
});
test.skip('Obtain format from the audio stream', function (t) {
var aStream = AudioBiquad();
var aStreamFormat = pcm.format(aStream);
var defaultFormat = pcm.format(pcm.defaults);
t.notEqual(aStream, aStreamFormat);
t.deepEqual(aStreamFormat, defaultFormat)
t.end()
});
test('Do not return defaults', function (t) {
t.deepEqual(pcm.format(), {});
t.end()
});
test('Normalize changed and normalized', function (t) {
var floatFormat = pcm.normalize(pcm.format(pcm.defaults));
floatFormat.float = true;
var floatFormat = pcm.normalize(floatFormat);
t.deepEqual(pcm.normalize({float: true}), floatFormat);
t.end()
});
// test.skip('Create typed array for the format', function () {
// assert(pcm.createArray() instanceof Int16Array);
// assert(pcm.createArray({float: true}) instanceof Float32Array);
// assert(pcm.createArray({float: false}) instanceof Int16Array);
// assert(pcm.createArray({float: false, bitDepth: 32}) instanceof Int32Array);
// // assert(pcm.createArray({float: false, bitDepth: 32, signed: false}) instanceof UInt32Array);
// assert(pcm.createArray({float: false, signed: false}) instanceof Uint16Array);
// });
test('Infer format from the typed array', function (t) {
t.deepEqual(pcm.format(new Float32Array), pcm.format({
float: true,
signed: false,
bitDepth: 32
}));
t.deepEqual(pcm.format(new Int16Array), pcm.format({
float: false,
signed: true,
bitDepth: 16
}));
t.end()
});
test('FloatLE to Int16BE', function (t) {
var buf = Buffer(8);
buf.writeFloatLE(1.0, 0);
buf.writeFloatLE(-0.5, 4);
var newBuf = pcm.convert(buf, { float: true }, { float: false, signed: true, bitDepth: 16, byteOrder: 'BE' });
newBuf = Buffer.from(newBuf)
var val1 = newBuf.readInt16BE(0);
var val2 = newBuf.readInt16BE(2);
t.equal(Math.pow(2, 15) - 1, val1);
t.equal(-Math.pow(2, 14), val2);
t.end()
});
// test.skip('interleave', function () {
// });
// test.skip('deinterleave', function () {
// });
test('Buffer to AudioBuffer', function (t) {
var b = Buffer(6*4);
[0, 1, 0, -1, 0, 1].forEach(function (value, idx) {
b.writeFloatLE(value, idx * 4);
});
var aBuf = pcm.toAudioBuffer(b, {
float: true,
channels: 3
});
t.deepEqual(aBuf.getChannelData(0), [0, -1]);
t.deepEqual(aBuf.getChannelData(1), [1, 0]);
t.deepEqual(aBuf.getChannelData(2), [0, 1]);
var aBuf = pcm.toAudioBuffer(b, {
float: true,
channels: 3,
interleaved: false
});
t.deepEqual(aBuf.getChannelData(0), [0, 1]);
t.deepEqual(aBuf.getChannelData(1), [0, -1]);
t.deepEqual(aBuf.getChannelData(2), [0, 1]);
t.end()
});
test('AudioBuffer to Buffer', function (t) {
var aBuffer = util.create([0, 1, 0, -1], 2);
var buffer = pcm.toArrayBuffer(aBuffer, {
signed: true,
float: false,
interleaved: true
});
buffer = Buffer.from(buffer)
t.equal(buffer.length, 8);
t.equal(buffer.readInt16LE(0), 0);
t.equal(buffer.readInt16LE(2), 0);
t.equal(buffer.readInt16LE(4), 32767);
t.equal(buffer.readInt16LE(6), -32768);
t.end()
});
test('toBuffer detect channels', function (t) {
var aBuffer = util.create([0, 1, 0, -1]);
var buffer = pcm.toArrayBuffer(aBuffer, {
signed: true,
float: false,
interleaved: true
});
buffer = Buffer.from(buffer)
t.equal(buffer.length, 8);
t.equal(buffer.readInt16LE(0), 0);
t.equal(buffer.readInt16LE(4), 0);
t.equal(buffer.readInt16LE(2), 32767);
t.equal(buffer.readInt16LE(6), -32768);
t.end()
});
test('AubioBuffer converting consistency', function (t) {
//64 array
// var src = util.create(pcm.defaults.samplesPerFrame)
// var buf = Buffer.from(pcm.toArrayBuffer(src))
// t.equal(buf.byteLength, pcm.defaults.samplesPerFrame * pcm.defaults.bitDepth * src.numberOfChannels / 8)
// var dst = pcm.toAudioBuffer(buf)
// t.equal(src.length, dst.length)
//32 array
var src = util.create(pcm.defaults.samplesPerFrame)
var buf = Buffer.from(pcm.toArrayBuffer(src))
t.equal(buf.byteLength, pcm.defaults.samplesPerFrame * pcm.defaults.bitDepth * src.numberOfChannels / 8)
var dst = pcm.toAudioBuffer(buf)
t.equal(src.length, dst.length)
t.end()
})