• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Head First Java: BeatBox music machine

 
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Help! I am trying to teach myself Java and object orientation with the help of this book and have run into a problem.

I have tried out the (almost) initial code for the music machine in Chap. 11; the piano note plays very nicely but then the program doesn't seem to know that it is finished.

I am using emacs as my editor and running the code from the cmd prompt in MS Windows XP. The only way that I can convince the command shell that the program is done is cntl-C. What should I add to the code in the book to say "stop now"? (of course, "sensible" procedural languages like the ones I grew up with have statements like STOP or CALL EXIT ...) ;-)

Thanks for any ideas!

Jinny Morris
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

At the end of the try block (immediately after calling player.start();), try adding the following...

This will cause a 2-second pause (which should be long enough for the player to play the note you specified), after which it will close the Sequencer and exit the program.

There's probably a "better" way, but I think this will work.
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Marc -

Thank you thank you! It works beautifully - unfortunately it also leaves me with a batch of further questions.

I have been crawling through the API docs for MidiEvent and Sequencer and have found that all of the time parameters used in these classes are in "MIDI ticks" - but I can't figure out from what I'm reading how a MIDI tick translates into "real" time in seconds or milliseconds or anything that I could use to set the thread sleep time for "playing around". Is there somewhere else I should look? Or do I just not understand what I'm reading?

Thanks again!

Jinny Morris
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Jinny Morris:
...I can't figure out from what I'm reading how a MIDI tick translates into "real" time in seconds or milliseconds...


Well, despite being a musician (and somewhat of a geek), I know very little about MIDI.

The documentation for MidiEvent says, "The duration of a tick is specified by the timing information contained in the MIDI file or Sequence object." And the constructor Sequence(float divisionType, int resolution)...

Constructs a new MIDI sequence with the specified timing division type and timing resolution. The division type must be one of the recognized MIDI timing types. For tempo-based timing, divisionType is PPQ (pulses per quarter note) and the resolution is specified in ticks per beat. For SMTPE timing, divisionType specifies the number of frames per second and the resolution is specified in ticks per frame.


So a Sequence looks like the entry point for controlling this, but I think I need some fundamentals about how MIDI tempo works before understanding how to use it. I found this MIDI Timing Concepts page that looks pretty good. See if that helps...
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks again - the page you found looks promising.

I wish I were a musician - but I'm afraid that my current "issue" is that if I'm going to use a black box I like to at least be able to fool myself into thinking that I understand what I'm putting into it ...

Jinny Morris
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hey, I'm a couple of chapters ahead of you. Alright, chapter 11 only offers up a single note, so it isn't too interested in timing except in regards of when to start. I bow to the superior knowledge of Mark Weber, (I'd wondered what the heck was for!!!) - but since we're talking about timing, the next chapter makes use of sequencer.setTempoInBPM(bpm) - from there you can do the math for timing the rhythm.
I've come across the program termination problem, too. At least for GUI you can use

(where frame is declared JFrame object).
Hope this helps!
[ May 28, 2007: Message edited by: Paul Paskin ]
 
Jinny Morris
Ranch Hand
Posts: 103
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Paul -

Thanks much! So I will be patient until I get to bpm ...

Yes, I am just now playing w/ the "very simple GUI" in chapter 11 so have now met "exit on close" (of frame) - but we weren't using GUIs yet when we first met the sound generator.

Occasionally I regret my decision to do this w/out benefit of IDE



- Jinny Morris
 
reply
    Bookmark Topic Watch Topic
  • New Topic