How to play sine waves in applet

I want to write a game applet that plays complex series of tones and accepts keyboard input. Unfortunately, I have no experience with Java applets other than the basic mechanics of using javac, jar, awt, and <applet>.

My program needs to play sine waves of various frequencies and amplitudes. It needs to play these tones overlapping (one tone may continue while others start; each tone has an independent lifetime).

I've found some examples on the Web but am not satisfied with them and so I'm hoping that someone out there can give me a few samples of code to get me started. I don't want to use the very high level facilities such as JavaSound or MIDI. Rather, I want to calculate sine waves "on the fly" and play them from memory.

I'll give more specifications, if necessary, in response to your questions.

David
LVL 1
dspectorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

esorfCommented:
The only way to play sounds in an Applet is to use the Applet.getAudioClip() method and then play the returned audio clip.  

Unfortunately, getAudioClip() takes a URL as a parameter.  So it expects the audio to exist as a file on a server, in AU format no less.

You could make a CGI process that can return the appropriate .au file by passing the sine wave parameters in the URL.

Or you can use the Java  Sound API.  I know you don't want to, but it provides the API to do exactly what you want through the SourceDataLine class.  http://java.sun.com/products/java-media/sound/doc-sampled.html

Cheers,

- Frank
0
dspectorAuthor Commented:
Thanks. I hadn't realized that Java Sound was the only way to go. I am willing to use it.

Now, can someone give me a simple working applet that plays a sine wave and accepts keystrokes?

Thanks,
David
0
esorfCommented:
Check this out for half of the problem.  It's a command line app that plays waveforms generated on the fly.

http://www.jsresources.org/examples/OscillatorPlayer.html

It should be straightforward to reuse the Oscillator class in your app.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dspectorAuthor Commented:
I'm not completely happy with the answer, since the example must be heavily modified to meet my needs and since the keyboard part of the question was not answered at all. But at least the answer was free and was somewhat helpful; I thank the submitter for their time and help.

David
0
esorfCommented:
Thanks for the points.  I don't want to leave you unsatisfied...

You could use a simple command loop:

InputStream in = System.in;
boolean done = false;

while (!done) {
if ( in.available() > 0 ) {
    int input = in.read();
    switch (input) {
case 'a': break;
case 'b': break;
case 'q': done = true; break;
default: System.out.println("Unknown choice");
    }  
}
else {
    Thread.currentThread.Sleep(250);
}
}

You may have to hit "Enter" after each keystroke to make it come visible to this code.  I don't have a build environment here to check.

Thanks for the points and good luck with your app.

- Frank
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.