[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 471
  • Last Modified:

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
0
dspector
Asked:
dspector
  • 3
  • 2
1 Solution
 
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
 
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

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now