Reputation: 779
What I need to do is to get the audio stream playing on my speakers, without any additional hardware.
If there is a speakers output (say a human voice) then I need to display some images. So How can i determine whether there is a sound coming out of the speakers??
I want to use C# for this on windows 7.
Thank you.
Upvotes: 0
Views: 2004
Reputation: 5994
You can use CSCore which allows you to get the peak of any applications and of the whole device. You can determine whether sound is getting played by checking that peak value. This is an example on how to get the peak of an application. And these are two examples how to get the peak of one particular device:
[TestMethod]
[TestCategory("CoreAudioAPI.Endpoint")]
public void CanGetAudioMeterInformationPeakValue()
{
using (var device = Utils.GetDefaultRenderDevice())
using (var meter = AudioMeterInformation.FromDevice(device))
{
Console.WriteLine(meter.PeakValue);
}
}
[TestMethod]
[TestCategory("CoreAudioAPI.Endpoint")]
public void CanGetAudioMeterInformationChannelsPeaks()
{
using (var device = Utils.GetDefaultRenderDevice())
using (var meter = AudioMeterInformation.FromDevice(device))
{
for (int i = 0; i < meter.MeteringChannelCount; i++)
{
Console.WriteLine(meter[i]);
}
}
}
Just check whether there is a peak bigger than zero or something like 0.05 (you may need to experiment with that). If the peak is bigger than a certain value, there is any application playing something.
Also take a look at this: http://cscore.codeplex.com/SourceControl/latest#CSCore.Test/CoreAudioAPI/EndpointTests.cs. To get get implementation of Utils.GetDefaultRendererDevice see take a look at this one: http://cscore.codeplex.com/SourceControl/latest#CSCore.Test/CoreAudioAPI/Utils.cs
The first example gets the average peak of all channel peaks and the second example gets the peaks of each channel of the output device.
Upvotes: 0
Reputation: 49522
You can do this with WASAPI Loopback Capture. My open source NAudio library includes a wrapper for this called WasapiLoopbackCapture
. One quirk of WASAPI Loopback Capture is that you get no callbacks whatsoever when the system is playing silence, although that might not matter for you
If you don't actually need to examine the values of the samples, WASAPI also allows you to monitor the volume level of a device. In NAudio you can access this with AudioMeterInformation
or AudioEndpointVolume
on the MMDevice
(you can get this with MMDeviceEnumerator.GetDefaultAudioEndpoint
for rendering)
Upvotes: 0