麦克风输入处理 [英] Processing of Microphone Input

查看:178
本文介绍了麦克风输入处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从麦克风获取的音频数据。我已经使用了 AudioRecord 类,填充式短裤的缓冲区来实现这一点。

I am trying to get audio data from the microphone. I have achieved this by using the AudioRecord class which fills a buffer with type shorts.

最后,我想绘制该缓冲区,使我得到这样的显示(实时信息)的示波器。现在的问题是,如果我要显示的数值(比如文本),那么我需要一个不同的线程来更新用户界面。目前我使用这样的的AsyncTask 更新UI AsyncTasks.publishProgress()。到目前为止,我还没有非常成功的,并想知道如果我在正确的轨道?手柄是一个更好的方式去?有没有人在那里谁做了类似的事情之前,如果是这样的工作有什么方法吗?此外,有可能的话简单地轮询麦克风?

Eventually I would like to graph this buffer so that I get an oscilloscope like display (realtime information). The problem is that if I want to display a value (say in text) then I need a different thread to update the UI. Currently I'm doing this by using an AsyncTask and updating the UI with AsyncTasks.publishProgress(). So far I haven't been very successful and would like to know if I'm on the right track? Are handles a better way to go? Is there anyone out there who has done something similar before, and if so what method worked for you? Also, is it at all possible to simply poll the microphone?

下面是我的code。是指,以输出从MIC每读取样本。它出现在可接受的速率来做到这一点,但偶尔会显示一个零。为什么呢?

Here is my code. It is meant to output every read sample from the MIC. It appears to do this at an acceptable rate but occasionally displays a zero. Why?

package com.ss.audioacquireapp3;

import android.app.Activity;
import android.content.Context;
import android.media.AudioFormat;
import android.media.AudioRecord;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextView;


public class AudioAcquireApp3Activity extends Activity 
{
        //Properties (AsyncTask)
        protected TextView _percentField;
        protected InitTask _initTask;

        //Properties (MIC)
        public AudioRecord audioRecord; 
        public int mSamplesRead; //how many samples read 
        public int recordingState;
        public int buffersizebytes; 
        public int channelConfiguration = AudioFormat.CHANNEL_IN_MONO; 
        public int audioEncoding = AudioFormat.ENCODING_PCM_16BIT; 
        public static short[] buffer; //+-32767 
        public static final int SAMPPERSEC = 44100; //samp per sec 8000, 11025, 22050 44100 or 48000

    @Override
    public void onCreate( Bundle savedInstanceState ) 
    {
        super.onCreate(savedInstanceState);
        setContentView( R.layout.main );

        _percentField = ( TextView ) findViewById( R.id.percent_field );

        buffersizebytes = AudioRecord.getMinBufferSize(SAMPPERSEC,channelConfiguration,audioEncoding); //4096 on ion 
        buffer = new short[buffersizebytes]; 
        audioRecord = new AudioRecord(android.media.MediaRecorder.AudioSource.MIC,SAMPPERSEC,channelConfiguration,audioEncoding,buffersizebytes); //constructor 

        _initTask = new InitTask();
        _initTask.execute( this );
    }

    /**
     * sub-class of AsyncTask
     */
    protected class InitTask extends AsyncTask<Context, Integer, String>
    {
        // -- run intensive processes here
        // -- notice that the datatype of the first param in the class definition matches the param passed to this method 
        // -- and that the datatype of the last param in the class definition matches the return type of this method
                @Override
                protected String doInBackground( Context... params ) 
                {
                        //-- on every iteration
                        //-- runs a while loop that causes the thread to sleep for 50 milliseconds 
                        //-- publishes the progress - calls the onProgressUpdate handler defined below
                        //-- and increments the counter variable i by one
                        //int i = 0;

                    audioRecord.startRecording();

                        while( true ) 
                        {
                                try{
                                        mSamplesRead = audioRecord.read(buffer, 0, buffersizebytes);

                                        int amp;

                                        for(int i = 0; i < buffersizebytes - 1; i++){
                                            amp = (int)buffer[i];
                                            publishProgress( amp );
                                        }

                                } catch( Exception e ){                        
                                }
                        }
                }

                // -- gets called just before thread begins
                @Override
                protected void onPreExecute() 
                {
                        //Log.i( "makemachine", "onPreExecute()" );
                        super.onPreExecute();

                }

                // -- called from the publish progress 
                // -- notice that the datatype of the second param gets passed to this method
                @Override
                protected void onProgressUpdate(Integer... values) 
                {
                        super.onProgressUpdate(values);
                        //Log.i( "makemachine", "onProgressUpdate(): " +  String.valueOf( values[0] ) );
                        _percentField.setText( String.valueOf(values[0]) );
                }

                // -- called as soon as doInBackground method completes
                // -- notice that the third param gets passed to this method
                @Override
                protected void onPostExecute( String result ) 
                {
                        super.onPostExecute(result);
                        //Log.i( "makemachine", "onPostExecute(): " + result );
                }   


     } 
}

这里是main.xml中

And here is main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_vertical|center_horizontal"
    >

<TextView android:id="@+id/percent_field"
          android:layout_width="fill_parent" 
          android:layout_height="wrap_content"
          android:gravity="center_horizontal"/>

</LinearLayout>

请注意,你需要把它添加到AndroidManifest.xml中

Note that you need to add this to AndroidManifest.xml

<uses-permission android:name="android.permission.RECORD_AUDIO" />

我在LG擎天柱黑色运行此。请帮我做这个code尽可能高效。

I am running this on a LG Optimus Black. Please help me make this code as efficient as possible.

推荐答案

这是一个迟到的答案,但是,也许有人需要为同一个问题的答案。这是开源的Andr​​oid示波器(OsciPrime)链接( http://android.serverbox.ch/?p= 268 )。该人士$ ​​C $ C使用线程,而不是AsyncTask的。如果您看到源$ C ​​$ C,你能弄清楚如何线程处理与活套和处理程序的AudioRecord。我希望它是有用的人:)

It is a late answer but, maybe someone needs an answer for the same question. Here is Open Source Android Oscilloscope (OsciPrime) link (http://android.serverbox.ch/?p=268). The source code uses the Thread instead of AsyncTask. If you see the source code, you can figure out how Thread handles the AudioRecord with Looper and Handler. I hope it is useful to others :)

这篇关于麦克风输入处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆