-
Notifications
You must be signed in to change notification settings - Fork 2
/
TestBuffer.sc
179 lines (142 loc) · 5.45 KB
/
TestBuffer.sc
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
/*
Buffer.test
UnitTest.gui
*/
TestBuffer : UnitTest {
test_freeAll {
var n;
this.bootServer;
Buffer.freeAll;
n = Buffer(Server.default,44100,2);
this.assertEquals( Server.default.bufferAllocator.blocks.size , 1 , " allocated one buffer");
Buffer.freeAll;
this.assertEquals( Server.default.bufferAllocator.blocks.size , 0 , "freeAll : no buffers allocated");
}
test_serverlang_dataexchange {
var d, b;
this.bootServer;
// Random data for test
d = {1.0.rand}.dup(Server.default.sampleRate);
// send then load
b = Buffer.sendCollection(Server.default, d);
0.2.wait;
Server.default.sync;
b.loadToFloatArray(0, -1, action:{|data|
this.assert(data.size==d.size, "test_serverlang_dataexchange sendCollection->loadToFloatArray received full-length data (%==%)".format(data.size, d.size));
this.assertArrayFloatEquals(data, d, "test_serverlang_dataexchange sendCollection->loadToFloatArray", report: true)
});
Server.default.sync;
b.free;
Server.default.sync;
// load then load
b = Buffer.loadCollection(Server.default, d);
0.2.wait;
Server.default.sync;
b.loadToFloatArray(0, -1, action:{|data|
this.assert(data.size==d.size, "test_serverlang_dataexchange loadCollection->loadToFloatArray received full-length data (%==%)".format(data.size, d.size));
this.assertArrayFloatEquals(data, d, "test_serverlang_dataexchange loadCollection->loadToFloatArray", report: true)
});
Server.default.sync;
// zero the Buffer and check that it worked
b.zero;
Server.default.sync;
b.loadToFloatArray(0, -1, action:{|data|
this.assertArrayFloatEquals(data, 0, "Buffer:zero should give a buffer containing zeroes", report: true)
});
Server.default.sync;
b.free;
Server.default.sync;
}
// Test basen on JP's test code, for the bug which DS introduced in svn rev 9362, and JP fixed in svn rev 9371
// Note that the "expected values" used in this test depend precisely on the samples in a11wlk01.wav, so will need updating if that changes.
test_allocAndQuery {
var o,p, expectq, expectg, s=this.s, sfpath, sf, b;
this.bootServer;
Buffer.freeAll;
o = OSCresponderNode(s.addr, '/b_info', {arg time, resp, msg;
msg.postln;
this.assertEquals(msg, expectq, "/b_info data returned from a /b_query message should be as expected");
});
p = OSCresponderNode(s.addr, '/b_set', {arg time, resp, msg;
msg.postln;
this.assertEquals(msg, expectg, "/b_set data returned from a /b_get message should be as expected");
});
o.add;
p.add;
// Hmm, seems not all systems have the standard soundfile available
// s.sendMsg(\b_allocRead, 1, "sounds/a11wlk01.wav");
// So instead we use this from the SoundFile helpfile:
sf = SoundFile.new.headerFormat_("AIFF").sampleFormat_("int16").numChannels_(1);
sfpath = PathName.tmp +/+ "sc3_test_allocAndQuery.aiff";
sf.openWrite(sfpath);
// sawtooth
b = Signal.sineFill(441, (1..20).reciprocal);
// write multiple cycles (441 * 100 = 1 sec worth)
100.do{ sf.writeData(b) };
sf.close;
0.1.wait;
s.sendMsg(\b_allocRead, 1, sfpath);
0.4.wait;
s.sync;
expectq = [ '/b_info', 1, 44100, 1, 44100 ];
expectg = [ '/b_set', 1, 1000, 0.43011474609375 ];
s.sendMsg(\b_query, 1);
s.sendMsg(\b_get, 1, 1000);
0.4.wait;
s.sync;
s.sendMsg(\b_alloc, 3, 30000);
s.sendMsg(\b_read, 3, sfpath, 0, 30000);
0.4.wait;
s.sync;
expectq = [ '/b_info', 3, 30000, 1, 44100 ];
expectg = [ '/b_set', 3, 1000, 0.43011474609375 ];
s.sendMsg(\b_query, 3);
s.sendMsg(\b_get, 3, 1000);
0.4.wait;
s.sync;
o.remove;
p.remove;
}
test_cheby {
var s = this.s;
var buf;
var fromBuffer;
var calcVal;
var bufSize = 512;
this.bootServer;
buf = Buffer.alloc(s, bufSize , 1, { arg b; b.chebyMsg([0, 1], normalize: false, asWavetable: false) });
s.sync;
buf.loadToFloatArray(action: { arg array; fromBuffer = array });
calcVal = cos(2 * acos((bufSize - 1)/(bufSize >> 1) - 1)) + 1;
this.assert(
fromBuffer.first.equalWithPrecision(2) &&
this.getSignalMidValue(fromBuffer).equalWithPrecision(0) &&
fromBuffer.last.equalWithPrecision(calcVal),
"Order 2 Cheby Buffer (zero-offset) should be a parabola between 0 and 2");
buf.cheby([1], asWavetable: false);
buf.loadToFloatArray(action: { arg array; fromBuffer = array });
calcVal = [bufSize >> 1, bufSize-1].linlin(0, bufSize, -1, 1);
this.assert(
fromBuffer.first.equalWithPrecision(-1) &&
this.getSignalMidValue(fromBuffer).equalWithPrecision(calcVal.first) &&
fromBuffer.last.equalWithPrecision(calcVal.last),
"Order 1 Cheby Buffer should go from -1 to % by default".format(calcVal.last));
buf.cheby([0, 1], normalize: true, asWavetable: false);
buf.loadToFloatArray(action: { arg array; fromBuffer = array });
calcVal = cos(2 * acos((bufSize - 1)/(bufSize >> 1) - 1)) + 1 / 2;
this.assert(
fromBuffer.first.equalWithPrecision(1) &&
this.getSignalMidValue(fromBuffer).equalWithPrecision(0) &&
fromBuffer.last.equalWithPrecision(calcVal),
"Order 2 Cheby Buffer should be a parabola between from 0 to 1 by default (normalized, zero-offset)");
buf.cheby([0, 1, 0.5, -0.25], normalize: false, asWavetable: false);
buf.loadToFloatArray(action: { arg array; fromBuffer = array });
this.assert(
this.getSignalMidValue(fromBuffer).equalWithPrecision(0),
"A zeroOffset Cheby Buffer should have value 0 for its middle value");
}
// This is just a helper method
getSignalMidValue { arg sig;
^sig[sig.size >> 1]
}
}