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
26package com.sun.media.sound;
27
28import java.util.TreeMap;
29
30import javax.sound.midi.MidiDevice;
31import javax.sound.midi.MidiDeviceReceiver;
32import javax.sound.midi.MidiMessage;
33import javax.sound.midi.ShortMessage;
34
35/**
36 * Software synthesizer MIDI receiver class.
37 *
38 * @author Karl Helgason
39 */
40public 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}
94