for the geeks among us here is what the code looks like
package com.znidarsic_science_books.voicestaff;
import android.Manifest;
import android.content.pm.PackageManager;
import android.graphics.Color;
import android.media.AudioFormat;
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.media.ToneGenerator;
import android.os.Build;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;
import java.io.IOException;
import static android.os.SystemClock.sleep;
import static android.widget.Toast.LENGTH_LONG;
import static android.widget.Toast.makeText;
@RequiresApi(api = Build.VERSION_CODES.M)
public class VoiceStaff extends AppCompatActivity {
//Implement an instance of tone for testing
ToneGenerator toneG = new ToneGenerator(AudioManager.STREAM_ALARM, 1000);
//init an audio buffer size and init Audio
Recorder.......................................................
int frequency =0;
int SAMPLE_RATE =44100;
int bufferSize = 5*(AudioRecord.getMinBufferSize(SAMPLE_RATE,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT));
AudioRecord recordSample = new AudioRecord(MediaRecorder.AudioSource.MIC,
44100,
AudioFormat.CHANNEL_IN_MONO,
AudioFormat.ENCODING_PCM_16BIT,
bufferSize);
//init an audio buffer size and init Audio
Recorder........................................
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_voice_staff);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
//permission to use
microphone.............................................
int permission = ContextCompat.checkSelfPermission(this,
Manifest.permission.RECORD_AUDIO);
int permission1 = ContextCompat.checkSelfPermission(this,
Manifest.permission.WRITE_EXTERNAL_STORAGE);
if (permission != PackageManager.PERMISSION_GRANTED || permission1 !=
PackageManager.PERMISSION_GRANTED ) {
makeText(getApplicationContext(), "If 'Unfortunately the Parrot
Teacher has stopped' appears", LENGTH_LONG).show();
makeText(getApplicationContext(), "Go to the GEAR and set the
permissions manually", LENGTH_LONG).show();
makeRequest();
}
//end onCreate below ...........................
}
protected void makeRequest() {
//ask permission to rescord audio and use external memory
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.RECORD_AUDIO,
Manifest.permission.WRITE_EXTERNAL_STORAGE }, 101);
}
//end permissions...................
short[] audioBuffer = new short[bufferSize];
int step=0;
int number=0;
// Begin Countdown
Timer....................................................................
final CountDownTimer PulseMaker;
{
PulseMaker = new CountDownTimer(580000, 250) {
public void onTick(long millisUntilFinished) {
//Test point beeper
//toneG.startTone(ToneGenerator.TONE_CDMA_ALERT_CALL_GUARD,
100);
if(step==0) {
//start audio recorder
recordSample.startRecording();
step=step+1;
}
if(step==1){
number= recordSample.read(audioBuffer, 0, bufferSize);
step=step+1;
}
if(step==2){
// do frequency calculation
frequency = calculate(SAMPLE_RATE, audioBuffer);
step=step+1;
}
//the read function below is responsible for writing the audio
data in the byte array
if(step==3){
//Displayfrequency
TextView frequency1;
frequency1 = (TextView) findViewById(R.id.frequency);
frequency1.setTextColor(Color.BLUE);
frequency1.setText("Frequency = " + frequency);
step=step+1;
}
if(step==4)
{
step=step+1;
}
if(step==5) {
;
step=0;
}
}
public void onFinish() {
recordSample.release();
}
}.start();
}
//End Countdown Timer.......................................................
//calculate frequency of a
sound.............................................
public static int calculate(int sampleRate, short [] audioData) {
int numCrossing = 0;
int numSamples = audioData.length;
int filtering =15;
float amplitude = 0;
float amplitude1 = 0;
float MaxClamp = 500;
// start of number of crossings algorithm
....................................................
for (int p = 1; p < (numSamples -filtering -2); p = p + 1) {
//filtering alglorithim below.........................
for (int f = p; f < (p + filtering-1); f = f + 1) {
amplitude = (((float)audioData[f])/((float)filtering) + amplitude);
if (amplitude > MaxClamp){amplitude = MaxClamp;}
if (amplitude < -MaxClamp){amplitude = -MaxClamp;}
}
//end of filtering alglorithm.............................................
if ((amplitude < 0.0) && ((amplitude1 >= 0.0)) ){
numCrossing++;
}
amplitude1=amplitude;
}
float numSecondsRecorded = (float) numSamples / (float) sampleRate;
float frequency = ((numCrossing / numSecondsRecorded) - 1);
return (int) frequency;
}
//end calculate frequency of a
sound.............................................
//Begin pop up
Menu...............................................................
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_voice_staff, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}