Android应用程序中的语音检测

前端之家收集整理的这篇文章主要介绍了Android应用程序中的语音检测前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
描述

我的应用录音来自手机麦克风.我正在使用android标准类(android.media.AudioRecord)来做到这一点.应用程序有2个按钮“开始”和“停止”,当我按开始按钮应用程序开始记录,当我按停止应用程序停止录制,并给我回缓冲区,语音数据.wav格式.一切正常

问题

我想以这种方式改变应用程序,当应用程序开始工作时,它开始分析来自麦克风的声音,如果用户保持静音应用程序继续分析来自麦克风的收音,如果用户开始说话应用程序开始录制来自麦克风的声音,然后用户完成通话应用程序停止录制,并给予我相同的缓冲区,语音数据以.wav格式.

>我如何检测到该用户开始说话
>我如何检测到该用户停下来说话

解决方法

只需将此代码添加到您的应用程序中,您将检测用户何时开始说话,何时停止.
  1. public void onCreate(Bundle savedInstanceState)
  2. {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.main);
  5.  
  6. // Get the minimum buffer size required for the successful creation of an AudioRecord object.
  7. int bufferSizeInBytes = AudioRecord.getMinBufferSize( RECORDER_SAMPLERATE,RECORDER_CHANNELS,RECORDER_AUdio_ENCODING
  8. );
  9. // Initialize Audio Recorder.
  10. AudioRecord audioRecorder = new AudioRecord( MediaRecorder.AudioSource.MIC,RECORDER_SAMPLERATE,RECORDER_AUdio_ENCODING,bufferSizeInBytes
  11. );
  12. // Start Recording.
  13. audioRecorder.startRecording();
  14.  
  15. int numberOfReadBytes = 0;
  16. byte audioBuffer[] = new byte[bufferSizeInBytes];
  17. boolean recording = false;
  18. float tempFloatBuffer[] = new float[3];
  19. int tempIndex = 0;
  20. int totalReadBytes = 0;
  21. byte totalByteBuffer[] = new byte[60 * 44100 * 2];
  22.  
  23.  
  24. // While data come from microphone.
  25. while( true )
  26. {
  27. float totalAbsValue = 0.0f;
  28. short sample = 0;
  29.  
  30. numberOfReadBytes = audioRecorder.read( audioBuffer,bufferSizeInBytes );
  31.  
  32. // Analyze Sound.
  33. for( int i=0; i<bufferSizeInBytes; i+=2 )
  34. {
  35. sample = (short)( (audioBuffer[i]) | audioBuffer[i + 1] << 8 );
  36. totalAbsValue += Math.abs( sample ) / (numberOfReadBytes/2);
  37. }
  38.  
  39. // Analyze temp buffer.
  40. tempFloatBuffer[tempIndex%3] = totalAbsValue;
  41. float temp = 0.0f;
  42. for( int i=0; i<3; ++i )
  43. temp += tempFloatBuffer[i];
  44.  
  45. if( (temp >=0 && temp <= 350) && recording == false )
  46. {
  47. Log.i("TAG","1");
  48. tempIndex++;
  49. continue;
  50. }
  51.  
  52. if( temp > 350 && recording == false )
  53. {
  54. Log.i("TAG","2");
  55. recording = true;
  56. }
  57.  
  58. if( (temp >= 0 && temp <= 350) && recording == true )
  59. {
  60. Log.i("TAG","Save audio to file.");
  61.  
  62. // Save audio to file.
  63. String filepath = Environment.getExternalStorageDirectory().getPath();
  64. File file = new File(filepath,"AudioRecorder");
  65. if( !file.exists() )
  66. file.mkdirs();
  67.  
  68. String fn = file.getAbsolutePath() + "/" + System.currentTimeMillis() + ".wav";
  69.  
  70. long totalAudioLen = 0;
  71. long totalDataLen = totalAudioLen + 36;
  72. long longSampleRate = RECORDER_SAMPLERATE;
  73. int channels = 1;
  74. long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels/8;
  75. totalAudioLen = totalReadBytes;
  76. totalDataLen = totalAudioLen + 36;
  77. byte finalBuffer[] = new byte[totalReadBytes + 44];
  78.  
  79. finalBuffer[0] = 'R'; // RIFF/WAVE header
  80. finalBuffer[1] = 'I';
  81. finalBuffer[2] = 'F';
  82. finalBuffer[3] = 'F';
  83. finalBuffer[4] = (byte) (totalDataLen & 0xff);
  84. finalBuffer[5] = (byte) ((totalDataLen >> 8) & 0xff);
  85. finalBuffer[6] = (byte) ((totalDataLen >> 16) & 0xff);
  86. finalBuffer[7] = (byte) ((totalDataLen >> 24) & 0xff);
  87. finalBuffer[8] = 'W';
  88. finalBuffer[9] = 'A';
  89. finalBuffer[10] = 'V';
  90. finalBuffer[11] = 'E';
  91. finalBuffer[12] = 'f'; // 'fmt ' chunk
  92. finalBuffer[13] = 'm';
  93. finalBuffer[14] = 't';
  94. finalBuffer[15] = ' ';
  95. finalBuffer[16] = 16; // 4 bytes: size of 'fmt ' chunk
  96. finalBuffer[17] = 0;
  97. finalBuffer[18] = 0;
  98. finalBuffer[19] = 0;
  99. finalBuffer[20] = 1; // format = 1
  100. finalBuffer[21] = 0;
  101. finalBuffer[22] = (byte) channels;
  102. finalBuffer[23] = 0;
  103. finalBuffer[24] = (byte) (longSampleRate & 0xff);
  104. finalBuffer[25] = (byte) ((longSampleRate >> 8) & 0xff);
  105. finalBuffer[26] = (byte) ((longSampleRate >> 16) & 0xff);
  106. finalBuffer[27] = (byte) ((longSampleRate >> 24) & 0xff);
  107. finalBuffer[28] = (byte) (byteRate & 0xff);
  108. finalBuffer[29] = (byte) ((byteRate >> 8) & 0xff);
  109. finalBuffer[30] = (byte) ((byteRate >> 16) & 0xff);
  110. finalBuffer[31] = (byte) ((byteRate >> 24) & 0xff);
  111. finalBuffer[32] = (byte) (2 * 16 / 8); // block align
  112. finalBuffer[33] = 0;
  113. finalBuffer[34] = RECORDER_BPP; // bits per sample
  114. finalBuffer[35] = 0;
  115. finalBuffer[36] = 'd';
  116. finalBuffer[37] = 'a';
  117. finalBuffer[38] = 't';
  118. finalBuffer[39] = 'a';
  119. finalBuffer[40] = (byte) (totalAudioLen & 0xff);
  120. finalBuffer[41] = (byte) ((totalAudioLen >> 8) & 0xff);
  121. finalBuffer[42] = (byte) ((totalAudioLen >> 16) & 0xff);
  122. finalBuffer[43] = (byte) ((totalAudioLen >> 24) & 0xff);
  123.  
  124. for( int i=0; i<totalReadBytes; ++i )
  125. finalBuffer[44+i] = totalByteBuffer[i];
  126.  
  127. FileOutputStream out;
  128. try {
  129. out = new FileOutputStream(fn);
  130. try {
  131. out.write(finalBuffer);
  132. out.close();
  133. } catch (IOException e) {
  134. // TODO Auto-generated catch block
  135. e.printStackTrace();
  136. }
  137.  
  138. } catch (FileNotFoundException e1) {
  139. // TODO Auto-generated catch block
  140. e1.printStackTrace();
  141. }
  142.  
  143. //*/
  144. tempIndex++;
  145. break;
  146. }
  147.  
  148. // -> Recording sound here.
  149. Log.i( "TAG","Recording Sound." );
  150. for( int i=0; i<numberOfReadBytes; i++ )
  151. totalByteBuffer[totalReadBytes + i] = audioBuffer[i];
  152. totalReadBytes += numberOfReadBytes;
  153. //*/
  154.  
  155. tempIndex++;
  156.  
  157. }
  158. }

检查这个link.

猜你在找的Android相关文章