Controlling Application's Volume & VU Meter - c#

I am using NAudio for a screen recording software I am designing and I need to know if it's possible to not only control the specific application's volume but also display a VU Meter for the application's sound.
I've Googled all over the place and it seems I can only get a VU Meter for the devices currently on my computer and set the volume for those devices.
Even though I am using NAudio, I am open to other solutions.

I asked the question in more detail after this question. I have since found the answer so I will leave the answer here for those who stumble upon it. Trying to use NAudio & CSCore has gotten me quite familiar with so please ask if you need further assistance.
This block of code uses CSCore and is a modified and commented version of the answer found here:Getting individual windows application current volume output level as visualized in audio Mixer
class PeakClass
{
static int CurrentProcessID = 0000;
private static void Main(string[] args)
{
//Basically gets your default audio device and session attached to it
using (var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render))
{
using (var sessionEnumerator = sessionManager.GetSessionEnumerator())
{
//This will go through a list of all processes uses the device
//the code got two line above.
foreach (var session in sessionEnumerator)
{
//This block of code will get the peak value(value needed for VU Meter)
//For whatever process you need it for (I believe you can also check by name
//but I found that less reliable)
using (var session2 = session.QueryInterface<AudioSessionControl2>())
{
if(session2.ProcessID == CurrentProcessID)
{
using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
{
Console.WriteLine(audioMeterInformation.GetPeakValue());
}
}
}
//Uncomment this block of code if you need the peak values
//of all the processes
//
//using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
//{
// Console.WriteLine(audioMeterInformation.GetPeakValue());
//}
}
}
}
}
private static AudioSessionManager2 GetDefaultAudioSessionManager2(DataFlow dataFlow)
{
using (var enumerator = new MMDeviceEnumerator())
{
using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia))
{
Console.WriteLine("DefaultDevice: " + device.FriendlyName);
var sessionManager = AudioSessionManager2.FromMMDevice(device);
return sessionManager;
}
}
}
}
The following code block will allow you to change the volume of the device using NAudio
MMDevice VUDevice;
public void SetVolume(float vol)
{
if(vol > 0)
{
VUDevice.AudioEndpointVolume.Mute = false;
VUDevice.AudioEndpointVolume.MasterVolumeLevelScalar = vol;
}
else
{
VUDevice.AudioEndpointVolume.Mute = true;
}
Console.WriteLine(vol);
}
I have code from two different libraries only to answer the question I posted directly which was how to both set the volume and get VU Meter values (peak values). CSCore and NAudio are very similar so most of the code here is interchangeable.

Related

Get current state of webcam in C#

I am trying to figure out how to check if a webcam/video capture device is already being used by another application without actually activating it.
My current approach is to use the AForge.NET library and using the .IsRunning property of the VideoCaptureDevice object like this:
var videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
foreach (FilterInfo videoDevice in videoDevices)
{
VideoCaptureDevice camera new AForge.Video.DirectShow.VideoCaptureDevice(videoDevice.MonikerString);
Debug.Print(camera.IsRunning)
}
I guess the IsRunning property only works on VideoCaptureDevices that have been started using the library and I need lower-level DirectShow access to the device.
While there are many ways to use DirectShow in C#, I have been unable to find a way to check the state even using DirectShow in C++. Is there some magic I need to perform here?
Thanks
Tobias Timpe
I'm not entirely sure if this will be helpful to you, but I found your question because I wanted to write a custom app to control my busylight. This is very much 'Works On My Machine' certified - it's not an attempt to give a general answer. However, I figure it may help you, and possibly the next person who comes across this page while googling...
private static bool IsWebCamInUse()
{
using (var key = Registry.CurrentUser.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\CapabilityAccessManager\ConsentStore\webcam\NonPackaged"))
{
foreach (var subKeyName in key.GetSubKeyNames())
{
using (var subKey = key.OpenSubKey(subKeyName))
{
if (subKey.GetValueNames().Contains("LastUsedTimeStop"))
{
var endTime = subKey.GetValue("LastUsedTimeStop") is long ? (long) subKey.GetValue("LastUsedTimeStop") : -1;
if (endTime <= 0)
{
return true;
}
}
}
}
}
return false;
}

C# Vibration Sensor using External Accelerometer

Its been almost 4 years since I've programmed in c# so I'm really rusty. I was given a project to create a program that will be able to read the vibration from an external Vibration Sensor connected via 2x micro usb cable. I am able to obtain the vibration x,y values from the external device however; I now need to plot the raw data obtained in a graph showing amplitude vs time Example Picture given!show raw data amplitude vs time. I also need to be able to grab these plotted points in the graph and export them to excel or any other means of storage for later analysis. Can you guys please help or point me in the right direction. Thanks Vibration results
Code example `
using Hardware.PR49;
using System;
using System.Collections.Generic;
using System.Threading;
namespace PR49SensorExample
{
class Program
{
static void Main(string[] args)
{
var task = PR49Detection.GetSerialPortSensorInfos();
if (!task.Wait(5000))
{
Console.WriteLine("Searching PR49 timed out.");
return;
}
var all49 = task.Result;
if (all49.Count == 0)
{
Console.WriteLine("No PR49 connected.");
return;
}
var port = all49[0].Port;
var sensor = new SerialDevice49(port);
var sources = sensor.SignalSources;
var xAxisSource = sources[0];
var yAxisSource = sources[1];
var xData = new List<double>();
var yData = new List<double>();
try
{
sensor.Connect();
sensor.StartDAQ();
}
catch
{
Console.WriteLine("Failed to connect or start sensor.");
return;
}
while (xData.Count < 20000)
{
xData.AddRange(xAxisSource.ReadDecodedData());
yData.AddRange(yAxisSource.ReadDecodedData());
Console.WriteLine($"data points: X: {xData.Count}, Y: {yData.Count}");
Thread.Sleep(200);
}
try
{
sensor.StopDAQ();
sensor.Disconnect();
}
catch
{
Console.WriteLine("Failed to stop or disconnect sensor.");
return;
}
Console.WriteLine("\nSensor stopped.");
Console.ReadKey();
}
}
}
`

How to create fast duplex scanning with WIA C#?

i am new to WIA. And i have been asked to make scaning service scan faster and duplex. My current service scan one page, then put it in pdf and so on untill there is less then 20 pages(this number just a crutch used before me, will be glad if someone explane how to get "if there is any paper in there" variable). I started to dig and found docs on MSDN describing properties and after found this post describing duplex sanning, but with mysterious 5 in set. After I found this and figured out what i need WIA_DPS_DOCUMENT_HANDLING_SELECT to set to 0x205(FEEDER + DUPLEX + AUTO_ADVANCE). So I tried to setup them like this:
private static void SetProperty(Property property, int value)
{
IProperty x = (IProperty)property;
Object val = value;
x.set_Value(ref val);
}
...some code...
foreach (Property prop in device.Properties)
{
//LOGGER.Warn(prop.Name);
//LOGGER.Warn(prop.PropertyID);
switch ((Int32)prop.PropertyID)
{
// Document Handling Select
case 3088:
SetProperty(prop, 517);
break;
// Pages
case 3096:
SetProperty(prop, 1);
break;
}
}
And it did't worked for me... It just stuck on setting... Can Somebody explain how to setup AUTO_ADVANCE and DUPLEX props? Or maybe "make scanning faster and duplex" need something more then just AUTO_ADVANCE and DUPLEX and my perception about them is wrong? Or I should considering "ISIS / TWAIN (Windows XP / Vista / 7 / 8 / 8.1 / 10)" string in my scan description and use other libraries?
(Window 10, Canon DR-M160||, DR-M160 & DR-M160II Driver for Windows)
and also here is the current fetch function:
public List<ImageFile> FetchImageList()
{
List<ImageFile> imageList = new List<ImageFile>();
//bool hasMorePages = true;
int testcount = 0;
while (testcount >= 0)
{
testcount--;
WIA.Device device = FindDevice(_deviceId);
if (device == null)
{
LOGGER.Warn("Scanner device not found");
return null;
}
// get item
WIA.Item scanItem = device.Items[1] as WIA.Item;
LOGGER.Debug($"ScanItem: {scanItem.ItemID}");
try
{
foreach (Property prop in device.Properties)
{
//LOGGER.Warn(prop.Name);
//LOGGER.Warn(prop.PropertyID);
switch ((Int32)prop.PropertyID)
{
// Document Handling Select
case 3088:
LOGGER.Warn("here");
SetProperty(prop, 517);
LOGGER.Warn("here");
break;
// Pages
case 3096:
SetProperty(prop, 1);
break;
}
}
// scan image
WIA.ICommonDialog wiaCommonDialog = new WIA.CommonDialog();
WIA.ImageFile image = (WIA.ImageFile)scanItem.Transfer(WIA.FormatID.wiaFormatPNG);
imageList.Add(image);
LOGGER.Warn("Front");
//get back side
image = (WIA.ImageFile)scanItem.Transfer(WIA.FormatID.wiaFormatPNG);
imageList.Add(image);
LOGGER.Warn("Back");
}
catch (Exception e)
{
throw (e);
}
}
return imageList;
}
Well... I tried to make duplex scan without AUTO_ADVANCE and got HRESULT: 0x8000FFFF (E_UNEXPECTED) on Transfer call. According to this post(even though that was on Windows 7) I guess there is no way to solve this for me by using WIA, still hope there will other suggestions...
Solved problem
I used saraff.twain and it worked for me:
- git page :https://github.com/saraff-9EB1047A4BEB4cef8506B29BA325BD5A/Saraff.Twain.NET
good library with grate wiki page.(Also have similar library for .net 4.6.1)

How do I play the Windows Notification sound? (It is not defined in System.Media.SystemSounds)

When using System.Media, there is something called SystemSounds where you can easily play a couple of operating system sounds:
System.Media.SystemSounds.Asterisk.Play();
System.Media.SystemSounds.Beep.Play();
System.Media.SystemSounds.Exclamation.Play();
System.Media.SystemSounds.Hand.Play();
System.Media.SystemSounds.Question.Play();
Unfortunately, there are only these five options, and in Windows 10, three of them are the same while one of them doesn't even play anything.
What I really want to do is play the Notification sound, as defined in the Sound panel (seen here):
Does anyone know how to do this?
Solution found. Code here:
using System.Media;
using Microsoft.Win32;
public void PlayNotificationSound()
{
bool found = false;
try
{
using (RegistryKey key = Registry.CurrentUser.OpenSubKey(#"AppEvents\Schemes\Apps\.Default\Notification.Default\.Current"))
{
if (key != null)
{
Object o = key.GetValue(null); // pass null to get (Default)
if (o != null)
{
SoundPlayer theSound = new SoundPlayer((String)o);
theSound.Play();
found = true;
}
}
}
}
catch
{ }
if (!found)
SystemSounds.Beep.Play(); // consolation prize
}
You can browse the keys in the registry editor to see the other sounds. Also, this example is coded to work for Windows 10, and I'm not sure what the registry structure is for other versions of Windows, so you'll need to double check what OS the user is using if you're trying to code for multiple platforms.

Getting individual windows application current volume output level as visualized in audio Mixer

I am trying to write a C# code that outputs the current audio output level from each of the windows application accessing the sound output (as shown with constantly changing green bars of the Volume mixer).
The program will check every 10 ms, and outputs sth like this: Windows Media Player: 30, Mozilla Firefox: 0, Adobe Flash Player: 35 (as per the figure)
I am using Windows 7, and trying it in C# (as Java cannot achieve this).
I have found ways to get and set the Master Volume (the handle bar which shows 65% for Windows Media Player) for a running application, is there a way to get the green fluctuating level data?
Thank you!
You can use CSCore.
There is a wrapper for the CoreAudioAPI-Audiosessions. Use something like that (for more details take a look at the unittests: AudioSession-UnitTests):
private static void Main(string[] args)
{
using (var sessionManager = GetDefaultAudioSessionManager2(DataFlow.Render))
{
using (var sessionEnumerator = sessionManager.GetSessionEnumerator())
{
foreach (var session in sessionEnumerator)
{
using (var audioMeterInformation = session.QueryInterface<AudioMeterInformation>())
{
Console.WriteLine(audioMeterInformation.GetPeakValue());
}
}
}
}
Console.ReadKey();
}
private static AudioSessionManager2 GetDefaultAudioSessionManager2(DataFlow dataFlow)
{
using (var enumerator = new MMDeviceEnumerator())
{
using (var device = enumerator.GetDefaultAudioEndpoint(dataFlow, Role.Multimedia))
{
Debug.WriteLine("DefaultDevice: " + device.FriendlyName);
var sessionManager = AudioSessionManager2.FromMMDevice(device);
return sessionManager;
}
}
}
To control an applications volume, take a look at the unit-tests here.
Here is a sample application which displays the audio levels from running applications in a graph. There are two versions, one in WPF and one in Windows.Forms. They use the method from Florian's answer to get the audio levels.
https://github.com/jeske/SoundLevelMonitor

Categories

Resources