Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Sequence sequence = MidiSystem.getSequence(new File(source));
- Synthesizer synth = MidiSystem.getSynthesizer();
- //Gets a MidiMessage and send it to Synthesizer
- Receiver rcv = synth.getReceiver();
- //Contains all tracks and events from MIDI file
- Track[] tracks = sequence.getTracks();
- synth.open();
- //If there are tracks
- if(tracks != null)
- {
- //Verify the division type of the sequence (PPQ, SMPT)
- if(sequence.getDivisionType() == Sequence.PPQ)
- {
- int ppq = sequence.getResolution();
- //Do the math to get the time (in miliseconds) each tick takes
- long tickTime = TicksToMiliseconds(BPM,ppq);
- //Returns the number of ticks from the longest track
- int longestTrackTicks = LongestTrackTicks(tracks);
- //Each iteration sends a new message to 'receiver'
- for(int tick = 0; tick < maiorTick ; tick++)
- {
- //Iteration of each track
- for(int trackNumber = 0; trackNumber < tracks.length; trackNumber++)
- {
- //If the number of ticks from a track isn't already finished
- //continue
- if(tick < tracks[trackNumber].size())
- {
- MidiEvent ev = tracks[trackNumber].get(tick);
- rcv.send(ev.getMessage(),-1);
- }
- }
- Thread.sleep(tickTime);
- }
- }
- }
- synth.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement