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
|
/*
* 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.engine;
import junit.framework.TestCase;
import com.jsyn.unitgen.LineOut;
import com.jsyn.unitgen.PitchDetector;
import com.jsyn.unitgen.SineOscillator;
import com.jsyn.unitgen.ZeroCrossingCounter;
public class TestEngine extends TestCase {
public void testInitialization() {
final int DEFAULT_FRAME_RATE = 44100;
SynthesisEngine synthesisEngine = new SynthesisEngine();
assertEquals("frameCount zero before starting", 0, synthesisEngine.getFrameCount());
assertEquals("default frameRate", DEFAULT_FRAME_RATE, synthesisEngine.getFrameRate());
assertEquals("default pullData", true, synthesisEngine.isPullDataEnabled());
}
public void checkPullData(boolean pullData) {
SynthesisEngine synthesisEngine = new SynthesisEngine();
assertEquals("default realTime", true, synthesisEngine.isRealTime());
synthesisEngine.setRealTime(false);
assertEquals("default pullData", true, synthesisEngine.isPullDataEnabled());
synthesisEngine.setPullDataEnabled(pullData);
SineOscillator sineOscillator = new SineOscillator();
synthesisEngine.add(sineOscillator);
LineOut lineOut = new LineOut();
synthesisEngine.add(lineOut);
sineOscillator.output.connect(0, lineOut.input, 0);
assertEquals("initial sine value", 0.0, sineOscillator.output.getValue());
synthesisEngine.start();
if (!pullData) {
sineOscillator.start();
}
// We always have to start the LineOut.
lineOut.start();
synthesisEngine.generateNextBuffer();
synthesisEngine.generateNextBuffer();
double value = sineOscillator.output.getValue();
assertTrue("sine value after generation = " + value, (value > 0.0));
}
public void testPullDataFalse() {
checkPullData(false);
}
public void testPullDataTrue() {
checkPullData(true);
}
public void testMixedAdding() {
boolean gotCaught = false;
SynthesisEngine synthesisEngine1 = new SynthesisEngine();
synthesisEngine1.setRealTime(false);
synthesisEngine1.setPullDataEnabled(true);
SynthesisEngine synthesisEngine2 = new SynthesisEngine();
synthesisEngine2.setRealTime(false);
synthesisEngine2.setPullDataEnabled(true);
// Create a sineOscillator but do not add it to the synth!
SineOscillator sineOscillator = new SineOscillator();
LineOut lineOut = new LineOut();
synthesisEngine1.add(lineOut);
synthesisEngine2.add(sineOscillator);
try {
sineOscillator.output.connect(0, lineOut.input, 0);
} catch (RuntimeException e) {
gotCaught = true;
assertTrue("informative MPE message", e.getMessage().contains("different synths"));
}
assertTrue("caught NPE caused by forgetting synth.add", gotCaught);
}
public void testNotAdding() {
SynthesisEngine synthesisEngine = new SynthesisEngine();
synthesisEngine.setRealTime(false);
synthesisEngine.setPullDataEnabled(true);
// Create a sineOscillator but do not add it to the synth!
SineOscillator sineOscillator = new SineOscillator();
LineOut lineOut = new LineOut();
sineOscillator.output.connect(0, lineOut.input, 0);
synthesisEngine.add(lineOut);
assertEquals("initial sine value", 0.0, sineOscillator.output.getValue());
synthesisEngine.start();
// We always have to start the LineOut.
lineOut.start();
boolean gotCaught = false;
try {
synthesisEngine.generateNextBuffer();
synthesisEngine.generateNextBuffer();
} catch (NullPointerException e) {
gotCaught = true;
assertTrue("informative MPE message", e.getMessage().contains("forgot to add"));
}
assertTrue("caught NPE caused by forgetting synth.add", gotCaught);
}
public void testMultipleStarts() throws InterruptedException {
SynthesisEngine synth = new SynthesisEngine();
// Create a sineOscillator but do not add it to the synth!
SineOscillator osc = new SineOscillator();
ZeroCrossingCounter counter = new ZeroCrossingCounter();
PitchDetector pitchDetector = new PitchDetector();
LineOut lineOut = new LineOut();
synth.add(osc);
synth.add(counter);
synth.add(lineOut);
synth.add(pitchDetector);
osc.output.connect(counter.input);
osc.output.connect(pitchDetector.input);
counter.output.connect(0, lineOut.input, 0);
assertEquals("initial count", 0, counter.getCount());
int[] rates = {
32000, 48000, 44100, 22050
};
for (int rate : rates) {
synth.start(rate);
lineOut.start();
pitchDetector.start();
double time = synth.getCurrentTime();
double interval = 1.0;
time += interval;
long previousFrameCount = counter.getCount();
synth.sleepUntil(time);
double frequencyMeasured = pitchDetector.frequency.get();
double confidenceMeasured = pitchDetector.confidence.get();
double oscFreq = osc.frequency.get();
String msg = "freq at " + rate + " Hz";
System.out.println(msg);
assertEquals(msg, oscFreq, frequencyMeasured, oscFreq * 0.1);
assertEquals("pitch confidence", 0.9, confidenceMeasured, 0.1);
double expectedCount = interval * oscFreq;
double framesMeasured = counter.getCount() - previousFrameCount;
msg = "count at " + rate + " Hz";
System.out.println(msg);
assertEquals(msg, expectedCount, framesMeasured, expectedCount * 0.1);
synth.stop();
}
}
}
|