/Users/lyon/j4p/src/sound/musica/TestMidi.java

1    package sound.musica; 
2     
3    import gui.run.RunJob; 
4     
5    import javax.sound.midi.*; 
6     
7    public class TestMidi { 
8        private Synthesizer synth = null; 
9        private MidiChannel channels[] = null; 
10       private int channelNumber = 10; 
11       // channel 10 is the percussion channel; 
12       final int ACOUSTIC_BASS = 35; 
13       final int ACOUSTIC_SNARE = 38; 
14       final int HAND_CLAP = 39; 
15       final int PEDAL_HIHAT = 44; 
16       final int LO_TOM = 45; 
17       final int CLOSED_HIHAT = 42; 
18       final int CRASH_CYMBAL1 = 49; 
19       final int HI_TOM = 50; 
20       final int RIDE_BELL = 53; 
21    
22       final int PROGRAM = 192; 
23    
24    
25       public TestMidi() { 
26    
27           // 1 & 2) Get Synthesizer 
28           getSynthesizer(); 
29           // 3) Open Synthesizer 
30           openSynthesizer(); 
31           System.out.println("synth.isOpen() = " + synth.isOpen()); 
32           // 4) Get Channels from Synthesizer 
33           channels = synth.getChannels(); 
34           System.out.println("channels = " + channels); 
35           // Play a note 
36           double alpha = 1; 
37           Instrument instruments[] = 
38                   synth.getAvailableInstruments(); 
39           for (int i=0; i < instruments.length; i++) 
40               System.out.println(instruments[i].toString()+i); 
41    
42           repeatNote(instruments[410], .3 * alpha, 42); 
43           repeatNote(instruments[409], .5 * alpha, 42); 
44           repeatNote(instruments[408], .7 * alpha, 42); 
45           repeatNote(instruments[407], 1.3 * alpha, 42); 
46           repeatNote(instruments[406], 1.7 * alpha, 42); 
47           repeatNote(instruments[405], 2.3 * alpha, 42); 
48           repeatNote(instruments[0], 2.7 * alpha, 42); 
49    
50    
51           sleep(5 * 3 * 7 * 2 * 1000); 
52    
53           synth.close(); 
54       } 
55    
56       private void repeatNote(Instrument instrument, double interval, final int nn) { 
57           synth.loadInstrument(instrument); 
58           new RunJob(interval) { 
59               public void run() { 
60                   playNote(nn); 
61               } 
62           }; 
63       } 
64    
65       private void playNote(int nn) { 
66           MidiChannel mc = channels[channelNumber]; 
67           mc.programChange(PROGRAM); 
68           mc.noteOn(nn, 127); 
69           sleep(150); 
70           mc.noteOff(nn, 127); 
71       } 
72    
73       private void testOpenSynthesizer() { 
74           openSynthesizer(); 
75           System.out.println("synth=" + synth); 
76           System.out.println("synthOpen=" + synth.isOpen()); 
77       } 
78    
79       private void openSynthesizer() { 
80           try { 
81               synth.open(); 
82           } catch (MidiUnavailableException e) { 
83               e.printStackTrace(); 
84               System.exit(1); 
85           } 
86       } 
87    
88       private void testGetSynthesizer() { 
89           getSynthesizer(); 
90       } 
91    
92       private void getSynthesizer() { 
93           try { 
94               synth = MidiSystem.getSynthesizer(); 
95           } catch (MidiUnavailableException e) { 
96               System.out.println("Synthesizer not available"); 
97           } 
98       } 
99    
100      private Synthesizer initSynth() { 
101          if (synth != null) 
102              return synth; 
103          try { 
104              synth = MidiSystem.getSynthesizer(); 
105          } catch (MidiUnavailableException e) { 
106              e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates. 
107          } 
108          return synth; 
109   
110      } 
111   
112      private static void sleep(int nDuration) { 
113          try { 
114              Thread.sleep(nDuration); 
115          } catch (InterruptedException e) { 
116          } 
117      } 
118   
119      public static void main(String[] args) { 
120          //testNote(); 
121   
122          TestMidi tm = new TestMidi(); 
123          //tm.testOpenSynthesizer(); 
124          System.out.println("This is TestMidi"); 
125      } 
126  } 
127