Search in sources :

Example 1 with Track

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

the class SoftMidiAudioFileReader method getAudioInputStream.

public AudioInputStream getAudioInputStream(Sequence seq) throws UnsupportedAudioFileException, IOException {
    AudioSynthesizer synth = (AudioSynthesizer) new SoftSynthesizer();
    AudioInputStream stream;
    Receiver recv;
    try {
        stream = synth.openStream(format, null);
        recv = synth.getReceiver();
    } catch (MidiUnavailableException e) {
        throw new IOException(e.toString());
    }
    float divtype = seq.getDivisionType();
    Track[] tracks = seq.getTracks();
    int[] trackspos = new int[tracks.length];
    int mpq = 500000;
    int seqres = seq.getResolution();
    long lasttick = 0;
    long curtime = 0;
    while (true) {
        MidiEvent selevent = null;
        int seltrack = -1;
        for (int i = 0; i < tracks.length; i++) {
            int trackpos = trackspos[i];
            Track track = tracks[i];
            if (trackpos < track.size()) {
                MidiEvent event = track.get(trackpos);
                if (selevent == null || event.getTick() < selevent.getTick()) {
                    selevent = event;
                    seltrack = i;
                }
            }
        }
        if (seltrack == -1)
            break;
        trackspos[seltrack]++;
        long tick = selevent.getTick();
        if (divtype == Sequence.PPQ)
            curtime += ((tick - lasttick) * mpq) / seqres;
        else
            curtime = (long) ((tick * 1000000.0 * divtype) / seqres);
        lasttick = tick;
        MidiMessage msg = selevent.getMessage();
        if (msg instanceof MetaMessage) {
            if (divtype == Sequence.PPQ) {
                if (((MetaMessage) msg).getType() == 0x51) {
                    byte[] data = ((MetaMessage) msg).getData();
                    if (data.length < 3) {
                        throw new UnsupportedAudioFileException();
                    }
                    mpq = ((data[0] & 0xff) << 16) | ((data[1] & 0xff) << 8) | (data[2] & 0xff);
                }
            }
        } else {
            recv.send(msg, curtime);
        }
    }
    long totallen = curtime / 1000000;
    long len = (long) (stream.getFormat().getFrameRate() * (totallen + 4));
    stream = new AudioInputStream(stream, stream.getFormat(), len);
    return stream;
}
Also used : MidiUnavailableException(javax.sound.midi.MidiUnavailableException) MidiMessage(javax.sound.midi.MidiMessage) Receiver(javax.sound.midi.Receiver) MidiEvent(javax.sound.midi.MidiEvent) IOException(java.io.IOException) AudioInputStream(javax.sound.sampled.AudioInputStream) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) MetaMessage(javax.sound.midi.MetaMessage) Track(javax.sound.midi.Track)

Example 2 with Track

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

the class StandardMidiFileWriter method getFileStream.

//=================================================================================
private InputStream getFileStream(int type, Sequence sequence) throws IOException {
    Track[] tracks = sequence.getTracks();
    int bytesBuilt = 0;
    int headerLength = 14;
    int length = 0;
    int timeFormat;
    float divtype;
    PipedOutputStream hpos = null;
    DataOutputStream hdos = null;
    PipedInputStream headerStream = null;
    InputStream[] trackStreams = null;
    InputStream trackStream = null;
    InputStream fStream = null;
    // Determine the filetype to write
    if (type == MIDI_TYPE_0) {
        if (tracks.length != 1) {
            return null;
        }
    } else if (type == MIDI_TYPE_1) {
        if (tracks.length < 1) {
            // $$jb: 05.31.99: we _can_ write TYPE_1 if tracks.length==1
            return null;
        }
    } else {
        if (tracks.length == 1) {
            type = MIDI_TYPE_0;
        } else if (tracks.length > 1) {
            type = MIDI_TYPE_1;
        } else {
            return null;
        }
    }
    // Now build the file one track at a time
    // Note that above we made sure that MIDI_TYPE_0 only happens
    // if tracks.length==1
    trackStreams = new InputStream[tracks.length];
    int trackCount = 0;
    for (int i = 0; i < tracks.length; i++) {
        try {
            trackStreams[trackCount] = writeTrack(tracks[i], type);
            trackCount++;
        } catch (InvalidMidiDataException e) {
            if (Printer.err)
                Printer.err("Exception in write: " + e.getMessage());
        }
    //bytesBuilt += trackStreams[i].getLength();
    }
    // Now seqence the track streams
    if (trackCount == 1) {
        trackStream = trackStreams[0];
    } else if (trackCount > 1) {
        trackStream = trackStreams[0];
        for (int i = 1; i < tracks.length; i++) {
            // don't include failed track streams
            if (trackStreams[i] != null) {
                trackStream = new SequenceInputStream(trackStream, trackStreams[i]);
            }
        }
    } else {
        throw new IllegalArgumentException("invalid MIDI data in sequence");
    }
    // Now build the header...
    hpos = new PipedOutputStream();
    hdos = new DataOutputStream(hpos);
    headerStream = new PipedInputStream(hpos);
    // Write the magic number
    hdos.writeInt(MThd_MAGIC);
    // Write the header length
    hdos.writeInt(headerLength - 8);
    // Write the filetype
    if (type == MIDI_TYPE_0) {
        hdos.writeShort(0);
    } else {
        // MIDI_TYPE_1
        hdos.writeShort(1);
    }
    // Write the number of tracks
    hdos.writeShort((short) trackCount);
    // Determine and write the timing format
    divtype = sequence.getDivisionType();
    if (divtype == Sequence.PPQ) {
        timeFormat = sequence.getResolution();
    } else if (divtype == Sequence.SMPTE_24) {
        timeFormat = (24 << 8) * -1;
        timeFormat += (sequence.getResolution() & 0xFF);
    } else if (divtype == Sequence.SMPTE_25) {
        timeFormat = (25 << 8) * -1;
        timeFormat += (sequence.getResolution() & 0xFF);
    } else if (divtype == Sequence.SMPTE_30DROP) {
        timeFormat = (29 << 8) * -1;
        timeFormat += (sequence.getResolution() & 0xFF);
    } else if (divtype == Sequence.SMPTE_30) {
        timeFormat = (30 << 8) * -1;
        timeFormat += (sequence.getResolution() & 0xFF);
    } else {
        // $$jb: 04.08.99: What to really do here?
        return null;
    }
    hdos.writeShort(timeFormat);
    // now construct an InputStream to become the FileStream
    fStream = new SequenceInputStream(headerStream, trackStream);
    hdos.close();
    length = bytesBuilt + headerLength;
    return fStream;
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) SequenceInputStream(java.io.SequenceInputStream) DataOutputStream(java.io.DataOutputStream) SequenceInputStream(java.io.SequenceInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) PipedInputStream(java.io.PipedInputStream) InputStream(java.io.InputStream) PipedOutputStream(java.io.PipedOutputStream) PipedInputStream(java.io.PipedInputStream) Track(javax.sound.midi.Track)

Example 3 with Track

use of javax.sound.midi.Track in project tika by apache.

the class MidiParser method parse.

public void parse(InputStream stream, ContentHandler handler, Metadata metadata, ParseContext context) throws IOException, SAXException, TikaException {
    metadata.set(Metadata.CONTENT_TYPE, "audio/midi");
    XHTMLContentHandler xhtml = new XHTMLContentHandler(handler, metadata);
    xhtml.startDocument();
    // MidiSystem expects the stream to support the mark feature
    if (!stream.markSupported()) {
        stream = new BufferedInputStream(stream);
    }
    try {
        Sequence sequence = MidiSystem.getSequence(stream);
        Track[] tracks = sequence.getTracks();
        metadata.set("tracks", String.valueOf(tracks.length));
        // TODO: Use XMPDM.TRACKS?
        Patch[] patches = sequence.getPatchList();
        metadata.set("patches", String.valueOf(patches.length));
        float type = sequence.getDivisionType();
        if (type == Sequence.PPQ) {
            metadata.set("divisionType", "PPQ");
        } else if (type == Sequence.SMPTE_24) {
            metadata.set("divisionType", "SMPTE_24");
        } else if (type == Sequence.SMPTE_25) {
            metadata.set("divisionType", "SMPTE_25");
        } else if (type == Sequence.SMPTE_30) {
            metadata.set("divisionType", "SMPTE_30");
        } else if (type == Sequence.SMPTE_30DROP) {
            metadata.set("divisionType", "SMPTE_30DROP");
        } else if (type == Sequence.SMPTE_24) {
            metadata.set("divisionType", String.valueOf(type));
        }
        for (Track track : tracks) {
            xhtml.startElement("p");
            for (int i = 0; i < track.size(); i++) {
                MidiMessage message = track.get(i).getMessage();
                if (message instanceof MetaMessage) {
                    MetaMessage meta = (MetaMessage) message;
                    // Types 1-15 are reserved for text events
                    if (meta.getType() >= 1 && meta.getType() <= 15) {
                        // FIXME: What's the encoding?
                        xhtml.characters(new String(meta.getData(), ISO_8859_1));
                    }
                }
            }
            xhtml.endElement("p");
        }
    } catch (InvalidMidiDataException ignore) {
    // There is no way to know whether this exception was
    // caused by the document being corrupted or by the format
    // just being unsupported. So we do nothing.
    }
    xhtml.endDocument();
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) MidiMessage(javax.sound.midi.MidiMessage) Sequence(javax.sound.midi.Sequence) XHTMLContentHandler(org.apache.tika.sax.XHTMLContentHandler) BufferedInputStream(java.io.BufferedInputStream) MetaMessage(javax.sound.midi.MetaMessage) Patch(javax.sound.midi.Patch) Track(javax.sound.midi.Track)

Aggregations

Track (javax.sound.midi.Track)3 InvalidMidiDataException (javax.sound.midi.InvalidMidiDataException)2 MetaMessage (javax.sound.midi.MetaMessage)2 MidiMessage (javax.sound.midi.MidiMessage)2 BufferedInputStream (java.io.BufferedInputStream)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 DataOutputStream (java.io.DataOutputStream)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 PipedInputStream (java.io.PipedInputStream)1 PipedOutputStream (java.io.PipedOutputStream)1 SequenceInputStream (java.io.SequenceInputStream)1 MidiEvent (javax.sound.midi.MidiEvent)1 MidiUnavailableException (javax.sound.midi.MidiUnavailableException)1 Patch (javax.sound.midi.Patch)1 Receiver (javax.sound.midi.Receiver)1 Sequence (javax.sound.midi.Sequence)1 AudioInputStream (javax.sound.sampled.AudioInputStream)1 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)1 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)1