Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: How to generate a frequency?

Is there a nice way in Android to generate and then play a frequency (eg. 1000hz, 245hz)?

like image 919
Flynn Avatar asked Feb 02 '12 02:02

Flynn


People also ask

What is frequency generator app?

Frequency Sound Generator is a simple wave form sound generator and oscillator. It is easy to use tool so you can create high variety of sounds and signals in just few seconds. All controls are in real time so you can dynamically change the sound signals.

How do you use the frequency generator app?

To use Frequency Generator, you could swipe your finger up and down to raise or lower the frequency of the tones. The user interface is very simple and clear. You could adjust the frequency up or down exactly in 1Hz with arrow button, and directly check the frequency by professional wave visualisation.

What is a signal generator app?

Signal Generator is an app that produces audio test tones. The basic app produces sine waves, and it can be extended and customized (via in-app purchases) to produce white noise, pink noise, frequency sweeps, and (on iPhone 4 and better) square waves, sawtooth waves, and triangle waves.

Is there a sound frequency app?

Spectrum analyser gives insight into your sounds and deconstructs the audio spectrum, showing the levels of the various frequencies in your audio signal. The built-in sound meter provides high-quality measurement results when measuring ambient noise levels in a multitude of real-life scenarios.


1 Answers

Take a look at android.media.ToneGenerator.

You can also try following Code.

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 102
Lucifer Avatar answered Oct 20 '22 17:10

Lucifer