Solved

Trying to get a VU level from the sound card.

Posted on 1999-01-06
14
925 Views
Last Modified: 2013-12-03
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
Comment
Question by:ben_cochran
  • 6
  • 4
  • 4
14 Comments
 

Expert Comment

by:overworked
Comment Utility
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
 

Author Comment

by:ben_cochran
Comment Utility
I thought that I would have to look at a buffer of some sort, but how is that done?
0
 

Accepted Solution

by:
overworked earned 150 total points
Comment Utility
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
 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
 

Expert Comment

by:overworked
Comment Utility
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
 
LVL 23

Expert Comment

by:chensu
Comment Utility
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
 

Author Comment

by:ben_cochran
Comment Utility
Thank You "chensu" you are the best.  After looking into the Mixer controls I found how to take the level of almost anything.
0
Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

 

Expert Comment

by:overworked
Comment Utility
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
 

Author Comment

by:ben_cochran
Comment Utility
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
 

Expert Comment

by:overworked
Comment Utility
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
 

Author Comment

by:ben_cochran
Comment Utility
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
 

Expert Comment

by:overworked
Comment Utility
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
 
LVL 23

Expert Comment

by:chensu
Comment Utility
0
 
LVL 23

Expert Comment

by:chensu
Comment Utility
0

Featured Post

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article describes a technique for converting RTF (Rich Text Format) data to HTML and provides C++ source that does it all in just a few lines of code. Although RTF is coming to be considered a "legacy" format, it is still in common use... po…
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now