aboutsummaryrefslogtreecommitdiffstats
path: root/src/test/java/com/jsyn/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/test/java/com/jsyn/util')
-rw-r--r--src/test/java/com/jsyn/util/DebugSampleLoader.java143
-rw-r--r--src/test/java/com/jsyn/util/TestFFT.java201
-rw-r--r--src/test/java/com/jsyn/util/TestPseudoRandom.java76
-rw-r--r--src/test/java/com/jsyn/util/TestVoiceAllocator.java111
4 files changed, 531 insertions, 0 deletions
diff --git a/src/test/java/com/jsyn/util/DebugSampleLoader.java b/src/test/java/com/jsyn/util/DebugSampleLoader.java
new file mode 100644
index 0000000..c0ddef5
--- /dev/null
+++ b/src/test/java/com/jsyn/util/DebugSampleLoader.java
@@ -0,0 +1,143 @@
+/*
+ * Copyright 2010 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.util;
+
+import java.io.File;
+import java.io.IOException;
+
+import com.jsyn.JSyn;
+import com.jsyn.Synthesizer;
+import com.jsyn.data.FloatSample;
+import com.jsyn.unitgen.LineOut;
+import com.jsyn.unitgen.VariableRateDataReader;
+import com.jsyn.unitgen.VariableRateMonoReader;
+import com.jsyn.unitgen.VariableRateStereoReader;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * Play a sample from a WAV file using JSyn.
+ *
+ * @author Phil Burk (C) 2010 Mobileer Inc
+ */
+public class DebugSampleLoader {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(DebugSampleLoader.class);
+
+ private Synthesizer synth;
+ private VariableRateDataReader samplePlayer;
+ private LineOut lineOut;
+
+ private void test() throws IOException {
+ // File sampleFile = new File("samples/cello_markers.wav");
+ // File sampleFile = new File("samples/Piano_A440_PT.aif");
+ File sampleFile = new File("samples/sine_400_loop_i16.wav");
+ // File sampleFile = new File("samples/TwoDiffPitchedSines_F32_PT.wav");
+ // File sampleFile = new File("samples/sine_400_u8.aif");
+ // File sampleFile = new File("samples/sine_400_s8.aif");
+ // File sampleFile = new File("samples/sine_400_ulaw.aif");
+ // File sampleFile = new File("samples/sine_400_ulaw.wav");
+
+ // File sampleFile = new File("samples/aaClarinet.wav");
+ // File sampleFile = new File("samples/sine_400_mono.wav");
+ // File sampleFile = new File("samples/sine_200_300_i16.wav");
+ // File sampleFile = new File("samples/sine_200_300_i24.wav");
+ // File sampleFile = new File("samples/M1F1-int16-AFsp.wav");
+ // File sampleFile = new File("samples/M1F1-int24-AFsp.wav");
+ // File sampleFile = new File("samples/M1F1-float32-AFsp.wav");
+ // File sampleFile = new File("samples/M1F1-int16WE-AFsp.wav");
+ // File sampleFile = new File("samples/M1F1-int24WE-AFsp.wav");
+ // File sampleFile = new File("samples/M1F1-float32WE-AFsp.wav");
+ // File sampleFile = new File("samples/sine_200_300_i16.aif");
+ // File sampleFile = new File("samples/sine_200_300_f32.wavex");
+ // File sampleFile = new File("samples/Sine32bit.aif");
+ // File sampleFile = new File("samples/Sine32bit.wav");
+ // File sampleFile = new File("samples/smartCue.wav");
+
+ // URL sampleFile = new URL("http://www.softsynth.com/samples/Clarinet.wav");
+
+ synth = JSyn.createSynthesizer();
+
+ FloatSample sample;
+ try {
+ // Add an output mixer.
+ synth.add(lineOut = new LineOut());
+
+ // Load the sample and display its properties.
+ SampleLoader.setJavaSoundPreferred(false);
+ sample = SampleLoader.loadFloatSample(sampleFile);
+ LOGGER.debug("Sample has: channels = " + sample.getChannelsPerFrame());
+ LOGGER.debug(" frames = " + sample.getNumFrames());
+ LOGGER.debug(" rate = " + sample.getFrameRate());
+ LOGGER.debug(" loopStart = " + sample.getSustainBegin());
+ LOGGER.debug(" loopEnd = " + sample.getSustainEnd());
+
+ if (sample.getChannelsPerFrame() == 1) {
+ synth.add(samplePlayer = new VariableRateMonoReader());
+ samplePlayer.output.connect(0, lineOut.input, 0);
+ } else if (sample.getChannelsPerFrame() == 2) {
+ synth.add(samplePlayer = new VariableRateStereoReader());
+ samplePlayer.output.connect(0, lineOut.input, 0);
+ samplePlayer.output.connect(1, lineOut.input, 1);
+ } else {
+ throw new RuntimeException("Can only play mono or stereo samples.");
+ }
+
+ // Start synthesizer using default stereo output at 44100 Hz.
+ synth.start();
+
+ samplePlayer.rate.set(sample.getFrameRate());
+
+ // We only need to start the LineOut. It will pull data from the
+ // sample player.
+ lineOut.start();
+
+ // We can simply queue the entire file.
+ // Or if it has a loop we can play the loop for a while.
+ if (sample.getSustainBegin() < 0) {
+ LOGGER.debug("queue the sample");
+ samplePlayer.dataQueue.queue(sample);
+ } else {
+ LOGGER.debug("queueOn the sample");
+ samplePlayer.dataQueue.queueOn(sample);
+ synth.sleepFor(8.0);
+ LOGGER.debug("queueOff the sample");
+ samplePlayer.dataQueue.queueOff(sample);
+ }
+
+ // Wait until the sample has finished playing.
+ do {
+ synth.sleepFor(1.0);
+ } while (samplePlayer.dataQueue.hasMore());
+
+ } catch (IOException e1) {
+ e1.printStackTrace();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ // Stop everything.
+ synth.stop();
+ }
+
+ public static void main(String[] args) {
+ try {
+ new DebugSampleLoader().test();
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+}
diff --git a/src/test/java/com/jsyn/util/TestFFT.java b/src/test/java/com/jsyn/util/TestFFT.java
new file mode 100644
index 0000000..5d130c5
--- /dev/null
+++ b/src/test/java/com/jsyn/util/TestFFT.java
@@ -0,0 +1,201 @@
+/*
+ * 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.util;
+
+import com.softsynth.math.FourierMath;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+
+public class TestFFT {
+
+ private static final Logger LOGGER = LoggerFactory.getLogger(TestFFT.class);
+
+ public void checkSingleSineDouble(int size, int bin) {
+ double[] ar = new double[size];
+ double[] ai = new double[size];
+ double[] magnitudes = new double[size];
+
+ double amplitude = 1.0;
+ addSineWave(size, bin, ar, amplitude);
+
+ FourierMath.transform(1, size, ar, ai);
+ FourierMath.calculateMagnitudes(ar, ai, magnitudes);
+
+ assertEquals(0.0, magnitudes[bin-1], 0.000001, "magnitude");
+ assertEquals(amplitude, magnitudes[bin], 0.000001, "magnitude");
+ assertEquals(0.0, magnitudes[bin+1], 0.000001, "magnitude");
+ /*
+ for (int i = 0; i < magnitudes.length; i++) {
+ System.out.printf("%d = %9.7f\n", i, magnitudes[i]);
+ }
+*/
+
+ }
+
+ public void checkSingleSineFloat(int size, int bin) {
+ float[] ar = new float[size];
+ float[] ai = new float[size];
+ float[] magnitudes = new float[size];
+
+ double amplitude = 1.0;
+ addSineWave(size, bin, ar, amplitude);
+
+ FourierMath.transform(1, size, ar, ai);
+ FourierMath.calculateMagnitudes(ar, ai, magnitudes);
+
+ assertEquals(0.0f, magnitudes[bin-1], 0.000001, "magnitude");
+ assertEquals(amplitude, magnitudes[bin], 0.000001, "magnitude");
+ assertEquals(0.0f, magnitudes[bin+1], 0.000001, "magnitude");
+/*
+ for (int i = 0; i < magnitudes.length; i++) {
+ System.out.printf("%d = %9.7f\n", i, magnitudes[i]);
+ }
+*/
+ }
+
+ public void checkMultipleSine(int size, int[] bins, double[] amplitudes) {
+ double[] ar = new double[size];
+ double[] ai = new double[size];
+ double[] magnitudes = new double[size];
+
+ for(int i = 0; i<bins.length; i++) {
+ addSineWave(size, bins[i], ar, amplitudes[i]);
+ }
+
+ FourierMath.transform(1, size, ar, ai);
+ FourierMath.calculateMagnitudes(ar, ai, magnitudes);
+
+ for(int bin = 0; bin<size; bin++) {
+ System.out.printf("%d = %9.7f\n", bin, magnitudes[bin]);
+
+ double amplitude = 0.0;
+ for(int i = 0; i<bins.length; i++) {
+ if ((bin == bins[i]) || (bin == (size - bins[i]))) {
+ amplitude = amplitudes[i];
+ break;
+ }
+ }
+ assertEquals(amplitude, magnitudes[bin], 0.000001, "magnitude");
+ }
+
+ }
+
+ private void addSineWave(int size, int bin, double[] ar, double amplitude) {
+ double phase = 0.0;
+ double phaseIncrement = 2.0 * Math.PI * bin / size;
+ for (int i = 0; i < size; i++) {
+ ar[i] += Math.sin(phase) * amplitude;
+ phase += phaseIncrement;
+ }
+ }
+ private void addSineWave(int size, int bin, float[] ar, double amplitude) {
+ double phase = 0.0;
+ double phaseIncrement = 2.0 * Math.PI * bin / size;
+ for (int i = 0; i < size; i++) {
+ ar[i] += (float) (Math.sin(phase) * amplitude);
+ phase += phaseIncrement;
+ }
+ }
+
+ public void testSinglesDouble() {
+ checkSingleSineDouble(32, 1);
+ checkSingleSineDouble(32, 4);
+ checkSingleSineDouble(64, 5);
+ checkSingleSineDouble(256, 3);
+ }
+
+ public void testSinglesFloat() {
+ checkSingleSineFloat(32, 1);
+ checkSingleSineFloat(32, 4);
+ checkSingleSineFloat(64, 5);
+ checkSingleSineFloat(256, 3);
+ }
+
+ public void testMultipleSines32() {
+ int[] bins = { 1, 5 };
+ double[] amplitudes = { 1.0, 2.0 };
+ checkMultipleSine(32, bins, amplitudes);
+ }
+
+ public void testMultipleSines64() {
+ int[] bins = { 2, 4, 7 };
+ double[] amplitudes = { 1.0, 0.3, 0.5 };
+ checkMultipleSine(64, bins, amplitudes);
+ }
+
+ public void checkInverseFftDouble(int size, int bin) {
+ double[] ar1 = new double[size];
+ double[] ai1 = new double[size];
+ double[] ar2 = new double[size];
+ double[] ai2 = new double[size];
+
+ double amplitude = 1.0;
+ addSineWave(size, bin, ar1, amplitude);
+
+ // Save a copy of the source.
+ System.arraycopy(ar1, 0, ar2, 0, size);
+ System.arraycopy(ai1, 0, ai2, 0, size);
+
+ FourierMath.transform(1, size, ar1, ai1); // FFT
+
+ FourierMath.transform(-1, size, ar1, ai1); // IFFT
+
+ for (int i = 0; i < size; i++) {
+ assertEquals(ar2[i], ar1[i], 0.00001);
+ assertEquals(ai2[i], ai1[i], 0.00001);
+ }
+ }
+
+ public void checkInverseFftFloat(int size, int bin) {
+ float[] ar1 = new float[size];
+ float[] ai1 = new float[size];
+ float[] ar2 = new float[size];
+ float[] ai2 = new float[size];
+
+ double amplitude = 1.0;
+ addSineWave(size, bin, ar1, amplitude);
+
+ // Save a copy of the source.
+ System.arraycopy(ar1, 0, ar2, 0, size);
+ System.arraycopy(ai1, 0, ai2, 0, size);
+
+ FourierMath.transform(1, size, ar1, ai1); // FFT
+
+ FourierMath.transform(-1, size, ar1, ai1); // IFFT
+
+ for (int i = 0; i < size; i++) {
+ assertEquals(ar2[i], ar1[i], 0.00001);
+ assertEquals(ai2[i], ai1[i], 0.00001);
+ }
+ }
+
+ public void testInverseDouble() {
+ checkInverseFftDouble(32, 1);
+ checkInverseFftDouble(32, 2);
+ checkInverseFftDouble(128, 17);
+ checkInverseFftDouble(512, 23);
+ }
+
+ public void testInverseFloat() {
+ checkInverseFftFloat(32, 1);
+ checkInverseFftFloat(32, 2);
+ checkInverseFftFloat(128, 17);
+ checkInverseFftFloat(512, 23);
+ }
+}
diff --git a/src/test/java/com/jsyn/util/TestPseudoRandom.java b/src/test/java/com/jsyn/util/TestPseudoRandom.java
new file mode 100644
index 0000000..b37475f
--- /dev/null
+++ b/src/test/java/com/jsyn/util/TestPseudoRandom.java
@@ -0,0 +1,76 @@
+/*
+ * 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.util;
+
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class TestPseudoRandom {
+ PseudoRandom pseudoRandom;
+ private int[] bins;
+ private final static int BIN_SHIFTER = 8;
+ private final static int BIN_COUNT = 1 << BIN_SHIFTER;
+ private final static int BIN_MASK = BIN_COUNT - 1;
+
+ @Test
+ public void testMath() {
+ long seed = 3964771111L;
+ int positiveInt = (int) (seed & 0x7FFFFFFF);
+ assertTrue((positiveInt >= 0), "masked random positive, " + positiveInt);
+ double rand = positiveInt * (1.0 / (1L << 31));
+ assertTrue((rand >= 0.0), "not too low, " + rand);
+ assertTrue((rand < 1.0), "not too high, " + rand);
+ }
+
+ @Test
+ public void testIntegerDistribution() {
+ int scaler = 100;
+ for (int i = 0; i < (bins.length * scaler); i++) {
+ int rand = pseudoRandom.nextRandomInteger();
+ int positiveInt = rand & 0x7FFFFFFF;
+ assertTrue((positiveInt >= 0), "masked random " + positiveInt);
+ int index = (rand >> (32 - BIN_SHIFTER)) & BIN_MASK;
+ bins[index] += 1;
+ }
+ checkDistribution(scaler);
+ }
+
+ @Test
+ public void test01Distribution() {
+ int scaler = 100;
+ for (int i = 0; i < (bins.length * scaler); i++) {
+ double rand = pseudoRandom.random();
+ assertTrue((rand >= 0.0), "not too low, #" + i + " = " + rand);
+ assertTrue((rand < 1.0), "not too high, #" + i + " = " + rand);
+ int index = (int) (rand * BIN_COUNT);
+ bins[index] += 1;
+ }
+ checkDistribution(scaler);
+ }
+
+ private void checkDistribution(int scaler) {
+ // Generate running average that should stay near scaler
+ double average = scaler;
+ double coefficient = 0.9;
+ for (int i = 0; i < (bins.length); i++) {
+ average = (average * coefficient) + (bins[i] * (1.0 - coefficient));
+ assertEquals(scaler, average, 0.2 * scaler, "average at " + i);
+ }
+ }
+}
diff --git a/src/test/java/com/jsyn/util/TestVoiceAllocator.java b/src/test/java/com/jsyn/util/TestVoiceAllocator.java
new file mode 100644
index 0000000..061e2ae
--- /dev/null
+++ b/src/test/java/com/jsyn/util/TestVoiceAllocator.java
@@ -0,0 +1,111 @@
+/*
+ * 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.util;
+
+import com.jsyn.instruments.SubtractiveSynthVoice;
+import com.jsyn.unitgen.UnitVoice;
+import org.junit.jupiter.api.BeforeAll;
+import org.junit.jupiter.api.BeforeEach;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+public class TestVoiceAllocator {
+ VoiceAllocator allocator;
+ int max = 4;
+ UnitVoice[] voices;
+
+ @BeforeEach
+ private void beforeEach() {
+ voices = new UnitVoice[max];
+ for (int i = 0; i < max; i++) {
+ voices[i] = new SubtractiveSynthVoice();
+ }
+
+ allocator = new VoiceAllocator(voices);
+ }
+
+ @Test
+ public void testAllocation() {
+ assertEquals(max, allocator.getVoiceCount(), "get max");
+
+ int tag1 = 61;
+ int tag2 = 62;
+ int tag3 = 63;
+ int tag4 = 64;
+ int tag5 = 65;
+ int tag6 = 66;
+ UnitVoice voice1 = allocator.allocate(tag1);
+ assertTrue((voice1 != null), "voice should be non-null");
+
+ UnitVoice voice2 = allocator.allocate(tag2);
+ assertTrue((voice2 != null), "voice should be non-null");
+ assertTrue((voice2 != voice1), "new voice ");
+
+ UnitVoice voice = allocator.allocate(tag1);
+ assertTrue((voice == voice1), "should be voice1 again ");
+
+ voice = allocator.allocate(tag2);
+ assertTrue((voice == voice2), "should be voice2 again ");
+
+ UnitVoice voice3 = allocator.allocate(tag3);
+ @SuppressWarnings("unused")
+ UnitVoice voice4 = allocator.allocate(tag4);
+
+ UnitVoice voice5 = allocator.allocate(tag5);
+ assertTrue((voice5 == voice1), "ran out so get voice1 as oldest");
+
+ voice = allocator.allocate(tag2);
+ assertTrue((voice == voice2), "should be voice2 again ");
+
+ // Now voice 3 should be the oldest cuz voice 2 was touched.
+ UnitVoice voice6 = allocator.allocate(tag6);
+ assertTrue((voice6 == voice3), "ran out so get voice3 as oldest");
+ }
+
+ @Test
+ public void testOff() {
+ int tag1 = 61;
+ int tag2 = 62;
+ int tag3 = 63;
+ int tag4 = 64;
+ int tag5 = 65;
+ int tag6 = 66;
+ UnitVoice voice1 = allocator.allocate(tag1);
+ UnitVoice voice2 = allocator.allocate(tag2);
+ UnitVoice voice3 = allocator.allocate(tag3);
+ UnitVoice voice4 = allocator.allocate(tag4);
+
+ assertTrue(allocator.isOn(tag3), "voice 3 should start on");
+ allocator.off(tag3);
+ assertFalse(allocator.isOn(tag3), "voice 3 should now be off");
+
+ allocator.off(tag2);
+
+ UnitVoice voice5 = allocator.allocate(tag5);
+ assertTrue((voice5 == voice3), "should get voice3 cuz off first");
+ UnitVoice voice6 = allocator.allocate(tag6);
+ assertTrue((voice6 == voice2), "should get voice2 cuz off second");
+ voice3 = allocator.allocate(tag3);
+ assertTrue((voice3 == voice1), "should get voice1 cuz on first");
+
+ voice1 = allocator.allocate(tag1);
+ assertTrue((voice1 == voice4), "should get voice4 cuz next up");
+ }
+}