Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Playing sounds through the PC speaker with Java

Thread view: 
TheOriginalThemePark@hotmail.com - 12 Feb 2006 16:23 GMT
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 - 12 Feb 2006 16:48 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Knute Johnson - 12 Feb 2006 21:03 GMT
>> http://fly.cc.fer.hr/GDM/articles/sndmus/speaker1.html
>>
[quoted text clipped - 6 lines]
>
> 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);
    }
}

Signature

Knute Johnson
email s/nospam/knute/

Roedy Green - 13 Feb 2006 08:58 GMT
On Sun, 12 Feb 2006 13:03:44 -0800, Knute Johnson
<nospam@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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

TheOriginalThemePark@hotmail.com - 13 Feb 2006 14:18 GMT
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?
TheOriginalThemePark@hotmail.com - 13 Feb 2006 14:42 GMT
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 - 13 Feb 2006 15:56 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

TheOriginalThemePark@hotmail.com - 14 Feb 2006 13:45 GMT
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?
TheOriginalThemePark@hotmail.com - 14 Feb 2006 14:22 GMT
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
TheOriginalThemePark@hotmail.com - 14 Feb 2006 16:04 GMT
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 - 14 Feb 2006 18:54 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 14 Feb 2006 18:54 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 14 Feb 2006 18:49 GMT
>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Roedy Green - 13 Feb 2006 15:55 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

David Segall - 13 Feb 2006 05:28 GMT
>I've found this site that describes how you play sounds with the PC
>speaker:
[quoted text clipped - 4 lines]
>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/>.


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.