001package bradleyross.music.imported;
002/*
003 *      ProgramChange.java
004 *
005 *      This file is part of jsresources.org
006 */
007
008/*
009 * Copyright (c) 2002 - 2003 by Matthias Pfisterer
010 * All rights reserved.
011 *
012 * Redistribution and use in source and binary forms, with or without
013 * modification, are permitted provided that the following conditions
014 * are met:
015 *
016 * - Redistributions of source code must retain the above copyright notice,
017 *   this list of conditions and the following disclaimer.
018 * - Redistributions in binary form must reproduce the above copyright
019 *   notice, this list of conditions and the following disclaimer in the
020 *   documentation and/or other materials provided with the distribution.
021 *
022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
028 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
029 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
030 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
031 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
032 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
033 * OF THE POSSIBILITY OF SUCH DAMAGE.
034 */
035
036/*
037|<---            this code is formatted to fit into 80 columns             --->|
038*/
039
040import javax.sound.midi.MidiChannel;
041import javax.sound.midi.MidiSystem;
042import javax.sound.midi.MidiUnavailableException;
043import javax.sound.midi.Synthesizer;
044// import java.io.IOException;
045
046
047
048/**     
049 * ProgramChange on a synthesizer.
050
051 * <p>Shows how to do a program change on a Synthesizer.
052 *   Just a code fragment...</p>
053 *   <p>This code was modified to set parameters if none were
054 *      set on the command line.</p>
055 *      @see MidiChannel.programChange(int, int)
056 */
057public class ProgramChange
058{
059        public static void main(String[] args)
060                throws MidiUnavailableException
061        {
062                int     nNoteNumber = 66;       // MIDI key number
063                int     nVelocity = 100;        // MIDI note on velocity
064                int nChannel = 0;
065
066                /*
067                 *      Time between note on and note off event in
068                 *      milliseconds. Note that on most systems, the
069                 *      best resolution you can expect are 10 ms.
070                 */
071                int     nDuration = 2000;
072                int nBank;
073                int nProgram;
074                if (args.length == 0) {
075                        nBank = 0;
076                        nProgram = 40;          
077                } else {
078                nBank = Integer.parseInt(args[0]);
079                nProgram = Integer.parseInt(args[1]);
080                }
081
082                Synthesizer synthesizer = MidiSystem.getSynthesizer();
083                out("Synthesizer: " + synthesizer);
084                /* Don't forget to open the Synthesizer!
085                   (One of the most common mistakes.)
086                */
087                synthesizer.open();
088                MidiChannel[] channels = synthesizer.getChannels();
089                out("Program before: " + channels[0].getProgram() + ", " + channels[0].getController(0) + ", " + channels[0].getController(20));
090                channels[0].programChange(nBank, nProgram);
091                out("Program after: " + channels[0].getProgram() + ", " + channels[0].getController(0) + ", " + channels[0].getController(20));
092
093                /*
094                 *      Turn the note on on MIDI channel 1.
095                 *      (Index zero means MIDI channel 1)
096                 */
097                channels[0].noteOn(nNoteNumber, nVelocity);
098
099                /*
100                 *      Wait for the specified amount of time
101                 *      (the duration of the note).
102                 */
103                sleep(nDuration);
104
105                /*
106                 *      Turn the note off.
107                 */
108                channels[nChannel].noteOff(nNoteNumber);
109
110                /* Wait some time before exiting.
111                 */
112                sleep(200);
113                synthesizer.close();
114        }
115
116
117
118        private static void sleep(int nDuration)
119        {
120                try
121                {
122                        Thread.sleep(nDuration);
123                }
124                catch (InterruptedException e)
125                {
126                }
127        }
128
129
130        private static void out(String strMessage)
131        {
132                System.out.println(strMessage);
133        }
134}
135
136
137/*** ProgramChange.java ***/