Android音頻可視化

來源:互聯網
上載者:User

相關連結:

Android 調用內建的錄製音頻程式
http://www.eoeandroid.com/thread-78949-1-1.html

 MediaRecoder實現音頻錄製並播放
http://www.eoeandroid.com/thread-237311-1-1.html

Android 音訊介紹
http://www.eoeandroid.com/thread-68377-1-1.html

--------------文章本文----------------

先看一下:

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();        } }

android音頻可視化的原理是使用離散傅裡葉變換,但是數學不好的同學不要擔心,有開源的java離散傅裡葉變換的代碼!!直接到www.netlib.org/fftpack/jfftpack.tgz,直接將裡面javasource目錄拖動到(ca目錄)src即可!!

 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.