public void play() { if (isPlaying) { // 如果已经在播放,返回 return; } try { sequencer = MidiSystem.getSequencer(); sequencer.open(); sequencer.setSequence(sequence); sequencer.setLoopCount(Sequencer.LOOP_CONTINUOUSLY ); sequencer.addMetaEventListener(this); } catch (InvalidMidiDataException ex) { } catch (MidiUnavailableException e) { } thread = new Thread(this); thread.start(); }
/** * Execute Receiver.send() and expect that there is no exception. */ private static boolean testReceiverSend() { boolean result = true; Receiver receiver; ShortMessage shMsg = new ShortMessage(); try { receiver = MidiSystem.getReceiver(); shMsg.setMessage(ShortMessage.NOTE_ON, 0,60, 93); try { receiver.send( shMsg, -1 ); } catch(IllegalStateException ilEx) { ilEx.printStackTrace(System.out); out("IllegalStateException was thrown incorrectly!"); result = false; } receiver.close(); } catch(MidiUnavailableException e) { out("Midi unavailable, cannot test."); } catch(InvalidMidiDataException ine) { out("InvalidMidiDataException, cannot test."); } return result; }
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."); }
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; } }
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; }
public MidiFileFormat getMidiFileFormat(File file) throws InvalidMidiDataException, IOException { FileInputStream fis = new FileInputStream(file); // throws IOException BufferedInputStream bis = new BufferedInputStream(fis, bisBufferSize); // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length long length = file.length(); if (length > Integer.MAX_VALUE) { length = MidiFileFormat.UNKNOWN_LENGTH; } MidiFileFormat fileFormat = null; try { fileFormat = getMidiFileFormatFromStream(bis, (int) length, null); } finally { bis.close(); } return fileFormat; }
@Override 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; } }
void sendMessage(byte[] data, long timeStamp) { try { synchronized(transmitters) { int size = transmitters.size(); if (TRACE_TRANSMITTER) Printer.println("Sending long message to "+size+" transmitter's receivers"); for (int i = 0; i < size; i++) { Receiver receiver = transmitters.get(i).getReceiver(); if (receiver != null) { //$$fb 2002-04-02: SysexMessages are mutable, so // an application could change the contents of this object, // or try to use the object later. So we can't get around object creation // But the array need not be unique for each FastSysexMessage object, // because it cannot be modified. receiver.send(new FastSysexMessage(data), timeStamp); } } } } catch (InvalidMidiDataException e) { // this happens when invalid data comes over the wire. Ignore it. return; } }
@Override public MidiFileFormat getMidiFileFormat(File file) throws InvalidMidiDataException, IOException { FileInputStream fis = new FileInputStream(file); // throws IOException BufferedInputStream bis = new BufferedInputStream(fis, bisBufferSize); // $$fb 2002-04-17: part of fix for 4635286: MidiSystem.getMidiFileFormat() returns format having invalid length long length = file.length(); if (length > Integer.MAX_VALUE) { length = MidiFileFormat.UNKNOWN_LENGTH; } MidiFileFormat fileFormat = null; try { fileFormat = getMidiFileFormatFromStream(bis, (int) length, null); } finally { bis.close(); } return fileFormat; }
@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; }
@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; }
/** * Starts playing the MIDI file using the sequencer. * * @param midiFile the MIDI file. */ public void startPlay(File midiFile) { try { startPlay(MidiSystem.getSequence(midiFile)); } catch (InvalidMidiDataException | IOException e) { throw SpongeUtils.wrapException(e); } }
/** * Starts playing the MIDI file using the sequencer. * * @param url the MIDI file url. */ public void startPlay(URL url) { try { startPlay(MidiSystem.getSequence(url)); } catch (InvalidMidiDataException | IOException e) { throw SpongeUtils.wrapException(e); } }
/** * Starts playing the MIDI file using the sequencer. * * @param sequence the MIDI sequence. */ public void startPlay(Sequence sequence) { try { sequencer.setSequence(sequence); } catch (InvalidMidiDataException e) { throw SpongeUtils.wrapException(e); } sequencer.start(); }
/** * Sets the MIDI MetaMessage type. * * @param type the MIDI MetaMessage type. */ public void setMessageType(int type) { try { getMessage().setMessage(type, getData(), getData().length); } catch (InvalidMidiDataException e) { throw SpongeUtils.wrapException(e); } }
/** * Sets the MIDI MetaMessage data. * * @param data the MIDI MetaMessage data. */ public void setData(byte[] data) { try { getMessage().setMessage(getMessageType(), data, data.length); } catch (InvalidMidiDataException e) { throw SpongeUtils.wrapException(e); } }
/** * Sets the MIDI short message command. * * @param command the MIDI short message command. */ public void setCommand(int command) { try { getMessage().setMessage(command, getChannel(), getData1(), getData2()); } catch (InvalidMidiDataException e) { throw SpongeUtils.wrapException(e); } }
static ShortMessage MidiMsg3(int a, int b, int c) { try { ShortMessage msg = new ShortMessage(); msg.setMessage((byte)a,(byte)b,(byte)c); return msg; } catch(InvalidMidiDataException ex) { throw new RuntimeException(); } }
/** * Sets the MIDI short message data1. * * @param data1 the MIDI short message data1. */ public void setData1(int data1) { try { getMessage().setMessage(getCommand(), getChannel(), data1, getData2()); } catch (InvalidMidiDataException e) { throw SpongeUtils.wrapException(e); } }
/** * Sets the MIDI short message data2. * * @param data2 the MIDI short message data2. */ public void setData2(int data2) { try { getMessage().setMessage(getCommand(), getChannel(), getData1(), data2); } catch (InvalidMidiDataException e) { throw SpongeUtils.wrapException(e); } }
/** * Sets the MIDI SysexMessage data. * * @param data the MIDI SysexMessage data. */ public void setData(byte[] data) { try { getMessage().setMessage(data, data.length); } catch (InvalidMidiDataException e) { throw SpongeUtils.wrapException(e); } }
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; }
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; } }
public Soundbank getSoundbank(URL url) throws InvalidMidiDataException, IOException { try { return new SF2Soundbank(url); } catch (RIFFInvalidFormatException e) { return null; } catch(IOException ioe) { return null; } }
public Soundbank getSoundbank(InputStream stream) throws InvalidMidiDataException, IOException { try { stream.mark(512); return new SF2Soundbank(stream); } catch (RIFFInvalidFormatException e) { stream.reset(); return null; } }
public Soundbank getSoundbank(File file) throws InvalidMidiDataException, IOException { try { return new SF2Soundbank(file); } catch (RIFFInvalidFormatException e) { return null; } }
public static void midi(MidiReceiver receiver, int command, int channel, int data1, int data2) { try { ShortMessage msg = new ShortMessage(command, channel, data1, data2); receiver.send(msg, Main.instance().getTime()); } catch (InvalidMidiDataException ex) { throw new RuntimeException(ex); } }
public Soundbank getSoundbank(URL url) throws InvalidMidiDataException, IOException { try { return new DLSSoundbank(url); } catch (RIFFInvalidFormatException e) { return null; } catch(IOException ioe) { return null; } }
public Soundbank getSoundbank(InputStream stream) throws InvalidMidiDataException, IOException { try { stream.mark(512); return new DLSSoundbank(stream); } catch (RIFFInvalidFormatException e) { stream.reset(); return null; } }
public Soundbank getSoundbank(File file) throws InvalidMidiDataException, IOException { try { return new DLSSoundbank(file); } catch (RIFFInvalidFormatException e) { return null; } }
@Override public Soundbank getSoundbank(InputStream stream) throws InvalidMidiDataException, IOException { try { stream.mark(512); return new DLSSoundbank(stream); } catch (RIFFInvalidFormatException e) { stream.reset(); return null; } }
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; }