1 /* 2 * Copyright (c) 2007, 2013, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 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 }