Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Frequency/tone generator in java tia sal22

Reply
Thread Tools

Frequency/tone generator in java tia sal22

 
 
ratullloch_delthis
Guest
Posts: n/a
 
      11-28-2010
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

 
Reply With Quote
 
 
 
 
Knute Johnson
Guest
Posts: n/a
 
      11-28-2010
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[i] = (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[i] = (byte)(buf[i] * 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.;
}
}

--

Knute Johnson
s/nospam/knute2010/
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      12-02-2010
On Sun, 28 Nov 2010 20:33:32 GMT, ratullloch_delthis
<(E-Mail Removed)> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
controlling Python script with usb midi controller tia sal22 Rick T Python 0 06-30-2011 09:40 AM
Problems with email.Generator.Generator Chris Withers Python 20 09-12-2006 11:13 AM
UART with fractional baudrate generator ? Or fractional baudrate generator alone Martin Maurer VHDL 3 04-19-2006 01:26 PM
subtle side effect of generator/generator expression bonono@gmail.com Python 9 10-16-2005 06:42 PM
generator function within a generator function doesn't execute? TheDustbustr Python 1 07-25-2003 10:45 AM



Advertisments