• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Measuring a bullet speed android app by using of microphone

I get stumbled at the part when i need to measure the time between the first and second "bang" (first sound being the bullet fired and the second the sound made by hiting the target). Because after i get the time took the bullet to reach the target i can calculate the speed by the well known formula : speed = distance/time. Please, some help ! Thanks !

Code:
package com.example.alex.soundrecorder;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import static java.lang.Thread.interrupted;


public class MainActivity extends AppCompatActivity {

    Button resume, stop, record;
    MediaRecorder mRecorder;
    TextView mStatusView;
    final Handler mHandler = new Handler();
    Thread runner;

    final Runnable updater = new Runnable() {
        public void run() {
            updateTv();
        };
    };

//  Timer mTimer = new Timer();
//  FirstSoundThread firstThread = new FirstSoundThread("First_Thread");


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

        mStatusView = (TextView) findViewById(R.id.status);
        stop = (Button) findViewById(R.id.stop);
        record = (Button) findViewById(R.id.record);
        resume = (Button) findViewById(R.id.resume);

        stop.setEnabled(false);
        record.setEnabled(true);
        resume.setEnabled(false);

        record.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startRecorder();
                threadStart();
                record.setEnabled(false);
                stop.setEnabled(true);
                resume.setEnabled(false);
            }
        });

        stop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                stopRecorder();
                record.setEnabled(false);
                stop.setEnabled(false);
                resume.setEnabled(true);
            }
        });

        resume.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onResume();
                record.setEnabled(false);
                stop.setEnabled(true);
                resume.setEnabled(false);
            }
        });
    }

    public void startRecorder() {
        if (mRecorder == null) {
            mRecorder = new MediaRecorder();
            mRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
            mRecorder.setOutputFormat(MediaRecorder.OutputFormat.THREE_GPP);
            mRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AMR_NB);
            mRecorder.setOutputFile("/dev/null");

            try {
                mRecorder.prepare();
            } catch (java.io.IOException ioe) {
                android.util.Log.e("soundrecorder", "IOException: " +
                        android.util.Log.getStackTraceString(ioe));
            } catch (java.lang.SecurityException e) {
                android.util.Log.e("soundrecorder", "SecurityException: " +
                        android.util.Log.getStackTraceString(e));
            }
            try {
                mRecorder.start();
            } catch (java.lang.SecurityException e) {
                android.util.Log.e("soundrecorder", "SecurityException: " +
                        android.util.Log.getStackTraceString(e));
            }
        }
    }

    public void stopRecorder() {
        if (mRecorder != null) {
            mRecorder.stop();
            mRecorder.release();
            mRecorder = null;
        }
    }

    public void onResume() {
        super.onResume();
        startRecorder();
    }

    private long startT, endT, diffT;

    public void updateTv() {
        int dB = (int) soundDb(0.1);
        mStatusView.setText(Integer.toString((dB)) + " dB");

        //double elapsedTime = mTimer.getElapsedTime();
        //Log.d("soundrecorder", "Elapsed time is : " + elapsedTime);

        /*try {
            if(dB > 100 ) {
                startT = System.currentTimeMillis();
                //Thread.sleep(1);

                if(dB > 100) {

                    endT = System.currentTimeMillis();
                    //Thread.sleep(1);

                }
            }

            diffT = endT - startT;

            } catch (Exception e) {
                 System.err.println("error ..");
                }

        Log.d("soundrecorder", "Difference is : " + diffT);
        diffT = 0;*/

    }

    public double getAmplitude() {
        if (mRecorder != null) {
            return (mRecorder.getMaxAmplitude());
        } else return 0;
    }

    public double soundDb(double ampl) {
        return 20 * Math.log10(getAmplitude() / ampl);
    }

    public void threadStart() {
        if (runner == null) {
            runner = new Thread() {
                public void run() {
                    while (runner != null) {
                       // try {
                            //Thread.sleep(10);

                            int dB = (int) soundDb(0.1);

                            if(dB > 100 ) {
                                startT = System.currentTimeMillis();
                                //Thread.sleep(1);
                            }

                            if(dB > 100) {
                                endT = System.currentTimeMillis();
                                //Thread.sleep(1);
                            }

                            diffT = endT - startT;

                            Log.d("soundrecorder", "Difference is : " + diffT);

                            //Thread.sleep(1);
                           // } catch (InterruptedException e) {
                          //      System.out.println(e);
                          //  }
                        mHandler.post(updater);
                    }
                }
            };
            runner.start();
            Log.d("soundrecorder", "start runner()");
        } else {
            if (!interrupted()) {
                runner.interrupt();
            }
        }
    }

}
 
Last edited:
I can't suggest anything for the code (I'm primarily a C++ programmer with no knowledge of the Android SDK), but I can suggest something for your user manual: this is only going to work if you place the microphone equidistant between the gun and the target. Otherwise the difference in travel times for the two sounds will need to be corrected for in the calculation, and I don't see any correction for that in the code. Due to the speed of a bullet, if the mic is badly placed (i.e. by either the gun or the target) this effect will be larger than the time difference you are trying to measure.
 
Upvote 0
Upvote 0
What exactly is your question?

My question is : how to record the time between first sound and the second sound (sounds are suppose to be record when the decibel level pass beyond 100db). So i need that time in miliseconds to continue in calculating the bullet speed based on time the bullet "fly" and the distance witch is gonna be a constant.
 
Upvote 0
As @Hadron pointed out, results will be relative to the position of the phone, unless you factor in the speed of sound as well. For example. If the phone were being held next to the gun, the time it took for the sound of the initial shot to reach the phone would be almost instantaneous. And, if the target was 50 meters down range, then the sound of the impact would return as the speed of bullet in Meters/millisecond plus the time it took for the sound to return those 50 meters to the phone. If, OTOH, the phone were placed at the target, then the initial bang would be delayed by the distance the sound must travel to the phone. Fire a supersonic round (let's say a .44 magnum) and theoretically the sound from the gun would arrive after the sound of the impact with the target.
 
Upvote 0
As @Hadron pointed out, results will be relative to the position of the phone, unless you factor in the speed of sound as well. For example. If the phone were being held next to the gun, the time it took for the sound of the initial shot to reach the phone would be almost instantaneous. And, if the target was 50 meters down range, then the sound of the impact would return as the speed of bullet in Meters/millisecond plus the time it took for the sound to return those 50 meters to the phone. If, OTOH, the phone were placed at the target, then the initial bang would be delayed by the distance the sound must travel to the phone. Fire a supersonic round (let's say a .44 magnum) and theoretically the sound from the gun would arrive after the sound of the impact with the target.

I understand that but at this point my problem is that i am not been able to get that time to use it in further calculation . So the problem is mainly in my code so far, after i pass this problem i have to think about calibration and so on .. So i am asking for a method or something to put that TIME IN A VARIABLE .
 
Upvote 0
I still think you're using the wrong tool for the job. Basically you are wanting to do real-time spectrum analysis of a sound source, so you need to be reading a constant stream of data bytes, and determining the amplitude at each sample point. From what I've read, AudioRecord can do that.
I can't offer any in-depth technical advice on this, and would be in a similar position to you. But if I were doing this, I'd be looking resources related to spectrum analysis. A cursory search of the web reveals some projects on Github which are doing this, so you may be able to use something there.
Good luck!
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones