1

Taken from Head First Java's book, I am doing a Beatbox, where 16 different instruments have 16 checkboxes you can check, each one of them would be a 'beat' and, if checked, produced sound.

I have a specific method to produce MIDI events:

private MidiEvent makeEvent(int comd, int chan, int one, int two, int tick) {
    MidiEvent event = null;
    try {
        ShortMessage a = new ShortMessage();
        a.setMessage(comd, chan, one, two);
        event = new MidiEvent(a, tick);
    } catch(Exception e) {e.printStackTrace(); }
    return event;

Now, at some point in the code, the following comes up:

track.add(makeEvent(176,1,126,0,16));

track.add(makeEvent(192,9,1,0,15)); 

The first one I don't understand or know what it's for. The second one is according to the book to 'make sure there is an event on beat 16, otherwise the Beatbox might not go the full 16 beats before starting over'.

What are the arguments being passed to the method? Why do I need these two in the code? I found 176 is a CONTROL_CHANGE and 192 PROGRAM_CHANGE, the second argument would be the channel, the third 'note to play' and the fourth 'velocity'.

I tried going through the APIs, and found nothing.

1 Answer 1

5

makeEvent(176,1,126,0,16) means "make a controller event at tick position 16 which sets the channel 1 of the connected Midi synthesizer in Mono mode". How do I know that:

  • 176 means it's a controller Midi event.
  • 1 means channel 1
  • 126 is the controller number, and it means "Set channel in Mono mode" (search the web for Midi controllers list to get the meaning of each number, and the meaning of the associated value)
  • 0 is the associated value
  • 16 is the tick position

makeEvent(192,9,1,0,15) means "make a program change event on channel 9 at tick position 15 which sets the patch #1 on the connected Midi synthesizer." :

  • 192 means it's a Program Change
  • 9 is the drums channel
  • 1 is the patch id
  • 0 is the 2nd value which is ignored in the case of a Program Change command
  • 15 is the tick position

It's very usual in Midi files to find "Program Change" messages at the start of the song, they tell the synth (for example) "Select an electric bass patch on channel 11" -if channel 11 notes contains the bass line.

From what you describe, I assume the sequencer is in loop mode, and sequence'tick resolution is 1 PPQ. If you don't explicitly use Sequencer.setLoopStartPoint() and Sequencer.setLoopEndPoint(), the Sequencer loops the whole sequence. Sequence length depends on the last MidiEvent tick position. If you don't add any NoteON/OFF events (e.g. if all your checkboxes are unchecked ?), then the Program Change presence preserves the sequence length=16 ticks.

The "Set mono mode" message is more rare, and it's at tick 16, so this makes sequence length=17 ticks actually...

A good short website to understand Midi basics for programming : https://www.cs.cmu.edu/~music/cmsip/readings/MIDI%20tutorial%20for%20programmers.html

The ShortMessage API defines a few constants for common Midi command/status values, it makes your code easier to read.

3
  • Thanks. Why would I need these two in my Beatbox code (simplified, I have 16 possible instruments with 16 beats per instrument, where each beat is a checkbox) if I'm only theoretically interested on whether or not the boxes are checked?
    – maverickar
    Commented Nov 12, 2022 at 19:39
  • I completed the answer
    – jjazzboss
    Commented Nov 13, 2022 at 9:57
  • Thanks! Still weird with that 17 ticks, but oh well...
    – maverickar
    Commented Nov 14, 2022 at 19:18

Not the answer you're looking for? Browse other questions tagged or ask your own question.