-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathtest.js
129 lines (106 loc) · 2.39 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
'use strict';
var Generator = require('./stream');
var Speaker = require('audio-speaker');
var assert = require('assert');
var Sink = require('audio-sink');
var test = require('tst');
var Source = require('./pull');
var pull = require('pull-stream');
// var Through = require('audio-through');
// var util = require('audio-buffer-utils');
test('Panned wave', function (done) {
var generator = Generator(
function (time) {
return [Math.sin(Math.PI * 2 * time * 439), Math.sin(Math.PI * 2 * time * 441)];
},
{
duration: .5
}
).on('end', done);
generator.pipe(Speaker());
});
test('Left channel noise', function (done) {
var generator = Generator({
generate: function (time) {
return Math.random();
},
duration: .5
}).on('end', done);
var speaker = Speaker();
generator.pipe(speaker);
});
test('Sin noise', function (done) {
var generator = Generator({
generate: function (time) {
var noise = Math.random() * 2 - 1;
var f = 400;
return Math.sin(Math.PI * 2 * time * f) * 0.5 + noise * 0.5;
},
duration: .5
}).on('end', done);
var speaker = Speaker();
generator.pipe(speaker);
});
test('Saw', function (done) {
Generator(function (time, n) {
return time*100;
}, { period: 1/100, duration: .5 })
.on('end', done)
.pipe(Speaker())
});
test('Pulse', function (done) {
Generator(function (time, n) {
return time < 0.00005 ? 1 : 0;
}, { frequency: 440, duration: 1 })
.pipe(Speaker());
setTimeout(done, 1000)
});
test.skip('setFunction', function (done) {
Generator(function (time) {
if (time > 0.1) {
this.period = 1/440;
this.setFunction(function (time) {
return time > this.period / 2 ? 1 : -1;
});
}
return Math.sin(Math.PI * 2 * time * 440);
}, {duration: 1})
.pipe(Speaker());
setTimeout(done, 300);
});
test('Errors in processing, throw errors', function (done) {
Generator(function (time) {
if (time > 0.0001) {
throw Error(123);
}
if (time > 0.0002) {
return null;
}
})
.on('error', function (e) {
assert.equal(e.message, 123);
// console.error(e);
})
.on('end', done)
.pipe(Sink());
});
test('Pull-stream', function (done) {
let src = Source(Math.random);
let count = 0;
pull(
src,
pull.asyncMap((buf, cb) => {
if (++count >= 5) {
return src.abort();
}
setTimeout(() => {
cb(null, buf);
}, 10);
}),
pull.drain()
);
setTimeout(() => {
assert.equal(count, 5);
done();
}, 200);
});