Android 實現視覺化動態音訊柱狀圖
阿新 • • 發佈:2018-12-31
public class FFTActivity extends Activity implements OnClickListener{
private Button button;
private ImageView imageView;
private int frequency = 8000;
private int channelConfiguration = AudioFormat.CHANNEL_CONFIGURATION_MONO;
private int audioEncoding = AudioFormat.ENCODING_PCM_16BIT;
private RealDoubleFFT transformer;
private int blockSize = 256;
private boolean started = false;
private Canvas canvas;
private Paint paint;
private Bitmap bitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fft);
button = (Button) findViewById(R.id.fft_button);
button.setOnClickListener(this);
imageView = (ImageView) findViewById(R.id.fft_imageView);
transformer = new RealDoubleFFT(blockSize);
bitmap = Bitmap.createBitmap(256, 100, Bitmap.Config.ARGB_8888);
canvas = new Canvas(bitmap);
paint = new Paint();
paint.setColor(Color.GREEN);
imageView.setImageBitmap(bitmap);
}
private class RecordAudio extends AsyncTask<Void, double[], Void> {
@Override
protected Void doInBackground(Void... params) {
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();
while (started) {
//將record的資料 讀到buffer中,但是我認為叫做write可能會比較合適些。
int bufferResult = audioRecord.read(buffer, 0, blockSize);
for (int i = 0; i < bufferResult; i++) {
toTransform<i> = (double) buffer<i> / Short.MAX_VALUE;
}
transformer.ft(toTransform);
publishProgress(toTransform);
}
audioRecord.stop();
return null;
}
@Override
protected void onProgressUpdate(double[]... values) {
super.onProgressUpdate(values);
canvas.drawColor(Color.BLACK);
for (int i = 0; i < values[0].length; i++) {
int x=i;
int downy=(int)(100-(values[0]<i>)*10);
int upy=100;
canvas.drawLine(x, downy, x, upy, paint);
}
imageView.invalidate();
}
}
@Override
public void onClick(View v) {
started=true;
new RecordAudio().execute();
}
}