Path: blob/master/test/jdk/javax/sound/midi/Gervill/SoftChannel/ProgramAndBankChange.java
41159 views
/*1* Copyright (c) 2010, 2015, 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 SoftChannel program and bank change25@modules java.desktop/com.sun.media.sound26*/2728import java.io.IOException;2930import javax.sound.midi.*;31import javax.sound.sampled.*;3233import com.sun.media.sound.*;3435public class ProgramAndBankChange {3637private static SimpleInstrument generateTestInstrument(Patch patch) {38ModelOscillator osc = new ModelOscillator() {39public float getAttenuation() {40return 0;41}4243public int getChannels() {44return 1;45}4647public ModelOscillatorStream open(float samplerate) {48return new ModelOscillatorStream() {49public void close() throws IOException {50}5152public void noteOff(int velocity) {53}5455public void noteOn(MidiChannel channel, VoiceStatus voice,56int noteNumber, int velocity) {57}5859public int read(float[][] buffer, int offset, int len)60throws IOException {61return len;62}6364public void setPitch(float ipitch) {65}66};67}68};69ModelPerformer performer = new ModelPerformer();70performer.getOscillators().add(osc);71SimpleInstrument testinstrument = new SimpleInstrument();72testinstrument.setPatch(patch);73testinstrument.add(performer);74return testinstrument;75}7677private static void assertTrue(boolean value) throws Exception {78if (!value)79throw new RuntimeException("assertTrue fails!");80}8182private static void testProgramAndBank(SoftSynthesizer soft,83AudioInputStream stream, Patch patch) throws Exception {8485int program = patch.getProgram();86int bank = patch.getBank();8788MidiChannel channel = soft.getChannels()[0];89byte[] buff = new byte[2048];9091channel.programChange(bank, program);92channel.noteOn(64, 64);93stream.read(buff, 0, buff.length);9495int foundprogram = -1;96int foundbank = -1;97VoiceStatus[] vstatus = soft.getVoiceStatus();98for (int i = 0; i < vstatus.length; i++) {99if (vstatus[i].active) {100foundprogram = vstatus[i].program;101foundbank = vstatus[i].bank;102break;103}104}105106assertTrue(foundprogram == program);107assertTrue(foundbank == bank);108109channel.noteOn(64, 0);110stream.read(buff, 0, buff.length);111112channel = soft.getChannels()[1];113// Send MSB Bank114channel.controlChange(0x00, bank / 128);115// Send LSB Bank116channel.controlChange(0x20, bank % 128);117// Send Program Change118channel.programChange(program);119channel.noteOn(64, 64);120stream.read(buff, 0, buff.length);121122foundprogram = -1;123foundbank = -1;124vstatus = soft.getVoiceStatus();125for (int i = 0; i < vstatus.length; i++) {126if (vstatus[i].active) {127foundprogram = vstatus[i].program;128foundbank = vstatus[i].bank;129break;130}131}132assertTrue(foundprogram == program);133assertTrue(foundbank == bank);134channel.noteOn(64, 0);135stream.read(buff, 0, buff.length);136}137138public static void main(String[] args) throws Exception {139SoftSynthesizer soft = new SoftSynthesizer();140AudioInputStream stream = soft.openStream(null, null);141soft.unloadAllInstruments(soft.getDefaultSoundbank());142143soft.loadInstrument(generateTestInstrument(new Patch(0, 0)));144soft.loadInstrument(generateTestInstrument(new Patch(7, 0)));145soft.loadInstrument(generateTestInstrument(new Patch(20, 10)));146soft.loadInstrument(generateTestInstrument(new Patch(3678, 15)));147soft.loadInstrument(generateTestInstrument(new Patch(4678, 15)));148149testProgramAndBank(soft, stream, new Patch(0, 0));150testProgramAndBank(soft, stream, new Patch(7, 0));151testProgramAndBank(soft, stream, new Patch(20, 10));152testProgramAndBank(soft, stream, new Patch(3678, 15));153testProgramAndBank(soft, stream, new Patch(4678, 15));154155soft.close();156}157}158159160