http://fly.cc.fer.hr/GDM/articles/sndmus/speaker1.html
But how can I get access to the hardware port with Java? And if that's
not possible, is there then another way with which I can play tones
with the PC speaker with Java?
>http://fly.cc.fer.hr/GDM/articles/sndmus/speaker1.html
>
>But how can I get access to the hardware port with Java? And if that's
>not possible, is there then another way with which I can play tones
>with the PC speaker with Java?
see the sample code on my website
http://mindprod.com/jgloss/products1.html#SPEAKER
you do it in C or assembler and use JNI.
--
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Or you can use JavaSound to make audio on your computer. It uses the
sound card not the PC speaker though.
package javaprojects.classes;
import javax.sound.sampled.*;
public class Tone {
public static void sound(int hz, int msecs, double vol)
throws IllegalArgumentException, LineUnavailableException {
if (vol > 1.0 || vol < 0.0)
throw new IllegalArgumentException("Volume out of range 0.0
- 1.0");
byte[] buf = new byte[msecs * 8];
for (int i=0; i<buf.length; i++) {
double angle = i / (8000.0 / hz) * 2.0 * Math.PI;
buf[i] = (byte)(Math.sin(angle) * 127.0 * vol);
}
// shape the front and back ends of the wave form
for (int i=0; i<20 && i < buf.length / 2; i++) {
buf[i] = (byte)(buf[i] * i / 20);
buf[buf.length - 1 - i] = (byte)(buf[buf.length - 1 - i] *
i / 20);
}
AudioFormat af = new AudioFormat(8000f,8,1,true,false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
sdl.write(buf,0,buf.length);
sdl.drain();
sdl.close();
}
public static void main(String[] args) throws
LineUnavailableException {
Tone.sound(2000,500,1.0);
}
}
--
Knute Johnson
email s/nospam/knute/
You can use the javax.sound API which calls the sound card drivers to
output the sound. Depending on the operating system, you may not need
to actually have a sound card. For example, Microsoft supply a "sound
card driver" that drives the speaker directly
<http://support.microsoft.com/kb/q138857/>.
>
>Or you can use JavaSound to make audio on your computer. It uses the
>sound card not the PC speaker though.
There is an advantage to using the PC Speaker. I prefer it for error
messages. I often have my volume on my sound turned way down at night
but the PC speaker squawks still come through. You can do them in a
reasonably platform independent way with \007 to the console. I forget
now if System.beep uses the speaker.
see http://mindprod.com/jgloss/beep.html
>Well, this sounds perfect, if I can get it to work :D I'll certainly be
Do don't have to write a C program. that is already done as is the
JNI. Just download it and use the Java native classes.
See http://mindprod.com/products1.html#SPEAKER
alternatively install a windows speaker fake sound card driver, use
the usual sound apis and plug your ears with cotton.
>One other thing. Since kernel32.dll in Windows contains a Beep method,
>I would be able to use that in my Java program, right?
as I said, see http://mindprod.com/jgloss/beep.html
which gives you your alternatives.
Now my only remaining question is, whether or not there are commands in
windows.h like outport and inport from dos.h?
>Yeah, I see now how to do it :) Just one question though. Since I wanna
>make my own C program, and the one in the ZIP file includes windows.h,
>where can I get that file?
javah generates it for you. See http://mindprod.com/jgloss/jni.html
>And can I get a dos.h that also includes the Beep command? I mean, it
>would sound weird to me, if that could only be used in Windows, because
>the internal speaker also existed while people were using DOS :S
DOS and widows use a totally different addressing mode and way of
accessing system services. the code inside is the same almost, but
the way you get at it is completely different.
In DOS/BBL/ABUNDANCE I had to write my own tone generator. It is not
an OS function other than the 007 eep. It exists in many packages,
but none mainstream other than DESQView.
>
>Now my only remaining question is, whether or not there are commands in
>windows.h like outport and inport from dos.h?
I havze given you the source. Look at it.