1a54 /***************************************************************************/ /* */ /* (c) Copyright IBM Corp. 2001 All rights reserved. */ /* */ /* This sample program is owned by International Business Machines */ /* Corporation or one of its subsidiaries ("IBM") and is copyrighted */ /* and licensed, not sold. */ /* */ /* You may copy, modify, and distribute this sample program in any */ /* form without payment to IBM, for any purpose including developing, */ /* using, marketing or distributing programs that include or are */ /* derivative works of the sample program. */ /* */ /* The sample program is provided to you on an "AS IS" basis, without */ /* warranty of any kind. IBM HEREBY EXPRESSLY DISCLAIMS ALL WARRANTIES, */ /* EITHER EXPRESS OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED */ /* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. */ /* Some jurisdictions do not allow for the exclusion or limitation of */ /* implied warranties, so the above limitations or exclusions may not */ /* apply to you. IBM shall not be liable for any damages you suffer as */ /* a result of using, modifying or distributing the sample program or */ /* its derivatives. */ /* */ /* Each copy of any portion of this sample program or any derivative */ /* work, must include the above copyright notice and disclaimer of */ /* warranty. */ /* */ /***************************************************************************/ /***************************************************************************/ /* */ /* This file accompanies the article "Understanding and using Java MIDI */ /* audio." This article was published in the Special Edition 2001 issue */ /* of the IBM DeveloperToolbox Technical Magazine at */ /* http://www.developer.ibm.com/devcon/mag.htm. */ /* */ /***************************************************************************/ //------------------------------------------------------------------------ // File Name: InstrumentTest // Description: Plays a sample music phrase on a range of instruments. //------------------------------------------------------------------------ import javax.sound.midi.*; import javax.sound.midi.MidiSystem; import javax.sound.midi.Synthesizer; import javax.sound.midi.MidiChannel; import javax.sound.midi.MidiUnavailableException; /** * InstrumentTest plays a sample music phrase on a range of instruments. * @author Dan Becker */ public class InstrumentTest { // The notes, velocities, and durations determin the tune. // Extra credit: create the tune dynamically from the program. public static int [] notes = { 70, 50, 60, 40, 70, 30 }; public static int [] velocities = { 60, 80, 70, 90, 80, 95 }; public static int [] durations = { 125, 500, 125, 500, 125, 2000 }; public static void main( String [] args ) throws MidiUnavailableException { int nMin = 0; // MIDI key number int nMax = 512; // MIDI key number if ( args.length == 2 ) { nMin = Integer.parseInt(args[0]); nMin = Math.min( 512, Math.max(0, nMin)); // 0..512 nMax = Integer.parseInt(args[1]); nMax = Math.min( 512, Math.max(0, nMax)); // 0..512 } else { System.out.println( "InstrumentTest: usage:" ); System.out.println( "java InstrumentTest " ); System.exit(1); } // We need a synthesizer to play the note on. (Here we request the default.) Synthesizer synthesizer = MidiSystem.getSynthesizer(); synthesizer.open(); Soundbank soundBank = synthesizer.getDefaultSoundbank(); Instrument [] instruments = soundBank.getInstruments(); System.out.println( "InstrumentTest instrument count=" + instruments.length ); Instrument [] loadedInstruments = synthesizer.getLoadedInstruments(); System.out.println( "InstrumentTest loaded instrument count=" + loadedInstruments.length ); // Turn the note on on MIDI channel 1. (Index zero means MIDI channel 1) MidiChannel [] channels = synthesizer.getChannels(); System.out.println( "InstrumentTest channel count=" + channels.length ); int channelNum = 0; for ( int i = nMin; i <= nMax; i++ ) { // System.out.println( "Channel " + i + ", program=" + channels[ i ].getProgram() ); String name = instruments[ i ].getName(); if ( name.endsWith( "\n" ) ) name = name.trim(); System.out.println( "Soundbank instrument " + i + ": " + name ); synthesizer.loadInstrument( instruments[ i ] ); channels[ channelNum ].programChange( i ); playChannel( channels[ channelNum ], notes, velocities, durations ); } // for System.exit( 0 ); } // main /** Plays the given tune values on the midi channel. */ public static void playChannel( MidiChannel channel, int [] notes, int [] vels, int [] dur ) { if (( notes == null ) || ( vels == null ) || ( dur == null )) throw new IllegalArgumentException( "null parameter given" ); if (( notes.length < 1 ) || ( vels.length < 1 ) || ( dur.length < 1 )) throw new IllegalArgumentException( "0 length array given" ); if (( notes.length != vels.length ) || ( vels.length != dur.length )) throw new IllegalArgumentException( "unequal length arrays given" ); for ( int i = 0; i < notes.length; i++ ) { channel.noteOn( notes[ i ], vels[ i ] ); try { Thread.sleep( dur[ i ] ); } catch ( InterruptedException e ) { } } // for for ( int i = 0; i < notes.length; i++ ) channel.noteOff( notes[ i ] ); } // playChannel } // InstrumentTest . 0