Search in sources :

Example 6 with InvalidMidiDataException

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

the class SMPTESequence method test.

static boolean test(float divisionType) {
    boolean result = false;
    try {
        log("Testing divisionType == " + divisionType);
        Sequence sequence = new Sequence(divisionType, 16, 1);
        float div1 = sequence.getDivisionType();
        ByteArrayOutputStream outStream = new ByteArrayOutputStream();
        MidiSystem.write(sequence, 1, outStream);
        InputStream inStream = new ByteArrayInputStream(outStream.toByteArray());
        sequence = MidiSystem.getSequence(inStream);
        float div2 = sequence.getDivisionType();
        log("After write/read got divisionType == " + div2);
        if (Math.abs(div2 - div1) < 0.001f) {
            result = true;
        }
    } catch (InvalidMidiDataException ex) {
        log(ex);
    } catch (IOException ex) {
        log(ex);
    } catch (IllegalArgumentException ex) {
        log(ex);
    }
    if (result) {
        log("OK");
    } else {
        log("FAIL");
        failed++;
    }
    return result;
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Sequence(javax.sound.midi.Sequence) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException)

Example 7 with InvalidMidiDataException

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

the class AudioFileSoundbankReader method getSoundbank.

public Soundbank getSoundbank(AudioInputStream ais) throws InvalidMidiDataException, IOException {
    try {
        byte[] buffer;
        if (ais.getFrameLength() == -1) {
            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            byte[] buff = new byte[1024 - (1024 % ais.getFormat().getFrameSize())];
            int ret;
            while ((ret = ais.read(buff)) != -1) {
                baos.write(buff, 0, ret);
            }
            ais.close();
            buffer = baos.toByteArray();
        } else {
            buffer = new byte[(int) (ais.getFrameLength() * ais.getFormat().getFrameSize())];
            new DataInputStream(ais).readFully(buffer);
        }
        ModelByteBufferWavetable osc = new ModelByteBufferWavetable(new ModelByteBuffer(buffer), ais.getFormat(), -4800);
        ModelPerformer performer = new ModelPerformer();
        performer.getOscillators().add(osc);
        SimpleSoundbank sbk = new SimpleSoundbank();
        SimpleInstrument ins = new SimpleInstrument();
        ins.add(performer);
        sbk.addInstrument(ins);
        return sbk;
    } catch (Exception e) {
        return null;
    }
}
Also used : ByteArrayOutputStream(java.io.ByteArrayOutputStream) DataInputStream(java.io.DataInputStream) InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) IOException(java.io.IOException)

Example 8 with InvalidMidiDataException

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

the class SMFParser method readTrack.

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;
            // initialize to invalid value
            int data1 = -1;
            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();
                            final byte[] metaData;
                            try {
                                metaData = new byte[metaLength];
                            } catch (final OutOfMemoryError oom) {
                                throw new IOException("Meta length too big", oom);
                            }
                            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");
    }
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) MidiMessage(javax.sound.midi.MidiMessage) MidiEvent(javax.sound.midi.MidiEvent) IOException(java.io.IOException) SysexMessage(javax.sound.midi.SysexMessage) EOFException(java.io.EOFException) MetaMessage(javax.sound.midi.MetaMessage)

Example 9 with InvalidMidiDataException

use of javax.sound.midi.InvalidMidiDataException 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 10 with InvalidMidiDataException

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

the class SoftMidiAudioFileReader method getAudioFileFormat.

public AudioFileFormat getAudioFileFormat(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 getAudioFileFormat(seq);
}
Also used : InvalidMidiDataException(javax.sound.midi.InvalidMidiDataException) UnsupportedAudioFileException(javax.sound.sampled.UnsupportedAudioFileException) Sequence(javax.sound.midi.Sequence) IOException(java.io.IOException)

Aggregations

InvalidMidiDataException (javax.sound.midi.InvalidMidiDataException)10 IOException (java.io.IOException)5 Sequence (javax.sound.midi.Sequence)5 ByteArrayInputStream (java.io.ByteArrayInputStream)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 MetaMessage (javax.sound.midi.MetaMessage)3 UnsupportedAudioFileException (javax.sound.sampled.UnsupportedAudioFileException)3 DataInputStream (java.io.DataInputStream)2 DataOutputStream (java.io.DataOutputStream)2 InputStream (java.io.InputStream)2 PipedInputStream (java.io.PipedInputStream)2 PipedOutputStream (java.io.PipedOutputStream)2 SequenceInputStream (java.io.SequenceInputStream)2 MidiEvent (javax.sound.midi.MidiEvent)2 MidiFileFormat (javax.sound.midi.MidiFileFormat)2 MidiMessage (javax.sound.midi.MidiMessage)2 SysexMessage (javax.sound.midi.SysexMessage)2 Track (javax.sound.midi.Track)2 BufferedInputStream (java.io.BufferedInputStream)1 EOFException (java.io.EOFException)1