2714N/A/*
2714N/A * Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
2714N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
2714N/A *
2714N/A * This code is free software; you can redistribute it and/or modify it
2714N/A * under the terms of the GNU General Public License version 2 only, as
2714N/A * published by the Free Software Foundation. Oracle designates this
2714N/A * particular file as subject to the "Classpath" exception as provided
2714N/A * by Oracle in the LICENSE file that accompanied this code.
2714N/A *
2714N/A * This code is distributed in the hope that it will be useful, but WITHOUT
2714N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
2714N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
2714N/A * version 2 for more details (a copy is included in the LICENSE file that
2714N/A * accompanied this code).
2714N/A *
2714N/A * You should have received a copy of the GNU General Public License version
2714N/A * 2 along with this work; if not, write to the Free Software Foundation,
2714N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
2714N/A *
2714N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2714N/A * or visit www.oracle.com if you need additional information or have any
2714N/A * questions.
2714N/A */
2714N/A
2714N/A/* @test
2714N/A @summary Test SoftChannel program and bank change */
2714N/A
2714N/Aimport java.io.IOException;
2714N/A
2714N/Aimport javax.sound.midi.*;
2714N/Aimport javax.sound.sampled.*;
2714N/A
2714N/Aimport com.sun.media.sound.*;
2714N/A
2714N/Apublic class ProgramAndBankChange {
2714N/A
2714N/A private static SimpleInstrument generateTestInstrument(Patch patch) {
2714N/A ModelOscillator osc = new ModelOscillator() {
2714N/A public float getAttenuation() {
2714N/A return 0;
2714N/A }
2714N/A
2714N/A public int getChannels() {
2714N/A return 1;
2714N/A }
2714N/A
2714N/A public ModelOscillatorStream open(float samplerate) {
2714N/A return new ModelOscillatorStream() {
2714N/A public void close() throws IOException {
2714N/A }
2714N/A
2714N/A public void noteOff(int velocity) {
2714N/A }
2714N/A
2714N/A public void noteOn(MidiChannel channel, VoiceStatus voice,
2714N/A int noteNumber, int velocity) {
2714N/A }
2714N/A
2714N/A public int read(float[][] buffer, int offset, int len)
2714N/A throws IOException {
2714N/A return len;
2714N/A }
2714N/A
2714N/A public void setPitch(float ipitch) {
2714N/A }
2714N/A };
2714N/A }
2714N/A };
2714N/A ModelPerformer performer = new ModelPerformer();
2714N/A performer.getOscillators().add(osc);
2714N/A SimpleInstrument testinstrument = new SimpleInstrument();
2714N/A testinstrument.setPatch(patch);
2714N/A testinstrument.add(performer);
2714N/A return testinstrument;
2714N/A }
2714N/A
2714N/A private static void assertTrue(boolean value) throws Exception {
2714N/A if (!value)
2714N/A throw new RuntimeException("assertTrue fails!");
2714N/A }
2714N/A
2714N/A private static void testProgramAndBank(SoftSynthesizer soft,
2714N/A AudioInputStream stream, Patch patch) throws Exception {
2714N/A
2714N/A int program = patch.getProgram();
2714N/A int bank = patch.getBank();
2714N/A
2714N/A MidiChannel channel = soft.getChannels()[0];
2714N/A byte[] buff = new byte[2048];
2714N/A
2714N/A channel.programChange(bank, program);
2714N/A channel.noteOn(64, 64);
2714N/A stream.read(buff, 0, buff.length);
2714N/A
2714N/A int foundprogram = -1;
2714N/A int foundbank = -1;
2714N/A VoiceStatus[] vstatus = soft.getVoiceStatus();
2714N/A for (int i = 0; i < vstatus.length; i++) {
2714N/A if (vstatus[i].active) {
2714N/A foundprogram = vstatus[i].program;
2714N/A foundbank = vstatus[i].bank;
2714N/A break;
2714N/A }
2714N/A }
2714N/A
2714N/A assertTrue(foundprogram == program);
2714N/A assertTrue(foundbank == bank);
2714N/A
2714N/A channel.noteOn(64, 0);
2714N/A stream.read(buff, 0, buff.length);
2714N/A
2714N/A channel = soft.getChannels()[1];
2714N/A // Send MSB Bank
2714N/A channel.controlChange(0x00, bank / 128);
2714N/A // Send LSB Bank
2714N/A channel.controlChange(0x20, bank % 128);
2714N/A // Send Program Change
2714N/A channel.programChange(program);
2714N/A channel.noteOn(64, 64);
2714N/A stream.read(buff, 0, buff.length);
2714N/A
2714N/A foundprogram = -1;
2714N/A foundbank = -1;
2714N/A vstatus = soft.getVoiceStatus();
2714N/A for (int i = 0; i < vstatus.length; i++) {
2714N/A if (vstatus[i].active) {
2714N/A foundprogram = vstatus[i].program;
2714N/A foundbank = vstatus[i].bank;
2714N/A break;
2714N/A }
2714N/A }
2714N/A assertTrue(foundprogram == program);
2714N/A assertTrue(foundbank == bank);
2714N/A channel.noteOn(64, 0);
2714N/A stream.read(buff, 0, buff.length);
2714N/A }
2714N/A
2714N/A public static void main(String[] args) throws Exception {
2714N/A SoftSynthesizer soft = new SoftSynthesizer();
2714N/A AudioInputStream stream = soft.openStream(null, null);
2714N/A soft.unloadAllInstruments(soft.getDefaultSoundbank());
2714N/A
2714N/A soft.loadInstrument(generateTestInstrument(new Patch(0, 0)));
2714N/A soft.loadInstrument(generateTestInstrument(new Patch(7, 0)));
2714N/A soft.loadInstrument(generateTestInstrument(new Patch(20, 10)));
2714N/A soft.loadInstrument(generateTestInstrument(new Patch(3678, 15)));
2714N/A soft.loadInstrument(generateTestInstrument(new Patch(4678, 15)));
2714N/A
2714N/A testProgramAndBank(soft, stream, new Patch(0, 0));
2714N/A testProgramAndBank(soft, stream, new Patch(7, 0));
2714N/A testProgramAndBank(soft, stream, new Patch(20, 10));
2714N/A testProgramAndBank(soft, stream, new Patch(3678, 15));
2714N/A testProgramAndBank(soft, stream, new Patch(4678, 15));
2714N/A
2714N/A soft.close();
2714N/A }
2714N/A}