Discussion:
How to generate sound of given frequency?
(too old to reply)
Gonzo
2009-03-13 18:27:13 UTC
Permalink
I'm looking for something as simple as possible.

Thanks,
G
Knute Johnson
2009-03-13 18:39:24 UTC
Permalink
Post by Gonzo
I'm looking for something as simple as possible.
Thanks,
G
package com.knutejohnson.classes;

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.8);
}
}
--
Knute Johnson
email s/nospam/knute2009/

--
Posted via NewsDemon.com - Premium Uncensored Newsgroup Service
------->>>>>>http://www.NewsDemon.com<<<<<<------
Unlimited Access, Anonymous Accounts, Uncensored Broadband Access
Gonzo
2009-03-13 18:51:17 UTC
Permalink
Thanks! This will do!
G
r***@augustana.edu
2013-09-03 20:12:26 UTC
Permalink
Hello,

A little after the fact, but thank you! I'm wondering why you scaled sin(angular frequency) by vol and 127. I'm assuming this is because 127 corresponds to the maximum volume setting and vol is the desired amplitude. Also, why did you reshape the first and last 10ms of the wave?

Again thank you very much. I've spent some time trying to figure this out and your code was the first that I came across that was straight forward.

Sincerely,
Ryan
John B. Matthews
2013-09-04 02:01:12 UTC
Permalink
Post by r***@augustana.edu
A little after the fact, but thank you! I'm wondering why you scaled
sin(angular frequency) by vol and 127. I'm assuming this is because
127 corresponds to the maximum volume setting and vol is the desired
amplitude. Also, why did you reshape the first and last 10ms of the
wave?
Again thank you very much. I've spent some time trying to figure this
out and your code was the first that I came across that was straight
forward.
Recalling this thread [1] from 2009, you may also like to examine these
two related examples [2, 3]:

[1] <https://groups.google.com/d/topic/comp.lang.java.help/7vR_AWw1AwQ/discussion>
[2] <http://stackoverflow.com/a/7782749/230513>
[3] <http://stackoverflow.com/a/2065693/230513>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Knute Johnson
2013-09-04 04:38:01 UTC
Permalink
Post by r***@augustana.edu
Hello,
A little after the fact, but thank you! I'm wondering why you scaled
sin(angular frequency) by vol and 127. I'm assuming this is because
127 corresponds to the maximum volume setting and vol is the desired
amplitude. Also, why did you reshape the first and last 10ms of the
wave?
Again thank you very much. I've spent some time trying to figure this
out and your code was the first that I came across that was straight
forward.
Sincerely, Ryan
The audio format uses signed data one byte in length. A byte can hold
values from -128 to 127. The data is basically amplitude modulation, so
that a signal from -127 to 127 would be the loudest volume possible.
The shorter the period of time it takes to make the transition would
increase the frequency of the tone.

The shaping is to prevent clicks in the sound at the beginning and end
of the tone. I should really change the ramp to something more
logarithmic rather than sawtooth. The effect I was trying to achieve is
similar to what is done with continuous wave radio transmission.
--
Knute Johnson
Roedy Green
2013-09-09 08:46:42 UTC
Permalink
On Tue, 3 Sep 2013 13:12:26 -0700 (PDT), ***@augustana.edu

see http://mindprod.com/download/products.html#SOUND
for source code to synthetically generate sound.
--
Roedy Green Canadian Mind Products http://mindprod.com
Truth is much too complicated to allow anything but approximations.
~ John von Neumann (born: 1903-12-28 died: 1957-02-08 at age: 53)
Knute Johnson
2013-09-09 13:59:26 UTC
Permalink
Post by Roedy Green
see http://mindprod.com/download/products.html#SOUND
for source code to synthetically generate sound.
Bad link
--
Knute Johnson
John B. Matthews
2013-09-09 19:45:57 UTC
Permalink
Post by Knute Johnson
Post by Roedy Green
see http://mindprod.com/download/products.html#SOUND
for source code to synthetically generate sound.
Bad link
No cookie! :-)

<http://mindprod.com/jgloss/sound.html>
--
John B. Matthews
trashgod at gmail dot com
<http://sites.google.com/site/drjohnbmatthews>
Roedy Green
2013-10-22 03:36:51 UTC
Permalink
A little after the fact, but thank you! I'm wondering why you scaled sin(a=
ngular frequency) by vol and 127. I'm assuming this is because 127 corresp=
onds to the maximum volume setting and vol is the desired amplitude. Also,=
why did you reshape the first and last 10ms of the wave?
see http://mindprod.com/products.html#SOUND
--
Roedy Green Canadian Mind Products http://mindprod.com
Unlike many machines, computers require no water once they are
manufactured.
s***@gmail.com
2013-11-11 23:38:25 UTC
Permalink
But why there is a little pause between two consecutive beep ?
How can i remove this pause ?

Tone.sound(1078+2*78,1000,.6);

Tone.sound(1078+2*78,333,.6);
Tone.sound(1078+2*78,333,.6);
Tone.sound(1078+2*78,333,.0);

Tone.sound(1078+2*78,333,.6);
Tone.sound(1078+2*78,333,.6);
Tone.sound(1078+2*78,333,.0);

-How can I play this without any pause between beeps ?
Please help me out.
Roedy Green
2013-11-25 03:50:04 UTC
Permalink
Post by s***@gmail.com
But why there is a little pause between two consecutive beep ?
How can i remove this pause ?
Look an see if there is a syntax where you create some sound channel
object then reuse it.
--
Roedy Green Canadian Mind Products http://mindprod.com
Unlike many machines, computers require no water once they are
manufactured.
Roedy Green
2009-03-14 12:39:28 UTC
Permalink
Post by Gonzo
I'm looking for something as simple as possible.
see http://mindprod.com/jgloss/sound.html
--
Roedy Green Canadian Mind Products
http://mindprod.com

"Climate change is no longer a doomsday prophecy, itÂ’s a reality."
~ Astrid Heiberg president of the International Federation of Red Cross and Red Crescent Societies
Andrew Thompson
2009-03-16 00:13:07 UTC
Permalink
<http://forums.sun.com/thread.jspa?threadID=5243872>

--
Andrew T.
Loading...