views:

67

answers:

2

Is it possible to detect if any sound plays on a windows xp machine? Help in any language would be useful. I basically need to write a program that runs all the time and outputs some text to a file whenever a sound plays. I don't need any specific information about the sound, just whether a sound is playing. I don't care whether the speakers are actually powered on or anything like that.

+1  A: 

The question was easy, but the answer is difficult. You'll need to utilize DirectSound to achieve your purpose. I haven't tested my solution yet, but you can try to call IDirectSoundBuffer8::GetStatus(), then check the return value of pdwStatus parameter. According to MSDN, DSBSTATUS_PLAYING is set if the buffer is being heard.

Since you didn't tell about programming language you are using, I implement the following example using my favorite language, Delphi.

  var
    dwStatus: DWORD;
    hResult: HRESULT;

  hResult := GetStatus(@dwStatus);
  if hResult = DS_OK then begin
    if dwStatus and DSBSTATUS_PLAYING <> 0 then
      ShowMessage('Sound card is playing sound now.');
  end;

UPDATE

I just found a VB forum discussed about how to detect silence (no output of sound card). Download DetSilence.zip. In the DXRecord_GotWavData Sub, modify the constants SilencePercent and NonSilencePercent to the values you need.

Vantomex
@saberworks, I suggest you consider to register at StackOverflow.com because by doing so, many experts might be interested in looking on your problem deeply. Please give a feedback whether the solution works or not.
Vantomex
Thanks for this, Vantomex. I went down this path but I think I was doing it wrong because I was only able to get it to give me the status of sounds that I was playing, not of "any sound at all on the card." I came up with a different solution which I will post in a separate message.
saberworks
A: 

I ended up approaching this in an unconventional manner. First I installed Virtual Audio Cable (http://www.ntonyx.com/vac.htm) and configured it as my primary sound device. I then configured the recording device to record the sound from the primary output device. This basically means I can hit "record" and it will record anything going to the sound card. Then I used a perl module, Win32::SoundRec to record sound to a file. I periodically check the wav file for activity and if there is some, I know sound was playing. I used another perl module, Audio::Wav, to parse the WAV file and look for activity (silence vs. non-silence).

saberworks
@saberworks, thanks for your feedback and your willingness to post the solution that works. BTW, I just found a VB forum discussed about *how to detect silence* (no output of sound card). I have updated my answer to include a link to the forum.
Vantomex