/*
* Musical Skill Coach - An interactive midi device friendly program to help music students improve their skills
* Copyright (C) 2011 Paul-Emile Gaudet
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package music.midi;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Iterator;
import javax.sound.midi.MidiMessage;
import javax.sound.midi.Receiver;
public class MidiReceiver implements Receiver
{
protected ArrayList<ActionListener> listeners;
public MidiReceiver()
{
listeners = new ArrayList<ActionListener>();
}
@Override
public void close()
{
}
@Override
public void send(MidiMessage message, long timeStamp)
{
byte [] b = message.getMessage();
if (b != null)
{
String s = "IN: ";
for (int i = 0;i < b.length;i ++)
s += Integer.toHexString(b[i] & 0xff) + " ";
if ((b[0] & 0xF0) == 0x90)
{
actionKey(b[1], b[2]);
}
}
}
public void addActionListener(ActionListener listener)
{
Iterator<ActionListener> iterator = listeners.iterator();
ActionListener existingListener;
while (iterator.hasNext())
{
existingListener = iterator.next();
if (existingListener.equals(listener) == true)
return;
}
listeners.add(listener);
}
public void actionKey(int midiKey, int pressure)
{
if (listeners.size() < 1)
return;
ActionEvent e = new ActionEvent(this, midiKey, Integer.toString(pressure));
Iterator<ActionListener> iterator = listeners.iterator();
ActionListener listener;
while (iterator.hasNext())
{
listener = iterator.next();
listener.actionPerformed(e);
}
}
}