NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/
October 17, 2017, 08:19:40 pm *
Welcome, Guest. Please login or register.
Did you miss your activation email?

Login with username, password and session length
 
   Home   Help Search Login Register  
"Make priority, take action and start now." ...Wisdom
Google Bookmarks Yahoo My Web MSN Live Netscape Del.icio.us FURL Stumble Upon Delirious Ask FaceBook

Pages: [1]   Go Down
  Print  
Author Topic: JAVA Sine Wave Ocsillator  (Read 50934 times)
0 Members and 1 Guest are viewing this topic. Click to toggle author information(expand message area).
Edleen
Newbie
*
Offline Offline

Posts: 3

«
Embed this message
on: March 27, 2009, 06:24:39 am » posted from:Ã…lborg,Nordjylland,Denmark

Hello People

I wanted to know if it is possible to program a Sine Wave Oscsillator in JAVA. As far as I know it has already been done a few times before, and I have found examples here

But these examples are either not very well documented, or I just dont understand enough it.

I am working on a project at the university, where we are designing a framework to use SunSPOTS to send data from the built-in accelerometer to control frequencies by hand movement (the SunSPOTS being placed ont he user's hands) and thereby changing the fequency. But I need to start off somewhere, and that would be to actually make a JAVA Sine Wave Oscillator.

Any suggestions greatly appreciated!
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #1 on: March 27, 2009, 09:36:20 am » posted from:Taipei,T\'ai-pei,Taiwan

There are a lot of resources available from the above links you have provided.
I think the following link from jMusic tutorial page might be useful to your project:
  Introduction to music for Java computer games

According to the document: You should be able to create different notes(frequency)  at run time.
Logged
Edleen
Newbie
*
Offline Offline

Posts: 3

«
Embed this message
Reply #2 on: March 30, 2009, 05:35:20 am »

Hello again Smiley

I have also been looking at the jmusic page a lot, but it seems that they are working from a lot of classes that you need to download and install before you can use their examples, and for some reason I cannot get those installations to work on my computer. Do you know if there are any ways to make a similar oscillator using the javax.sound library? Or is that pretty much what they did in the other example I linked to?

Cheers Smiley
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #3 on: March 30, 2009, 08:37:27 am » posted from:Taipei,T\'ai-pei,Taiwan

May I know what do you mean by "make a similar oscillator "?
Do you want to generate sound output dynamically at run time?
I did something like that more than 10 years ago (before javax.sound was available), please check out Fourier Synthesis.
I learned it from some code available on the web at that time.
The source code for that applet I have created is available,too! But I did not work on similar topic for a long time.
Your links provide a lot of useful information. (I checked out those pagaes, but I did not have time to study or test it).
Logged
Edleen
Newbie
*
Offline Offline

Posts: 3

«
Embed this message
Reply #4 on: March 30, 2009, 07:02:02 pm »

The oscillator I want to make, should be able to change frequencies when the program is running.

Combined with the SunSPOTS, we want to use the built-in accelerometer to measure how far the users hand has traveled on the x-direction for example, and that number will be fed into the oscillator and then play the frequency of that number. So if the users movements totals a value of 560, then the program and the oscillator will play a continuous note at 560 Hz untill the user again moves the hand.
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #5 on: March 30, 2009, 11:37:52 pm » posted from:Taipei,T\'ai-pei,Taiwan

You might be able to create what you want with the following class:
Code:
import sun.audio.*;
class sinwaveSoundGenerator{
public int period,baseF; // base frequency
private byte record[];
private double Y[];
private static double UNIT=8000.;
private double omega;//=2.*Math.PI/period
private java.io.InputStream soundStream=null;
private boolean playstatus=false;
private double amplitude,y,amplitudeCST=1000.,A;//maxAmplitude=8160.;
sinwaveSoundGenerator(int _baseF,double _amplitude){
baseF=_baseF;
amplitude=_amplitude;
setBaseFrequency(baseF);
}
public void setBaseFrequency(int _baseF){ // input base frequency
period=(int)(UNIT/_baseF);
record=new byte[period];
Y=new double[period];
omega=2.*Math.PI/period;
setit();
}
public void setAmplitude(int _amplitude){
amplitude=_amplitude;
setit();
}
public void setit(){
if(playstatus)Stop();
calc();
if(playstatus)Play();
}

double[] yVal(){ return Y;}
double getBaseFrequency(){ return baseF;}
double getAmplitude(){return amplitude;}

public void Play(){
try{
if(soundStream!=null)AudioPlayer.player.stop(soundStream);
soundStream = new ContinuousAudioDataStream(new AudioData(record));
AudioPlayer.player.start(soundStream);
}catch(SecurityException e){
}
}
public void Stop(){
if(soundStream!=null)
try{
AudioPlayer.player.stop(soundStream);
soundStream=null;
}catch(SecurityException e){
}
}
public void calc() {
A=amplitude*amplitudeCST;
for (int i = 0; i < period; i++){
y=A*Math.sin(i*omega);
record[i] = int2ulaw((int)(y));
Y[i]=y;
}
}

public static byte int2ulaw(int ch) {
int mask;
if (ch < 0) {
ch = -ch;
mask = 0x7f;
}else mask = 0xff;

if (ch < 32) ch = 0xF0 | 15 - (ch/2);
else if (ch < 96) ch = 0xE0 | 15 - (ch-32)/4;
else if (ch < 224) ch = 0xD0 | 15 - (ch-96)/8;
else if (ch < 480) ch = 0xC0 | 15 - (ch-224)/16;
else if (ch < 992 ) ch = 0xB0 | 15 - (ch-480)/32;
else if (ch < 2016) ch = 0xA0 | 15 - (ch-992)/64;
else if (ch < 4064) ch = 0x90 | 15 - (ch-2016)/128;
else if (ch < 8160) ch = 0x80 | 15 - (ch-4064)/256;
else ch = 0x80;
return (byte)(mask & ch);
}
}

Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #6 on: April 02, 2009, 06:11:23 pm » posted from:Taipei,T\'ai-pei,Taiwan

Here is the EJS version of sinwave Oscillator (I add my own sound class into EJS).
You can change the frequency or amplitude of the sound with slider.
Click play to play the sound!

Embed a running copy of this simulation

Embed a running copy link(show simulation in a popuped window)
Full screen applet or Problem viewing java?Add http://www.phy.ntnu.edu.tw/ to exception site list
Press the Alt key and the left mouse button to drag the applet off the browser and onto the desktop. This work is licensed under a Creative Commons Attribution 2.5 Taiwan License
  • Please feel free to post your ideas about how to use the simulation for better teaching and learning.
  • Post questions to be asked to help students to think, to explore.
  • Upload worksheets as attached files to share with more users.
Let's work together. We can help more users understand physics conceptually and enjoy the fun of learning physics!
Logged
gizmo64
Newbie
*
Offline Offline

Posts: 1

«
Embed this message
Reply #7 on: December 26, 2010, 01:41:55 am » posted from:Horn� Dubov�,Trnava,Slovakia

1) Yes, but how to modify the code for accepting input float[] frequencies, and output int[] buffer which will contain compound wave from all the frequencies?

2) How to rewrite the code without the use of sun.audio.*
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #8 on: December 26, 2010, 09:02:53 am » posted from:Taipei,T'ai-pei,Taiwan

Please check out EJS version of Fourier Synthesis (You can hear the sound,too!) if you need to do Fourier analysis!
Logged
thejmc
Newbie
*
Offline Offline

Posts: 1

«
Embed this message
Reply #9 on: January 17, 2012, 10:51:26 am »

The jMusic music library referred to in this link has changed web address:

The jMusic web site is http://explodingart.com/jmusic
Logged
balijani
Newbie
*
Offline Offline

Posts: 6



«
Embed this message
Reply #10 on: April 12, 2012, 02:16:25 pm » posted from:Budapest,Budapest,Hungary

Hello!  Smiley

I saw this topic a while ago, when I tried to create my function generator from the scratch. Now I finally managed to output some noise on the PC soundcard output.. Thanks a lot, I surely couldn't have managed without this thread, as I found this description the best among the tutorials on the web in the topic. Yet I have a final issue that I can't really solve:
How can you ensure, that the audio buffer receives continuos stream? There's this cruicial part in the code: Stop(); calc(); Play(); with which the playback is highly fragmented.
I use a 2000 sample buffer, tried with 20.000 samples, same results, only the continuos parts were longer...
Any help would be highly appreciated! Smiley
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #11 on: April 15, 2012, 08:35:04 pm » posted from:Taipei,T'ai-pei,Taiwan

The code seems to be working fine.
You can check out another example at  EJS version of Fourier Synthesis (You can hear the sound,too!
Logged
balijani
Newbie
*
Offline Offline

Posts: 6



«
Embed this message
Reply #12 on: April 17, 2012, 02:59:19 am » posted from:Budapest,Budapest,Hungary

Thank you!

Instead of using only some methods of yours I used the whole class, so that I heard the sound continuously Smiley Great! ^^
Now I am just wondering why the physical limit of it is around 2660 Hz? Not sure of which of its properties limiting it at that certain value. Is it because of its digital resolution?
I tried to double "UNIT" and half the frequency, and I could raise the higher limit a tiny bit, but I couldn't succeed in the end.. I could see that in your example the top frequency is 1000Hz. Was it because of the same reason?

Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #13 on: April 17, 2012, 06:04:16 pm » posted from:Taipei,T'ai-pei,Taiwan

I do not really the answer.  Huh
My guess is: it is due to the limit of the sound card (being used).
e.g. Audio Frequency Generator FREEWARE can generate audio frequencies in the range 100 to 15000 Hz.
You can also check out What are usual maximum output frequencies on computer sound cards?

Logged
balijani
Newbie
*
Offline Offline

Posts: 6



«
Embed this message
Reply #14 on: April 18, 2012, 03:11:02 am » posted from:Budapest,Budapest,Hungary

Thank you!
I hope I'll come back with the answer and the solution  Cheesy
Logged
balijani
Newbie
*
Offline Offline

Posts: 6



«
Embed this message
Reply #15 on: April 19, 2012, 02:30:17 am » posted from:Budapest,Budapest,Hungary

I think I could find the problem  Smiley
As we know it, increasing a wave's frequency means decreasing in wavelength.  As lambda=1/frequency, when reaching 2kHz the wave's period shortens to the size of only 4 samples, which I think can not be considered as a sine wave anymore, but more like a triangular wave or so... This phenomenon occured, because of the small size of the sampling rate. You chose 8000 samples for sample rate.
I've been looking for the sun.audio & sun.audio.AudioPlayer APIs but could not find my way to change it to 44kHz.
May I ask you, why you chose 8000 samples? How did you know, that this should be the exact sample rate? Or what documentation have you followed when creating your code?  Smiley
Logged
Fu-Kwun Hwang
Administrator
Hero Member
*****
Offline Offline

Posts: 3080



WWW
«
Embed this message
Reply #16 on: April 19, 2012, 01:19:42 pm » posted from:Taipei,T'ai-pei,Taiwan

It has been more than 10 years when I worked on this code (with JDK1.0.2).
I might have check out some of the example released at that time (but I can not remember it now).
Sorry!
Logged
balijani
Newbie
*
Offline Offline

Posts: 6



«
Embed this message
Reply #17 on: April 19, 2012, 02:43:16 pm » posted from:Budapest,Budapest,Hungary

Ah, nevermind!  Smiley
Thanks for everything, I really could not have made it this far without you and your thread! Thanks a lot! Smiley
Logged
nunaridu
Newbie
*
Offline Offline

Posts: 1

«
Embed this message
Reply #18 on: September 24, 2012, 03:21:49 pm » posted from:Fuzhou,Fujian,China

Las supersticiones de Mourinho camiseta barcelona  para el Clásico
Logged
ioven
Newbie
*
Offline Offline

Posts: 1

«
Embed this message
Reply #19 on: October 22, 2012, 07:32:33 pm » posted from:Fuzhou,Fujian,China

"Sporting Intelligence" publica el ránking de las camisetas de camisetas de futbol fútbol más vendidas en los últimos cinco años. La lista está liderada por el Manchester United (Nike) y el Real Madrid (Adidas) que venden un promedio de 1.400.000 camisetas oficiales al año. En tercera posición se encuentra el Camiseta Barcelona (Nike) con 1.150.000 camisetas al año.
Logged
Femida
Newbie
*
Offline Offline

Posts: 0

«
Embed this message
Reply #20 on: April 06, 2015, 10:02:04 pm » posted from:,,Satellite Provider

which sine wave do i use when using a voltmeter to set my limiter for use with my ep2500 amp and two 3012lf loaded t39s?? thanks...
60 hz or 100  hz??
Logged
Pages: [1]   Go Up
  Print  
"Make priority, take action and start now." ...Wisdom
 
Jump to:  


Related Topics
Subject Started by Replies Views Last post
Rflection of a wave
Wave
koveras00 3 23955 Last post April 02, 2016, 07:13:53 pm
by steveervin
Trigonometric function: Sine and Cosine
misc
Fu-Kwun Hwang 0 15189 Last post February 18, 2008, 08:38:14 pm
by Fu-Kwun Hwang
How to add Java Source: Calc3.java into EJS ?
Questions related to EJS
lookang 4 7844 Last post January 08, 2009, 09:53:44 pm
by lookang
Longitudinal Wave Function Java Applet
Collaborative Community of EJS
lookang 5 12967 Last post January 31, 2009, 10:21:23 am
by lookang
Powered by MySQL Powered by PHP Powered by SMF 1.1.13 | SMF © 2006-2011, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Page created in 0.086 seconds with 22 queries.since 2011/06/15