public TGMixer() { super(new Line.Info(Mixer.class)); this.lines = new HashMap<Class<?>, Line>(); this.sourceLineInfo = new ArrayList<DataLine.Info>(); this.targetLineInfo = new ArrayList<DataLine.Info>(); List<AudioFormat> formats = new ArrayList<AudioFormat>(); for (int channels = 1; channels <= 2; channels++) { formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, 8, channels, channels, AudioSystem.NOT_SPECIFIED, false)); formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, 8, channels, channels, AudioSystem.NOT_SPECIFIED, false)); for (int bits = 16; bits < 32; bits += 8) { formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, false)); formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, false)); formats.add(new AudioFormat(Encoding.PCM_SIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, true)); formats.add(new AudioFormat(Encoding.PCM_UNSIGNED, AudioSystem.NOT_SPECIFIED, bits, channels, channels * bits / 8, AudioSystem.NOT_SPECIFIED, true)); } formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4, AudioSystem.NOT_SPECIFIED, false)); formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 32, channels, channels * 4, AudioSystem.NOT_SPECIFIED, true)); formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8, AudioSystem.NOT_SPECIFIED, false)); formats.add(new AudioFormat(AudioFloatConverter.PCM_FLOAT, AudioSystem.NOT_SPECIFIED, 64, channels, channels * 8, AudioSystem.NOT_SPECIFIED, true)); } this.sourceLineInfo.add(new DataLine.Info(SourceDataLine.class, formats.toArray(new AudioFormat[formats.size()]), AudioSystem.NOT_SPECIFIED, AudioSystem.NOT_SPECIFIED)); }
private static void test(final Encoding enc, final byte[] expected, boolean end) { AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100, end); byte[] bytes = new byte[FLOATS.length * af.getFrameSize()]; AudioFloatConverter conv = AudioFloatConverter.getConverter(af); conv.toByteArray(FLOATS, bytes); if (!Arrays.equals(bytes, expected)) { System.err.println("Actual: " + Arrays.toString(bytes)); System.err.println("Expected: " + Arrays.toString(expected)); throw new RuntimeException(); } float[] floats = new float[bytes.length / af.getFrameSize()]; conv.toFloatArray(bytes, floats); if (!Arrays.equals(floats, FLOATS)) { System.err.println("Actual: " + Arrays.toString(floats)); System.err.println("Expected: " + Arrays.toString(FLOATS)); throw new RuntimeException(); } }
private static void test(final Encoding enc, final byte[] expected, boolean end) { System.err.println(enc); AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100, end); byte[] bytes = new byte[FLOATS.length * af.getFrameSize()]; AudioFloatConverter conv = AudioFloatConverter.getConverter(af); conv.toByteArray(FLOATS, bytes); if (!Arrays.equals(bytes, expected)) { System.err.println("Actual: " + Arrays.toString(bytes)); System.err.println("Expected: " + Arrays.toString(expected)); throw new RuntimeException(); } float[] floats = new float[bytes.length / af.getFrameSize()]; conv.toFloatArray(bytes, floats); if (!Arrays.equals(floats, FLOATS)) { System.err.println("Actual: " + Arrays.toString(floats)); System.err.println("Expected: " + Arrays.toString(FLOATS)); throw new RuntimeException(); } }
private static void test(final Encoding enc, final byte[] expected, boolean end) { System.err.println("enc = " + enc); AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100, end); byte[] bytes = new byte[FLOATS.length * af.getFrameSize()]; AudioFloatConverter conv = AudioFloatConverter.getConverter(af); conv.toByteArray(FLOATS, bytes); if (!Arrays.equals(bytes, expected)) { System.err.println("Actual: " + Arrays.toString(bytes)); System.err.println("Expected: " + Arrays.toString(expected)); throw new RuntimeException(); } float[] floats = new float[bytes.length / af.getFrameSize()]; conv.toFloatArray(bytes, floats); if (!Arrays.equals(floats, FLOATS)) { System.err.println("Actual: " + Arrays.toString(floats)); System.err.println("Expected: " + Arrays.toString(FLOATS)); throw new RuntimeException(); } }
private static void test(final Encoding enc, final byte[] expected) { AudioFormat af = new AudioFormat(enc, 44100, SIZE, 1, SIZE / 8, 44100, true); byte[] bytes = new byte[FLOATS.length * af.getFrameSize()]; AudioFloatConverter conv = AudioFloatConverter.getConverter(af); conv.toByteArray(FLOATS, bytes); if (!Arrays.equals(bytes, expected)) { System.err.println("Actual: " + Arrays.toString(bytes)); System.err.println("Expected: " + Arrays.toString(expected)); throw new RuntimeException(); } float[] floats = new float[bytes.length / af.getFrameSize()]; conv.toFloatArray(bytes, floats); if (!Arrays.equals(floats, FLOATS)) { System.err.println("Actual: " + Arrays.toString(floats)); System.err.println("Expected: " + Arrays.toString(FLOATS)); throw new RuntimeException(); } }
public static SF2Soundbank createTestSoundbank() { // Create impulse instrument // used to measure timing of note-on playback SF2Soundbank soundbank = new SF2Soundbank(); float[] data = new float[100]; Arrays.fill(data, 0); data[0] = 1.0f; byte[] bdata = new byte[data.length * format.getFrameSize()]; AudioFloatConverter.getConverter(format).toByteArray(data, bdata); SF2Sample sample = new SF2Sample(soundbank); sample.setName("Test Sample"); sample.setData(bdata); sample.setSampleRate((long) format.getSampleRate()); sample.setOriginalPitch(69); soundbank.addResource(sample); SF2Layer layer = new SF2Layer(soundbank); layer.setName("Test Layer"); soundbank.addResource(layer); SF2LayerRegion region = new SF2LayerRegion(); region.setSample(sample); layer.getRegions().add(region); SF2Instrument ins = new SF2Instrument(soundbank); ins.setName("Test Instrument"); soundbank.addInstrument(ins); SF2InstrumentRegion insregion = new SF2InstrumentRegion(); insregion.setLayer(layer); ins.getRegions().add(insregion); return soundbank; }
public static void test(Soundbank soundbank) throws Exception { // Create instance of synthesizer using the testing soundbank above AudioSynthesizer synth = new SoftSynthesizer(); AudioInputStream stream = synth.openStream(format, null); synth.unloadAllInstruments(synth.getDefaultSoundbank()); synth.loadAllInstruments(soundbank); Receiver recv = synth.getReceiver(); // Set volume to max and turn reverb off ShortMessage reverb_off = new ShortMessage(); reverb_off.setMessage(ShortMessage.CONTROL_CHANGE, 91, 0); recv.send(reverb_off, -1); ShortMessage full_volume = new ShortMessage(); full_volume.setMessage(ShortMessage.CONTROL_CHANGE, 7, 127); recv.send(full_volume, -1); Random random = new Random(3485934583945l); // Create random timestamps long[] test_timestamps = new long[30]; for (int i = 1; i < test_timestamps.length; i++) { test_timestamps[i] = i * 44100 + (int) (random.nextDouble() * 22050.0); } // Send midi note on message to synthesizer for (int i = 0; i < test_timestamps.length; i++) { ShortMessage midi_on = new ShortMessage(); midi_on.setMessage(ShortMessage.NOTE_ON, 69, 127); recv.send(midi_on, (long) ((test_timestamps[i] / 44100.0) * 1000000.0)); } // Measure timing from rendered audio float[] fbuffer = new float[100]; byte[] buffer = new byte[fbuffer.length * format.getFrameSize()]; long firsts = -1; int counter = 0; long s = 0; long max_jitter = 0; outerloop: for (int k = 0; k < 10000000; k++) { stream.read(buffer); AudioFloatConverter.getConverter(format).toFloatArray(buffer, fbuffer); for (int i = 0; i < fbuffer.length; i++) { if (fbuffer[i] != 0) { if (firsts == -1) firsts = s; long measure_time = (s - firsts); long predicted_time = test_timestamps[counter]; long jitter = Math.abs(measure_time - predicted_time); if (jitter > 10) max_jitter = jitter; counter++; if (counter == test_timestamps.length) break outerloop; } s++; } } synth.close(); if (counter == 0) throw new Exception("Nothing was measured!"); if (max_jitter != 0) { throw new Exception("Jitter has occurred! " + "(max jitter = " + max_jitter + ")"); } }
public SoftAudioBuffer(int size, AudioFormat format) { this.size = size; this.format = format; converter = AudioFloatConverter.getConverter(format); }
public byte[] toBytes(float[] in, AudioFormat format) { byte[] out = new byte[in.length * format.getFrameSize()]; return AudioFloatConverter.getConverter(format).toByteArray(in, out); }