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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Head First Java - MIDI question" Watch "Head First Java - MIDI question" New topic
Author

Head First Java - MIDI question

Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
I am revisiting the Head First "boom box" and am stuck in two places:

First, the very first example program, which turns a note on and then off, never stops - I have to use Cntl-C in the command window. I tried using System.exit(0) (this suggestion was also made by Daniel Gagnon), but then I never hear the note at all (the program does stop, though!)

Second, I am one of several who would like to know what instruments I have available. I have tried using the getAvailableInstruments method of the Synthesizer class by reading Sun's API and got 0 instruments; so I tried searching the ranch. I found 2 hunks of code, one posted by Kathy Sierra and one posted by Timothy Stone (both a long time ago). Both of these programs also tell me that I have 0 instruments.

If I try the getDefaultSoundbank method of class Synthesizer, it returns null.

However, if I just play the program in the Head First book, I get the promised piano note. So there's obviously something somewhere! I just wish I could find it/them ...

I am very confused! Could anyone cast any light on this?

PS - I also tried the getLoadedInstruments method. Same results.

[ January 13, 2008: Message edited by: Jinny Morris ]

[ January 13, 2008: Message edited by: Jinny Morris ]

[ January 13, 2008: Message edited by: Jinny Morris ]
[ January 13, 2008: Message edited by: Jinny Morris ]
Shannon Holck
Greenhorn

Joined: Jan 04, 2008
Posts: 5
First, the very first example program, which turns a note on and then off, never stops - I have to use Cntl-C in the command window. I tried using System.exit(0) (this suggestion was also made by Daniel Gagnon), but then I never hear the note at all (the program does stop, though!)

Jinny, near as I can tell when you open a Sequence, you are opening a stream between your application and the midi device hooked into the OS (e.g., your sound card). The device doesn't know when you are done accessing it (maybe you want to play multiple tracks), so you have to tell it. will terminate the JVM ... 0 meaning it terminated normally, however, the latency between your application and the OS then might cause the stream to close before it ever gets to the device. The best way to handle appears to be the way explained in this article Understanding and using Java MIDI audio. However, you don't learn events until the next chapter of "Head First Java". There's also an example here of another way of doing it, but it involves inner classes which are covered in the next chapter, but I find this method of coding inner classes harder to read e722. Determining When a Midi Audio Player Has Finished Playing

What you will probably find most helpful is the new lines added to the Wickedly Smart source code for the "Head First Java" book. You can download most of the baked code and some of the answers to some of the puzzles from here.

As to the
Second, I am one of several who would like to know what instruments I have available.

Have you tried the next sample in the book? Version 2 on pg 346? If you have and you don't hear a different instrument, it maybe that your Midi device doesn't support any. You might be able to change Midi devices if you have more available(you use the default one if you follow this code sample). If you really want to get into it, you can see Sun(tm) Java Sound Programmers Guide or Java Sound Resources has some examples. And there is some sample code available via the Java Sound homepage: see Sun (tm) Java Code Samples

If you would like more specific help, could you post the code you used?
Shannon Holck
Greenhorn

Joined: Jan 04, 2008
Posts: 5
Jinny,

Stealing a bit from one of the sites I mentioned earlier (See: Java Sound Resources Soundbank example), I was able to run this code:


Which resulted in a huge list:

Instrument: Agogo = 113
Instrument: Steel Drums = 114
Instrument: Woodblock = 115
Instrument: Taiko Drum = 116
Instrument: Melodic Tom = 117
Instrument: Syn Drum = 118
Instrument: Reverse Cymbal = 119
Instrument: Guitar Fret Noise = 120
Instrument: Breath Noise = 121
Instrument: Seashore = 122
Instrument: Bird = 123
Instrument: Telephone = 124
Instrument: Helicopter = 125
Instrument: Applause = 126
Instrument: Gunshot = 127
Instrument: Hi-Q = 27
Instrument: Slap = 28
(etc. for all 128 programs)

Is this what you are looking for?
Jinny Morris
Ranch Hand

Joined: Apr 29, 2007
Posts: 101
Shannon -

Thank you so much! I finally did solve my problem (with help) in a very unexpected way. I had ok'd automatic updates from Sun; it turned out that in addition to putting the executables in the program files \ java directory, the updates were also putting copies of the executables in my windows \ system32 directory. Because I had added the "java" path to the end of my list of paths, the executables in the windows directory were being used to run my programs. Unfortunately, none of the subsidiary files (in this case lib \ audio \ soundbank) were in the windows directory. So my BeatBox program couldn't find a soundbank, let alone any instruments! (I had tried getting the default soundbank in code; null was returned.)

So I disabled the executables that were "in the wrong place" and also disabled the automatic updates. Now I have something appalling like 400 or so "instruments"; since I also have screwed-up speakers, the long trek continues ...

Thanks again!
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Head First Java - MIDI question