Results 1 to 2 of 2

Thread: Making Sounds

  1. #1
    Join Date
    Mar 2011
    Posts
    93
    Rep Power
    0

    Default Making Sounds

    Hello. I want to write a program that will generate two audio tones - one high-pitched and one low-pitched - and randomly chose between them and play them repeatedly with a random delay between sounds.

    Here is a quickie code I modified that plays one tone with a random delay, using class Toolkit().beep. But what if I want two different tones? I know the java API has a class Synthesizer() and class SamplePlayer(), tho' I don't think I need all the functionality of a synthesizer or a sample-player.

    Can anyone suggest other ways to get this done? Thanks.

    Java Code:
    import java.awt.*;
    import java.io.*;
    
    public class timer_beep {
    	public static void main(String[] args) throws IOException {
    		
    		try {
    			System.out.println("Countdown has started!");
    			Thread.sleep(2000);
    			for (int x=10; x>=0; x-- ) {
    				System.out.println(x);
    				Toolkit.getDefaultToolkit().beep();
    				Thread.sleep((long) (Math.random() * 3000));
    			}
    			Thread.sleep(2000);
    			Toolkit.getDefaultToolkit().beep();
    			System.out.println("Beep beep testing");
    		}
    		catch(InterruptedException e){ }
    	}
    }

  2. #2
    DarrylBurke's Avatar
    DarrylBurke is offline Member
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,238
    Rep Power
    19

    Default Re: Making Sounds

    Quote Originally Posted by Humphrey Bogart View Post
    I know the java API has a class Synthesizer() and class SamplePlayer(), tho' I don't think I need all the functionality of a synthesizer or a sample-player.

    Can anyone suggest other ways to get this done?
    It's no great effort to use Synthesizer, but be aware that the soundbank.gm file distributed with the JDK isn't included with a JRE, so if you need to run your program on a computer which doesn't have a JDK installed you'll need to copy the .../java/jdkXXX/jre/lib/audio folder to .../java/jreN/lib (folders may be different on Linux/MacOS).

    An alternative is to obtain a SourceDataLine and write an appropriate byte array to produce the desired tone and duration. Never done that, but I do know it's doable, just a lot more work than simply using a Synthesizer.

    db
    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

Similar Threads

  1. Playing sounds on other computers
    By jeffpaulwilson in forum AWT / Swing
    Replies: 0
    Last Post: 09-14-2011, 09:25 PM
  2. Sounds?
    By Alerhau in forum New To Java
    Replies: 3
    Last Post: 08-29-2011, 07:30 PM
  3. How can I loop sounds in JPanel?
    By rajkobie in forum New To Java
    Replies: 4
    Last Post: 04-24-2011, 03:49 PM
  4. Wave Sounds
    By Doctor Cactus in forum New To Java
    Replies: 2
    Last Post: 10-22-2008, 01:44 PM
  5. Turn off sounds in Eclipse
    By gio.fou in forum Eclipse
    Replies: 2
    Last Post: 09-06-2008, 08:31 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •