I am acquiring an audio signal at 44.1k using the following code and need to understand the mechanics better. Just for reference, all the code works.
I am defining a block of 1024 that is being pulled from AudioRecord.read and being placed into a buffer. Then I do a hanning window and an FT (jfftpack), then use this data elsewhere (publishProgress).
Since my buffer is only 1024, and the min audio buffer is 4096, what happens to the remaining ~3000?
Does the *.read(buffer...) command read sequencially, ie 0-1024 on first use, 1025-2048 on second, etc.?
Basically I am wondering now how much information I am losing.
THanks for the help!
Below is code:------------ (It is modified versions of the jfft utilization in a frequency analyzer coupled with a hanning window, and then it uses a buffered writer to write all the data points to a log file.)
package com.example.frequencytest;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import jfftpack.javasource.ca.uol.aig.fftpack.RealDoubleFFT;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.media.MediaRecorder;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
int frequency = 44100;
int channelConfiguration = AudioFormat.CHANNEL_IN_MONO;
int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
private RealDoubleFFT transformer;
int blockSize = 1024;
public double movingAvg = 0.0;
public int movingAvgCnt = 0;
public BufferedWriter buf = null;
File tempFile = new File("sdcard/Data_Log.txt");
Button startStopButton;
boolean started = false;
RecordAudio recordTask;
TextView textView1;
ImageView imageView;
Bitmap bitmap;
Canvas canvas;
Paint paint;
//AudioRecord audioRecord;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startStopButton = (Button) this.findViewById(R.id.StartStopButton);
startStopButton.setOnClickListener(this);
transformer = new RealDoubleFFT(blockSize);
imageView = (ImageView) this.findViewById(R.id.ImageView01);
textView1 = (TextView) findViewById(R.id.textView1);
bitmap = Bitmap.createBitmap((int) 1024, (int) 300,
Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.GREEN);
paint.setStrokeWidth(2);
imageView.setImageBitmap(bitmap);
}
public class RecordAudio extends AsyncTask<Void, double[], Void> {
@Override
protected Void doInBackground(Void... arg0) {
try {
int bufferSize = AudioRecord.getMinBufferSize(frequency,
channelConfiguration, audioEncoding);
AudioRecord audioRecord = new AudioRecord(
MediaRecorder.AudioSource.MIC, frequency,
channelConfiguration, audioEncoding, bufferSize);
short[] buffer = new short[blockSize];
double[] toTransform = new double[blockSize];
audioRecord.startRecording();
// started = true; hopes this should true before calling
// following while loop
while (started) {
int bufferReadResult = audioRecord.read(buffer, 0,
blockSize);
for (int i = 0; i < blockSize && i < bufferReadResult; i++) {
toTransform[i] = (double) buffer[i] / 32768.0;
}
toTransform = HanningWindow(toTransform,0,blockSize);
transformer.ft(toTransform);
publishProgress(toTransform);
}
audioRecord.stop();
buf.close();
} catch (Throwable t) {
t.printStackTrace();
Log.e("AudioRecord", "Recording Failed");
}
return null;
}
@Override
protected void onProgressUpdate(double[]... toTransform) {
canvas.drawColor(Color.BLACK);
double average = 0.0;
int averageCnt=1;
for (int i = 0; i < toTransform[0].length; i++) {
int x = i;
int downy = (int) (100 - (toTransform[0][i] * 10));
int upy = 100;
if(i>2 && i<(toTransform[0].length/2)){
average += Math.sqrt(Math.abs(toTransform[0][i]));
averageCnt++;
}
canvas.drawLine(x, downy, x, upy, paint);
}
average = average/averageCnt;
movingAvg += average;
movingAvgCnt++;
if(movingAvgCnt==5){
movingAvg = movingAvg/movingAvgCnt;
textView1.setText(""+movingAvg);
try{
buf.append("" + average + ","+movingAvg);
buf.newLine();
}catch (IOException e)
{
e.printStackTrace();
}
movingAvg = 0.0;
movingAvgCnt = 0;
}
imageView.invalidate();
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void onClick(View arg0) {
// TODO Auto-generated method stub
if (started) {
started = false;
startStopButton.setText("Start");
recordTask.cancel(true);
} else {
started = true;
startStopButton.setText("Stop");
setupTempFile();
recordTask = new RecordAudio();
recordTask.execute();
}
}
public void setupTempFile(){
Log.d("Process", "startRecording");
if (tempFile.exists()){tempFile.delete();}
if (!tempFile.exists())
{
try
{
tempFile.createNewFile();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
try
{
//BufferedWriter for performance, true to set append to file flag
buf = new BufferedWriter(new FileWriter(tempFile, true));
buf.append("Data Val, Moving Average");
buf.newLine();
}
catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public short[] HanningWindow(short[] signal_in, int pos, int size)
{
for (int i = pos; i < pos + size; i++)
{
int j = i - pos; // j = index into Hann window function
signal_in[i] = (short) (signal_in[i] * 0.5 * (1.0 - Math.cos(2.0 * Math.PI * j / size)));
}
return signal_in;
}
public double[] HanningWindow(double[] signal_in, int pos, int size)
{
for (int i = pos; i < pos + size; i++)
{
int j = i - pos; // j = index into Hann window function
signal_in[i] = (double) (signal_in[i] * 0.5 * (1.0 - Math.cos(2.0 * Math.PI * j / size)));
}
return signal_in;
}
}
If it works similarly to socket reads, then you should be getting the full 0 to 1023 the first time, 1024 to 2047 the second, etc.
That being said - I am observing a bit of somewhat related strangeness myself. I have placed timing code around the read. I have minBuffer of 256 and buffer size of 256 running at 44.1KHz. It seems that sometimes the read function takes 0 time... Yet still returns 256 bytes worth of goodness.
01-10 23:37:07.270: E/WTF(15224): EllapsedTime 9 and readResult is 256 01-10 23:37:07.280: E/WTF(15224): EllapsedTime 0 and readResult is 256