- 浏览: 1515548 次
- 性别:
- 来自: 南京
-
文章分类
- 全部博客 (419)
- XMPP (19)
- Android (180)
- Java (59)
- Network (4)
- HTML5 (13)
- Eclipse (9)
- SCM (23)
- C/C++ (4)
- UML (4)
- Libjingle (15)
- Tools&Softwares (29)
- Linphone (5)
- Linux&UNIX (6)
- Windows (18)
- Google (10)
- MISC (3)
- SIP (6)
- SQLite (5)
- Security (4)
- Opensource (29)
- Online (2)
- 文章 (3)
- MemoryLeak (10)
- Decompile (5)
- Ruby (1)
- Image (1)
- Bat (4)
- TTS&ASR (28)
- Multimedia (1)
- iOS (20)
- Asciiflow - ASCII Flow Diagram Tool.htm (1)
- Networking (1)
- DLNA&UPnP (2)
- Chrome (2)
- CI (1)
- SmartHome (0)
- CloudComputing (1)
- NodeJS (3)
- MachineLearning (2)
最新评论
-
bzhao:
点赞123!
Windows的adb shell中使用vi不乱码方法及AdbPutty -
wahahachuang8:
我觉得这种东西自己开发太麻烦了,就别自己捣鼓了,找个第三方,方 ...
HTML5 WebSocket 技术介绍 -
obehavior:
view.setOnTouchListenerview是什么
[转]android 一直在最前面的浮动窗口效果 -
wutenghua:
[转]android 一直在最前面的浮动窗口效果 -
zee3.lin:
Sorry~~
When I build "call ...
Step by Step about How to Build libjingle 0.4
Here it is, my fist JAVA application for Android. This is very simple application which introduce how you can get sound from microphone of your phone analyze it and record it on your phones SD card. What new I learn writing this application
- Initializing AudioRecorder
- Analyzing every byte of audio buffer
- Create .wav file from existing audio buffer.
Below you can see whole code of my application. As this is mt first JAVA application for Android maybe it contains some mistakes, but I promise to modify this version.
package com.vproject.voicedetection; import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import android.app.Activity; import android.media.AudioFormat; import android.media.AudioRecord; import android.media.MediaRecorder; import android.os.Bundle; import android.os.Environment; import android.os.Handler; import android.os.Message; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; import android.widget.TextView; public class VoiceDetectionActivity extends Activity { // Constant members. private static final int SAMPLE_RATE_IN_HZ = 8000; private static final int RECORDER_BPP = 16; private static final int CHANNEL_CONFIG = AudioFormat.CHANNEL_IN_MONO; private static final int AUDIO_FORMAT = AudioFormat.ENCODING_PCM_16BIT; private static final int AUDIO_SOURCE = MediaRecorder.AudioSource.MIC; private static final String TAG = "VoiceDetection"; private static final int MAX_VOL = 600; private static final int MIN_VAL = 0; private static final int START_RECORD_FROM = 350; private static final int CHECK_BLOCK_COUNT = 5; private Thread onCreateThread = null; private AudioRecord audioRecorder = null; private int minBufferSizeInBytes = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView textView = (TextView)findViewById(R.id.textView3); Button button = (Button)findViewById(R.id.button1); button.setVisibility( android.view.View.INVISIBLE ); textView.setVisibility( android.view.View.VISIBLE ); Start(); } public void Start( ) { // Create Handler final Handler handler = new Handler() { @Override public void handleMessage(Message msg) { int value = msg.what; ImageView picture = (ImageView) findViewById(R.id.imageView); int resID[] = { R.drawable.image0001, R.drawable.image20, R.drawable.image40, R.drawable.image60, R.drawable.image80, R.drawable.image100 }; for( int i=MIN_VAL, step = (MAX_VOL - MIN_VAL)/resID.length; i<resID.length; i++ ) { if( value >= i*step && value <= i*step + step ) picture.setImageResource( resID[i] ); } // Set image for maximum value. if( value >= MAX_VOL ) picture.setImageResource( R.drawable.image100 ); } }; // Text change handler final Handler changeTexthandler = new Handler() { @Override public void handleMessage(Message msg) { TextView textView = (TextView)findViewById(R.id.textView3); Button button = (Button)findViewById(R.id.button1); switch( msg.what ) { case 0: textView.setText("Waiting"); button.setVisibility( android.view.View.INVISIBLE ); textView.setVisibility( android.view.View.VISIBLE ); break; case 1: textView.setText("Recording"); button.setVisibility( android.view.View.INVISIBLE ); textView.setVisibility( android.view.View.VISIBLE ); break; default: button.setVisibility( android.view.View.VISIBLE ); textView.setVisibility( android.view.View.INVISIBLE ); break; } } }; // Initialize minimum buffer size in bytes. minBufferSizeInBytes = AudioRecord.getMinBufferSize( SAMPLE_RATE_IN_HZ, CHANNEL_CONFIG, AUDIO_FORMAT ); if( minBufferSizeInBytes == AudioRecord.ERROR_BAD_VALUE ) Log.e( TAG, "Bad Value for \"minBufferSize\", recording parameters are not supported by the hardware" ); if( minBufferSizeInBytes == AudioRecord.ERROR ) Log.e( TAG, "Bad Value for \"minBufferSize\", implementation was unable to query the hardware for its output properties" ); // Initialize Audio Recorder. try { audioRecorder = new AudioRecord( AUDIO_SOURCE, SAMPLE_RATE_IN_HZ, CHANNEL_CONFIG, AUDIO_FORMAT, minBufferSizeInBytes ); } catch(IllegalArgumentException ex) { Log.e( TAG, "Illegal Arguments: " + ex.getMessage() ); } // Launch Thread. onCreateThread = new Thread( new Runnable() { @Override public void run() { // Starts recording from the AudioRecord instance. audioRecorder.startRecording(); int numberOfBytesRead = 0; byte audioBuffer[] = new byte[minBufferSizeInBytes]; float tempBuffer[] = new float[CHECK_BLOCK_COUNT]; int tempIndex = 0; boolean isRecording = false; int totalReadBytes = 0; byte totalByteBuffer[] = new byte[60 * 44100 * 2]; // While data coming from microphone. while( true ) { float totalAbsValue = 0.0f; short sample = 0; numberOfBytesRead = audioRecorder.read( audioBuffer, 0, minBufferSizeInBytes ); // Analyze income sound. for( int i=0; i<minBufferSizeInBytes; i+=2 ) { sample = (short)( (audioBuffer[i]) | audioBuffer[i + 1] << 8 ); totalAbsValue += Math.abs( sample ) / (numberOfBytesRead/2); } // Set Animation of microphone. handler.sendEmptyMessage((int)totalAbsValue); // Analyze tempBuffer. tempBuffer[tempIndex%CHECK_BLOCK_COUNT] = totalAbsValue; float tempBufferTotalCount = 0.0f; for( int i=0; i<CHECK_BLOCK_COUNT; ++i ) tempBufferTotalCount += tempBuffer[i]; // Finalize value. tempBufferTotalCount = tempBufferTotalCount/CHECK_BLOCK_COUNT; // Waiting for load speak to start recording. if( (tempBufferTotalCount >=0 && tempBufferTotalCount <= START_RECORD_FROM) && !isRecording ) { Log.i("TAG", "Waiting for voice to start record."); tempIndex++; changeTexthandler.sendEmptyMessage(0); continue; } if( tempBufferTotalCount > START_RECORD_FROM && !isRecording ) { Log.i("TAG", "Recording"); changeTexthandler.sendEmptyMessage(1); isRecording = true; } // Stop Recording and save data to file. if( (tempBufferTotalCount >= 0 && tempBufferTotalCount <= START_RECORD_FROM) && isRecording ) { Log.i("TAG", "Stop Recording and Save data to file"); changeTexthandler.sendEmptyMessage(2); audioRecorder.stop(); audioRecorder.release(); audioRecorder = null; SaveDataToFile( totalReadBytes, totalByteBuffer ); totalReadBytes = 0; tempIndex++; break; } // Record Sound. for( int i=0; i<numberOfBytesRead; i++ ) totalByteBuffer[totalReadBytes + i] = audioBuffer[i]; totalReadBytes += numberOfBytesRead; tempIndex++; } } }, "Voice Detection Thread" ); // Run the Thread. onCreateThread.start(); //*/ } public void SaveDataToFile( int totalReadBytes, byte[] totalByteBuffer ) { // Save audio to file. String filepath = Environment.getExternalStorageDirectory().getPath(); File file = new File( filepath, "VioceDetectionDemo" ); if( !file.exists( ) ) file.mkdirs(); // String fileName = file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".wav"; // File always saved by same name. String fileName = file.getAbsolutePath() + "/" + "VoiceDetectionDemo" + ".wav"; long totalAudioLen = 0; long totalDataLen = totalAudioLen + 36; long longSampleRate = SAMPLE_RATE_IN_HZ; int channels = 1; long byteRate = RECORDER_BPP * SAMPLE_RATE_IN_HZ * channels/8; totalAudioLen = totalReadBytes; totalDataLen = totalAudioLen + 36; byte finalBuffer[] = new byte[totalReadBytes + 44]; finalBuffer[0] = 'R'; // RIFF/WAVE header finalBuffer[1] = 'I'; finalBuffer[2] = 'F'; finalBuffer[3] = 'F'; finalBuffer[4] = (byte) (totalDataLen & 0xff); finalBuffer[5] = (byte) ((totalDataLen >> 8) & 0xff); finalBuffer[6] = (byte) ((totalDataLen >> 16) & 0xff); finalBuffer[7] = (byte) ((totalDataLen >> 24) & 0xff); finalBuffer[8] = 'W'; finalBuffer[9] = 'A'; finalBuffer[10] = 'V'; finalBuffer[11] = 'E'; finalBuffer[12] = 'f'; // 'fmt ' chunk finalBuffer[13] = 'm'; finalBuffer[14] = 't'; finalBuffer[15] = ' '; finalBuffer[16] = 16; // 4 bytes: size of 'fmt ' chunk finalBuffer[17] = 0; finalBuffer[18] = 0; finalBuffer[19] = 0; finalBuffer[20] = 1; // format = 1 finalBuffer[21] = 0; finalBuffer[22] = (byte) channels; finalBuffer[23] = 0; finalBuffer[24] = (byte) (longSampleRate & 0xff); finalBuffer[25] = (byte) ((longSampleRate >> 8) & 0xff); finalBuffer[26] = (byte) ((longSampleRate >> 16) & 0xff); finalBuffer[27] = (byte) ((longSampleRate >> 24) & 0xff); finalBuffer[28] = (byte) (byteRate & 0xff); finalBuffer[29] = (byte) ((byteRate >> 8) & 0xff); finalBuffer[30] = (byte) ((byteRate >> 16) & 0xff); finalBuffer[31] = (byte) ((byteRate >> 24) & 0xff); finalBuffer[32] = (byte) (2 * 16 / 8); // block align finalBuffer[33] = 0; finalBuffer[34] = RECORDER_BPP; // bits per sample finalBuffer[35] = 0; finalBuffer[36] = 'd'; finalBuffer[37] = 'a'; finalBuffer[38] = 't'; finalBuffer[39] = 'a'; finalBuffer[40] = (byte) (totalAudioLen & 0xff); finalBuffer[41] = (byte) ((totalAudioLen >> 8) & 0xff); finalBuffer[42] = (byte) ((totalAudioLen >> 16) & 0xff); finalBuffer[43] = (byte) ((totalAudioLen >> 24) & 0xff); for( int i=0; i<totalReadBytes; ++i ) finalBuffer[44+i] = totalByteBuffer[i]; FileOutputStream out; try { out = new FileOutputStream(fileName); try { out.write(finalBuffer); out.close(); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (FileNotFoundException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } } public void OnClickButtonTryAgain(View view) { Start(); } }
发表评论
-
[Android] 为Android安装BusyBox —— 完整的bash shell
2013-12-27 10:19 1520http://www.cnblogs.com/xiaowen ... -
Windows的adb shell中使用vi不乱码方法及AdbPutty
2013-12-27 10:17 7641http://www.veryhuo.com/down/ht ... -
AppMobi推出新XDK,可创建测试PhoneGap项目
2012-09-03 13:39 2672AppMobi今天发布了一个新的工具PhoneGap Mobi ... -
Sencha
2012-09-03 12:59 1209http://www.sencha.com/ Se ... -
jQuery Mobile学习
2012-09-01 12:33 1725使用Jquery Mobile设计Android通讯录 ... -
BackBone
2012-09-01 12:34 1279Backbone.js 是一种重量级javascript M ... -
jQTouch
2012-08-30 15:57 1004A Zepto/jQuery plugin for mobil ... -
SwiFTP
2012-08-30 15:43 1331SwiFTP is a FTP server that run ... -
kWS
2012-08-30 15:41 1226kWS is a lightweight and fast W ... -
jQuery Mobile
2012-08-30 15:07 1062http://jquerymobile.com/ -
PhoneGap
2012-08-30 15:07 1063http://phonegap.com/ -
Android Button background image pressed/highlighted and disabled states without
2012-08-06 12:49 1707http://shikii.net/blog/android- ... -
[AndriodTips]Image, saved to sdcard, doesn't appear in Android's Gallery app
2012-08-04 16:15 1185http://stackoverflow.com/questi ... -
[AndroidTip]local reference table overflow (max=512)的错误解决
2012-07-22 22:56 6102JNI层coding经常会遇到ReferenceTable o ... -
[AndroidTip]EditText如何初始状态不获得焦点?
2012-07-22 15:35 1246最简单的办法是在EditText前面放置一个看不到的Linea ... -
[AndroidTip]android textview滚动条
2012-07-21 14:29 1325本来是想做一个显示文字信息的,当文字很多时View的高度不能超 ... -
Google公布Android 4.1完整功能
2012-07-16 09:48 3215http://www.android.com/about/je ... -
Android开发:使用AudioTrack播放PCM音频数据【附源码】
2012-07-13 15:20 20912http://www.linuxidc.com/Linux/2 ... -
Android上的行车记录仪
2012-07-11 22:31 2034MyCar Recorder DailyRoads -
Google hired one of Nuance soft engineers to help work around all Nuance patents
2012-07-10 14:33 1126很有趣的消息: http://forums.macrumor ...
相关推荐
✩ VAD (Voice Activation Detection) ✩ QoS (Quality of Service) ✩ ToS Marking ★ Security and encryption ✩ TLS ✩ SRTP ✩ Logging support for trouble shooting ✩ DNS SRV record lookups Accessories...
✩ VAD (Voice Activation Detection) ✩ QoS (Quality of Service) ✩ ToS Marking ★ Security and encryption ✩ TLS ✩ SRTP ✩ Logging support for trouble shooting ✩ DNS SRV record lookups Accessories...
将实现各个平台上能快速使用的音频处理库...VAD(Voice Activity Detection 静音检测) AECM(Acoustic Echo Canceller for Mobile 声学回声消除) AGC(Auto Gain Control 自动增益控制) 现在只有一个Android Demo 。
VAD(Voice Activity Detection 静音检测) AECM(Acoustic Echo Canceller for Mobile 声学回声消除) AGC(Auto Gain Control 自动增益控制) 现在只有一个Android Demo Now,let's do it. 博客地址:
将实现各个平台上能快速使用的音频处理...VAD(Voice Activity Detection 静音检测) AECM(Acoustic Echo Canceller for Mobile 声学回声消除) AGC(Auto Gain Control 自动增益控制) 现在只有一个Android Demo 。
在这个名为“TestVAD”的Android项目中,开发者利用了WebRTC的一项关键功能——语音活动检测(Voice Activity Detection,简称VAD),用于判断用户是否正在说话。下面我们将深入探讨WebRTC的VAD机制及其在Android...