< prev index next >

src/java.desktop/share/classes/com/sun/media/sound/SoftReceiver.java

Print this page




   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */

  25 package com.sun.media.sound;
  26 
  27 import java.util.TreeMap;
  28 
  29 import javax.sound.midi.MidiDevice;
  30 import javax.sound.midi.MidiDeviceReceiver;
  31 import javax.sound.midi.MidiMessage;
  32 import javax.sound.midi.ShortMessage;
  33 
  34 /**
  35  * Software synthesizer MIDI receiver class.
  36  *
  37  * @author Karl Helgason
  38  */
  39 public final class SoftReceiver implements MidiDeviceReceiver {
  40 
  41     boolean open = true;
  42     private final Object control_mutex;
  43     private final SoftSynthesizer synth;
  44     TreeMap<Long, Object> midimessages;
  45     SoftMainMixer mainmixer;
  46 
  47     public SoftReceiver(SoftSynthesizer synth) {
  48         this.control_mutex = synth.control_mutex;
  49         this.synth = synth;
  50         this.mainmixer = synth.getMainMixer();
  51         if (mainmixer != null)
  52             this.midimessages = mainmixer.midimessages;
  53     }
  54 

  55     public MidiDevice getMidiDevice() {
  56         return synth;
  57     }
  58 

  59     public void send(MidiMessage message, long timeStamp) {
  60 
  61         synchronized (control_mutex) {
  62             if (!open)
  63                 throw new IllegalStateException("Receiver is not open");
  64         }
  65 
  66         if (timeStamp != -1) {
  67             synchronized (control_mutex) {
  68                 mainmixer.activity();
  69                 while (midimessages.get(timeStamp) != null)
  70                     timeStamp++;
  71                 if (message instanceof ShortMessage
  72                         && (((ShortMessage)message).getChannel() > 0xF)) {
  73                     midimessages.put(timeStamp, message.clone());
  74                 } else {
  75                     midimessages.put(timeStamp, message.getMessage());
  76                 }
  77             }
  78         } else {
  79             mainmixer.processMessage(message);
  80         }
  81     }
  82 

  83     public void close() {
  84         synchronized (control_mutex) {
  85             open = false;
  86         }
  87         synth.removeReceiver(this);
  88     }
  89 }


   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.  Oracle designates this
   8  * particular file as subject to the "Classpath" exception as provided
   9  * by Oracle in the LICENSE file that accompanied this code.
  10  *
  11  * This code is distributed in the hope that it will be useful, but WITHOUT
  12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  14  * version 2 for more details (a copy is included in the LICENSE file that
  15  * accompanied this code).
  16  *
  17  * You should have received a copy of the GNU General Public License version
  18  * 2 along with this work; if not, write to the Free Software Foundation,
  19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  20  *
  21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  22  * or visit www.oracle.com if you need additional information or have any
  23  * questions.
  24  */
  25 
  26 package com.sun.media.sound;
  27 
  28 import java.util.TreeMap;
  29 
  30 import javax.sound.midi.MidiDevice;
  31 import javax.sound.midi.MidiDeviceReceiver;
  32 import javax.sound.midi.MidiMessage;
  33 import javax.sound.midi.ShortMessage;
  34 
  35 /**
  36  * Software synthesizer MIDI receiver class.
  37  *
  38  * @author Karl Helgason
  39  */
  40 public final class SoftReceiver implements MidiDeviceReceiver {
  41 
  42     boolean open = true;
  43     private final Object control_mutex;
  44     private final SoftSynthesizer synth;
  45     TreeMap<Long, Object> midimessages;
  46     SoftMainMixer mainmixer;
  47 
  48     public SoftReceiver(SoftSynthesizer synth) {
  49         this.control_mutex = synth.control_mutex;
  50         this.synth = synth;
  51         this.mainmixer = synth.getMainMixer();
  52         if (mainmixer != null)
  53             this.midimessages = mainmixer.midimessages;
  54     }
  55 
  56     @Override
  57     public MidiDevice getMidiDevice() {
  58         return synth;
  59     }
  60 
  61     @Override
  62     public void send(MidiMessage message, long timeStamp) {
  63 
  64         synchronized (control_mutex) {
  65             if (!open)
  66                 throw new IllegalStateException("Receiver is not open");
  67         }
  68 
  69         if (timeStamp != -1) {
  70             synchronized (control_mutex) {
  71                 mainmixer.activity();
  72                 while (midimessages.get(timeStamp) != null)
  73                     timeStamp++;
  74                 if (message instanceof ShortMessage
  75                         && (((ShortMessage)message).getChannel() > 0xF)) {
  76                     midimessages.put(timeStamp, message.clone());
  77                 } else {
  78                     midimessages.put(timeStamp, message.getMessage());
  79                 }
  80             }
  81         } else {
  82             mainmixer.processMessage(message);
  83         }
  84     }
  85 
  86     @Override
  87     public void close() {
  88         synchronized (control_mutex) {
  89             open = false;
  90         }
  91         synth.removeReceiver(this);
  92     }
  93 }
< prev index next >