Docjar: A Java Source and Docuemnt Enginecom.*    java.*    javax.*    org.*    all    new    plug-in

Quick Search    Search Deep

Source code: jmx/midi/RTMidiIn.java


1   /*
2   
3   < This Java Class is part of the jMusic API Version 2000.11>
4   
5   Copyright (C) 2001 Andrew Sorensen & Andrew Brown
6   
7   This program is free software; you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation; either version 2 of the License, or any
10  later version.
11  
12  This program is distributed in the hope that it will be useful, but
13  WITHOUT ANY WARRANTY; without even the implied warranty of
14  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15  GNU General Public License for more details.
16  
17  You should have received a copy of the GNU General Public License
18  along with this program; if not, write to the Free Software
19  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
20  
21  */
22  package jmx.midi;
23  
24  import java.lang.InterruptedException;
25  import java.io.ByteArrayInputStream;
26  import java.io.DataInputStream;
27  import java.util.Vector;
28  import java.util.Enumeration;
29  import javax.sound.midi.*;
30  import java.io.IOException;
31  
32  import jm.midi.event.Event;
33  import jm.midi.event.VoiceEvt;
34  import jm.midi.MidiUtil;
35  import jm.midi.MidiInputListener;
36  
37  /**
38   * Real time midi input.
39   *
40   * @author Andrew Sorensen 
41    */
42  
43  public class RTMidiIn implements Receiver {
44  
45    /** Used to hold running status state information */
46    private int oldStatus;
47    /** contains a list of listeners for this object */
48    private Vector listeners;
49    /** The transmitter which sends this receiver its events */
50    private Transmitter trans = null;
51    
52    /**
53     * Constructor
54     */
55    public RTMidiIn(){
56      listeners = new Vector();
57      this.init();
58    }
59  
60    /**
61     * Attached Listeners
62     */
63    public void addMidiInputListener(MidiInputListener mil){
64      listeners.add(mil);
65    }
66  
67    /**
68     * Notify all listeners of a new event
69     */
70    public void notifyListeners(Event event){
71      Enumeration en = listeners.elements();
72      while(en.hasMoreElements()){
73        ((MidiInputListener)en.nextElement()).newEvent(event);
74      }
75    }
76  
77    /**
78     * Called from the JavaSound MIDI Input Port for each new MIDI event
79     */
80    public void send(MidiMessage message, long deltaTime){
81      System.out.println("New MIDI message");
82      Event event = null;
83      ByteArrayInputStream bais=new ByteArrayInputStream(message.getMessage());
84      DataInputStream dis = new DataInputStream(bais);
85      try{
86        dis.mark(2);
87        int status = dis.readUnsignedByte();
88        int length = 0;
89        //check running status
90        if(status < 0x80){
91          status = oldStatus;
92          dis.reset();
93        }
94        if(status >= 0xFF){//MetaEvent
95          int type = dis.readUnsignedByte();
96          length = MidiUtil.readVarLength(dis);
97          event = MidiUtil.createMetaEvent(type);
98        }else if(status >= 0xF0){ //System Exclusive -- Not Supported
99          System.out.println("SysEX---");
100         length = MidiUtil.readVarLength(dis);
101       }else if(status >= 0x80){ //MIDI voice event
102         short selection = (short) (status /0x10);
103         short midiChannel = (short) (status - (selection * 0x10));
104         VoiceEvt evt = (VoiceEvt)MidiUtil.createVoiceEvent(selection);
105         evt.setMidiChannel(midiChannel);
106         event = evt;
107         if(event == null){
108           throw new IOException("Read Error");
109         }
110       }
111       if(event != null){
112         event.setTime((int)deltaTime);
113         event.read(dis);
114       }
115       oldStatus = status;
116     }catch(Exception e){
117       e.printStackTrace();
118       System.exit(1);
119     }
120     this.notifyListeners(event);
121   }
122 
123   /**
124    * Close method to release resources
125    */
126   public void close(){
127     this.trans.close();
128   }
129   
130   /**
131    * Initialise the input source
132    */
133      private boolean init() {
134        if (trans == null) {
135           try {
136              if (MidiSystem.getReceiver() == null) {
137                   System.err.println("MidiSystem Receiver Unavailable");
138                      return false;
139               }
140       MidiDevice.Info[] mdi=MidiSystem.getMidiDeviceInfo();
141       for(int i=0;i<mdi.length;i++){
142         System.out.println(mdi[i]);
143       }
144       trans = MidiSystem.getTransmitter();
145       trans.setReceiver(this);
146           }catch (MidiUnavailableException e) {
147              System.err.println("Midi System Unavailable:" + e);
148                return false;
149            }
150      }
151          return true;
152   }
153 }// MidiSynth