Frequency/tone generator in java tia sal22

Discussion in 'Java' started by ratullloch_delthis, Nov 28, 2010.

  1. Greetings all
    I'm trying to find a tutorial that shows me how to make a Frequency/tone generator in java.
    Example I would like to have a slider that changes the value x for the formula y=sin(x)+cos(x) so if the slider is moved
    to 440 it would play a 440hz sin wave out the speaker does such a tutorial exist if so can someone post a link to it
    tia sal22

    PS: I'm using netbeans 6.9.1 if anyone wants to know
    ratullloch_delthis, Nov 28, 2010
    #1
    1. Advertising

  2. On 11/28/2010 12:33 PM, ratullloch_delthis wrote:
    > Greetings all
    > I'm trying to find a tutorial that shows me how to make a Frequency/tone generator in java.
    > Example I would like to have a slider that changes the value x for the formula y=sin(x)+cos(x) so if the slider is moved
    > to 440 it would play a 440hz sin wave out the speaker does such a tutorial exist if so can someone post a link to it
    > tia sal22
    >
    > PS: I'm using netbeans 6.9.1 if anyone wants to know
    >



    Here is a method I wrote to create a single tone.

    import java.util.*;
    import javax.sound.sampled.*;

    public class Tone {
    public static float SAMPLE_RATE = 8000f;

    public static void sound(int hz, int msecs, double vol)
    throws LineUnavailableException {

    if (hz <= 0)
    throw new IllegalArgumentException("Frequency <= 0 hz");

    if (msecs <= 0)
    throw new IllegalArgumentException("Duration <= 0 msecs");

    if (vol > 1.0 || vol < 0.0)
    throw new IllegalArgumentException("Volume out of range 0.0
    - 1.0");

    byte[] buf = new byte[(int)SAMPLE_RATE * msecs / 1000];

    for (int i=0; i<buf.length; i++) {
    double angle = i / (SAMPLE_RATE / hz) * 2.0 * Math.PI;
    buf = (byte)(Math.sin(angle) * 127.0 * vol);
    }

    // shape the front and back 10ms of the wave form
    for (int i=0; i < SAMPLE_RATE / 100.0 && i < buf.length / 2; i++) {
    buf = (byte)(buf * i / (SAMPLE_RATE / 100.0));
    buf[buf.length-1-i] =
    (byte)(buf[buf.length-1-i] * i / (SAMPLE_RATE / 100.0));
    }

    AudioFormat af = new AudioFormat(SAMPLE_RATE,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(800,1000,0.8);
    }
    }

    --

    Knute Johnson
    s/nospam/knute2010/
    Knute Johnson, Nov 28, 2010
    #2
    1. Advertising

  3. ratullloch_delthis

    Roedy Green Guest

    On Sun, 28 Nov 2010 20:33:32 GMT, ratullloch_delthis
    <> wrote, quoted or indirectly quoted
    someone who said :

    >I'm trying to find a tutorial that shows me how to make a Frequency/tone generator in java.
    >Example I would like to have a slider that changes the value x for the formula y=sin(x)+cos(x) so if the slider is moved
    >to 440 it would play a 440hz sin wave out the speaker does such a tutorial exist if so can someone post a link to it
    >tia sal22


    see sample code at http://mindprod.com/jgloss/sound.html
    --
    Roedy Green Canadian Mind Products
    http://mindprod.com

    In programming, and documenting programs, keep vocabulary consistent and precisely defined! Variation in vocabulary to relieve the tedium is for novels.
    Roedy Green, Dec 2, 2010
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Macky G
    Replies:
    3
    Views:
    1,139
    Macky G
    Jun 30, 2009
  2. Rick T
    Replies:
    0
    Views:
    268
    Rick T
    Jun 30, 2011
  3. x1
    Replies:
    9
    Views:
    305
    Rick DeNatale
    Oct 12, 2006
  4. PerlFAQ Server
    Replies:
    0
    Views:
    197
    PerlFAQ Server
    Feb 1, 2011
  5. PerlFAQ Server
    Replies:
    0
    Views:
    183
    PerlFAQ Server
    Mar 26, 2011
Loading...

Share This Page