Search in sources :

Example 11 with ShortMessage

use of javax.sound.midi.ShortMessage in project jdk8u_jdk by JetBrains.

the class StandardMidiFileWriter method writeTrack.

private InputStream writeTrack(Track track, int type) throws IOException, InvalidMidiDataException {
    int bytesWritten = 0;
    int lastBytesWritten = 0;
    int size = track.size();
    PipedOutputStream thpos = new PipedOutputStream();
    DataOutputStream thdos = new DataOutputStream(thpos);
    PipedInputStream thpis = new PipedInputStream(thpos);
    ByteArrayOutputStream tdbos = new ByteArrayOutputStream();
    tddos = new DataOutputStream(tdbos);
    ByteArrayInputStream tdbis = null;
    SequenceInputStream fStream = null;
    long currentTick = 0;
    long deltaTick = 0;
    long eventTick = 0;
    int runningStatus = -1;
    // -----------------------------
    for (int i = 0; i < size; i++) {
        MidiEvent event = track.get(i);
        int status;
        int eventtype;
        int metatype;
        int data1, data2;
        int length;
        byte[] data = null;
        ShortMessage shortMessage = null;
        MetaMessage metaMessage = null;
        SysexMessage sysexMessage = null;
        // get the tick
        // $$jb: this gets easier if we change all system-wide time to delta ticks
        eventTick = event.getTick();
        deltaTick = event.getTick() - currentTick;
        currentTick = event.getTick();
        // get the status byte
        status = event.getMessage().getStatus();
        eventtype = getType(status);
        switch(eventtype) {
            case ONE_BYTE:
                shortMessage = (ShortMessage) event.getMessage();
                data1 = shortMessage.getData1();
                bytesWritten += writeVarInt(deltaTick);
                if (status != runningStatus) {
                    runningStatus = status;
                    tddos.writeByte(status);
                    bytesWritten += 1;
                }
                tddos.writeByte(data1);
                bytesWritten += 1;
                break;
            case TWO_BYTE:
                shortMessage = (ShortMessage) event.getMessage();
                data1 = shortMessage.getData1();
                data2 = shortMessage.getData2();
                bytesWritten += writeVarInt(deltaTick);
                if (status != runningStatus) {
                    runningStatus = status;
                    tddos.writeByte(status);
                    bytesWritten += 1;
                }
                tddos.writeByte(data1);
                bytesWritten += 1;
                tddos.writeByte(data2);
                bytesWritten += 1;
                break;
            case SYSEX:
                sysexMessage = (SysexMessage) event.getMessage();
                length = sysexMessage.getLength();
                data = sysexMessage.getMessage();
                bytesWritten += writeVarInt(deltaTick);
                // $$jb: 04.08.99: always write status for sysex
                runningStatus = status;
                tddos.writeByte(data[0]);
                bytesWritten += 1;
                // $$jb: 10.18.99: we don't maintain length in
                // the message data for SysEx (it is not transmitted
                // over the line), so write the calculated length
                // minus the status byte
                bytesWritten += writeVarInt((data.length - 1));
                // $$jb: 10.18.99: now write the rest of the
                // message
                tddos.write(data, 1, (data.length - 1));
                bytesWritten += (data.length - 1);
                break;
            case META:
                metaMessage = (MetaMessage) event.getMessage();
                length = metaMessage.getLength();
                data = metaMessage.getMessage();
                bytesWritten += writeVarInt(deltaTick);
                // $$jb: 10.18.99: getMessage() returns the
                // entire valid midi message for a file,
                // including the status byte and the var-length-int
                // length value, so we can just write the data
                // here.  note that we must _always_ write the
                // status byte, regardless of runningStatus.
                runningStatus = status;
                tddos.write(data, 0, data.length);
                bytesWritten += data.length;
                break;
            case IGNORE:
                // ignore this event
                break;
            case ERROR:
                // ignore this event
                break;
            default:
                throw new InvalidMidiDataException("internal file writer error");
        }
    }
    // ---------------------------------
    // End write each event in the track
    // ---------------------------------
    // Build Track header now that we know length
    thdos.writeInt(MTrk_MAGIC);
    thdos.writeInt(bytesWritten);
    bytesWritten += 8;
    // Now sequence them
    tdbis = new ByteArrayInputStream(tdbos.toByteArray());
    fStream = new SequenceInputStream(thpis, tdbis);
    thdos.close();
    tddos.close();
    return fStream;
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) DataOutputStream(java.io.DataOutputStream) MidiEvent(javax.sound.midi.MidiEvent) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) ByteArrayOutputStream(java.io.ByteArrayOutputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) ShortMessage(javax.sound.midi.ShortMessage) SysexMessage(javax.sound.midi.SysexMessage) MetaMessage(javax.sound.midi.MetaMessage)

Example 12 with ShortMessage

use of javax.sound.midi.ShortMessage in project jdk8u_jdk by JetBrains.

the class SoftMainMixer method processMessage.

public void processMessage(MidiMessage message) {
    if (message instanceof ShortMessage) {
        ShortMessage sms = (ShortMessage) message;
        processMessage(sms.getChannel(), sms.getCommand(), sms.getData1(), sms.getData2());
        return;
    }
    processMessage(message.getMessage());
}
Also used : ShortMessage(javax.sound.midi.ShortMessage)

Example 13 with ShortMessage

use of javax.sound.midi.ShortMessage in project blue by kunstmusik.

the class MidiImportUtilities method getNoteListForTrack.

private static NoteList getNoteListForTrack(Track track, double divType, double ticksLength, String template, String instrId) throws NoteParseException {
    if (track.size() == 0) {
        return null;
    }
    MNote[] notes = new MNote[128];
    for (int j = 0; j < notes.length; j++) {
        notes[j] = new MNote();
    }
    NoteList nl = new NoteList();
    for (int j = 0; j < track.size(); j++) {
        MidiEvent me = track.get(j);
        MidiMessage message = me.getMessage();
        if (message instanceof ShortMessage) {
            ShortMessage shortMsg = (ShortMessage) message;
            int noteNum, velocity;
            MNote n;
            double time = (me.getTick() / ticksLength) * divType;
            switch(shortMsg.getCommand()) {
                case ShortMessage.NOTE_ON:
                    noteNum = shortMsg.getData1();
                    velocity = shortMsg.getData2();
                    n = notes[noteNum];
                    if (velocity > 0) {
                        n.start = time;
                        n.velocity = velocity;
                    } else {
                        double start = n.start;
                        double duration = time - n.start;
                        String note = MidiUtilities.processNoteTemplate(template, instrId, start, duration, noteNum, n.velocity);
                        nl.add(Note.createNote(note));
                        n.clear();
                    }
                    break;
                case ShortMessage.NOTE_OFF:
                    noteNum = shortMsg.getData1();
                    velocity = shortMsg.getData2();
                    n = notes[noteNum];
                    double start = n.start;
                    double duration = time - n.start;
                    String note = MidiUtilities.processNoteTemplate(template, instrId, start, duration, noteNum, n.velocity);
                    nl.add(Note.createNote(note));
                    n.clear();
                    break;
            }
        }
    }
    return nl;
}
Also used : NoteList(blue.soundObject.NoteList) MidiMessage(javax.sound.midi.MidiMessage) ShortMessage(javax.sound.midi.ShortMessage) MidiEvent(javax.sound.midi.MidiEvent)

Aggregations

ShortMessage (javax.sound.midi.ShortMessage)13 InvalidMidiDataException (javax.sound.midi.InvalidMidiDataException)4 MidiEvent (javax.sound.midi.MidiEvent)4 MetaMessage (javax.sound.midi.MetaMessage)3 AudioSynthesizer (com.sun.media.sound.AudioSynthesizer)2 SoftSynthesizer (com.sun.media.sound.SoftSynthesizer)2 MidiMessage (javax.sound.midi.MidiMessage)2 Receiver (javax.sound.midi.Receiver)2 SysexMessage (javax.sound.midi.SysexMessage)2 NoteList (blue.soundObject.NoteList)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 DataOutputStream (java.io.DataOutputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 Random (java.util.Random)1 ControllerEventListener (javax.sound.midi.ControllerEventListener)1 MetaEventListener (javax.sound.midi.MetaEventListener)1 Track (javax.sound.midi.Track)1