Results 1 to 8 of 8
Thread: Midi Generation
- 07-04-2011, 11:37 PM #1
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Midi Generation
I've been learning about midi and trying my hand at creating something, but I'm stumped by the Sequencer-Synthesizer connection. I can't figure out why this doesn't generate sound when I use the sequencer. When I send messages directly to the Synthesizer's receiver, it works, but no such luck when I try to set the Sequencer's sequence and start it.
Probably nothing incredibly big, but I don't know much about midi just yet, so I'm stuck. Thanks in advance for any and all help!Java Code:import javax.sound.midi.InvalidMidiDataException; import javax.sound.midi.MidiEvent; import javax.sound.midi.MidiSystem; import javax.sound.midi.MidiUnavailableException; import javax.sound.midi.Receiver; import javax.sound.midi.Sequence; import javax.sound.midi.Sequencer; import javax.sound.midi.ShortMessage; import javax.sound.midi.Synthesizer; import javax.sound.midi.Track; import javax.sound.midi.Transmitter; class MusiCube { /** * @param args * @throws MidiUnavailableException * @throws InterruptedException * @throws InvalidMidiDataException */ public static void main(String[] args) throws MidiUnavailableException, InterruptedException, InvalidMidiDataException { Sequence seq = new Sequence(Sequence.PPQ, 8); Track track = seq.createTrack (); ShortMessage msg1 = new ShortMessage (); msg1.setMessage (ShortMessage.PROGRAM_CHANGE, 1, 0, 0/*ignored*/); track.add (new MidiEvent (msg1, -1)); ShortMessage msg2 = new ShortMessage(); msg2.setMessage (ShortMessage.NOTE_ON, 1, 60, 90); track.add (new MidiEvent(msg2,2)); Sequencer sequencer = MidiSystem.getSequencer (); sequencer.open (); Synthesizer synth = MidiSystem.getSynthesizer (); synth.open (); Transmitter seqTrans = sequencer.getTransmitter (); Receiver rec = synth.getReceiver (); seqTrans.setReceiver (rec); sequencer.setSequence (seq); sequencer.start (); //if I take out the Sequencer and use these, it works. why? //rec.send (msg1, 1); //rec.send (msg2, 2); Thread.sleep(3000); sequencer.close(); synth.close (); } }If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 07-04-2011, 11:48 PM #2
I never really messed with midi, but just one observation that might help you in the future. If you use import javax.sound.midi.*; it will import everything you are trying to import with just one import statement.
- Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
- +Rep people for helpful posts.
- 07-04-2011, 11:56 PM #3
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Eclipse has trained me to just import my unknown classes with a keyboard shortcut... should go back to Notepad++ for a bit, just to remind myself of things like that. Or just figure out how to make eclipse auto-wildcard the long import lists.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 07-05-2011, 12:19 AM #4
Add a NOTE_OFF.
db
- 07-05-2011, 12:33 AM #5
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Works, thanks. That begs the question though: why? Does something (Sequence/Track or Sequencer) look for a matching NOTE_OFF before executing/transmitting a NOTE_ON? It seems odd to me that it would work when I send the messages directly to the Receiver, but not when I put them in a Track.
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 07-05-2011, 01:39 AM #6
Probably because the eotEvent (end-of-Track) is at the same tick as the NOTE_ON so the Track comes to an end before the note can be heard. Adding any valid ShortMessage -- even another NOTE_ON -- results in the first note being heard.
Read the source of Track.add(MidiEvent event) if you want to know more.
db
- 07-05-2011, 08:03 AM #7
Senior Member
- Join Date
- Mar 2009
- Posts
- 552
- Rep Power
- 5
Right, that makes sense. Hadn't reached MetaEventListeners and the eotEvent yet, so I didn't realize that the Sequencer was treating the track as if it was done. Thanks again!
If the above doesn't make sense to you, ignore it, but remember it - might be useful!
And if you just randomly taught yourself to program, well... you're just like me!
- 07-05-2011, 08:45 AM #8
- Join Date
- Sep 2008
- Location
- Voorschoten, the Netherlands
- Posts
- 11,392
- Blog Entries
- 7
- Rep Power
- 17
I just read this thread for the first time so my reply might be a little late: keep it the way Eclipse does for you: import single files, not wildcards. A simple Ctrl-Shift-O organizes the imports for you anyway, so there's no need to import half the world in order to reduce the number of keystrokes; that's what IDEs are for.
kind regards,
JosWhen people rob a bank they get a penalty; when banks rob people they get a bonus.
Similar Threads
-
Midi
By Maya in forum Advanced JavaReplies: 4Last Post: 02-23-2011, 09:40 PM -
Report generation
By anilkumar_vist in forum Advanced JavaReplies: 1Last Post: 12-14-2009, 12:26 PM -
Auto id generation
By jboy in forum New To JavaReplies: 2Last Post: 08-31-2009, 11:27 PM -
MIDI to PCM
By Ben Wheatley in forum Advanced JavaReplies: 1Last Post: 08-05-2009, 05:58 PM -
random generation
By carlos123 in forum New To JavaReplies: 10Last Post: 01-09-2008, 03:43 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks