In the following code, which is copied as it is in page 390 of the book Head First Java, I came across a little issue I hope someone will help me clarify.
The expected output from running the program should be hearing the piano playing and at the same time in the console, everytime a note was played, the word "la" should we written. The purpose of this exercise was to explain how to handle ControlChange MIDI events (with code number 176) so we could do something at the same time any note was played (since we could not use the NoteOn event for the same purpose). The problem is that adding the event with command 176 on the same tick but right after the NoteOn event (command 144) (lines 18 and 19) seem to silence the note somehow. I tried to add it on tick i+1 and as expected I could hear more of the the note but silenced half way by the ControChange event.
I found the solution was to add the ControlChange midi event on the same tick but BEFORE the NoteOn event and not AFTER. Seems like the events are stored in a FIFO queue and this prevents the note from being silenced by the ControlChange event as it happened before.
The question is: am I doing the right thing changing the order of these two lines of code or should the program have worked in the order they were originally in the book?
A control change event should not silence a note event. Are you sure you copied this line correctly?
Control change 127 is a message to set poly mode On. It's been a while since I worked with MIDI, but I wouldn't expect the repeated sending of this message to have any audible effect. Especially here, where there is nothing that would make 2 tones sound simultaneously.
Joined: Mar 12, 2011
Oh, I see now. It is just using 127 to fire the listener and print out "la".
Joined: Mar 12, 2011
I did a little reading on Midi Mode messages since my knowledge is beyond rusty. Apparently, according to the MIDI spec, mode messages are also supposed to behave as All Notes Off commands. So you are absolutely right to move it before the Note On event.
Joined: Feb 25, 2012
OK, thanks a lot It's strange no one has mentioned this error before (have they?).
I did notice this issue when I read the book. I found the solution on the forums for the book (although surprisingly, there was no response from the authors on the book forums). Dennis, thanks for looking it up and posting why this solution works. I didn't really dig too deep as I figured that I probably never would be using MIDI in anything I write. I was a little put-off that the book chose a MIDI based application as the main programming project, as it seems that they ended up devoting far too many pages on explaining how to get a working MIDI sequence...pages that I personally feel could have been devoted to some of the things they left out in the book. Still, it is an excellent introductory book to OOP concepts and to Java.