Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Generate A Particular Sound Frequency?

Tags:

android

I am developing an Android Application.I want to generate Sound of Frequency Ranges between 1KHz to 20KHz. is there anyway to generate sound at a specific Frequency.

like image 608
Lucifer Avatar asked Jan 02 '12 08:01

Lucifer


People also ask

How do you change the frequency of sound?

Visit the Export Settings window (File > Export > Adobe Media Encoder). In the Export Settings window, click on the Audio tab. In the Audio tab, there is a set of options called Basic Audio Settings. Click on the drop-down menu to the right of Frequency and change your audio sampling rate to 44.1 kHz.

Does sound have a particular frequency?

The units of frequency are called hertz (Hz). Humans with normal hearing can hear sounds between 20 Hz and 20,000 Hz. Frequencies above 20,000 Hz are known as ultrasound. When your dog tilts his head to listen to seemingly imaginary sounds, he is tuning in to ultrasonic frequencies, as high as 45,000 Hz.


1 Answers

I found this code from another SO post. from what I can tell it is still a little buggy but it should do the trick.

public class PlaySound extends Activity {
    // originally from http://marblemice.blogspot.com/2010/04/generate-and-play-tone-in-android.html
    // and modified by Steve Pomeroy <[email protected]>
    private final int duration = 3; // seconds
    private final int sampleRate = 8000;
    private final int numSamples = duration * sampleRate;
    private final double sample[] = new double[numSamples];
    private final double freqOfTone = 440; // hz

    private final byte generatedSnd[] = new byte[2 * numSamples];

    Handler handler = new Handler();

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    @Override
    protected void onResume() {
        super.onResume();

        // Use a new tread as this can take a while
        final Thread thread = new Thread(new Runnable() {
            public void run() {
                genTone();
                handler.post(new Runnable() {

                    public void run() {
                        playSound();
                    }
                });
            }
        });
        thread.start();
    }

    void genTone(){
        // fill out the array
        for (int i = 0; i < numSamples; ++i) {
            sample[i] = Math.sin(2 * Math.PI * i / (sampleRate/freqOfTone));
        }

        // convert to 16 bit pcm sound array
        // assumes the sample buffer is normalised.
        int idx = 0;
        for (final double dVal : sample) {
            // scale to maximum amplitude
            final short val = (short) ((dVal * 32767));
            // in 16 bit wav PCM, first byte is the low order byte
            generatedSnd[idx++] = (byte) (val & 0x00ff);
            generatedSnd[idx++] = (byte) ((val & 0xff00) >>> 8);

        }
    }

    void playSound(){
        final AudioTrack audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                sampleRate, AudioFormat.CHANNEL_CONFIGURATION_MONO,
                AudioFormat.ENCODING_PCM_16BIT, numSamples,
                AudioTrack.MODE_STATIC);
        audioTrack.write(generatedSnd, 0, generatedSnd.length);
        audioTrack.play();
    }
}
like image 92
Reid Avatar answered Oct 19 '22 19:10

Reid