javax.sound.midi.InvalidMidiDataException Java Examples

The following examples show how to use javax.sound.midi.InvalidMidiDataException. 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: MidiTest.java    From kyoko with MIT License 7 votes vote down vote up
public static void main(String... args) throws MidiUnavailableException, IOException, InvalidMidiDataException {
    var synth = MidiSystem.getSynthesizer();
    synth.loadAllInstruments(synth.getDefaultSoundbank());

    var sequencer = MidiSystem.getSequencer();
    var sequence = MidiSystem.getSequence(new File("test.mid"));

    // sequencer.getTransmitter().setReceiver(synth.getReceiver());
    sequencer.open();
    sequencer.setSequence(sequence);
    sequencer.setTempoFactor(1f);
    sequencer.addMetaEventListener(meta -> {
        if (meta.getType() == 47) { // track end
            sequencer.setTickPosition(0);
            sequencer.start();
        }
    });
    sequencer.start();
}
 
Example #2
Source File: FastShortMessage.java    From openjdk-jdk9 with GNU General Public License v2.0 6 votes vote down vote up
@Override
public byte[] getMessage() {
    int length = 0;
    try {
        // fix for bug 4851018: MidiMessage.getLength and .getData return wrong values
        // fix for bug 4890405: Reading MidiMessage byte array fails in 1.4.2
        length = getDataLength(packedMsg & 0xFF) + 1;
    } catch (InvalidMidiDataException imde) {
        // should never happen
    }
    byte[] returnedArray = new byte[length];
    if (length>0) {
        returnedArray[0] = (byte) (packedMsg & 0xFF);
        if (length>1) {
            returnedArray[1] = (byte) ((packedMsg & 0xFF00) >> 8);
            if (length>2) {
                returnedArray[2] = (byte) ((packedMsg & 0xFF0000) >> 16);
            }
        }
    }
    return returnedArray;
}
 
Example #3
Source File: JavaSoundAudioClip.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
private boolean createSequencer(BufferedInputStream in) throws IOException {
    // get the sequencer
    try {
        sequencer = MidiSystem.getSequencer( );
    } catch(MidiUnavailableException me) {
        if (Printer.err) me.printStackTrace();
        return false;
    }
    if (sequencer==null) {
        return false;
    }

    try {
        sequence = MidiSystem.getSequence(in);
        if (sequence == null) {
            return false;
        }
    } catch (InvalidMidiDataException e) {
        if (Printer.err) e.printStackTrace();
        return false;
    }
    return true;
}
 
Example #4
Source File: StandardMidiFileReader.java    From Bytecoder with Apache License 2.0 6 votes vote down vote up
@Override
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;
}
 
Example #5
Source File: StandardMidiFileReader.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #6
Source File: AudioFileSoundbankReader.java    From openjdk-8-source with GNU General Public License v2.0 6 votes vote down vote up
public Soundbank getSoundbank(File file)
        throws InvalidMidiDataException, IOException {
    try {
        AudioInputStream ais = AudioSystem.getAudioInputStream(file);
        ais.close();
        ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
                new ModelByteBuffer(file, 0, file.length()), -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 (UnsupportedAudioFileException e1) {
        return null;
    } catch (IOException e) {
        return null;
    }
}
 
Example #7
Source File: AudioFileSoundbankReader.java    From dragonwell8_jdk with GNU General Public License v2.0 6 votes vote down vote up
public Soundbank getSoundbank(File file)
        throws InvalidMidiDataException, IOException {
    try {
        AudioInputStream ais = AudioSystem.getAudioInputStream(file);
        ais.close();
        ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
                new ModelByteBuffer(file, 0, file.length()), -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 (UnsupportedAudioFileException e1) {
        return null;
    } catch (IOException e) {
        return null;
    }
}
 
Example #8
Source File: StandardMidiFileReader.java    From hottub with GNU General Public License v2.0 6 votes vote down vote up
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;
}
 
Example #9
Source File: AudioFileSoundbankReader.java    From openjdk-jdk8u-backup with GNU General Public License v2.0 6 votes vote down vote up
public Soundbank getSoundbank(File file)
        throws InvalidMidiDataException, IOException {
    try {
        AudioInputStream ais = AudioSystem.getAudioInputStream(file);
        ais.close();
        ModelByteBufferWavetable osc = new ModelByteBufferWavetable(
                new ModelByteBuffer(file, 0, file.length()), -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 (UnsupportedAudioFileException e1) {
        return null;
    } catch (IOException e) {
        return null;
    }
}
 
Example #10
Source File: MetaMessage.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
/**
 * Sets the message parameters for a <code>MetaMessage</code>.
 * Since only one status byte value, <code>0xFF</code>, is allowed for meta-messages,
 * it does not need to be specified here.  Calls to <code>{@link MidiMessage#getStatus getStatus}</code> return
 * <code>0xFF</code> for all meta-messages.
 * <p>
 * The <code>type</code> argument should be a valid value for the byte that
 * follows the status byte in the <code>MetaMessage</code>.  The <code>data</code> argument
 * should contain all the subsequent bytes of the <code>MetaMessage</code>.  In other words,
 * the byte that specifies the type of <code>MetaMessage</code> is not considered a data byte.
 *
 * @param type              meta-message type (must be less than 128)
 * @param data              the data bytes in the MIDI message
 * @param length    the number of bytes in the <code>data</code>
 * byte array
 * @throws                  <code>InvalidMidiDataException</code>  if the
 * parameter values do not specify a valid MIDI meta message
 */
public void setMessage(int type, byte[] data, int length) throws InvalidMidiDataException {

    if (type >= 128 || type < 0) {
        throw new InvalidMidiDataException("Invalid meta event with type " + type);
    }
    if ((length > 0 && length > data.length) || length < 0) {
        throw new InvalidMidiDataException("length out of bounds: "+length);
    }

    this.length = 2 + getVarIntLength(length) + length;
    this.dataLength = length;
    this.data = new byte[this.length];
    this.data[0] = (byte) META;        // status value for MetaMessages (meta events)
    this.data[1] = (byte) type;        // MetaMessage type
    writeVarInt(this.data, 2, length); // write the length as a variable int
    if (length > 0) {
        System.arraycopy(data, 0, this.data, this.length - this.dataLength, this.dataLength);
    }
}
 
Example #11
Source File: GetSoundBankIOException.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 {
    boolean failed = false;
    try {
        String filename = "GetSoundBankIOException.java";
        System.out.println("Opening "+filename+" as soundbank...");
        File midiFile = new File(System.getProperty("test.src", "."), filename);
        MidiSystem.getSoundbank(midiFile);
        //Soundbank sBank = MidiSystem.getSoundbank(new NonMarkableIS());
        System.err.println("InvalidMidiDataException was not thrown!");
        failed = true;
    } catch (InvalidMidiDataException invMidiEx) {
        System.err.println("InvalidMidiDataException was thrown. OK.");
    } catch (IOException ioEx) {
        System.err.println("Unexpected IOException was caught!");
        System.err.println(ioEx.getMessage());
        ioEx.printStackTrace();
        failed = true;
    }

    if (failed) throw new Exception("Test FAILED!");
    System.out.println("Test passed.");
}
 
Example #12
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 #13
Source File: StandardMidiFileReader.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
public MidiFileFormat getMidiFileFormat(URL url) throws InvalidMidiDataException, IOException {
    InputStream urlStream = url.openStream(); // throws IOException
    BufferedInputStream bis = new BufferedInputStream( urlStream, bisBufferSize );
    MidiFileFormat fileFormat = null;
    try {
        fileFormat = getMidiFileFormat( bis ); // throws InvalidMidiDataException
    } finally {
        bis.close();
    }
    return fileFormat;
}
 
Example #14
Source File: SF2SoundbankReader.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public Soundbank getSoundbank(InputStream stream)
        throws InvalidMidiDataException, IOException {
    try {
        stream.mark(512);
        return new SF2Soundbank(stream);
    } catch (RIFFInvalidFormatException e) {
        stream.reset();
        return null;
    }
}
 
Example #15
Source File: StandardMidiFileReader.java    From Bytecoder with Apache License 2.0 5 votes vote down vote up
boolean nextTrack() throws IOException, InvalidMidiDataException {
    int magic;
    trackLength = 0;
    do {
        // $$fb 2003-08-20: fix for 4910986: MIDI file parser breaks up on http connection
        if (stream.skipBytes(trackLength) != trackLength) {
            if (!STRICT_PARSER) {
                return false;
            }
            throw new EOFException("invalid MIDI file");
        }
        magic = readIntFromStream();
        trackLength = readIntFromStream();
    } while (magic != MTrk_MAGIC);
    if (!STRICT_PARSER) {
        if (trackLength < 0) {
            return false;
        }
    }
    // now read track in a byte array
    try {
        trackData = new byte[trackLength];
    } catch (final OutOfMemoryError oom) {
        throw new IOException("Track length too big", oom);
    }
    try {
        // $$fb 2003-08-20: fix for 4910986: MIDI file parser breaks up on http connection
        stream.readFully(trackData);
    } catch (EOFException eof) {
        if (!STRICT_PARSER) {
            return false;
        }
        throw new EOFException("invalid MIDI file");
    }
    pos = 0;
    return true;
}
 
Example #16
Source File: SF2SoundbankReader.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
public Soundbank getSoundbank(File file)
        throws InvalidMidiDataException, IOException {
    try {
        return new SF2Soundbank(file);
    } catch (RIFFInvalidFormatException e) {
        return null;
    }
}
 
Example #17
Source File: FastShortMessage.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
@Override
public void setMessage(int status) throws InvalidMidiDataException {
    // check for valid values
    int dataLength = getDataLength(status); // can throw InvalidMidiDataException
    if (dataLength != 0) {
        super.setMessage(status); // throws Exception
    }
    packedMsg = (packedMsg & 0xFFFF00) | (status & 0xFF);
}
 
Example #18
Source File: AudioFileSoundbankReader.java    From jdk8u-jdk with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example #19
Source File: MidiMessageUtils.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static MidiMessage noteOn(int channel,int note,int velocity){
	try {
		ShortMessage message = new ShortMessage();
		message.setMessage(ShortMessage.NOTE_ON, fixChannel(channel), fixValue(note), fixValue(velocity));
		return message;
	} catch (InvalidMidiDataException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example #20
Source File: MidiMessageUtils.java    From tuxguitar with GNU Lesser General Public License v2.1 5 votes vote down vote up
public static MidiMessage programChange(int channel,int instrument){
	try {
		ShortMessage message = new ShortMessage();
		message.setMessage(ShortMessage.PROGRAM_CHANGE, fixChannel(channel), fixValue(instrument), 0);
		return message;
	} catch (InvalidMidiDataException e) {
		e.printStackTrace();
	}
	return null;
}
 
Example #21
Source File: DLSSoundbankReader.java    From openjdk-8 with GNU General Public License v2.0 5 votes vote down vote up
public Soundbank getSoundbank(InputStream stream)
        throws InvalidMidiDataException, IOException {
    try {
        stream.mark(512);
        return new DLSSoundbank(stream);
    } catch (RIFFInvalidFormatException e) {
        stream.reset();
        return null;
    }
}
 
Example #22
Source File: DLSSoundbankReader.java    From TencentKona-8 with GNU General Public License v2.0 5 votes vote down vote up
public Soundbank getSoundbank(InputStream stream)
        throws InvalidMidiDataException, IOException {
    try {
        stream.mark(512);
        return new DLSSoundbank(stream);
    } catch (RIFFInvalidFormatException e) {
        stream.reset();
        return null;
    }
}
 
Example #23
Source File: JavaSoundAudioClip.java    From openjdk-jdk8u with GNU General Public License v2.0 5 votes vote down vote up
private boolean createSequencer(BufferedInputStream in) throws IOException {

        if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.createSequencer()");

        // get the sequencer
        try {
            sequencer = MidiSystem.getSequencer( );
        } catch(MidiUnavailableException me) {
            if (DEBUG || Printer.err)me.printStackTrace();
            return false;
        }
        if (sequencer==null) {
            return false;
        }

        try {
            sequence = MidiSystem.getSequence(in);
            if (sequence == null) {
                return false;
            }
        } catch (InvalidMidiDataException e) {
            if (DEBUG || Printer.err)e.printStackTrace();
            return false;
        }

        if (DEBUG || Printer.debug)Printer.debug("Created Sequencer.");
        return true;
    }
 
Example #24
Source File: JavaSoundAudioClip.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public JavaSoundAudioClip(InputStream in) throws IOException {
    if (DEBUG || Printer.debug)Printer.debug("JavaSoundAudioClip.<init>");

    BufferedInputStream bis = new BufferedInputStream(in, STREAM_BUFFER_SIZE);
    bis.mark(STREAM_BUFFER_SIZE);
    boolean success = false;
    try {
        AudioInputStream as = AudioSystem.getAudioInputStream(bis);
        // load the stream data into memory
        success = loadAudioData(as);

        if (success) {
            success = false;
            if (loadedAudioByteLength < CLIP_THRESHOLD) {
                success = createClip();
            }
            if (!success) {
                success = createSourceDataLine();
            }
        }
    } catch (UnsupportedAudioFileException e) {
        // not an audio file
        try {
            MidiFileFormat mff = MidiSystem.getMidiFileFormat(bis);
            success = createSequencer(bis);
        } catch (InvalidMidiDataException e1) {
            success = false;
        }
    }
    if (!success) {
        throw new IOException("Unable to create AudioClip from input stream");
    }
}
 
Example #25
Source File: SF2SoundbankReader.java    From openjdk-8-source with GNU General Public License v2.0 5 votes vote down vote up
public Soundbank getSoundbank(URL url)
        throws InvalidMidiDataException, IOException {
    try {
        return new SF2Soundbank(url);
    } catch (RIFFInvalidFormatException e) {
        return null;
    } catch(IOException ioe) {
        return null;
    }
}
 
Example #26
Source File: SF2SoundbankReader.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public Soundbank getSoundbank(InputStream stream)
        throws InvalidMidiDataException, IOException {
    try {
        stream.mark(512);
        return new SF2Soundbank(stream);
    } catch (RIFFInvalidFormatException e) {
        stream.reset();
        return null;
    }
}
 
Example #27
Source File: AudioFileSoundbankReader.java    From openjdk-jdk9 with GNU General Public License v2.0 5 votes vote down vote up
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;
    }
}
 
Example #28
Source File: StandardMidiFileReader.java    From jdk8u60 with GNU General Public License v2.0 5 votes vote down vote up
public MidiFileFormat getMidiFileFormat(URL url) throws InvalidMidiDataException, IOException {
    InputStream urlStream = url.openStream(); // throws IOException
    BufferedInputStream bis = new BufferedInputStream( urlStream, bisBufferSize );
    MidiFileFormat fileFormat = null;
    try {
        fileFormat = getMidiFileFormat( bis ); // throws InvalidMidiDataException
    } finally {
        bis.close();
    }
    return fileFormat;
}
 
Example #29
Source File: DLSSoundbankReader.java    From jdk8u_jdk with GNU General Public License v2.0 5 votes vote down vote up
public Soundbank getSoundbank(InputStream stream)
        throws InvalidMidiDataException, IOException {
    try {
        stream.mark(512);
        return new DLSSoundbank(stream);
    } catch (RIFFInvalidFormatException e) {
        stream.reset();
        return null;
    }
}
 
Example #30
Source File: DavidMusicModule.java    From mochadoom with GNU General Public License v3.0 5 votes vote down vote up
private static void sendControlChange(Receiver receiver, int midiChan, int ctrlId, int value) {
    ShortMessage msg = new ShortMessage();
    try {
        msg.setMessage(ShortMessage.CONTROL_CHANGE, midiChan, ctrlId, value);
    } catch (InvalidMidiDataException ex) {
        throw new RuntimeException(ex);
    }
    receiver.send(msg, -1);
}