gpt4 book ai didi

java - 关于音频采集的机制;安卓、Java

转载 作者:行者123 更新时间:2023-12-01 13:41:58 26 4
gpt4 key购买 nike

我正在使用以下代码获取 44.1k 的音频信号,并且需要更好地了解其机制。仅供引用,所有代码都有效。

我正在定义一个 1024 的 block ,该 block 从 AudioRecord.read 中拉出并放入缓冲区中。然后我创建一个 hanning 窗口和一个 FT (jfftpack),然后在其他地方使用这些数据 (publishProgress)。

由于我的缓冲区只有 1024,而最小音频缓冲区为 4096,那么剩余的 ~3000 会怎样?

*.read(buffer...) 命令是否按顺序读取,即第一次使用时读取 0-1024,第二次使用时读取 1025-2048,等等?

基本上我现在想知道我丢失了多少信息。

感谢您的帮助!

下面是代码:------------(它是频率分析仪中 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 到 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 为 25601-10 23:37:07.280: E/WTF(15224): EllapsedTime 0 且 readResult 为 256

关于java - 关于音频采集的机制;安卓、Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20663481/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com