Java Code Examples for javax.sound.midi.MidiMessage#getMessage()

The following examples show how to use javax.sound.midi.MidiMessage#getMessage() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: MidiUtils.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
/** parses this message for a META tempo message and returns
 * the tempo in MPQ, or -1 if this isn't a tempo message
 */
public static int getTempoMPQ(MidiMessage midiMsg) {
    // first check if it is a META message at all
    if (midiMsg.getLength() != 6
        || midiMsg.getStatus() != MetaMessage.META) {
        return -1;
    }
    byte[] msg = midiMsg.getMessage();
    if (((msg[1] & 0xFF) != META_TEMPO_TYPE) || (msg[2] != 3)) {
        return -1;
    }
    int tempo =    (msg[5] & 0xFF)
                | ((msg[4] & 0xFF) << 8)
                | ((msg[3] & 0xFF) << 16);
    return tempo;
}
 
Example 2
Source File: MidiReceiverImpl.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
private void parseMessage(MidiMessage message) throws MidiPlayerException{
	byte[] data = message.getMessage();
	if( data.length > 0 ){
		//NOTE ON
		if(((data[0] & 0xFF) & 0xF0) == ShortMessage.NOTE_ON){
			parseNoteOn(data, findChannel(message), findVoice(message), findBendMode(message));
		}
		//NOTE OFF
		else if(((data[0] & 0xFF) & 0xF0) == ShortMessage.NOTE_OFF){
			parseNoteOff(data, findChannel(message), findVoice(message), findBendMode(message));
		}
		//PITCH BEND
		else if(((data[0] & 0xFF) & 0xF0) == ShortMessage.PITCH_BEND){
			parsePitchBend(data, findChannel(message), findVoice(message), findBendMode(message));
		}
		//PROGRAM CHANGE
		else if(((data[0] & 0xFF) & 0xF0) == ShortMessage.PROGRAM_CHANGE){
			parseProgramChange(data, findChannel(message));
		}
		//CONTROL CHANGE
		else if(((data[0] & 0xFF) & 0xF0) == ShortMessage.CONTROL_CHANGE){
			parseControlChange(data, findChannel(message));
		}
	}
}
 
Example 3
Source File: MidiUtils.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** return if the given message is a meta tempo message */
public static boolean isMetaTempo(MidiMessage midiMsg) {
    // first check if it is a META message at all
    if (midiMsg.getLength() != 6
        || midiMsg.getStatus() != MetaMessage.META) {
        return false;
    }
    // now get message and check for tempo
    byte[] msg = midiMsg.getMessage();
    // meta type must be 0x51, and data length must be 3
    return ((msg[1] & 0xFF) == META_TEMPO_TYPE) && (msg[2] == 3);
}
 
Example 4
Source File: VolumeScalingReceiver.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
/** Determine if the given message is a channel volume change.
 *
 * @return Channel number for which volume is being changed, or -1 if not a
 * channel volume change command.
 */
private int getVolumeChangeChannel(MidiMessage message) {
    if (message.getLength() >= 3) {
        byte[] mBytes = message.getMessage();
        if ((byte) 0xb0 <= mBytes[0] && mBytes[0] < (byte) 0xc0 &&
            mBytes[1] == 7) {
            return mBytes[0] & 15;
        }
    }
    return -1;
}
 
Example 5
Source File: VolumeScalingReceiver.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
/** Forward a message to the synthesizer.
 * 
 *  If <code>message</code> is a volume change message, the volume is
 *  first multiplied by the global volume.  Otherwise, the message is
 *  passed unmodified to the synthesizer.
 */
@Override
public synchronized void send(MidiMessage message, long timeStamp) {
    int chan = getVolumeChangeChannel(message);
    if (chan < 0) {
        synthReceiver.send(message, timeStamp);
    } else {
        int newVolUnscaled = message.getMessage()[2];
        channelVolume[chan] = newVolUnscaled;
        int newVolScaled = (int) Math.round(newVolUnscaled * globalVolume);
        sendVolumeChange(chan, newVolScaled, timeStamp);
    }
}
 
Example 6
Source File: MidiReceiverImpl.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
private int findChannel(MidiMessage midiMessage){
	if( midiMessage instanceof MidiShortMessage ){
		return ((MidiShortMessage)midiMessage).getChannel();
	}
	byte[] data = midiMessage.getMessage();
	if( data != null && data.length > 0){
		return ((data[0] & 0xFF) & 0x0F);
	}
	return -1;
}
 
Example 7
Source File: IOLoop.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
private static boolean messagesEqual(MidiMessage m1, MidiMessage m2) {
    if (m1 == null || m2 == null) {
        return false;
    }
    if (m1.getLength() != m2.getLength()) {
        return false;
    }
    byte[] array1 = m1.getMessage();
    byte[] array2 = m2.getMessage();
    return messagesEqual(array1, array2);
}
 
Example 8
Source File: SeqRecordsRealTimeEvents.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public static void printEvent(MidiEvent event)
{
    MidiMessage message = event.getMessage();
    long tick = event.getTick();
    byte[] data = message.getMessage();

    StringBuffer sb = new StringBuffer((data.length * 3) - 1);

    for (int i = 0; i < data.length; i++)
    {
            sb.append(toHexByteString(data[i]));
            if (i < data.length - 1) sb.append(' ');
    }
    System.out.printf("%5d: %s%n", tick, sb);
}
 
Example 9
Source File: MidiUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** return if the given message is a meta tempo message */
public static boolean isMetaTempo(MidiMessage midiMsg) {
    // first check if it is a META message at all
    if (midiMsg.getLength() != 6
        || midiMsg.getStatus() != MetaMessage.META) {
        return false;
    }
    // now get message and check for tempo
    byte[] msg = midiMsg.getMessage();
    // meta type must be 0x51, and data length must be 3
    return ((msg[1] & 0xFF) == META_TEMPO_TYPE) && (msg[2] == 3);
}
 
Example 10
Source File: MidiUtils.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
/** return true if the passed message is Meta End Of Track */
public static boolean isMetaEndOfTrack(MidiMessage midiMsg) {
    // first check if it is a META message at all
    if (midiMsg.getLength() != 3
        || midiMsg.getStatus() != MetaMessage.META) {
        return false;
    }
    // now get message and check for end of track
    byte[] msg = midiMsg.getMessage();
    return ((msg[1] & 0xFF) == META_END_OF_TRACK_TYPE) && (msg[2] == 0);
}
 
Example 11
Source File: MidiUtils.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
/** return true if the passed message is Meta End Of Track */
public static boolean isMetaEndOfTrack(MidiMessage midiMsg) {
    // first check if it is a META message at all
    if (midiMsg.getLength() != 3
        || midiMsg.getStatus() != MetaMessage.META) {
        return false;
    }
    // now get message and check for end of track
    byte[] msg = midiMsg.getMessage();
    return ((msg[1] & 0xFF) == META_END_OF_TRACK_TYPE) && (msg[2] == 0);
}
 
Example 12
Source File: MidiCommunication.java    From jmg with GNU General Public License v2.0 4 votes vote down vote up
/**
 * The method required by the recieve interface - It has a stupid name so we
 * pass data for short messages to handleMidiInput (a better name).
 */
// This method is based on test code in Plumbstone - OSX Core MIDI package.
public void send (MidiMessage message, long timeStamp) {
    // Get the message bytes
    byte [] m = message.getMessage ();
    int status = message.getStatus();
    // Is it a short message ?
    if (message instanceof ShortMessage) {
        // Get command and channel data
        int type = (m [0] & 0xFF) >> 4;
        int channel = m [0] & 0xF;
        int data1 = m[1];
        int data2 = -1;
        if (m.length > 2) {
            data2 = m[2];
        }
        
        // Is it a channel message?
        if (type != 15) {
            handleMidiInput(status - channel, channel, data1, data2);
        }
        else {
            // System message - some need special handling due to vast quanitity
            if (status == ShortMessage.TIMING_CLOCK) {
                System.out.print ("MIDI Clock message");
            }
            else if (status == ShortMessage.ACTIVE_SENSING) {
                System.out.print ("MIDI Active sensing message");
            }
            else {
                // Some other system message - just print status byte
                System.out.print ("A non-identified MIDI system message " + status);
            }
        }
    }
    
    // System exclusive message?
    else if (message instanceof SysexMessage) {
        // Print out sysex message
        System.out.println ();
        System.out.print ("Sysex MIDI message <<");
        for (int i=0; i<m.length; i++) {
            System.out.print (" " + m [i]);
        }
        System.out.println (">>");
    }
    
    // Meta message (probably shouldn't ever get this)
    else if (message instanceof MetaMessage) {
        // Print out meta message
        System.out.println ();
        System.out.print ("Meta MIDI Message {");
        for (int i=0; i<m.length; i++) {
            System.out.print (" " + m [i]);
        }
        System.out.println ("}");
    }
    
    // Uncast message
    else {
        System.out.println ("Unknown MIDI message [");
        for (int i=0; i<m.length; i++) {
            System.out.print (" " + m [i]);
        }
        System.out.println ("]");
    }
}
 
Example 13
Source File: RealTimeSequencer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * chase all events from beginning of Track
 * and send note off for those events that are active
 * in noteOnCache array.
 * It is possible, of course, to catch notes from other tracks,
 * but better than more complicated logic to detect
 * which notes are really from this track
 */
private void sendNoteOffIfOn(Track track, long endTick) {
    int size = track.size();
    int done = 0;
    try {
        for (int i = 0; i < size; i++) {
            MidiEvent event = track.get(i);
            if (event.getTick() > endTick) break;
            MidiMessage msg = event.getMessage();
            int status = msg.getStatus();
            int len = msg.getLength();
            if (len == 3 && ((status & 0xF0) == ShortMessage.NOTE_ON)) {
                int note = -1;
                if (msg instanceof ShortMessage) {
                    ShortMessage smsg = (ShortMessage) msg;
                    if (smsg.getData2() > 0) {
                        // only consider Note On with velocity > 0
                        note = smsg.getData1();
                    }
                } else {
                    byte[] data = msg.getMessage();
                    if ((data[2] & 0x7F) > 0) {
                        // only consider Note On with velocity > 0
                        note = data[1] & 0x7F;
                    }
                }
                if (note >= 0) {
                    int bit = 1<<(status & 0x0F);
                    if ((noteOnCache[note] & bit) != 0) {
                        // the bit is set. Send Note Off
                        getTransmitterList().sendMessage(status | (note<<8), -1);
                        // clear the bit
                        noteOnCache[note] &= (0xFFFF ^ bit);
                        done++;
                    }
                }
            }
        }
    } catch (ArrayIndexOutOfBoundsException aioobe) {
        // this happens when messages are removed
        // from the track while this method executes
    }
    if (DEBUG_PUMP) Printer.println("  sendNoteOffIfOn: sent "+done+" messages.");
}
 
Example 14
Source File: TestMidiLoop.java    From jsyn with Apache License 2.0 4 votes vote down vote up
@Override
public void send(MidiMessage message, long timeStamp) {
    byte[] bytes = message.getMessage();
    System.out.println("Got " + bytes.length + " bytes.");
}
 
Example 15
Source File: RealTimeSequencer.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * chase all events from beginning of Track
 * and send note off for those events that are active
 * in noteOnCache array.
 * It is possible, of course, to catch notes from other tracks,
 * but better than more complicated logic to detect
 * which notes are really from this track
 */
private void sendNoteOffIfOn(Track track, long endTick) {
    int size = track.size();
    int done = 0;
    try {
        for (int i = 0; i < size; i++) {
            MidiEvent event = track.get(i);
            if (event.getTick() > endTick) break;
            MidiMessage msg = event.getMessage();
            int status = msg.getStatus();
            int len = msg.getLength();
            if (len == 3 && ((status & 0xF0) == ShortMessage.NOTE_ON)) {
                int note = -1;
                if (msg instanceof ShortMessage) {
                    ShortMessage smsg = (ShortMessage) msg;
                    if (smsg.getData2() > 0) {
                        // only consider Note On with velocity > 0
                        note = smsg.getData1();
                    }
                } else {
                    byte[] data = msg.getMessage();
                    if ((data[2] & 0x7F) > 0) {
                        // only consider Note On with velocity > 0
                        note = data[1] & 0x7F;
                    }
                }
                if (note >= 0) {
                    int bit = 1<<(status & 0x0F);
                    if ((noteOnCache[note] & bit) != 0) {
                        // the bit is set. Send Note Off
                        getTransmitterList().sendMessage(status | (note<<8), -1);
                        // clear the bit
                        noteOnCache[note] &= (0xFFFF ^ bit);
                        done++;
                    }
                }
            }
        }
    } catch (ArrayIndexOutOfBoundsException aioobe) {
        // this happens when messages are removed
        // from the track while this method executes
    }
}
 
Example 16
Source File: InstrumentTester.java    From jsyn with Apache License 2.0 4 votes vote down vote up
@Override
public void send(MidiMessage message, long timeStamp) {
    byte[] bytes = message.getMessage();
    messageParser.parse(bytes);
}
 
Example 17
Source File: UseMidiKeyboard.java    From jsyn with Apache License 2.0 4 votes vote down vote up
@Override
public void send(MidiMessage message, long timeStamp) {
    byte[] bytes = message.getMessage();
    midiSynthesizer.onReceive(bytes, 0, bytes.length);
}
 
Example 18
Source File: TestMidiLoop.java    From jsyn with Apache License 2.0 4 votes vote down vote up
@Override
public void send(MidiMessage message, long timeStamp) {
    byte[] bytes = message.getMessage();
    System.out.println("Got " + bytes.length + " bytes.");
}
 
Example 19
Source File: InstrumentTester.java    From jsyn with Apache License 2.0 4 votes vote down vote up
@Override
public void send(MidiMessage message, long timeStamp) {
    byte[] bytes = message.getMessage();
    messageParser.parse(bytes);
}
 
Example 20
Source File: RTMidiIn.java    From jmg with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Called from the JavaSound MIDI Input Port for each new MIDI event
 */
public void send(MidiMessage message, long deltaTime){
	System.out.println("New MIDI message");
	Event event = null;
	ByteArrayInputStream bais=new ByteArrayInputStream(message.getMessage());
	DataInputStream dis = new DataInputStream(bais);
	try{
		dis.mark(2);
		int status = dis.readUnsignedByte();
		int length = 0;
		//check running status
		if(status < 0x80){
			status = oldStatus;
			dis.reset();
		}
		if(status >= 0xFF){//MetaEvent
			int type = dis.readUnsignedByte();
			length = MidiUtil.readVarLength(dis);
			event = MidiUtil.createMetaEvent(type);
		}else if(status >= 0xF0){ //System Exclusive -- Not Supported
			System.out.println("SysEX---");
			length = MidiUtil.readVarLength(dis);
		}else if(status >= 0x80){ //MIDI voice event
			short selection = (short) (status /0x10);
			short midiChannel = (short) (status - (selection * 0x10));
			VoiceEvt evt = (VoiceEvt)MidiUtil.createVoiceEvent(selection);
			evt.setMidiChannel(midiChannel);
			event = evt;
			if(event == null){
				throw new IOException("Read Error");
			}
		}
		if(event != null){
			event.setTime((int)deltaTime);
			event.read(dis);
		}
		oldStatus = status;
	}catch(Exception e){
		e.printStackTrace();
		System.exit(1);
	}
	this.notifyListeners(event);
}