[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 949
  • Last Modified:

Trying to get a VU level from the sound card.

I'm trying to get a VU level from the sound card.  I'm using the MCI (Media Control Interface) and programming in C++.  I know how to start and stop recording or playing but I'm having trouble obtaining the level.  

I open the MCI Device like this:
 
  // open the wave device
  open_parms.lpstrDeviceType = "waveaudio";
  open_parms.lpstrElementName = "";

  mciSendCommand( 0, MCI_OPEN, MCI_OPEN_TYPE |
                     MCI_OPEN_ELEMENT,
                     (DWORD)(LPVOID) &open_parms);
  // set PCM format of recording
  set_parms.wFormatTag = WAVE_FORMAT_PCM;
  set_parms.wBitsPerSample = 16;
  set_parms.nChannels = 1;
  set_parms.nSamplesPerSec = 22050;
  set_parms.nAvgBytesPerSec = ((set_parms.wBitsPerSample)/8)
                                *set_parms.nChannels
                                *set_parms.nSamplesPerSec;
    set_parms.nBlockAlign = ((set_parms.wBitsPerSample) /8)
                            *set_parms.nChannels;

  mciSendCommand(wave_device_id,MCI_SET, MCI_WAIT |
                                MCI_WAVE_SET_FORMATTAG |
                                MCI_WAVE_SET_BITSPERSAMPLE|
                                MCI_WAVE_SET_CHANNELS |
                                MCI_WAVE_SET_SAMPLESPERSEC |
                                MCI_WAVE_SET_AVGBYTESPERSEC|
                                MCI_WAVE_SET_BLOCKALIGN,
                               (DWORD) (LPVOID) &set_parms);

I try to sample the Level like this:
  status_parms.dwItem = MCI_WAVE_STATUS_LEVEL;
  mciSendCommand( wave_device_id,MCI_STATUS ,                                                MCI_STATUS_ITEM, (DWORD)                                  &status_parms);
From what I understand after this: stat_parms.dwReturn should contain the level from the MCI Device.  Instead the value is 0.

This will not give me any errors and if I send the MCI device an MCI SAVE and close the device a file of nothing will be created.  I don't know if I'm going about this in the wrong way or not but I need to obtain a level from the sound card.  I will appreciate the help.

Thanks.
0
ben_cochran
Asked:
ben_cochran
  • 6
  • 4
  • 4
1 Solution
 
overworkedCommented:
You need to use wavein to record audio. You'll then need to examine each buffer of audio data as it comes in in real time. Your analyze buffer routine should make note of the highest sample within buffer. The highest sample in buffer is your active VU level. However, you may want to implement a VU fall-off rate or gravity effect so that your VU level will fall off at a steady rate unless a new 'highest sample reading' exceeds the current level.

When determining highest sample in buffer, be sure to differentiate between 8 and 16 bit audio data. 8bit audio is unsigned while 16bit is signed.

On another note, you can make an oscilloscope type display to go along w/ur VU meters. Simply graph each buffer of audio data onto a display using time as your X axis and the sample as your Y.
0
 
ben_cochranAuthor Commented:
I thought that I would have to look at a buffer of some sort, but how is that done?
0
 
overworkedCommented:
Your gonna create the buffer yourself and turn it over to wavein for filling with recorded audio data (Look up wavein funcs in your winsdk). When it gives it back full, your gonna perform a loop looking at each sample within the buffer. If that sample is higher than the highest sample you have so far, you swap it with your highest.
0
Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

 
chensuCommented:
Try combining MCI_WAIT with MCI_STATUS_ITEM. If it still does not work, I would recommend using Audio Mixers functions. See my article at http://www.codeguru.com/multimedia/ADMixer.shtml.
0
 
overworkedCommented:
chensu: the wave meter control will give you VU for wave playback but not record. Therefore, you can't get VU for any external audio sources.
0
 
chensuCommented:
The code at CodeGuru is for wave playback. However, there is a wave meter control for wave recording. You just need to change my code slightly to deal with the wavein audio line. To prove it, run the Volume Control application in Windows, choose menu Options/Properties and switch it to Recording. You will see the control.
0
 
ben_cochranAuthor Commented:
Thank You "chensu" you are the best.  After looking into the Mixer controls I found how to take the level of almost anything.
0
 
overworkedCommented:
If your app is to be run on all types of sound cards, I personally don't recommend that you use mixer api for getting VU readings since you'll be relying on the driver written by card manufacturer. In my experience from dealing with these drivers, I've noticed that there is no guarantee that the VU control will be present in the driver or the extent of its ability. This goes for line input controls as well. Unfortunately, there are some driver programmers who don't use the correct labeling with their controls. I've noticed that it's not always possible to find cd and external input line in controls and I've even had to substring search the control labels for words like "cd", "line", "ext" etc. to do so.
0
 
ben_cochranAuthor Commented:
Overworked:  You were right is saying, "there is no guarantee that the VU control will be present in the driver".  I still have trouble getting the buffer from the sound card.  Do you know of any good reference or web sites that have good documentation on retrieving the buffer from the sound card?    
0
 
overworkedCommented:
hmm, buffer from the sound card? Do you mean the RAM onboard sound card? If so, I think DirectX would be your best bet. I'm pretty sure dx sound will let you copy stuff to/from sound card RAM. However, I don't think it will give you a ptr address since the RAM isn't mapped into cpu mem space.
0
 
ben_cochranAuthor Commented:
As you said to me, "Your gonna create the buffer yourself and turn it over to wavein for filling with recorded audio data." Do you know of any good reference or web sites that have good documentation on how to do this?
0
 
overworkedCommented:
Hmm, not off hand. You can usually find sample source code to do almost anything at msdn (microsoft developer network) http://www.microsoft.com/msdn
0
 
chensuCommented:
0

Featured Post

Microsoft Certification Exam 74-409

VeeamĀ® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

  • 6
  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now