Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Thumbs up 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

  2. #2
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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.

  3. #3
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Default 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

  4. #4
    toadaly is offline Senior Member
    Join Date
    Jan 2009
    Posts
    671
    Rep Power
    6

    Default

    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.

  5. #5
    Join Date
    Feb 2009
    Posts
    4
    Rep Power
    0

    Thumbs up generate sound for 1 ear at a time in java with the pure tone to the other ear

    Hello. I want to generate noise to 1 ear(left) and the pure tone to the other ear(right).
    Can someone help me out.
    Thanks in advance.

    wall

Similar Threads

  1. add sound to my applet
    By anotsu in forum Java Applets
    Replies: 4
    Last Post: 03-25-2008, 12:26 AM
  2. uploading sound file in java
    By po0oker in forum Advanced Java
    Replies: 8
    Last Post: 11-04-2007, 12:00 AM
  3. Make sound play in a java application
    By lenny in forum AWT / Swing
    Replies: 2
    Last Post: 08-13-2007, 12:45 PM
  4. Sound in applet Java
    By toby in forum Java Applets
    Replies: 1
    Last Post: 08-07-2007, 06:52 AM
  5. Applets having sound
    By peiceonly in forum Java Applets
    Replies: 2
    Last Post: 03-31-2007, 10:55 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •