Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Playing sounds through the PC speaker with Java

53 views
Skip to first unread message

TheOrigina...@hotmail.com

unread,
Feb 12, 2006, 11:23:21 AM2/12/06
to
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?

Roedy Green

unread,
Feb 12, 2006, 11:48:14 AM2/12/06
to
On 12 Feb 2006 08:23:21 -0800, TheOrigina...@hotmail.com wrote,
quoted or indirectly quoted someone who said :

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

Knute Johnson

unread,
Feb 12, 2006, 4:03:44 PM2/12/06
to
Roedy Green wrote:
> On 12 Feb 2006 08:23:21 -0800, TheOrigina...@hotmail.com wrote,
> quoted or indirectly quoted someone who said :
>
>> 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.

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/

David Segall

unread,
Feb 13, 2006, 12:28:56 AM2/13/06
to
TheOrigina...@hotmail.com wrote:

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

Roedy Green

unread,
Feb 13, 2006, 3:58:58 AM2/13/06
to
On Sun, 12 Feb 2006 13:03:44 -0800, Knute Johnson
<nos...@ljr-2.frazmtn.com> wrote, quoted or indirectly quoted someone
who said :

>


>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

TheOrigina...@hotmail.com

unread,
Feb 13, 2006, 9:18:41 AM2/13/06
to
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?

TheOrigina...@hotmail.com

unread,
Feb 13, 2006, 9:42:09 AM2/13/06
to
One other thing. Since kernel32.dll in Windows contains a Beep method,
I would be able to use that in my Java program, right?

Roedy Green

unread,
Feb 13, 2006, 10:55:56 AM2/13/06
to
On 13 Feb 2006 06:18:41 -0800, TheOrigina...@hotmail.com wrote,

quoted or indirectly quoted someone who said :

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

Roedy Green

unread,
Feb 13, 2006, 10:56:27 AM2/13/06
to
On 13 Feb 2006 06:42:09 -0800, TheOrigina...@hotmail.com wrote,

quoted or indirectly quoted someone who said :

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

TheOrigina...@hotmail.com

unread,
Feb 14, 2006, 8:45:13 AM2/14/06
to
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?

TheOrigina...@hotmail.com

unread,
Feb 14, 2006, 9:22:00 AM2/14/06
to
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

TheOrigina...@hotmail.com

unread,
Feb 14, 2006, 11:04:49 AM2/14/06
to
Well, nevermind those questions, I solved them myself. I found a
diskette in a book, I've borrowed that should contain dos.h and it has
outport and inport which I can use. And windows.h, it seems, can be
downloaded here:

http://www.microsoft.com/downloads/details.aspx?FamilyId=A55B6B43-E24F-4EA3-A93E-40C0EC4F68E5&displaylang=en#filelist

Now my only remaining question is, whether or not there are commands in
windows.h like outport and inport from dos.h?

Roedy Green

unread,
Feb 14, 2006, 1:49:25 PM2/14/06
to
On 14 Feb 2006 05:45:13 -0800, TheOrigina...@hotmail.com wrote,

quoted or indirectly quoted someone who said :

>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

Roedy Green

unread,
Feb 14, 2006, 1:54:12 PM2/14/06
to
On 14 Feb 2006 06:22:00 -0800, TheOrigina...@hotmail.com wrote,

quoted or indirectly quoted someone who said :

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

Roedy Green

unread,
Feb 14, 2006, 1:54:55 PM2/14/06
to
On 14 Feb 2006 08:04:49 -0800, TheOrigina...@hotmail.com wrote,

quoted or indirectly quoted someone who said :

>http://www.microsoft.com/downloads/details.aspx?FamilyId=A55B6B43-E24F-4EA3-A93E-40C0EC4F68E5&displaylang=en#filelist


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

0 new messages