C # microphone acquisition and Playback,

Source: Internet
Author: User

C # microphone acquisition and Playback,

In the online chat system, collecting and playing the microphone sound is one of the most basic modules. This article describes how to quickly implement this basic module.

I. Basic Knowledge

Several terms related to sound collection and playback must be understood first. Otherwise, the subsequent sections will not be available. Voice acquisition refers to the acquisition of audio data from the microphone, that is, the conversion of audio samples into digital signals. It involves several important parameters: Sampling Rate, number of sampling digits, and number of channels.

To put it simply:

Sampling Rate: that is, the sampling frequency, that is, the number of acquisition operations in one second.

Number of sampling digits: the sampling depth, that is, the Data Length obtained by each collection action, that is, how many bits are used to record a sample.

Number of channels: generally single or dual channels (STEREO ). Almost all common microphone acquisition is single-channel.

In this way, the size of the sound data collected in one second is (unit: byte): (sampling frequency × number of sampling digits × number of channels × time)/8.

Audio frame: Generally, the length of an audio frame is 10 ms, that is, every 10 ms of data forms an audio frame. Assume that the sampling rate is 16 k, the number of sampling digits is 16 bits, and the number of audio channels is 1. The size of a 10 ms audio frame is: (16000*16*1*0.01)/8 = 320 bytes. In the formula, 0.01 is the second, that is, 10 ms.

2. How to collect and play videos?

If the microphone is directly collected and played based on the underlying DirectX, it will be very cumbersome. Fortunately, we have ready-made components to do this. MCapture is used to collect hardware devices (such as microphones, cameras, sound cards, and screens), and MPlayer is used to play the collected data.

1. Collect microphones

MCapture provides IMicrophoneCapturer for collecting microphone input sound. It triggers an AudioCaptured event every 20 ms and exposes the collected data in 20 ms through the event parameter byte.

The value of IMicrophoneCapturer-related collection parameters is as follows:

Sampling frequency: 16000, sampling bits: 16 bits, channels: 1.

Therefore, according to the formula above, we can obtain that the length of the byte [] parameter of the AudioCaptured event is 640.

2. play audio data

MPlayer provides IAudioPlayer for playing audio data. When creating an IAudioPlayer instance, you must correctly set the values of the sampling frequency, number of sampling digits, and number of audio channels. If they are different from the characteristics of the audio data to be played, an error will occur during playback.

After obtaining the audio data collected by MCapture, we can submit it to the Play method of IAudioPlayer for playing.

Iii. Demo implementation

Based on the previous introduction, it is quite easy to collect and play the microphone. In the following demo, we not only demonstrated playing the sound collected from the microphone, but also added the function of directly playing the wav audio file, which is quite simple.

Public partial class Form1: Form {private IAudioPlayer audioPlayer; private extends microphoneCapturer; public Form1 () {InitializeComponent ();} private void button_mic_Click (object sender, EventArgs e) {try {this. microphoneCapturer = CapturerFactory. createMicrophoneCapturer (int. parse (this. textBox_mic.Text); this. microphoneCapturer. audioCaptured + = new ESBasic. cbGeneric <byte []> (microphoneCapturer_AudioCaptured); this. audioPlayer = PlayerFactory. createAudioPlayer (int. parse (this. textBox_speaker.Text), 16000, 1, 16, 2); this. microphoneCapturer. start (); this. label_msg.Text = "collecting microphone and playing... "; this. label_msg.Visible = true; this. button_wav.Enabled = false; this. button_mic.Enabled = false; this. button_stop.Enabled = true;} catch (Exception ee) {MessageBox. show (ee. message );}}Void microphoneCapturer_AudioCaptured (byte [] audioData) {if (this. audioPlayer! = Null) {this. audioPlayer. Play (audioData );}}Private void button_wav_Click (object sender, EventArgs e) {try {string path = ESBasic. helpers. fileHelper. getFileToOpen2 ("select the wav file to be played", AppDomain. currentDomain. baseDirectory ,". wav "); if (path = null) {return;} AudioInformation info = PlayerFactory. parseWaveFile (path); if (info. formatTag! = (Int) WaveFormats. Pcm) {MessageBox. Show ("only supports PCM encoding Speech Data! "); Return;} int secs = info. getTimeInMsecs ()/1000; // The playback duration of the audio data. this. audioPlayer = PlayerFactory. createAudioPlayer (int. parse (this. textBox_speaker.Text), info. sampleRate, info. channelCount, info. bitsNumber, secs + 1); this. audioPlayer. play (info. audioData); this. label_msg.Text = "playing wav files... "; this. label_msg.Visible = true; this. button_wav.Enabled = false; this. button_mic.Enabled = false; this. Button_stop.Enabled = true;} catch (Exception ee) {MessageBox. Show (ee. Message) ;}} private void form=formclosing (object sender, FormClosingEventArgs e) {if (this. microphoneCapturer! = Null) {this. microphoneCapturer. Stop (); this. microphoneCapturer. Dispose (); this. microphoneCapturer = null;} if (this. audioPlayer! = Null) {this. audioPlayer. dispose (); this. audioPlayer = null;} private void button_stop_Click (object sender, EventArgs e) {if (this. audioPlayer = null) {return;} if (this. microphoneCapturer! = Null) {this. microphoneCapturer. stop (); this. microphoneCapturer. dispose (); this. microphoneCapturer = null;} this. audioPlayer. clear (); this. audioPlayer. dispose (); this. audioPlayer = null; this. label_msg.Visible = false; this. button_wav.Enabled = true; this. button_mic.Enabled = true; this. button_stop.Enabled = false ;}}

Look at the demo running:

  

Download microphone collection and playback Demo source code



Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.