I want to change sound level of local computer's microphone. I research so much I found this links
--> It is give codeproject link but it is so mix project and I want to change only microphone sound level
--> and this gives a project again and it is so compilcated and ı could not build project.
and ı try this.
it is work only for exe's voice level like lync,skype. And It need processes name. Microphone exe could be changed frequently. So I could not give stable name. I want to try win32 change So how can I change microphone sound level ? ı want to increase and decrease sound level
public static void SetMicrophoneVolume(string name, float level)
full code here
private static List<ISimpleAudioVolume> getAppVol(string name, EDataFlow eDataFlow)
{
List<ISimpleAudioVolume> simpleAudioVolList = new System.Collections.Generic.List<ISimpleAudioVolume>();
// get the speakers (1st render + multimedia) device
IMMDeviceEnumerator deviceEnumerator = (IMMDeviceEnumerator)(new MMDeviceEnumerator());
IMMDevice speakers;
deviceEnumerator.GetDefaultAudioEndpoint(eDataFlow, ERole.eMultimedia, out speakers);
// activate the session manager. we need the enumerator
Guid IID_IAudioSessionManager2 = typeof(IAudioSessionManager2).GUID;
object o;
speakers.Activate(ref IID_IAudioSessionManager2, 0, IntPtr.Zero, out o);
IAudioSessionManager2 mgr = (IAudioSessionManager2)o;
// enumerate sessions for on this device
IAudioSessionEnumerator sessionEnumerator;
mgr.GetSessionEnumerator(out sessionEnumerator);
int count;
sessionEnumerator.GetCount(out count);
for(int i = 0; i < count; i++)
{
IAudioSessionControl ctl;
sessionEnumerator.GetSession(i, out ctl);
IAudioSessionControl2 sessionCtl = ctl as IAudioSessionControl2;
uint processId;
string displayName;
string iconPath;
string sessionIdentifier;
string sessionInstanceIdentifier;
sessionCtl.GetProcessId(out processId);
sessionCtl.GetDisplayName(out displayName);
sessionCtl.GetIconPath(out iconPath);
sessionCtl.GetSessionIdentifier(out sessionIdentifier);
sessionCtl.GetSessionInstanceIdentifier(out sessionInstanceIdentifier);
Debug.WriteLine("process id:" + processId + " name:" + displayName + " icon:" + iconPath + " sessionIdentifier:" + sessionIdentifier + " sessionInstanceIdentifier:" + sessionInstanceIdentifier);
//Process process = Process.GetProcessesByName(name).FirstOrDefault<Process>(delegate(Process item) { return processId == item.Id; });
if(sessionIdentifier.Contains(name))
{
simpleAudioVolList.Add((ISimpleAudioVolume)sessionCtl);
}
}
Marshal.ReleaseComObject(sessionEnumerator);
Marshal.ReleaseComObject(mgr);
Marshal.ReleaseComObject(speakers);
Marshal.ReleaseComObject(deviceEnumerator);
return simpleAudioVolList;
}
private static void releasesimpleAudioVolList(List<ISimpleAudioVolume> volumeControlList)
{
foreach(ISimpleAudioVolume item in volumeControlList)
{
Marshal.ReleaseComObject(item);
}
}
public static void SetMicrophoneVolume(string name, float level)
{
List<ISimpleAudioVolume> volumeList = getAppVol(name, EDataFlow.eCapture);
Guid guid = Guid.Empty;
foreach(ISimpleAudioVolume item in volumeList)
{
try
{
item.SetMasterVolume(level / 100, ref guid);
}
catch(Exception ex)
{
Debug.WriteLine(ex.ToString());
}
}
releasesimpleAudioVolList(volumeList);
return;
}