making noises with Java

K

kvnsmnsn

Java is pretty much standardized when it comes to what you see on the
computer display. What about what you hear? Are there Java classes
for making noises? I'd like to build a Java application that beeps at
different pitches, depending on what the user does. How do I figure
out how to do that?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
M

Malte

Java is pretty much standardized when it comes to what you see on the
computer display. What about what you hear? Are there Java classes
for making noises? I'd like to build a Java application that beeps at
different pitches, depending on what the user does. How do I figure
out how to do that?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
Even the laziest action, like looking at
http://java.sun.com/j2se/1.4.2/docs/api/
would have led you to java.sound.* packages.
 
R

Roland

Java is pretty much standardized when it comes to what you see on the
computer display. What about what you hear? Are there Java classes
for making noises? I'd like to build a Java application that beeps at
different pitches, depending on what the user does. How do I figure
out how to do that?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
<http://java.sun.com/products/java-media/sound/samples/JavaSoundDemo/>

--
Regards,

Roland de Ruiter
___ ___
/__/ w_/ /__/
/ \ /_/ / \
 
K

kvnsmnsn

Malte wrote:

=> Java is pretty much standardized when it comes to what you see on
the
=> computer display. What about what you hear? Are there Java classes
=> for making noises? I'd like to build a Java application that beeps
at
=> different pitches, depending on what the user does. How do I figure
=> out how to do that?
..........<snip>..........
=Even the laziest action, like looking at
=http://java.sun.com/j2se/1.4.2/docs/api/
=would have led you to java.sound.* packages.

And Roland wrote:

=> Java is pretty much standardized when it comes to what you see on
the
=> computer display. What about what you hear? Are there Java classes
=> for making noises? I'd like to build a Java application that beeps
at
=> different pitches, depending on what the user does. How do I figure
=> out how to do that?
..........<snip>..........
=<http://java.sun.com/products/java-media/sound/samples/JavaSoundDemo/>

I took a look at both of these. They both seem to assume I have some
sound in an audio file I want to play. All I want is to be able to
give some beeps in a specific tone to go with my application. Does
anybody know how to do that? Is there some place in the API that des-
cribes that?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_
 
K

Knute Johnson

Malte wrote:

=> Java is pretty much standardized when it comes to what you see on
the
=> computer display. What about what you hear? Are there Java classes
=> for making noises? I'd like to build a Java application that beeps
at
=> different pitches, depending on what the user does. How do I figure
=> out how to do that?
..........<snip>..........
=Even the laziest action, like looking at
=http://java.sun.com/j2se/1.4.2/docs/api/
=would have led you to java.sound.* packages.

And Roland wrote:

=> Java is pretty much standardized when it comes to what you see on
the
=> computer display. What about what you hear? Are there Java classes
=> for making noises? I'd like to build a Java application that beeps
at
=> different pitches, depending on what the user does. How do I figure
=> out how to do that?
..........<snip>..........
=<http://java.sun.com/products/java-media/sound/samples/JavaSoundDemo/>

I took a look at both of these. They both seem to assume I have some
sound in an audio file I want to play. All I want is to be able to
give some beeps in a specific tone to go with my application. Does
anybody know how to do that? Is there some place in the API that des-
cribes that?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

If you want to make sounds you will need the JavaSound API. That being
said, here is a simple static method to make a beep at a given frequency
and duration. It uses an 8 bit audio format so you probably can't get a
tone higher than about 3000hz.

import javax.sound.sampled.*;

public class Tone {
public static void sound(int hz, int msecs)
throws LineUnavailableException {
byte[] buf = new byte[1];

AudioFormat af = new AudioFormat(8000f,8,1,true,false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
for (int i=0; i<msecs*8; i++) {
double angle = i / (8000.0 / hz) * 2.0 * Math.PI;
buf[0] = (byte)(Math.sin(angle) * 110.0);
sdl.write(buf,0,1);
}
sdl.drain();
sdl.stop();
sdl.close();
}

public static void main(String[] args) {
try {
Tone.sound(1000,100);
} catch (LineUnavailableException lue) {
System.out.println(lue);
}
}
}
 
B

Bill Tschumy

I took a look at both of these. They both seem to assume I have some
sound in an audio file I want to play. All I want is to be able to
give some beeps in a specific tone to go with my application. Does
anybody know how to do that? Is there some place in the API that des-
cribes that?

---Kevin Simonson

"You'll never get to heaven, or even to LA,
if you don't believe there's a way."
from _Why Not_

If you don't soundfiles to play, the only other alternative is

Toolkit.getDefaultToolkit().beep();

There are no different pitches however.
 
C

Chris Smith

Knute Johnson said:
AudioFormat af = new AudioFormat(8000f,8,1,true,false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
for (int i=0; i<msecs*8; i++) {
double angle = i / (8000.0 / hz) * 2.0 * Math.PI;
buf[0] = (byte)(Math.sin(angle) * 110.0);
sdl.write(buf,0,1);
}
sdl.drain();
sdl.stop();
sdl.close();

Note that this generates a pure sine wave. For a variety of reasons
having to do with physics of sound, most people *really* dislike hearing
pure sine waves of any substantial volume. You might consider a
triangle wave instead (and the math is easier, too!)

--
www.designacourse.com
The Easiest Way To Train Anyone... Anywhere.

Chris Smith - Lead Software Developer/Technical Trainer
MindIQ Corporation
 
K

Knute Johnson

Chris said:
Knute Johnson said:
AudioFormat af = new AudioFormat(8000f,8,1,true,false);
SourceDataLine sdl = AudioSystem.getSourceDataLine(af);
sdl.open(af);
sdl.start();
for (int i=0; i<msecs*8; i++) {
double angle = i / (8000.0 / hz) * 2.0 * Math.PI;
buf[0] = (byte)(Math.sin(angle) * 110.0);
sdl.write(buf,0,1);
}
sdl.drain();
sdl.stop();
sdl.close();


Note that this generates a pure sine wave. For a variety of reasons
having to do with physics of sound, most people *really* dislike hearing
pure sine waves of any substantial volume. You might consider a
triangle wave instead (and the math is easier, too!)

Chris:

I'll try the triangle wave that could be interesting. You will notice
however that I don't use max volume but instead reduced it so that it
sounds really good on my sound card. If it is still buzzy reduce the
volume some more and it should clean up.
 

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,995
Messages
2,570,225
Members
46,815
Latest member
treekmostly22

Latest Threads

Top