luistavares

Criando sequência de sons MIDI

Feb 19th, 2013
58
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package exemplo.midi;
  2.  
  3. import javax.sound.midi.InvalidMidiDataException;
  4. import javax.sound.midi.MidiEvent;
  5. import javax.sound.midi.MidiSystem;
  6. import javax.sound.midi.MidiUnavailableException;
  7. import javax.sound.midi.Sequence;
  8. import javax.sound.midi.Sequencer;
  9. import javax.sound.midi.ShortMessage;
  10. import javax.sound.midi.Track;
  11.  
  12. public class Midi {
  13.  
  14.     /**
  15.      * @param args
  16.      * @throws InvalidMidiDataException
  17.      * @throws MidiUnavailableException
  18.      * @throws InterruptedException
  19.      */
  20.     public static void main(String[] args) throws InvalidMidiDataException, MidiUnavailableException, InterruptedException {
  21.                
  22.         Sequencer player = MidiSystem.getSequencer();
  23.         player.open();
  24.         Sequence seq = new Sequence(Sequence.PPQ, 4);
  25.         Track track = seq.createTrack();
  26.        
  27.         ShortMessage a = new ShortMessage();
  28.         a.setMessage(144, 1, 44, 100);              // 144 significa reproduzir a nota 44
  29.         MidiEvent noteOn = new MidiEvent(a, 1);     // Nota inicia no momento 1
  30.         track.add(noteOn);
  31.        
  32.         ShortMessage b = new ShortMessage();
  33.         b.setMessage(128, 1, 44, 100);              // 128 significa parar a nota 44
  34.         MidiEvent noteOff = new MidiEvent(b, 12);   // Nota para no momento 12
  35.         track.add(noteOff);    
  36.        
  37.         ShortMessage c = new ShortMessage();
  38.         c.setMessage(144, 1, 59, 100);              // 144 significa reproduzir a nota 59
  39.         noteOn = new MidiEvent(c, 9);               // Nota inicia no momento 9
  40.         track.add(noteOn);
  41.        
  42.         ShortMessage d = new ShortMessage();
  43.         d.setMessage(128, 1, 59, 100);              // 128 significa parar a nota 59
  44.         noteOff = new MidiEvent(d, 16);             // Nota para no momento 16
  45.         track.add(noteOff);
  46.    
  47.         player.setSequence(seq);
  48.        
  49.         player.start();
  50.         Thread.sleep(1000 * 2);
  51.         player.close();    
  52.         System.exit(0);
  53.     }
  54.  
  55. }
RAW Paste Data