javax.sound.midi.MetaMessage Java Examples

The following examples show how to use javax.sound.midi.MetaMessage. 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: MidiWaveformSynthesizer.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
public static final void addNotesToTrack(final Track track, final Track trk) throws InvalidMidiDataException {
    for (int ii = 0; ii < track.size(); ii++) {
        final MidiEvent me = track.get(ii);
        final MidiMessage mm = me.getMessage();
        if (mm instanceof ShortMessage) {
            final ShortMessage sm = (ShortMessage) mm;
            final int command = sm.getCommand();
            int com = -1;
            if (command == ShortMessage.NOTE_ON) {
                com = LOCAL_NOTE_ON;
            } else if (command == ShortMessage.NOTE_OFF) {
                com = LOCAL_NOTE_OFF;
            }
            if (com > 0) {
                final byte[] b = sm.getMessage();
                final int l = (b == null ? 0 : b.length);
                final MetaMessage metaMessage = new MetaMessage(com, b, l);
                final MidiEvent me2 = new MidiEvent(metaMessage, me.getTick());
                trk.add(me2);
            }
        }
    }
}
 
Example #2
Source File: MusReader.java    From mochadoom with GNU General Public License v3.0 6 votes vote down vote up
/** Create a sequence from an InputStream.
 *  This is the counterpart of {@link MidiSystem#getSequence(InputStream)}
 *  for MUS format.
 *
 * @param is MUS data (this method does not try to auto-detect the format.)
 */
public static Sequence getSequence(InputStream is)
throws IOException, InvalidMidiDataException {
    DataInputStream dis = new DataInputStream(is);
    dis.skip(6);
    int rus = dis.readUnsignedShort();
    short scoreStart = Swap.SHORT((char) rus);
    dis.skip(scoreStart - 8);
    Sequence sequence = new Sequence(Sequence.SMPTE_30, 14, 1);
    Track track = sequence.getTracks()[0];
    int[] chanVelocity = new int[16];
    Arrays.fill(chanVelocity, 100);
    EventGroup eg;
    long tick = 0;
    while ((eg = nextEventGroup(dis, chanVelocity)) != null) {
        tick = eg.appendTo(track, tick);
    }
    MetaMessage endOfSequence = new MetaMessage();
    endOfSequence.setMessage(47, new byte[] {0}, 1);
    track.add(new MidiEvent(endOfSequence, tick));
    return sequence;
}
 
Example #3
Source File: MetaMessageClone.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
public static void main(String[] args) throws Exception {
    // let's create some MetaMessages and check them
    MetaMessage msg=new MetaMessage();
    String text="a textmarker";
    msg.setMessage(1, text.getBytes(), text.length());
    checkClone(msg);
    msg.setMessage(0x2E, new byte[0], 0);
    checkClone(msg);
    byte[] data=new byte[17000];
    for (int i=0; i<30; data[i]=(byte) (i++ & 0xFF));
    msg.setMessage(0x02, data, 80); checkClone(msg);
    msg.setMessage(0x02, data, 160); checkClone(msg);
    msg.setMessage(0x02, data, 400); checkClone(msg);
    msg.setMessage(0x02, data, 1000); checkClone(msg);
    msg.setMessage(0x02, data, 10000); checkClone(msg);
    msg.setMessage(0x02, data, 17000); checkClone(msg);
}
 
Example #4
Source File: MetaMessageClone.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
private static void checkClone(MetaMessage msg) throws Exception {
    System.out.print("Original: ");
    byte[] msgData=msg.getData();
    printMsg(msg, msgData);
    MetaMessage msg2=(MetaMessage) msg.clone();
    byte[] msg2Data=msg2.getData();
    System.out.print("Clone:    ");
    printMsg(msg2, msg2Data);

    if (msg2.getLength()!=msg.getLength()
        || msg.getType()!=msg2.getType()
        || msgData.length!=msg2Data.length) {
            throw new Exception("cloned MetaMessage is not equal.");
    }
    int max=Math.min(msgData.length, 10);
    for (int i=0; i<max; i++) {
        if (msgData[i]!=msg2Data[i]) {
            throw new Exception("Cloned MetaMessage data is not equal.");
        }
    }
}
 
Example #5
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 #6
Source File: MidiUtils.java    From openjdk-jdk9 with GNU General Public License v2.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 #7
Source File: MidiMessageUtils.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static MidiMessage tempoInUSQ(int usq){
	try {
		MetaMessage message = new MetaMessage();
		message.setMessage(0x51, new byte[]{ (byte)((usq >> 16) & 0x00FF),(byte)((usq >> 8) & 0x00FF),(byte)((usq) & 0x00FF) }, 3);
		return message;
	} catch (InvalidMidiDataException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example #8
Source File: MetaCallback.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void meta(MetaMessage msg) {
    System.out.println(""+metaCount+": got "+msg);
    if (msg.getType() == 0x2F) {
        finished = true;
    } else if (msg.getData().length > 0 && msg.getType() == 1) {
        metaCount++;
    }
}
 
Example #9
Source File: JavaSoundAudioClip.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #10
Source File: JavaSoundAudioClip.java    From hottub with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #11
Source File: JavaSoundAudioClip.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #12
Source File: JavaSoundAudioClip.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #13
Source File: JavaSoundAudioClip.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #14
Source File: MidiToAudioWriter.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static void write(OutputStream out, List<MidiEvent> events, MidiToAudioSettings settings) throws Throwable {
	MidiToAudioSynth.instance().openSynth();
	MidiToAudioSynth.instance().loadSoundbank(getPatchs(events), settings.getSoundbankPath());
	
	int usqTempo = 60000000 / 120;
	long previousTick = 0;
	long timePosition = 0;
	MidiToAudioWriter.sort(events);
	Receiver receiver = MidiToAudioSynth.instance().getReceiver();
	AudioInputStream stream = MidiToAudioSynth.instance().getStream();
	
	Iterator<MidiEvent> it = events.iterator();
	while(it.hasNext()){
		MidiEvent event = (MidiEvent)it.next();
		MidiMessage msg = event.getMessage();
		
		timePosition += ( (event.getTick() - previousTick) * usqTempo) / TGDuration.QUARTER_TIME;
		
		if (msg instanceof MetaMessage) {
			if (((MetaMessage) msg).getType() == 0x51) {
				byte[] data = ((MetaMessage) msg).getData();
				usqTempo = ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
			}
		} else {
			receiver.send(msg, timePosition);
		}
		previousTick = event.getTick();
	}
	
	long duration = (long) (stream.getFormat().getFrameRate() * ( (timePosition / 1000000.0) ));
	
	AudioInputStream srcStream = new AudioInputStream(stream, stream.getFormat(), duration );
	AudioInputStream dstStream = AudioSystem.getAudioInputStream(settings.getFormat(), srcStream );
	AudioSystem.write(new AudioInputStream(dstStream, dstStream.getFormat(), duration ), settings.getType(), out);
	
	dstStream.close();
	srcStream.close();
	
	MidiToAudioSynth.instance().closeSynth();
}
 
Example #15
Source File: JavaSoundAudioClip.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #16
Source File: MidiMessageUtils.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static MidiMessage timeSignature(TGTimeSignature ts){
	try {
		MetaMessage message = new MetaMessage();
		message.setMessage(0x58, new byte[]{  (byte)ts.getNumerator(),(byte)ts.getDenominator().getIndex(),(byte)(96 / ts.getDenominator().getValue()),8 }, 4);
		return message;
	} catch (InvalidMidiDataException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example #17
Source File: MetaMessage.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Constructs a new <code>MetaMessage</code>.
 * @param data an array of bytes containing the complete message.
 * The message data may be changed using the <code>setMessage</code>
 * method.
 * @see #setMessage
 */
protected MetaMessage(byte[] data) {
    super(data);
    //$$fb 2001-10-06: need to calculate dataLength. Fix for bug #4511796
    if (data.length>=3) {
        dataLength=data.length-3;
        int pos=2;
        while (pos<data.length && (data[pos] & 0x80)!=0) {
            dataLength--; pos++;
        }
    }
}
 
Example #18
Source File: MetaMessage.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
/**
 * Creates a new object of the same class and with the same contents
 * as this object.
 * @return a clone of this instance
 */
public Object clone() {
    byte[] newData = new byte[length];
    System.arraycopy(data, 0, newData, 0, newData.length);

    MetaMessage event = new MetaMessage(newData);
    return event;
}
 
Example #19
Source File: JavaSoundAudioClip.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #20
Source File: JavaSoundAudioClip.java    From jdk8u-dev-jdk with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #21
Source File: MidiSynth.java    From jmg with GNU General Public License v2.0 5 votes vote down vote up
/**
    * Invoked when a Sequencer has encountered and processed a MetaMessage
    * in the Sequence it is processing.
    * @param MetaMessage meta - the meta-message that the sequencer
    *                           encountered
    */
   public void meta(MetaMessage metaEvent) {
	//System.out.println("JavaSound sequencer sent meta event");
	if (metaEvent.getType() == StopType) {
		if (msCycle) {
			rePlay();
		} else { 
			stop();
		}
	}
}
 
Example #22
Source File: TickLength.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
public void meta(MetaMessage p1) {
 if(p1.getType() ==47) {
   return;
 }
 System.out.println("getTickPosition:\t"+theSequencer.getTickPosition()
     +"\t Sequencer.getTickLength:\t"+theSequencer.getTickLength()
     +"\tReal Length:\t"+lastTick
     +"\t Sequence.getTickLength:\t"+theSequence.getTickLength()
     //(theSequencer.getTickLength()/64));
     );
}
 
Example #23
Source File: JavaSoundAudioClip.java    From dragonwell8_jdk with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #24
Source File: JavaSoundAudioClip.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #25
Source File: JavaSoundAudioClip.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #26
Source File: JavaSoundAudioClip.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 5 votes vote down vote up
public synchronized void meta( MetaMessage message ) {

        if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

        if( message.getType() == 47 ) {
            if (sequencerloop){
                //notifyAll();
                sequencer.setMicrosecondPosition(0);
                loop();
            } else {
                stop();
            }
        }
    }
 
Example #27
Source File: JavaSoundAudioClip.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
@Override
public synchronized void meta(MetaMessage message) {
    if( message.getType() == 47 ) {
        if (sequencerloop){
            //notifyAll();
            sequencer.setMicrosecondPosition(0);
            loop();
        } else {
            stop();
        }
    }
}
 
Example #28
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 #29
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 #30
Source File: JavaSoundAudioClip.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public synchronized void meta(MetaMessage message) {

    if (DEBUG || Printer.debug)Printer.debug("META EVENT RECEIVED!!!!! ");

    if( message.getType() == 47 ) {
        if (sequencerloop){
            //notifyAll();
            sequencer.setMicrosecondPosition(0);
            loop();
        } else {
            stop();
        }
    }
}