Use C # and DirectX Real-time monitor microphone input volume

Source: Internet
Author: User

The control was streamlined on the work of Jacob Klint. I hereby acknowledge.
Now you are developing a voice recording software. You need to actually monitor the microphone input volume and display it. Similar to QQ voice function.
There are a lot of available technologies that can be implemented by Windows Media Encoder, Microsoft Expression Encoder, Microsoft SAPI, and Microsoft DirectX. Taking into account the compatibility of XP, Vista and Win7, DirectX was used as a technical solution.
Let's take a look at the effect of the finished product:



The idea is this:
Capture the sound, then start the thread, read the data from the cache at intervals, find the level value of the group's data, and update the ProgressBar control.

First we need to capture sound from the sound card (audio capture). The specific steps for capturing sound using DirectX are as follows: C # code   //  Create  capture  object    capture cap =  New capture (Audiodevices[deviceindex]. DRIVERGUID);  //  Create a description of  capture buffer object    capturebufferdescription  Desc = new capturebufferdescription ();   waveformat wf = new  Waveformat ();   WF. bitspersample = 16;   WF. samplespersecond = 16000;  //  Mono    WF. channels = 1;  //  data for the smallest atomic unit    WF. blockalign =  (short) (WF). CHANNELS * WF. BITSPERSAMPLE / 8);   WF. AVERAGEBYTESPERSECOND = WF. BLOCKALIGN * WF. samplespersecond;  //  uncompressed pcm   WF. formattag = waveformattag.pcm;   Desc. format = wf;   Desc. BUFFERBYTES = SAMPLES * WF. Blockalign;  //  Create  capturebuffer objects    buffer = new  Microsoft.DirectX.DirectSound.CaptureBuffer (desc, cap);  //  capture data to cache    buffer. Start (True);  
Then we need to start a thread to monitor the volume and modify the UI controls.   C # code Livevolumethread = new Thread (new ThreadStart (updateprogress));   livevolumethread.priority = Threadpriority.lowest; Livevolumethread.start ();
Let's implement the UpdateProgress method below. The first step is to read the sound data and write an array. C # code Array samples = buffer. Read (0, typeof (Int16), Lockflag.fromwritecursor, Sample_format_array);
The second step is to find out the level value of this group of data C # code int goal = 0; for (int i = 0; i < samples; i++) {Goal = = (Int16) samples.   GetValue (i, 0, 0);   } goal = (int) math.abs (goal/samples); In the final step, set the value of the ProgressBar to allow the thread to sleep a fixed value.   C # code Progressbar.value = goal; Thread.Sleep (Tempframedelay);
The control implements the UserControl interface, so you can drag and drop it directly in the interface designer.
Invoke the Start () method to open the capture;
Call the Stop () method to close the capture;
For more properties, see the Properties panel.

When you run the sample code, if you are prompted with an exception such as "Loaderlocker detected", the processing method is:
Debug-Exception-managed debugging assistants-will loaderlocker the hook to reverse the selection. (as pictured)





Volumemeter.zip (189 KB) Download number of times: 711 View Picture Attachments

from:http://fantasticinblur.iteye.com/blog/678527


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.