Playing sounds through the PC speaker with Java

  • Thread starter TheOriginalThemePark
  • Start date
K

Knute Johnson

Roedy said:
see the sample code on my website
http://mindprod.com/jgloss/products1.html#SPEAKER

you do it in C or assembler and use JNI.

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 = (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 = (byte)(buf * 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);
}
}
 
D

David Segall

I've found this site that describes how you play sounds with the PC
speaker:

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?
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/>.
 
R

Roedy Green

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
 
T

TheOriginalThemePark

Well, this sounds perfect, if I can get it to work :D I'll certainly be
looking at it, but just to make sure I got it right. I access the
speaker by programming something in C and I then access that C program
from Java using JNI, is that correctly understood?
 
T

TheOriginalThemePark

One other thing. Since kernel32.dll in Windows contains a Beep method,
I would be able to use that in my Java program, right?
 
R

Roedy Green

Well, this sounds perfect, if I can get it to work :D I'll certainly be
looking at it, but just to make sure I got it right. I access the
speaker by programming something in C and I then access that C program
from Java using JNI, is that correctly understood?

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.
 
T

TheOriginalThemePark

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?
 
T

TheOriginalThemePark

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top