Search in sources :

Example 1 with Sequence

use of javax.sound.midi.Sequence in project yyl_example by Relucent.

the class PlayMidi method play.

public static void play(URL url) {
    try {
        Sequence seq = MidiSystem.getSequence(url);
        midi = MidiSystem.getSequencer();
        midi.open();
        midi.setSequence(seq);
        if (!midi.isRunning())
            midi.start();
    } catch (Exception ex) {
        ex.printStackTrace();
    }
}
Also used : Sequence(javax.sound.midi.Sequence)

Example 2 with Sequence

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

the class SoftMidiAudioFileReader method getAudioInputStream.

public AudioInputStream getAudioInputStream(InputStream inputstream) throws UnsupportedAudioFileException, IOException {
    inputstream.mark(200);
    Sequence seq;
    try {
        seq = MidiSystem.getSequence(inputstream);
    } catch (InvalidMidiDataException e) {
        inputstream.reset();
        throw new UnsupportedAudioFileException();
    } catch (IOException e) {
        inputstream.reset();
        throw new UnsupportedAudioFileException();
    }
    return getAudioInputStream(seq);
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) Sequence(javax.sound.midi.Sequence) IOException(java.io.IOException)

Example 3 with Sequence

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

the class SMFParser method getSequence.

public Sequence getSequence(InputStream stream) throws InvalidMidiDataException, IOException {
    SMFParser smfParser = new SMFParser();
    MidiFileFormat format = getMidiFileFormatFromStream(stream, MidiFileFormat.UNKNOWN_LENGTH, smfParser);
    // must be MIDI Type 0 or Type 1
    if ((format.getType() != 0) && (format.getType() != 1)) {
        throw new InvalidMidiDataException("Invalid or unsupported file type: " + format.getType());
    }
    // construct the sequence object
    Sequence sequence = new Sequence(format.getDivisionType(), format.getResolution());
    // for each track, go to the beginning and read the track events
    for (int i = 0; i < smfParser.tracks; i++) {
        if (smfParser.nextTrack()) {
            smfParser.readTrack(sequence.createTrack());
        } else {
            break;
        }
    }
    return sequence;
}
Also used : MidiFileFormat(javax.sound.midi.MidiFileFormat) InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) Sequence(javax.sound.midi.Sequence)

Example 4 with Sequence

use of javax.sound.midi.Sequence 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)

Example 5 with Sequence

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

the class SMFParser method getSequence.

public Sequence getSequence(File file) throws InvalidMidiDataException, IOException {
    // throws IOException
    InputStream is = new FileInputStream(file);
    is = new BufferedInputStream(is, bisBufferSize);
    Sequence seq = null;
    try {
        seq = getSequence(is);
    } finally {
        is.close();
    }
    return seq;
}
Also used : BufferedInputStream(java.io.BufferedInputStream) DataInputStream(java.io.DataInputStream) BufferedInputStream(java.io.BufferedInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) Sequence(javax.sound.midi.Sequence) FileInputStream(java.io.FileInputStream)

Aggregations

Sequence (javax.sound.midi.Sequence)8 InvalidMidiDataException (javax.sound.midi.InvalidMidiDataException)5 BufferedInputStream (java.io.BufferedInputStream)3 IOException (java.io.IOException)3 InputStream (java.io.InputStream)3 DataInputStream (java.io.DataInputStream)2 FileInputStream (java.io.FileInputStream)2 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)2 ByteArrayInputStream (java.io.ByteArrayInputStream)1 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 MetaMessage (javax.sound.midi.MetaMessage)1 MidiFileFormat (javax.sound.midi.MidiFileFormat)1 MidiMessage (javax.sound.midi.MidiMessage)1 Patch (javax.sound.midi.Patch)1 Track (javax.sound.midi.Track)1 XHTMLContentHandler (org.apache.tika.sax.XHTMLContentHandler)1