-
Notifications
You must be signed in to change notification settings - Fork 14
/
test.js
190 lines (154 loc) · 3.72 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
'use strict';
var Speaker = require('./stream');
var Generator = require('audio-generator/stream');
var Generate = require('audio-generator/index');
var Readable = require('stream').Readable;
var util = require('audio-buffer-utils');
var pcm = require('pcm-util');
var Through = require('audio-through');
Through.log = true;
var Volume = require('pcm-volume');
var test = require('tape')
var SpeakerWriter = require('./direct');
var pull = require('pull-stream');
var PullSpeaker = require('./pull');
var pullGenerator = require('audio-generator/pull');
require('insert-styles')(`
@font-face {
font-family: wavefont;
src: url(./wavefont.otf) format("opentype");
}
`);
test('Pure function', function (t) {
let generate = Generate(t => {
return Math.sin(t * Math.PI * 2 * 440);
}, 1);
let write = SpeakerWriter();
(function loop (err, buf) {
if (err) return write(null);
write(generate(buf), loop)
})();
setTimeout(() => {
write(null);
t.end();
}, 200);
});
test('Pull stream', function (t) {
let out = PullSpeaker();
pull(
pullGenerator(time => 2 * time * 440 - 1, {frequency: 440}),
out
);
setTimeout(() => {
out.abort();
t.end();
}, 500);
});
test('Cleanness of wave', function (t) {
Through(function (buffer) {
var self = this;
util.fill(buffer, function (sample, idx, channel) {
return Math.sin(Math.PI * 2 * (self.count + idx) * 440 / 44100);
});
if (this.time > 2) return this.end();
return buffer;
})
.on('end', function () {
t.end()
})
.pipe(Speaker())
// .pipe(WAASteam(context.destination));
});
test('Feed audio-through', function (t) {
Generator({
generate: function (time) {
return [
Math.sin(Math.PI * 2 * time * 538 ) / 5,
Math.sin(Math.PI * 2 * time * 542 ) / 5
// Math.random()
]
},
duration: .4
})
.on('end', function () {t.end()})
.pipe(Speaker())
});
test('Feed raw pcm', function (t) {
var count = 0;
Readable({
read: function (size) {
var abuf = util.create(1024, 2, 44100);
//EGG: swap ch & i and hear wonderful sfx
util.fill(abuf, function (v, i, ch) {
v = Math.sin(Math.PI * 2 * ((count + i)/44100) * (738 + ch*2) ) / 5;
return v;
});
count += 1024;
if (count > 1e4 ) return this.push(null);
let buf = pcm.toBuffer(abuf);
this.push(buf);
}
})
.on('end', function () {t.end()})
.pipe(Speaker({
channels: 2
}))
});
//FIXME: use transform stream here to send floats data to speaker
test.skip('Feed custom pcm', function (t) {
var count = 0;
Readable({
// objectMode: 1,
read: function (size) {
var abuf = util.create(1024, 2, 44100);
util.fill(abuf, function (v, i, ch) {
return Math.sin(Math.PI * 2 * ((count + i)/44100) * (938 + ch*2) );
});
count += 1024;
if (count > 1e4 ) return this.push(null);
let buf = pcm.toBuffer(abuf, {
float: true
});
this.push(buf);
}
})
.on('end', function () {t.end()})
.pipe(Speaker({
channels: 2,
//EGG: comment this and hear wonderful sfx
float: true
}))
});
test.skip('Feed random buffer size');
test('Volume case', function (t) {
//TODO: fix the case!
Generator(function (time) {
return [
Math.sin(Math.PI * 2 * time * 1038 ) / 5,
Math.sin(Math.PI * 2 * time * 1042 ) / 5
];
}, {
duration: 1
})
.on('end', function () {t.end()})
.pipe(Volume(5))
.pipe(Speaker())
});
//little debigger
if (typeof document !== 'undefined') {
var el = document.body.appendChild(document.createElement('div'));
el.style.cssText = `
font-family: wavefont;
max-width: 100vw;
word-break: break-all;
white-space: pre-wrap;
font-size: 32px;
`;
function draw (arr) {
let str = '';
for (let i = 0; i < arr.length; i++) {
str += String.fromCharCode(0x200 + Math.floor(arr[i] * 128 * 5));
}
el.innerHTML += '\n' + str;
}
}