Results 1 to 5 of 5
Thread: Calibration of Sound in Java
- 02-03-2009, 06:10 AM #1
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
Calibration of Sound in Java
Hello frenz.. I have written a code for creation of sound in java.. But now i have been stuck in the calibration of the sound generated. Can somebody please guide me how to calibrate the sound. I want the sound to vary for different sound intensity and frequency. If somebody has the code plz send.
Thanks in advance.
satish
- 02-04-2009, 06:54 AM #2
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
If you're creating the sound, and it isn't the frequency or amplitude you thought it should be, then your code has bugs.
There is no need to calibrate it if you are generating it, as long as you generate it properly.
- 02-04-2009, 07:08 AM #3
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
Calibration of Sound in Java
Thanks for the reply. Can you plz gimme a sample code for generating sound at different frequency and intensity.
Thanks in advance.
wall
- 02-04-2009, 07:50 AM #4
Senior Member
- Join Date
- Jan 2009
- Posts
- 671
- Rep Power
- 5
The easiest way is just to use the midi api (javax.sound.midi). But you can also directly create sampled sound with the sampled api (javax.sound.sampled). The javax sound api is complex, and there are good tutorials on it online. But as a general overview,
Decide upon a sample rate first, let's say you want high quality sound, so you sample at 44 kHz.
float sampleRate = 44000.0f;
create an AudioFormat object, and then from it, create an AudioInputStream by passing it an InputStream. I suggest using a PipedInputStream that you've connected to a PipedOutputStream. This will allow you to populate the AudioInputStream by writing (which is what you want if you are directly creating the sound yourself). You will need to make sure to write to the pipe in it's own thread to prevent deadlock.
Next, create an AudioFileWriter (assuming you want to write your sound to say an mp3 file) using the AudioInputStream from above, and start writing data to your PipedOutputStream.
That's the mechanics of creating a sound file from sampled data. To create a 1 second long pure tone at 500 Hz under these conditions (and assuming you selected a bit depth of 32 for your audio format to support float data...)
// float audio data is full scale at +- 1. We'll use half volume
float amplitude = 0.5;
float frquency = 500;
byte[] bytes = new byte[44000*4];
java.nio.ByteBuffer buf = java.nio.ByteBuffer.wrap(bytes);
buf.rewind();
for(int i=0; i<44000; i++)
buf.writeFloat(amplitude * Math.sin(2*Math.PI*frequency*i/sampleRate));
- Now write 'bytes' to your PipedOutput.
It all seems needlessly complex, but it's easier than trying to manually encode an mp3.
- 02-12-2009, 01:44 PM #5
Member
- Join Date
- Feb 2009
- Posts
- 4
- Rep Power
- 0
Similar Threads
-
add sound to my applet
By anotsu in forum Java AppletsReplies: 4Last Post: 03-24-2008, 11:26 PM -
uploading sound file in java
By po0oker in forum Advanced JavaReplies: 8Last Post: 11-03-2007, 11:00 PM -
Make sound play in a java application
By lenny in forum AWT / SwingReplies: 2Last Post: 08-13-2007, 11:45 AM -
Sound in applet Java
By toby in forum Java AppletsReplies: 1Last Post: 08-07-2007, 05:52 AM -
Applets having sound
By peiceonly in forum Java AppletsReplies: 2Last Post: 03-31-2007, 09:55 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks