Results 1 to 8 of 8

Thread: Midi Generation

  1. #1
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default 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.

    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 ();
        }
    
    }
    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!
    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!

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    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.

  3. #3
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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!

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    Add a NOTE_OFF.

    db

  5. #5
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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!

  6. #6
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,457
    Rep Power
    20

    Default

    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

  7. #7
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    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!

  8. #8
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,785
    Blog Entries
    7
    Rep Power
    21

    Default

    Quote Originally Posted by Singing Boyo View Post
    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.
    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,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Midi
    By Maya in forum Advanced Java
    Replies: 4
    Last Post: 02-23-2011, 10:40 PM
  2. Report generation
    By anilkumar_vist in forum Advanced Java
    Replies: 1
    Last Post: 12-14-2009, 01:26 PM
  3. Auto id generation
    By jboy in forum New To Java
    Replies: 2
    Last Post: 09-01-2009, 12:27 AM
  4. MIDI to PCM
    By Ben Wheatley in forum Advanced Java
    Replies: 1
    Last Post: 08-05-2009, 06:58 PM
  5. random generation
    By carlos123 in forum New To Java
    Replies: 10
    Last Post: 01-09-2008, 04:43 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •