Edleen
Newbie
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
|
 |
«
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
Posts: 3
|
 |
«
Embed this message
Reply #2 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 
|
|
|
Logged
|
|
|
|
Fu-Kwun Hwang
|
 |
«
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
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
|
 |
«
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: 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
|
 |
«
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 listPress 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
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
|
 |
«
Embed this message
Reply #8 on: December 26, 2010, 09:02:53 am » posted from:Taipei,T'ai-pei,Taiwan |
|
|
|
|
Logged
|
|
|
|
thejmc
Newbie
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
Posts: 6
|
 |
«
Embed this message
Reply #10 on: April 12, 2012, 02:16:25 pm » posted from:Budapest,Budapest,Hungary |
|
 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! 
|
|
|
Logged
|
|
|
|
|
balijani
Newbie
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  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
|
|
|
|
|
balijani
Newbie
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 
|
|
|
Logged
|
|
|
|
balijani
Newbie
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  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? 
|
|
|
Logged
|
|
|
|
Fu-Kwun Hwang
|
 |
«
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
Posts: 6
|
 |
«
Embed this message
Reply #17 on: April 19, 2012, 02:43:16 pm » posted from:Budapest,Budapest,Hungary |
|
 Ah, nevermind!  Thanks for everything, I really could not have made it this far without you and your thread! Thanks a lot! 
|
|
|
Logged
|
|
|
|
nunaridu
Newbie
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
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
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
|
|
|
|
|