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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
|
/*
* Copyright 2009 Phil Burk, Mobileer Inc
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.jsyn.ports;
import junit.framework.TestCase;
import com.jsyn.JSyn;
import com.jsyn.Synthesizer;
import com.jsyn.data.FloatSample;
import com.jsyn.data.SequentialData;
import com.jsyn.data.ShortSample;
import com.jsyn.unitgen.FixedRateMonoReader;
/**
* Test sample and envelope queuing and looping.
*
* @author Phil Burk, (C) 2009 Mobileer Inc
*/
public class TestQueuedDataPort extends TestCase {
Synthesizer synth;
float[] floatData = {
0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f
};
FloatSample floatSample;
FixedRateMonoReader reader;
@Override
protected void setUp() throws Exception {
super.setUp();
synth = JSyn.createSynthesizer();
synth.setRealTime(false);
synth.start();
}
@Override
protected void tearDown() throws Exception {
synth.stop();
super.tearDown();
}
private void queueDirect(UnitDataQueuePort port, SequentialData data, int startFrame,
int numFrames) {
queueDirect(port, data, startFrame, numFrames, 0);
}
private void queueDirect(UnitDataQueuePort port, SequentialData data, int startFrame,
int numFrames, int numLoops) {
QueueDataCommand command = port.createQueueDataCommand(data, startFrame, numFrames);
command.setNumLoops(numLoops);
port.addQueuedBlock(command);
}
public void testQueueSingleShort() {
short[] data = {
234, -9876, 4567
};
ShortSample sample = new ShortSample(data.length, 1);
sample.write(data);
UnitDataQueuePort dataQueue = new UnitDataQueuePort("test");
assertEquals("start empty", false, dataQueue.hasMore());
queueDirect(dataQueue, sample, 0, data.length);
checkQueuedData(data, dataQueue, 0, data.length);
assertEquals("end empty", false, dataQueue.hasMore());
}
public void testQueueSingleFloat() {
float[] data = {
0.4f, 1.9f, 22.7f
};
FloatSample sample = new FloatSample(data.length, 1);
sample.write(data);
UnitDataQueuePort dataQueue = new UnitDataQueuePort("test");
assertEquals("start empty", false, dataQueue.hasMore());
queueDirect(dataQueue, sample, 0, data.length);
checkQueuedData(data, dataQueue, 0, data.length);
assertEquals("end empty", false, dataQueue.hasMore());
}
public void testQueueOutOfBounds() {
float[] data = {
0.4f, 1.9f, 22.7f
};
FloatSample sample = new FloatSample(data.length, 1);
sample.write(data);
UnitDataQueuePort dataQueue = new UnitDataQueuePort("test");
boolean caught = false;
try {
queueDirect(dataQueue, sample, 0, sample.getNumFrames() + 1); // should cause an error!
} catch(IllegalArgumentException e) {
caught = true;
}
assertTrue("expect exception when we go past end of the array", caught);
caught = false;
try {
queueDirect(dataQueue, sample, 1, sample.getNumFrames()); // should cause an error!
} catch(IllegalArgumentException e) {
caught = true;
}
assertTrue("expect exception when we go past end of the array", caught);
caught = false;
try {
queueDirect(dataQueue, sample, -1, sample.getNumFrames()); // should cause an error!
} catch(IllegalArgumentException e) {
caught = true;
}
assertTrue("expect exception when we start before beginning of the array", caught);
}
public void testQueueMultiple() {
short[] data = {
234, 17777, -9876, 4567, -14287
};
ShortSample sample = new ShortSample(data.length, 1);
sample.write(data);
UnitDataQueuePort dataQueue = new UnitDataQueuePort("test");
assertEquals("start empty", false, dataQueue.hasMore());
queueDirect(dataQueue, sample, 1, 3);
queueDirect(dataQueue, sample, 0, 5);
queueDirect(dataQueue, sample, 2, 2);
checkQueuedData(data, dataQueue, 1, 3);
checkQueuedData(data, dataQueue, 0, 5);
checkQueuedData(data, dataQueue, 2, 2);
assertEquals("end empty", false, dataQueue.hasMore());
}
public void testQueueNoLoops() throws InterruptedException {
System.out.println("testQueueNoLoops() ================");
UnitDataQueuePort dataQueue = setupFloatSample();
dataQueue.queueOn(floatSample, synth.createTimeStamp());
// Advance synth so that the queue command propagates to the engine.
synth.sleepUntil(synth.getCurrentTime() + 0.01);
// play entire sample
checkQueuedData(floatData, dataQueue, 0, floatData.length);
assertEquals("end empty", false, dataQueue.hasMore());
}
public void testQueueLoopForever() throws InterruptedException {
System.out.println("testQueueLoopForever() ================");
UnitDataQueuePort dataQueue = setupFloatSample();
dataQueue.queue(floatSample, 0, 3);
dataQueue.queueLoop(floatSample, 3, 4);
// Advance synth so that the queue commands propagate to the engine.
synth.sleepUntil(synth.getCurrentTime() + 0.01);
checkQueuedData(floatData, dataQueue, 0, 3);
checkQueuedData(floatData, dataQueue, 3, 4);
checkQueuedData(floatData, dataQueue, 3, 4);
checkQueuedData(floatData, dataQueue, 3, 4);
checkQueuedData(floatData, dataQueue, 3, 1);
// queue final release
dataQueue.queue(floatSample, 3, 5);
synth.sleepUntil(synth.getCurrentTime() + 0.01);
// current loop will finish
checkQueuedData(floatData, dataQueue, 4, 3);
// release portion will play
checkQueuedData(floatData, dataQueue, 3, 5);
assertEquals("end empty", false, dataQueue.hasMore());
}
public void testQueueLoopAtLeastOnce() throws InterruptedException {
System.out.println("testQueueLoopAtLeastOnce() ================");
UnitDataQueuePort dataQueue = setupFloatSample();
dataQueue.queue(floatSample, 0, 3);
dataQueue.queueLoop(floatSample, 3, 2); // this should play at least once
dataQueue.queue(floatSample, 5, 2);
// Advance synth so that the queue commands propagate to the engine.
synth.sleepUntil(synth.getCurrentTime() + 0.01);
checkQueuedData(floatData, dataQueue, 0, 3);
checkQueuedData(floatData, dataQueue, 3, 2);
checkQueuedData(floatData, dataQueue, 5, 2);
assertEquals("end empty", false, dataQueue.hasMore());
}
public void testQueueNumLoops() throws InterruptedException {
System.out.println("testQueueNumLoops() ================");
UnitDataQueuePort dataQueue = setupFloatSample();
dataQueue.queue(floatSample, 0, 2);
int numLoopsA = 5;
dataQueue.queueLoop(floatSample, 2, 3, numLoopsA);
dataQueue.queue(floatSample, 4, 2);
int numLoopsB = 3;
dataQueue.queueLoop(floatSample, 3, 4, numLoopsB);
dataQueue.queue(floatSample, 5, 2);
// Advance synth so that the queue commands propagate to the engine.
synth.sleepUntil(synth.getCurrentTime() + 0.01);
checkQueuedData(floatData, dataQueue, 0, 2);
for (int i = 0; i < (numLoopsA + 1); i++) {
System.out.println("loop A #" + i);
checkQueuedData(floatData, dataQueue, 2, 3);
}
checkQueuedData(floatData, dataQueue, 4, 2);
for (int i = 0; i < (numLoopsB + 1); i++) {
System.out.println("loop B #" + i);
checkQueuedData(floatData, dataQueue, 3, 4);
}
checkQueuedData(floatData, dataQueue, 5, 2);
assertEquals("end empty", false, dataQueue.hasMore());
}
private UnitDataQueuePort setupFloatSample() {
floatSample = new FloatSample(floatData.length, 1);
floatSample.write(floatData);
synth.add(reader = new FixedRateMonoReader());
UnitDataQueuePort dataQueue = reader.dataQueue;
assertEquals("start empty", false, dataQueue.hasMore());
return dataQueue;
}
public void testQueueSustainLoop() throws InterruptedException {
System.out.println("testQueueSustainLoop() ================");
UnitDataQueuePort dataQueue = setupFloatSample();
// set up sustain loops ===========================
floatSample.setSustainBegin(2);
floatSample.setSustainEnd(4);
floatSample.setReleaseBegin(-1);
floatSample.setReleaseEnd(-1);
dataQueue.queueOn(floatSample, synth.createTimeStamp());
// Advance synth so that the queue command propagates to the engine.
synth.sleepUntil(synth.getCurrentTime() + 0.01);
checkQueuedData(floatData, dataQueue, 0, 2);
checkQueuedData(floatData, dataQueue, 2, 2);
checkQueuedData(floatData, dataQueue, 2, 2);
checkQueuedData(floatData, dataQueue, 2, 1); // looping
dataQueue.queueOff(floatSample, true); // queue off in middle of loop
synth.sleepUntil(synth.getCurrentTime() + 0.01);
checkQueuedData(floatData, dataQueue, 3, 5); // release
assertEquals("end empty", false, dataQueue.hasMore());
}
public void testQueueReleaseLoop() throws InterruptedException {
System.out.println("testQueueReleaseLoop() ================");
UnitDataQueuePort dataQueue = setupFloatSample();
// set up sustain loops ===========================
floatSample.setSustainBegin(-1);
floatSample.setSustainEnd(-1);
floatSample.setReleaseBegin(4);
floatSample.setReleaseEnd(6);
dataQueue.queueOn(floatSample, synth.createTimeStamp());
// Advance synth so that the queue command propagates to the engine.
synth.sleepUntil(synth.getCurrentTime() + 0.01);
checkQueuedData(floatData, dataQueue, 0, 4);
checkQueuedData(floatData, dataQueue, 4, 2);
checkQueuedData(floatData, dataQueue, 4, 2);
checkQueuedData(floatData, dataQueue, 4, 2); // looping in release cuz no
// sustain loop
dataQueue.queueOff(floatSample, true); // queue off in middle of loop
synth.sleepUntil(synth.getCurrentTime() + 0.01);
checkQueuedData(floatData, dataQueue, 4, 2);
checkQueuedData(floatData, dataQueue, 4, 2); // still looping
assertEquals("end full", true, dataQueue.hasMore());
}
public void testQueueSustainReleaseLoops() throws InterruptedException {
System.out.println("testQueueSustainReleaseLoops() ================");
UnitDataQueuePort dataQueue = setupFloatSample();
// set up sustain loops ===========================
floatSample.setSustainBegin(2);
floatSample.setSustainEnd(4);
floatSample.setReleaseBegin(5);
floatSample.setReleaseEnd(7);
dataQueue.queueOn(floatSample, synth.createTimeStamp());
// Advance synth so that the queue command propagates to the engine.
synth.sleepUntil(synth.getCurrentTime() + 0.01);
checkQueuedData(floatData, dataQueue, 0, 4);
checkQueuedData(floatData, dataQueue, 2, 2);
checkQueuedData(floatData, dataQueue, 2, 1); // middle of sustain loop
dataQueue.queueOff(floatSample, true); // queue off in middle of loop
synth.sleepUntil(synth.getCurrentTime() + 0.01);
checkQueuedData(floatData, dataQueue, 3, 2);
checkQueuedData(floatData, dataQueue, 5, 2); // release loop
checkQueuedData(floatData, dataQueue, 5, 2); // release loop
assertEquals("end full", true, dataQueue.hasMore());
}
private void checkQueuedData(short[] data, UnitDataQueuePort dataQueue, int offset,
int numFrames) {
for (int i = 0; i < numFrames; i++) {
assertEquals("got data", true, dataQueue.hasMore());
double value = dataQueue.readNextMonoDouble(synth.getFramePeriod());
assertEquals("data matches", data[i + offset] / 32768.0, value, 0.0001);
}
}
private void checkQueuedData(float[] data, UnitDataQueuePort dataQueue, int offset,
int numFrames) {
for (int i = 0; i < numFrames; i++) {
assertEquals("got data", true, dataQueue.hasMore());
double value = dataQueue.readNextMonoDouble(synth.getFramePeriod());
assertEquals("data matches", data[i + offset], value, 0.0001);
}
}
class TestQueueCallback implements UnitDataQueueCallback {
boolean gotStarted = false;
boolean gotLooped = false;
boolean gotFinished = false;
QueueDataEvent lastEvent;
@Override
public void started(QueueDataEvent event) {
System.out.println("Callback started.");
gotStarted = true;
lastEvent = event;
}
@Override
public void looped(QueueDataEvent event) {
System.out.println("Callback looped.");
gotLooped = true;
lastEvent = event;
}
@Override
public void finished(QueueDataEvent event) {
System.out.println("Callback finished.");
gotFinished = true;
lastEvent = event;
}
}
public void testQueueCallback() {
float[] data = {
0.2f, -8.9f, 2.7f
};
FloatSample sample = new FloatSample(data.length, 1);
sample.write(data);
UnitDataQueuePort dataQueue = new UnitDataQueuePort("test");
assertEquals("start empty", false, dataQueue.hasMore());
// Create an object to be called when the queued data is done.
TestQueueCallback callback = new TestQueueCallback();
QueueDataCommand command = dataQueue.createQueueDataCommand(sample, 0, data.length);
command.setCallback(callback);
command.setNumLoops(2);
dataQueue.addQueuedBlock(command);
// Check to see if flags get set true by callback.
dataQueue.firePendingCallbacks();
assertEquals("not started yet", false, callback.gotStarted);
assertEquals("not looped yet", false, callback.gotLooped);
assertEquals("not finished yet", false, callback.gotFinished);
checkQueuedData(data, dataQueue, 0, 1);
dataQueue.firePendingCallbacks();
assertEquals("should be started now", true, callback.gotStarted);
assertEquals("not looped yet", false, callback.gotLooped);
assertEquals("not finished yet", false, callback.gotFinished);
assertEquals("check source of event", dataQueue, callback.lastEvent.getSource());
assertEquals("check sample", sample, callback.lastEvent.getSequentialData());
assertEquals("check loopCount", 2, callback.lastEvent.getLoopsLeft());
checkQueuedData(data, dataQueue, 1, data.length - 1);
dataQueue.firePendingCallbacks();
assertEquals("should be looped now", true, callback.gotLooped);
assertEquals("check loopCount", 1, callback.lastEvent.getLoopsLeft());
assertEquals("not finished yet", false, callback.gotFinished);
checkQueuedData(data, dataQueue, 0, data.length);
dataQueue.firePendingCallbacks();
assertEquals("check loopCount", 0, callback.lastEvent.getLoopsLeft());
checkQueuedData(data, dataQueue, 0, data.length);
dataQueue.firePendingCallbacks();
assertEquals("should be finished now", true, callback.gotFinished);
assertEquals("end empty", false, dataQueue.hasMore());
}
public void testImmediate() {
float[] data = {
0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f, 10.0f, 11.0f
};
FloatSample sample = new FloatSample(data.length, 1);
sample.write(data);
UnitDataQueuePort dataQueue = new UnitDataQueuePort("test");
dataQueue.queue(sample);
// Only play some of the data then interrupt it with an immediate block.
checkQueuedData(data, dataQueue, 0, 3);
QueueDataCommand command = dataQueue.createQueueDataCommand(sample, 7, 3);
command.setImmediate(true);
command.run(); // execute "immediate" operation and add to block list
// Should already be in new data.
checkQueuedData(data, dataQueue, 7, 3);
}
public void testCrossFade() {
float[] data1 = {
0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f
};
float[] data2 = {
20.0f, 19.0f, 18.0f, 17.0f, 16.0f, 15.0f, 14.0f, 13.0f, 12.0f, 11.0f
};
FloatSample sample1 = new FloatSample(data1);
FloatSample sample2 = new FloatSample(data2);
UnitDataQueuePort dataQueue = new UnitDataQueuePort("test");
dataQueue.queue(sample1, 0, 4);
QueueDataCommand command = dataQueue.createQueueDataCommand(sample2, 1, 8);
command.setCrossFadeIn(3);
command.run(); // execute "immediate" operation and add to block list
// Only play some of the data then crossfade to another sample.
checkQueuedData(data1, dataQueue, 0, 4);
for (int i = 0; i < 3; i++) {
double factor = i / 3.0;
double value = ((1.0 - factor) * data1[i + 4]) + (factor * data2[i + 1]);
System.out.println("i = " + i + ", factor = " + factor + ", value = " + value);
double actual = dataQueue.readNextMonoDouble(synth.getFramePeriod());
assertEquals("crossfade " + i, value, actual, 0.00001);
}
// Should already be in new data.
checkQueuedData(data2, dataQueue, 4, 5);
}
public void testImmediateCrossFade() {
float[] data1 = {
0.0f, 1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f, 9.0f
};
float[] data2 = {
20.0f, 19.0f, 18.0f, 17.0f, 16.0f, 15.0f, 14.0f, 13.0f, 12.0f, 11.0f
};
FloatSample sample1 = new FloatSample(data1);
FloatSample sample2 = new FloatSample(data2);
UnitDataQueuePort dataQueue = new UnitDataQueuePort("test");
dataQueue.queue(sample1, 0, 4);
// Only play some of the data then crossfade to another sample.
int beforeInterrupt = 2;
checkQueuedData(data1, dataQueue, 0, beforeInterrupt);
QueueDataCommand command = dataQueue.createQueueDataCommand(sample2, 1, 8);
command.setImmediate(true);
command.setCrossFadeIn(3);
command.run(); // execute "immediate" operation and add to block list
for (int i = 0; i < 3; i++) {
double factor = i / 3.0;
double value = ((1.0 - factor) * data1[i + beforeInterrupt]) + (factor * data2[i + 1]);
System.out.println("i = " + i + ", factor = " + factor + ", value = " + value);
double actual = dataQueue.readNextMonoDouble(synth.getFramePeriod());
assertEquals("crossfade " + i, value, actual, 0.00001);
}
// Should already be in new data.
checkQueuedData(data2, dataQueue, 4, 5);
}
}
|