aspose file tools*
The moose likes Beginning Java and the fly likes Head First Java: BeatBox music machine Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java: BeatBox music machine" Watch "Head First Java: BeatBox music machine" New topic
Author

Head First Java: BeatBox music machine

Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
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
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Apr 29, 2007
Posts: 101
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
Paul Paskin
Greenhorn

Joined: May 28, 2007
Posts: 3
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

Joined: Apr 29, 2007
Posts: 101
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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Head First Java: BeatBox music machine
 
Similar Threads
Which IDE to use?
NIO.2
Error message
Application doesn't quit -- MiniMiniMusicApp sample from Head First Java
MUSIC start, not stops