Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/midi/Gervill/SoftTuning/RealTimeTuning.java
38862 views
/*1* Copyright (c) 2010, 2013, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/* @test24@summary Test RealTime-tunings using SoftReciver.send method */2526import java.io.IOException;2728import javax.sound.midi.*;29import javax.sound.sampled.*;3031import com.sun.media.sound.*;3233public class RealTimeTuning {3435private static class PitchSpy {36public float pitch = 0;3738public Soundbank getSoundBank() {39ModelOscillator osc = new ModelOscillator() {40public float getAttenuation() {41return 0;42}4344public int getChannels() {45return 0;46}4748public ModelOscillatorStream open(float samplerate) {49return new ModelOscillatorStream() {50public void close() throws IOException {51pitch = 0;52}5354public void noteOff(int velocity) {55pitch = 0;56}5758public void noteOn(MidiChannel channel,59VoiceStatus voice, int noteNumber, int velocity) {60pitch = noteNumber * 100;61}6263public int read(float[][] buffer, int offset, int len)64throws IOException {65return len;66}6768public void setPitch(float ipitch) {69pitch = ipitch;70}71};72}73};74ModelPerformer performer = new ModelPerformer();75performer.getOscillators().add(osc);76SimpleInstrument testinstrument = new SimpleInstrument();77testinstrument.setPatch(new Patch(0, 0));78testinstrument.add(performer);79SimpleSoundbank testsoundbank = new SimpleSoundbank();80testsoundbank.addInstrument(testinstrument);81return testsoundbank;82}83}8485public static void sendTuningChange(Receiver recv, int channel,86int tuningpreset, int tuningbank) throws InvalidMidiDataException {87// Data Entry88ShortMessage sm1 = new ShortMessage();89sm1.setMessage(ShortMessage.CONTROL_CHANGE, channel, 0x64, 04);90ShortMessage sm2 = new ShortMessage();91sm2.setMessage(ShortMessage.CONTROL_CHANGE, channel, 0x65, 00);9293// Tuning Bank94ShortMessage sm3 = new ShortMessage();95sm3.setMessage(ShortMessage.CONTROL_CHANGE, channel, 0x06, tuningbank);96// Data Increment97ShortMessage sm4 = new ShortMessage();98sm4.setMessage(ShortMessage.CONTROL_CHANGE, channel, 0x60, 0x7F);99// Data Decrement100ShortMessage sm5 = new ShortMessage();101sm5.setMessage(ShortMessage.CONTROL_CHANGE, channel, 0x61, 0x7F);102103// Data Entry104ShortMessage sm6 = new ShortMessage();105sm6.setMessage(ShortMessage.CONTROL_CHANGE, channel, 0x64, 03);106ShortMessage sm7 = new ShortMessage();107sm7.setMessage(ShortMessage.CONTROL_CHANGE, channel, 0x65, 00);108109// Tuning program110ShortMessage sm8 = new ShortMessage();111sm8112.setMessage(ShortMessage.CONTROL_CHANGE, channel, 0x06,113tuningpreset);114// Data Increment115ShortMessage sm9 = new ShortMessage();116sm9.setMessage(ShortMessage.CONTROL_CHANGE, channel, 0x60, 0x7F);117// Data Decrement118ShortMessage sm10 = new ShortMessage();119sm10.setMessage(ShortMessage.CONTROL_CHANGE, channel, 0x61, 0x7F);120121recv.send(sm1, -1);122recv.send(sm2, -1);123recv.send(sm3, -1);124recv.send(sm4, -1);125recv.send(sm5, -1);126recv.send(sm6, -1);127recv.send(sm7, -1);128recv.send(sm8, -1);129recv.send(sm9, -1);130recv.send(sm10, -1);131132}133134private static void assertTrue(boolean value) throws Exception {135if (!value)136throw new RuntimeException("assertTrue fails!");137}138139public static void testTunings(int[] msg, int tuningProgram,140int tuningBank, int targetNote, float targetPitch, boolean realtime)141throws Exception {142AudioSynthesizer synth = new SoftSynthesizer();143AudioInputStream stream = synth.openStream(null, null);144Receiver recv = synth.getReceiver();145MidiChannel channel = synth.getChannels()[0];146byte[] buff = new byte[2048];147148// Create test instrument which we can use to monitor pitch changes149PitchSpy pitchspy = new PitchSpy();150151synth.unloadAllInstruments(synth.getDefaultSoundbank());152synth.loadAllInstruments(pitchspy.getSoundBank());153154SysexMessage sysex = null;155156// Send tuning changes157if (msg != null) {158byte[] bmsg = new byte[msg.length];159for (int i = 0; i < bmsg.length; i++)160bmsg[i] = (byte) msg[i];161sysex = new SysexMessage();162sysex.setMessage(bmsg, bmsg.length);163if (targetPitch == 0) {164targetPitch = (float) new SoftTuning(bmsg)165.getTuning(targetNote);166// Check if targetPitch != targetNote * 100167assertTrue(Math.abs(targetPitch - targetNote * 100.0) > 0.001);168}169}170171if (tuningProgram != -1)172sendTuningChange(recv, 0, tuningProgram, tuningBank);173174// First test without tunings175channel.noteOn(targetNote, 64);176stream.read(buff, 0, buff.length);177assertTrue(Math.abs(pitchspy.pitch - (targetNote * 100.0)) < 0.001);178179// Test if realtime/non-realtime works180if (sysex != null)181recv.send(sysex, -1);182stream.read(buff, 0, buff.length);183if (realtime)184assertTrue(Math.abs(pitchspy.pitch - targetPitch) < 0.001);185else186assertTrue(Math.abs(pitchspy.pitch - (targetNote * 100.0)) < 0.001);187188// Test if tunings works189channel.noteOn(targetNote, 0);190stream.read(buff, 0, buff.length);191assertTrue(Math.abs(pitchspy.pitch - 0.0) < 0.001);192193channel.noteOn(targetNote, 64);194stream.read(buff, 0, buff.length);195assertTrue(Math.abs(pitchspy.pitch - targetPitch) < 0.001);196197channel.noteOn(targetNote, 0);198stream.read(buff, 0, buff.length);199assertTrue(Math.abs(pitchspy.pitch - 0.0) < 0.001);200201stream.close();202}203204public static void main(String[] args) throws Exception {205// Test with no-tunings206testTunings(null, -1, -1, 60, 6000, false);207208int[] msg;209// 0x02 SINGLE NOTE TUNING CHANGE (REAL-TIME)210msg = new int[] { 0xf0, 0x7f, 0x7f, 0x08, 0x02, 0x10, 0x02, 36, 36, 64,2110, 60, 70, 0, 0, 0xf7 };212testTunings(msg, 0x10, 0, 60, 7000, true);213214// 0x07 SINGLE NOTE TUNING CHANGE (NON REAL-TIME) (BANK)215msg = new int[] { 0xf0, 0x7e, 0x7f, 0x08, 0x07, 0x05, 0x07, 0x02, 36,21636, 64, 0, 60, 80, 0, 0, 0xf7 };217testTunings(msg, 0x07, 0x05, 60, 8000, false);218219// 0x07 SINGLE NOTE TUNING CHANGE (REAL-TIME) (BANK)220msg = new int[] { 0xf0, 0x7f, 0x7f, 0x08, 0x07, 0x05, 0x07, 0x02, 36,22136, 64, 0, 60, 80, 0, 0, 0xf7 };222testTunings(msg, 0x07, 0x05, 60, 8000, true);223224// 0x08 scale/octave tuning 1-byte form (Non Real-Time)225msg = new int[] { 0xf0, 0x7e, 0x7f, 0x08, 0x08, 0x03, 0x7f, 0x7f, 5,22610, 15, 20, 25, 30, 35, 40, 45, 50, 51, 52, 0xf7 };227testTunings(msg, -1, -1, 60, 0, false);228229// 0x08 scale/octave tuning 1-byte form (REAL-TIME)230msg = new int[] { 0xf0, 0x7f, 0x7f, 0x08, 0x08, 0x03, 0x7f, 0x7f, 5,23110, 15, 20, 25, 30, 35, 40, 45, 50, 51, 52, 0xf7 };232testTunings(msg, -1, -1, 60, 0, true);233234// 0x09 scale/octave tuning 2-byte form (Non Real-Time)235msg = new int[] { 0xf0, 0x7e, 0x7f, 0x08, 0x09, 0x03, 0x7f, 0x7f, 5,23610, 15, 20, 25, 30, 35, 40, 45, 50, 51, 52, 5, 10, 15, 20, 25,23730, 35, 40, 45, 50, 51, 52, 0xf7 };238testTunings(msg, -1, -1, 60, 0, false);239240// 0x09 scale/octave tuning 2-byte form (REAL-TIME)241msg = new int[] { 0xf0, 0x7f, 0x7f, 0x08, 0x09, 0x03, 0x7f, 0x7f, 5,24210, 15, 20, 25, 30, 35, 40, 45, 50, 51, 52, 5, 10, 15, 20, 25,24330, 35, 40, 45, 50, 51, 52, 0xf7 };244testTunings(msg, -1, -1, 60, 0, true);245246}247}248249250