views:

110

answers:

3

Basically, what I need is a way to tap into the current audio output and check the sound level, i.e. I need to be able to check whether there is something playing on the audio device or not.

I do not need to check the volume setting, but the actual playing audio stream's sound level.

Sorry, I was asking about how to do it in Windows, on Visual Studio 2008.

@mikerobi: That forms a part of my reasoning - if it is being displayed on the system volume meter, there must be a system call that can get it back

+1  A: 

This is a good question. The answer, for 32-bit Windows apps, is to hook into winmm.dll and other low-level audio control DLLs. In C# I'd create a wrapper class containing extern method prototypes:

public class MyAudioWrapper
{
   [DLLImport("winmm.dll", EntryPoint="waveOutGetVolume")]
   public extern void GetWaveVolume(IntPtr devicehandle, out int Volume);

   ...
}

Have a look at this link for a list of Windows audio methods; you can use the mixer, or just the wave-out controller, to set volume. What you want to use will dictate what libraries to import. You'll have to research how best to define the prototype, and how to get the handle to the audio/mixer device.

KeithS
The better answer for Vista and beyond is to open an audio stream in loopbacked mode and capture from that.
Larry Osterman
A: 

Here is a helpful link for Windows API invokations, and here's exactly what you are looking for:

http://www.pinvoke.net/default.aspx/winmm.waveOutGetVolume


Since the requirement changed and you don't need the audio level I suggest the following might help:

I think you need to read what is being playedback on the output stream and by analyzing the data in some algorithms you might be able to decide weather something is being playedback or not. To do this you need the MMDevice API

http://msdn.microsoft.com/en-us/library/dd316556(v=VS.85).aspx

I don't want to discorage you but believe me this is not going to be easy to accomplish if you are not familiar with unmanaged code.

  • You have to fill many structures in each invokation.
  • You have to perform invokations in specific order.
  • Marshalling references to structures.

And even if you accomplish that you can't anticipate the outcome behavior of the device. Good luck.

A_Nablsi
A: 

I've recently answered such a question here, see How to detect if any sound plays on a Windows machine.

Vantomex