我使用以下代码以44.1k获取音频信号,需要更好地理解机制。仅供参考,所有代码都可以使用。
我正在定义一个1024块,它从AudioRecord.read中拉出并放入缓冲区。然后我做了一个hanning窗口和一个FT(jfftpack),然后在别处使用这个数据(publishProgress)。
由于我的缓冲区仅为1024,而最小音频缓冲区为4096,剩余的~3000会发生什么?
* .read(buffer ...)命令是否按顺序读取,即首次使用时为0-1024,第二次使用为1025-2048等?
基本上我想知道我失去了多少信息。
谢谢你的帮助!
以下是代码:------------ (它是在频率分析器中与hanning窗口结合使用的jfft利用率的修改版本,然后它使用缓冲写入器将所有数据点写入日志文件。)
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;
}
}
答案 0 :(得分:1)
如果它与套接字读取的工作方式类似,那么第一次应该是0到1023,第二次是1024到2047,等等。
话虽如此 - 我自己也在观察一些有点相关的陌生感。我在读取时放置了时间码。我的minBuffer为256,缓冲区大小为256,运行频率为44.1KHz。似乎有时读取函数需要0次......但仍然会返回256字节的优点。
01-10 23:37:07.230: E/(15224): EllapsedTime 9 and readResult is 256
01-10 23:37:07.240: E/(15224): EllapsedTime 0 and readResult is 256
01-10 23:37:07.240: E/(15224): EllapsedTime 8 and readResult is 256
01-10 23:37:07.250: E/(15224): EllapsedTime 9 and readResult is 256
01-10 23:37:07.260: E/(15224): EllapsedTime 1 and readResult is 256
01-10 23:37:07.260: E/(15224): EllapsedTime 8 and readResult is 256
01-10 23:37:07.260: E/(15224): EllapsedTime 1 and readResult is 256
01-10 23:37:07.270:E / WTF(15224):EllapsedTime 9和readResult是256 01-10 23:37:07.280:E / WTF(15224):EllapsedTime 0和readResult是256