NTNUJAVA Virtual Physics Laboratory
Enjoy the fun of physics with simulations!
Backup site http://enjoy.phy.ntnu.edu.tw/ntnujava/

JDK1.0.2 simulations (1996-2001) => Wave => Topic started by: Edleen on March 27, 2009, 06:24:39 am



Title: JAVA Sine Wave Ocsillator
Post by: Edleen on March 27, 2009, 06:24:39 am
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
http://jmusic.ci.qut.edu.au/
http://www.jsresources.org/examples/OscillatorPlayer.html

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!


Title: Re: JAVA Sine Wave Ocsillator
Post by: Fu-Kwun Hwang on March 27, 2009, 09:36:20 am
There are a lot of resources available from the above links you have provided.
I think the following link from jMusic tutorial page (http://jmusic.ci.qut.edu.au/jmtutorial/t1.html) might be useful to your project:
  Introduction to music for Java computer games (http://jmusic.ci.qut.edu.au/jmtutorial/BasicGameTour/BasicGameFrameset1.html)

According to the document: You should be able to create different notes(frequency)  at run time.


Title: Re: JAVA Sine Wave Ocsillator
Post by: Edleen on March 30, 2009, 05:35:20 am
Hello again :)

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 :)


Title: Re: JAVA Sine Wave Ocsillator
Post by: Fu-Kwun Hwang on March 30, 2009, 08:37:27 am
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 (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=17.0).
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).


Title: Re: JAVA Sine Wave Ocsillator
Post by: Edleen 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.


Title: Re: JAVA Sine Wave Ocsillator
Post by: Fu-Kwun Hwang on March 30, 2009, 11:37:52 pm
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);
}
}



Title: Re: JAVA Sine Wave Ocsillator
Post by: Fu-Kwun Hwang on April 02, 2009, 06:11:23 pm
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!


Title: Re: JAVA Sine Wave Ocsillator
Post by: gizmo64 on December 26, 2010, 01:41:55 am
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.*


Title: Re: JAVA Sine Wave Ocsillator
Post by: Fu-Kwun Hwang on December 26, 2010, 09:02:53 am
Please check out EJS version of Fourier Synthesis (You can hear the sound,too!) (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=1073.0) if you need to do Fourier analysis!


Title: Re: JAVA Sine Wave Ocsillator
Post by: thejmc 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


Title: Re: JAVA Sine Wave Ocsillator
Post by: balijani on April 12, 2012, 02:16:25 pm
Hello!  :)

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! :)


Title: Re: JAVA Sine Wave Ocsillator
Post by: Fu-Kwun Hwang on April 15, 2012, 08:35:04 pm
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! (http://www.phy.ntnu.edu.tw/ntnujava/index.php?topic=1073.0)


Title: Re: JAVA Sine Wave Ocsillator
Post by: balijani on April 17, 2012, 02:59:19 am
Thank you!

Instead of using only some methods of yours I used the whole class, so that I heard the sound continuously :) 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?



Title: Re: JAVA Sine Wave Ocsillator
Post by: Fu-Kwun Hwang on April 17, 2012, 06:04:16 pm
I do not really the answer.  ???
My guess is: it is due to the limit of the sound card (being used).
e.g. Audio Frequency Generator FREEWARE (http://www.ringbell.co.uk/software/audio.htm) 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? (http://superuser.com/questions/238278/what-are-usual-maximum-output-frequencies-on-computer-sound-cards)



Title: Re: JAVA Sine Wave Ocsillator
Post by: balijani on April 18, 2012, 03:11:02 am
Thank you!
I hope I'll come back with the answer and the solution  :D


Title: Re: JAVA Sine Wave Ocsillator
Post by: balijani on April 19, 2012, 02:30:17 am
I think I could find the problem  :)
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?  :)


Title: Re: JAVA Sine Wave Ocsillator
Post by: Fu-Kwun Hwang on April 19, 2012, 01:19:42 pm
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!


Title: Re: JAVA Sine Wave Ocsillator
Post by: balijani on April 19, 2012, 02:43:16 pm
Ah, nevermind!  :)
Thanks for everything, I really could not have made it this far without you and your thread! Thanks a lot! :)


Title: Re: JAVA Sine Wave Ocsillator
Post by: nunaridu on September 24, 2012, 03:21:49 pm
Las supersticiones de Mourinho camiseta barcelona (http://www.comprafutbol2012.com/)  para el Clásico


Title: Re: JAVA Sine Wave Ocsillator
Post by: ioven on October 22, 2012, 07:32:33 pm
"Sporting Intelligence" publica el ránking de las camisetas de camisetas de futbol (http://www.camisetasdefutbol2013.es/) 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 (http://www.camisetasdefutbol2013.es/camiseta-barcelona) (Nike) con 1.150.000 camisetas al año.


Title: JAVA Sine Wave Ocsillator
Post by: Femida on April 06, 2015, 10:02:04 pm
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??