Java Code Examples for javax.sound.midi.MetaMessage#setMessage()

The following examples show how to use javax.sound.midi.MetaMessage#setMessage() . 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: 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 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: MetaCallback.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
MetaCallback() throws Exception {

        sequencer=MidiSystem.getSequencer();
        sequence=new Sequence(Sequence.PPQ,240);
        track=sequence.createTrack();
        sequencer.addMetaEventListener(this);

        byte[] data = new byte[1];

        track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,100),0));
        track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+0,45,0),0 + 240));
        int c;
        for(c=0; c < TOTAL_COUNT; c++) {
            data[0]=(byte)(c+1);
            MetaMessage meta = new MetaMessage();
            meta.setMessage(1, data, 1); // type, data, length
            track.add(new MidiEvent(meta,c*20));
        }
        track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,100),c*20));
        track.add(new MidiEvent(MidiMsg3(ShortMessage.NOTE_ON+9,45,0),c*20 + 10));

        sequencer.setSlaveSyncMode(Sequencer.SyncMode.INTERNAL_CLOCK);
        sequencer.setMasterSyncMode(Sequencer.SyncMode.INTERNAL_CLOCK);
        sequencer.open();
        sequencer.setSequence(sequence);
        sequencer.setTempoInBPM(100);
        System.out.println("Starting playback...");
        this.start();
        while (!finished && sequencer.getTickPosition() < sequencer.getTickLength()) {
            System.out.println("Tick "+sequencer.getTickPosition()+"...");
            Thread.sleep(1000);
        }
        System.out.println("Stopping playback...");
        this.stop();
        if (metaCount != TOTAL_COUNT) {
            throw new Exception("Expected "+TOTAL_COUNT+" callbacks, but got "+metaCount+"!");
        }
    }
 
Example 4
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 5
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 6
Source File: RealTimeSequencer.java    From Bytecoder with Apache License 2.0 4 votes vote down vote up
/**
 * Main process loop driving the media flow.
 *
 * Make sure to NOT synchronize on RealTimeSequencer
 * anywhere here (even implicit). That is a sure deadlock!
 */
@Override
public void run() {

    while (!interrupted) {
        boolean EOM = false;
        boolean wasRunning = running;
        isPumping = !interrupted && running;
        while (!EOM && !interrupted && running) {
            EOM = dataPump.pump();

            try {
                Thread.sleep(1);
            } catch (InterruptedException ie) {
                // ignore
            }
        }

        playThreadImplStop();
        if (wasRunning) {
            dataPump.notesOff(true);
        }
        if (EOM) {
            dataPump.setTickPos(sequence.getTickLength());

            // send EOT event (mis-used for end of media)
            MetaMessage message = new MetaMessage();
            try{
                message.setMessage(MidiUtils.META_END_OF_TRACK_TYPE, new byte[0], 0);
            } catch(InvalidMidiDataException e1) {}
            sendMetaEvents(message);
        }
        synchronized (lock) {
            isPumping = false;
            // wake up a waiting stop() method
            lock.notifyAll();
            while (!running && !interrupted) {
                try {
                    lock.wait();
                } catch (Exception ex) {}
            }
        }
    } // end of while(!EOM && !interrupted && running)
}
 
Example 7
Source File: RealTimeSequencer.java    From openjdk-jdk9 with GNU General Public License v2.0 4 votes vote down vote up
/**
 * Main process loop driving the media flow.
 *
 * Make sure to NOT synchronize on RealTimeSequencer
 * anywhere here (even implicit). That is a sure deadlock!
 */
@Override
public void run() {

    while (!interrupted) {
        boolean EOM = false;
        boolean wasRunning = running;
        isPumping = !interrupted && running;
        while (!EOM && !interrupted && running) {
            EOM = dataPump.pump();

            try {
                Thread.sleep(1);
            } catch (InterruptedException ie) {
                // ignore
            }
        }
        if (Printer.debug) {
            Printer.debug("Exited main pump loop because: ");
            if (EOM) Printer.debug(" -> EOM is reached");
            if (!running) Printer.debug(" -> running was set to false");
            if (interrupted) Printer.debug(" -> interrupted was set to true");
        }

        playThreadImplStop();
        if (wasRunning) {
            dataPump.notesOff(true);
        }
        if (EOM) {
            dataPump.setTickPos(sequence.getTickLength());

            // send EOT event (mis-used for end of media)
            MetaMessage message = new MetaMessage();
            try{
                message.setMessage(MidiUtils.META_END_OF_TRACK_TYPE, new byte[0], 0);
            } catch(InvalidMidiDataException e1) {}
            sendMetaEvents(message);
        }
        synchronized (lock) {
            isPumping = false;
            // wake up a waiting stop() method
            lock.notifyAll();
            while (!running && !interrupted) {
                try {
                    lock.wait();
                } catch (Exception ex) {}
            }
        }
    } // end of while(!EOM && !interrupted && running)
    if (Printer.debug) Printer.debug("end of play thread");
}
 
Example 8
Source File: StandardMidiFileReader.java    From openjdk-8-source with GNU General Public License v2.0 4 votes vote down vote up
void readTrack(Track track) throws IOException, InvalidMidiDataException {
    try {
        // reset current tick to 0
        long tick = 0;

        // reset current status byte to 0 (invalid value).
        // this should cause us to throw an InvalidMidiDataException if we don't
        // get a valid status byte from the beginning of the track.
        int status = 0;
        boolean endOfTrackFound = false;

        while (!trackFinished() && !endOfTrackFound) {
            MidiMessage message;

            int data1 = -1;         // initialize to invalid value
            int data2 = 0;

            // each event has a tick delay and then the event data.

            // first read the delay (a variable-length int) and update our tick value
            tick += readVarInt();

            // check for new status
            int byteValue = readUnsigned();

            if (byteValue >= 0x80) {
                status = byteValue;
            } else {
                data1 = byteValue;
            }

            switch (status & 0xF0) {
            case 0x80:
            case 0x90:
            case 0xA0:
            case 0xB0:
            case 0xE0:
                // two data bytes
                if (data1 == -1) {
                    data1 = readUnsigned();
                }
                data2 = readUnsigned();
                message = new FastShortMessage(status | (data1 << 8) | (data2 << 16));
                break;
            case 0xC0:
            case 0xD0:
                // one data byte
                if (data1 == -1) {
                    data1 = readUnsigned();
                }
                message = new FastShortMessage(status | (data1 << 8));
                break;
            case 0xF0:
                // sys-ex or meta
                switch(status) {
                case 0xF0:
                case 0xF7:
                    // sys ex
                    int sysexLength = (int) readVarInt();
                    byte[] sysexData = new byte[sysexLength];
                    read(sysexData);

                    SysexMessage sysexMessage = new SysexMessage();
                    sysexMessage.setMessage(status, sysexData, sysexLength);
                    message = sysexMessage;
                    break;

                case 0xFF:
                    // meta
                    int metaType = readUnsigned();
                    int metaLength = (int) readVarInt();

                    byte[] metaData = new byte[metaLength];
                    read(metaData);

                    MetaMessage metaMessage = new MetaMessage();
                    metaMessage.setMessage(metaType, metaData, metaLength);
                    message = metaMessage;
                    if (metaType == 0x2F) {
                        // end of track means it!
                        endOfTrackFound = true;
                    }
                    break;
                default:
                    throw new InvalidMidiDataException("Invalid status byte: " + status);
                } // switch sys-ex or meta
                break;
            default:
                throw new InvalidMidiDataException("Invalid status byte: " + status);
            } // switch
            track.add(new MidiEvent(message, tick));
        } // while
    } catch (ArrayIndexOutOfBoundsException e) {
        if (DEBUG) e.printStackTrace();
        // fix for 4834374
        throw new EOFException("invalid MIDI file");
    }
}
 
Example 9
Source File: StandardMidiFileReader.java    From openjdk-8 with GNU General Public License v2.0 4 votes vote down vote up
void readTrack(Track track) throws IOException, InvalidMidiDataException {
    try {
        // reset current tick to 0
        long tick = 0;

        // reset current status byte to 0 (invalid value).
        // this should cause us to throw an InvalidMidiDataException if we don't
        // get a valid status byte from the beginning of the track.
        int status = 0;
        boolean endOfTrackFound = false;

        while (!trackFinished() && !endOfTrackFound) {
            MidiMessage message;

            int data1 = -1;         // initialize to invalid value
            int data2 = 0;

            // each event has a tick delay and then the event data.

            // first read the delay (a variable-length int) and update our tick value
            tick += readVarInt();

            // check for new status
            int byteValue = readUnsigned();

            if (byteValue >= 0x80) {
                status = byteValue;
            } else {
                data1 = byteValue;
            }

            switch (status & 0xF0) {
            case 0x80:
            case 0x90:
            case 0xA0:
            case 0xB0:
            case 0xE0:
                // two data bytes
                if (data1 == -1) {
                    data1 = readUnsigned();
                }
                data2 = readUnsigned();
                message = new FastShortMessage(status | (data1 << 8) | (data2 << 16));
                break;
            case 0xC0:
            case 0xD0:
                // one data byte
                if (data1 == -1) {
                    data1 = readUnsigned();
                }
                message = new FastShortMessage(status | (data1 << 8));
                break;
            case 0xF0:
                // sys-ex or meta
                switch(status) {
                case 0xF0:
                case 0xF7:
                    // sys ex
                    int sysexLength = (int) readVarInt();
                    byte[] sysexData = new byte[sysexLength];
                    read(sysexData);

                    SysexMessage sysexMessage = new SysexMessage();
                    sysexMessage.setMessage(status, sysexData, sysexLength);
                    message = sysexMessage;
                    break;

                case 0xFF:
                    // meta
                    int metaType = readUnsigned();
                    int metaLength = (int) readVarInt();

                    byte[] metaData = new byte[metaLength];
                    read(metaData);

                    MetaMessage metaMessage = new MetaMessage();
                    metaMessage.setMessage(metaType, metaData, metaLength);
                    message = metaMessage;
                    if (metaType == 0x2F) {
                        // end of track means it!
                        endOfTrackFound = true;
                    }
                    break;
                default:
                    throw new InvalidMidiDataException("Invalid status byte: " + status);
                } // switch sys-ex or meta
                break;
            default:
                throw new InvalidMidiDataException("Invalid status byte: " + status);
            } // switch
            track.add(new MidiEvent(message, tick));
        } // while
    } catch (ArrayIndexOutOfBoundsException e) {
        if (DEBUG) e.printStackTrace();
        // fix for 4834374
        throw new EOFException("invalid MIDI file");
    }
}