/** * Loads a sequence from an input stream. Returns null if an error occurs. * * @param is * @return */ public Sequence getSequence(InputStream is) { try { if (!is.markSupported()) { is = new BufferedInputStream(is); } Sequence s = MidiSystem.getSequence(is); is.close(); return s; } catch (InvalidMidiDataException ex) { ex.printStackTrace(); return null; } catch (IOException ex) { ex.printStackTrace(); return null; } }
/** * Constructor. It breaks a Song into its MidiEvent components, and adds them together in order to * create a MIDI file. * * @param song The song to export */ public MidiExporter() { // Create the Sequence that will contain everything try { sequence = new Sequence(Sequence.PPQ, 2); } catch (InvalidMidiDataException ex) { ex.printStackTrace(); System.exit(1); } }
/** * Plays a sequence, optionally looping. This method returns immediately. The sequence is not * played if it is invalid. * * @param sequence * @param loop */ public void play(Sequence sequence, boolean loop) { if (sequencer != null && sequence != null && sequencer.isOpen()) { try { sequencer.setSequence(sequence); sequencer.start(); this.loop = loop; } catch (InvalidMidiDataException ex) { ex.printStackTrace(); } } }
/** Carrega a seqüência do sistema de arquivos. Retorna null se um erro ocorrer. */ public Sequence getSequence(String name) { String filename = "/recursos/sons/" + name; try { return MidiSystem.getSequence(getClass().getResource((filename))); } catch (InvalidMidiDataException ex) { ex.printStackTrace(); return null; } catch (IOException ex) { ex.printStackTrace(); return null; } }
/** * Create the MidiEvent for a note, given the data. * * @param command The command value for the ShortMessage * @param note The MIDI value for the note to be played * @param eventTime When this event should occur * @param velocity The velocity of this note * @return The MidiEvent for the note */ private MidiEvent createNoteEvent(int command, int note, int eventTime, int velocity) { // Create the message and set its parameters to the ones given. ShortMessage message = new ShortMessage(); try { message.setMessage(command, 0, note, velocity); } catch (InvalidMidiDataException ex) { // Something went wrong. ex.printStackTrace(); System.exit(1); } // Create the MidiEvent and return it. return new MidiEvent(message, eventTime); }