Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.05 KB | None | 0 0
  1. class PlayAndRecordAudio
  2. {
  3. private const int RECORDER_BPP = 16;
  4. private const string AUDIO_RECORDER_FILE_EXT_WAV = ".wav";
  5. private const string AUDIO_RECORDER_TEMP_EXT = ".raw";
  6. private const int RECORDER_SAMPLERATE = 44100;
  7.  
  8. private int _BufferSize;
  9. protected MediaPlayer _Player;
  10. protected AudioRecord _Recorder;
  11. private Thread recordingThread = null;
  12. private bool _IsRecording;
  13. public void StartPlayer()
  14. {
  15. var docsPath = CreatePathToFile(AUDIO_RECORDER_FILE_EXT_WAV);
  16. if (_Player == null)
  17. {
  18. _Player = new MediaPlayer();
  19. }
  20. _Player.Reset();
  21. _Player.SetDataSource(docsPath);
  22. _Player.Prepare();
  23. _Player.Start();
  24. }
  25. public void PausePlayer()
  26. {
  27. _Player.Pause();
  28. }
  29. public void StopPlayer()
  30. {
  31. _Player.Stop();
  32. _Player.Release();
  33. _Player = null;
  34. }
  35. public void StartRecord()
  36. {
  37. try
  38. {
  39. DelRecordFile(AUDIO_RECORDER_FILE_EXT_WAV);
  40. _BufferSize = AudioRecord.GetMinBufferSize(44100, ChannelIn.Stereo, Encoding.Pcm16bit);
  41. _Recorder = new AudioRecord(
  42. // Hardware source of recording.
  43. AudioSource.Mic,
  44. // Frequency
  45. 44100,
  46. // Mono or stereo
  47. ChannelIn.Stereo,
  48. // Audio encoding
  49. Encoding.Pcm16bit,
  50. // Length of the audio clip.
  51. _BufferSize
  52. );
  53. AddEffects();
  54. _Recorder.StartRecording();
  55. _IsRecording = true;
  56. recordingThread = new Thread(new ThreadStart(WriteAudioDataToFile));
  57. recordingThread.Start();
  58. }
  59. catch (Exception ex)
  60. {
  61. System.Console.Out.WriteLine(ex.StackTrace);
  62. }
  63. }
  64. public void StopRecord()
  65. {
  66. // stops the recording activity
  67. if (_Recorder != null)
  68. {
  69. _IsRecording = false;
  70.  
  71. _Recorder.Stop();
  72. _Recorder.Release();
  73.  
  74. _Recorder = null;
  75. recordingThread = null;
  76. }
  77. copyWaveFile(CreatePathToFile(AUDIO_RECORDER_TEMP_EXT),
  78. CreatePathToFile(AUDIO_RECORDER_FILE_EXT_WAV));
  79.  
  80. //del tmp file
  81. DelRecordFile(AUDIO_RECORDER_TEMP_EXT);
  82. }
  83. private void AddEffects()
  84. {
  85. if (NoiseSuppressor.IsAvailable)
  86. {
  87. NoiseSuppressor.Create(_Recorder.AudioSessionId);
  88. }
  89. if (AcousticEchoCanceler.IsAvailable)
  90. {
  91. AcousticEchoCanceler.Create(_Recorder.AudioSessionId);
  92. }
  93. }
  94. private byte[] Short2Byte(short[] sData)
  95. {
  96. int shortArrsize = sData.Length;
  97. byte[] bytes = new byte[shortArrsize * 2];
  98.  
  99. for (int i = 0; i < shortArrsize; i++)
  100. {
  101. bytes[i * 2] = (byte)(sData[i] & 0x00FF);
  102. bytes[(i * 2) + 1] = (byte)(sData[i] >> 8);
  103. sData[i] = 0;
  104. }
  105. return bytes;
  106.  
  107. }
  108. private void WriteAudioDataToFile()
  109. {
  110. // Write the output audio in byte
  111. string filePath = CreatePathToFile(AUDIO_RECORDER_TEMP_EXT);
  112. short[] sData = new short[_BufferSize];
  113.  
  114. FileOutputStream os = null;
  115. try
  116. {
  117. os = new FileOutputStream(filePath);
  118. }
  119. catch (FileNotFoundException e)
  120. {
  121. System.Console.Out.WriteLine(e.Message);
  122. }
  123.  
  124. while (_IsRecording)
  125. {
  126. // gets the voice output from microphone to byte format
  127.  
  128. _Recorder.Read(sData, 0, _BufferSize);
  129. try
  130. {
  131. // // writes the data to file from buffer
  132. // // stores the voice buffer
  133.  
  134. byte[] bData = Short2Byte(sData);
  135.  
  136. os.Write(bData);
  137. }
  138. catch (IOException e)
  139. {
  140. System.Console.Out.WriteLine(e.StackTrace); ;
  141. }
  142. }
  143. try
  144. {
  145. os.Close();
  146. }
  147. catch (IOException e)
  148. {
  149. System.Console.Out.WriteLine(e.StackTrace);
  150. }
  151. }
  152. private void copyWaveFile(string inFilename, string outFilename)
  153. {
  154. FileInputStream input = null;
  155. FileOutputStream output = null;
  156. long totalAudioLen = 0;
  157. long totalDataLen = totalAudioLen + 36;
  158. long longSampleRate = RECORDER_SAMPLERATE;
  159. int channels = 2;
  160. long byteRate = RECORDER_BPP * RECORDER_SAMPLERATE * channels / 8;
  161.  
  162. byte[] data = new byte[_BufferSize];
  163.  
  164. try
  165. {
  166. input = new FileInputStream(inFilename);
  167. output = new FileOutputStream(outFilename);
  168. totalAudioLen = input.Channel.Size();
  169. totalDataLen = totalAudioLen + 36;
  170. WriteWaveFileHeader(output, totalAudioLen, totalDataLen,
  171. longSampleRate, channels, byteRate);
  172.  
  173. while (input.Read(data) != -1){
  174. output.Write(data);
  175. }
  176.  
  177. input.Close();
  178. output.Close();
  179. }
  180. catch (FileNotFoundException e)
  181. {
  182. System.Console.Out.WriteLine(e.StackTrace);
  183. }
  184. catch (IOException e)
  185. {
  186. System.Console.Out.WriteLine(e.StackTrace);
  187. }
  188. }
  189. private void WriteWaveFileHeader(
  190. FileOutputStream @out, long totalAudioLen,
  191. long totalDataLen, long longSampleRate, int channels,
  192. long byteRate)
  193. {
  194. byte[] header = new byte[44];
  195. header[0] = Convert.ToByte('R'); // RIFF/WAVE header
  196. header[1] = Convert.ToByte('I');
  197. header[2] = Convert.ToByte('F');
  198. header[3] = Convert.ToByte('F');
  199. header[4] = (byte) (totalDataLen & 0xff);
  200. header[5] = (byte) ((totalDataLen >> 8) & 0xff);
  201. header[6] = (byte) ((totalDataLen >> 16) & 0xff);
  202. header[7] = (byte) ((totalDataLen >> 24) & 0xff);
  203. header[8] = Convert.ToByte('W');
  204. header[9] = Convert.ToByte('A');
  205. header[10] = Convert.ToByte('V');
  206. header[11] = Convert.ToByte('E');
  207. header[12] = Convert.ToByte('f'); // 'fmt ' chunk
  208. header[13] = Convert.ToByte('m');
  209. header[14] = Convert.ToByte('t');
  210. header[15] = Convert.ToByte(' ');
  211. header[16] = 16; // 4 bytes: size of 'fmt ' chunk
  212. header[17] = 0;
  213. header[18] = 0;
  214. header[19] = 0;
  215. header[20] = 1; // format = 1
  216. header[21] = 0;
  217. header[22] = (byte) channels;
  218. header[23] = 0;
  219. header[24] = (byte) (longSampleRate & 0xff);
  220. header[25] = (byte) ((longSampleRate >> 8) & 0xff);
  221. header[26] = (byte) ((longSampleRate >> 16) & 0xff);
  222. header[27] = (byte) ((longSampleRate >> 24) & 0xff);
  223. header[28] = (byte) (byteRate & 0xff);
  224. header[29] = (byte) ((byteRate >> 8) & 0xff);
  225. header[30] = (byte) ((byteRate >> 16) & 0xff);
  226. header[31] = (byte) ((byteRate >> 24) & 0xff);
  227. header[32] = (byte) (2 * 16 / 8); // block align
  228. header[33] = 0;
  229. header[34] = RECORDER_BPP; // bits per sample
  230. header[35] = 0;
  231. header[36] = Convert.ToByte('d');
  232. header[37] = Convert.ToByte('a');
  233. header[38] = Convert.ToByte('t');
  234. header[39] = Convert.ToByte('a');
  235. header[40] = (byte) (totalAudioLen & 0xff);
  236. header[41] = (byte) ((totalAudioLen >> 8) & 0xff);
  237. header[42] = (byte) ((totalAudioLen >> 16) & 0xff);
  238. header[43] = (byte) ((totalAudioLen >> 24) & 0xff);
  239. @out.Write(header, 0, 44);
  240. }
  241. private void DelRecordFile(string extension)
  242. {
  243. File file = new File(CreatePathToFile(extension));
  244. if (file != null)
  245. {
  246. file.Delete();
  247. }
  248. }
  249. string CreatePathToFile(string extension)
  250. {
  251. var docsPath = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
  252. return System.IO.Path.Combine(docsPath, "tmp" + extension);
  253. }
  254.  
  255. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement