Friday, October 31, 2014

Generate and Play a tone in Android

Generate and Play a tone in Android

Here is the code to generate and play the tone with frequency base in Android.

This will be playing for 3 second whenever you change the frequency rate by using seekbar it will be start play for 3 second.

PlaySound Activity


import android.app.Activity;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioTrack;
import android.os.Bundle;
import android.os.Handler;
import android.widget.SeekBar;
import android.widget.SeekBar.OnSeekBarChangeListener;
import android.widget.TextView;

public class PlaySound extends Activity{

    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 double freqOfTone = 500; // 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);
        
        genTone();
        
        final TextView tv = (TextView)findViewById(R.id.textview);
        tv.setText(""+freqOfTone);
        SeekBar seekbar = (SeekBar)findViewById(R.id.seekbar);
        seekbar.setProgress((int)freqOfTone);
        seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
   @Override
   public void onStopTrackingTouch(SeekBar seekBar) {
    freqOfTone = seekBar.getProgress();
    tv.setText(""+freqOfTone);
    genTone();
    handler.post(new Runnable() {

                    public void run() {
                        playSound();
                    }
                });
   }
   
   @Override
   public void onStartTrackingTouch(SeekBar seekBar) {
    
   }
   
   @Override
   public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
    // TODO Auto-generated method stub
    
   }
  });
        
        
    }

    @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() {
                
                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);

        }
    }

    AudioTrack audioTrack;
    void playSound(){
     if(audioTrack!=null){
      audioTrack.stop();
      audioTrack.release();
      audioTrack = null;
     }
     audioTrack = new AudioTrack(AudioManager.STREAM_MUSIC,
                sampleRate, AudioFormat.CHANNEL_OUT_MONO,
                AudioFormat.ENCODING_PCM_16BIT, generatedSnd.length,
                AudioTrack.MODE_STATIC);
        
        
        audioTrack.write(generatedSnd, 0, generatedSnd.length);
        audioTrack.play();
    }
}

Layout file which have TextView which display current frequency value and Seekbar to change the frequency.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <SeekBar
        android:layout_margin="10dp"
        android:id="@+id/seekbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:max="3000" />

    
    <TextView
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:id="@+id/textview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
</LinearLayout>

Happy to help you, Happy to code

No comments:

Post a Comment