Path: blob/aarch64-shenandoah-jdk8u272-b10/jdk/test/javax/sound/midi/Gervill/SoftChannel/ProgramAndBankChange.java
38860 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 SoftChannel program and bank change */2526import java.io.IOException;2728import javax.sound.midi.*;29import javax.sound.sampled.*;3031import com.sun.media.sound.*;3233public class ProgramAndBankChange {3435private static SimpleInstrument generateTestInstrument(Patch patch) {36ModelOscillator osc = new ModelOscillator() {37public float getAttenuation() {38return 0;39}4041public int getChannels() {42return 1;43}4445public ModelOscillatorStream open(float samplerate) {46return new ModelOscillatorStream() {47public void close() throws IOException {48}4950public void noteOff(int velocity) {51}5253public void noteOn(MidiChannel channel, VoiceStatus voice,54int noteNumber, int velocity) {55}5657public int read(float[][] buffer, int offset, int len)58throws IOException {59return len;60}6162public void setPitch(float ipitch) {63}64};65}66};67ModelPerformer performer = new ModelPerformer();68performer.getOscillators().add(osc);69SimpleInstrument testinstrument = new SimpleInstrument();70testinstrument.setPatch(patch);71testinstrument.add(performer);72return testinstrument;73}7475private static void assertTrue(boolean value) throws Exception {76if (!value)77throw new RuntimeException("assertTrue fails!");78}7980private static void testProgramAndBank(SoftSynthesizer soft,81AudioInputStream stream, Patch patch) throws Exception {8283int program = patch.getProgram();84int bank = patch.getBank();8586MidiChannel channel = soft.getChannels()[0];87byte[] buff = new byte[2048];8889channel.programChange(bank, program);90channel.noteOn(64, 64);91stream.read(buff, 0, buff.length);9293int foundprogram = -1;94int foundbank = -1;95VoiceStatus[] vstatus = soft.getVoiceStatus();96for (int i = 0; i < vstatus.length; i++) {97if (vstatus[i].active) {98foundprogram = vstatus[i].program;99foundbank = vstatus[i].bank;100break;101}102}103104assertTrue(foundprogram == program);105assertTrue(foundbank == bank);106107channel.noteOn(64, 0);108stream.read(buff, 0, buff.length);109110channel = soft.getChannels()[1];111// Send MSB Bank112channel.controlChange(0x00, bank / 128);113// Send LSB Bank114channel.controlChange(0x20, bank % 128);115// Send Program Change116channel.programChange(program);117channel.noteOn(64, 64);118stream.read(buff, 0, buff.length);119120foundprogram = -1;121foundbank = -1;122vstatus = soft.getVoiceStatus();123for (int i = 0; i < vstatus.length; i++) {124if (vstatus[i].active) {125foundprogram = vstatus[i].program;126foundbank = vstatus[i].bank;127break;128}129}130assertTrue(foundprogram == program);131assertTrue(foundbank == bank);132channel.noteOn(64, 0);133stream.read(buff, 0, buff.length);134}135136public static void main(String[] args) throws Exception {137SoftSynthesizer soft = new SoftSynthesizer();138AudioInputStream stream = soft.openStream(null, null);139soft.unloadAllInstruments(soft.getDefaultSoundbank());140141soft.loadInstrument(generateTestInstrument(new Patch(0, 0)));142soft.loadInstrument(generateTestInstrument(new Patch(7, 0)));143soft.loadInstrument(generateTestInstrument(new Patch(20, 10)));144soft.loadInstrument(generateTestInstrument(new Patch(3678, 15)));145soft.loadInstrument(generateTestInstrument(new Patch(4678, 15)));146147testProgramAndBank(soft, stream, new Patch(0, 0));148testProgramAndBank(soft, stream, new Patch(7, 0));149testProgramAndBank(soft, stream, new Patch(20, 10));150testProgramAndBank(soft, stream, new Patch(3678, 15));151testProgramAndBank(soft, stream, new Patch(4678, 15));152153soft.close();154}155}156157158