• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

MiniMusicPlayer2 class in Head First Java book (Chapter 12)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?

 
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Oh, I see now. It is just using 127 to fire the listener and print out "la".
 
dennis deems
Ranch Hand
Posts: 808
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Juan MenendezB
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
OK, thanks a lot It's strange no one has mentioned this error before (have they?).
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Juan,

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.
 
Legend has it that if you rub the right tiny ad, a genie comes out.
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic