I’m using NAudio to do a few tasks:
- Locate the “Stereo Mix” source line
- Un-mute the “Stereo Mix” source line by enabling any line controls present
- Mute all other source lines on the same input device by disabling any line controls present
The program I’ve written can perform task 1 okay, but tasks 2 and 3 fail.
Specifically, this block of code causes the ArgumentException to be thrown:
if( control.IsBoolean ) {
BooleanMixerControl boolControl = (BooleanMixerControl)control;
boolControl.Value = isMuted;
set = true;
if( boolControl.Value != isMuted )
throw new ArgumentException("Could not set line muted value.");
}
Here is a static class that I use to perform these tasks. It has a dependency on the current version of NAudio:
public static class RecordSourceManager {
public static Boolean GetMicrophoneMuted(String deviceName) {
Mixer mixer = GetMixer( deviceName );
if( mixer == null ) throw new ArgumentException("Specified device \"" + deviceName + "\" does not exist.");
foreach(MixerLine line in mixer.Destinations) {
foreach(MixerLine sourceLine in line.Sources) {
if( sourceLine.ComponentType == MixerLineComponentType.SourceMicrophone ) {
return GetLineMuted( sourceLine );
}
}
}
throw new ArgumentException("Specified device \"" + deviceName + "\" does not contain a microphone device.");
}
public static void SetMicrophoneExclusive(String deviceName, Boolean enableMicrophoneExclusivity) {
Mixer mixer = GetMixer( deviceName );
if( mixer == null ) throw new ArgumentException("Specified device \"" + deviceName + "\" does not exist.");
foreach(MixerLine line in mixer.Destinations) {
foreach(MixerLine sourceLine in line.Sources) {
if( sourceLine.ComponentType == MixerLineComponentType.SourceMicrophone ) {
SetLineMuted( sourceLine, !enableMicrophoneExclusivity );
} else {
SetLineMuted( sourceLine, enableMicrophoneExclusivity );
}
}
}
}
public static Boolean GetStereoMixMuted(String deviceName) {
Mixer mixer = GetMixer( deviceName );
if( mixer == null ) throw new ArgumentException("Specified device \"" + deviceName + "\" does not exist.");
foreach(MixerLine line in mixer.Destinations) {
foreach(MixerLine sourceLine in line.Sources) {
if( IsStereoMix( sourceLine.Name ) ) {
return GetLineMuted( sourceLine );
}
}
}
throw new ArgumentException("Specified device \"" + deviceName + "\" does not contain a microphone device.");
}
public static void SetStereoMixExclusive(String deviceName, Boolean enableStereoMixExclusivity) {
Mixer mixer = GetMixer( deviceName );
if( mixer == null ) throw new ArgumentException("Specified device \"" + deviceName + "\" does not exist.");
MixerLine stereoMix;
MixerLine parentLine;
GetStereoMixLine( mixer, out stereoMix, out parentLine );
if( stereoMix == null ) throw new ArgumentException("Specified device \"" + deviceName + "\" does not contain a Stereo Mix line.");
foreach(MixerLine source in parentLine.Sources) {
Boolean ok;
if( IsStereoMix( source.Name ) ) {
ok = SetLineMuted( source, !enableStereoMixExclusivity );
} else {
ok = SetLineMuted( source, enableStereoMixExclusivity );
}
if( !ok ) throw new ArgumentException("Could not set line muted state.");
}
}
private static Mixer GetMixer(String deviceName) {
foreach(Mixer mixer in Mixer.Mixers) {
//wtr.WriteLine("Mixer: {0}, Mfg: {1}", mixer.Name, mixer.Manufacturer );
if( String.Equals( mixer.Name, deviceName, StringComparison.OrdinalIgnoreCase ) ) return mixer;
}
return null;
}
private static void GetStereoMixLine(Mixer device, out MixerLine stereoMix, out MixerLine parentLine) {
foreach(MixerLine line in device.Destinations) {
foreach(MixerLine source in line.Sources) {
if( IsStereoMix( source.Name ) ) {
stereoMix = source;
parentLine = line;
return;
}
}
}
stereoMix = null;
parentLine = null;
}
private static Boolean IsStereoMix(String sourceName) {
String[] names = new String[] {
"Stereo Mix",
"What U Hear",
"What \"U\" Hear",
"What-U-Hear",
"Playback Redirect",
"Wave Out",
"Wave Out Mix",
"Wave-Out Mix"
};
foreach(String name in names) {
if( String.Equals( sourceName, name, StringComparison.OrdinalIgnoreCase ) ) return true;
}
return false;
}
private static Boolean SetLineMuted(MixerLine line, Boolean isMuted) {
Boolean set = false;
foreach(MixerControl control in line.Controls) {
// Can't test if control's name == "Mute" because sometimes it's "Mic Volume" (even though it's boolean). Same goes for GetLineMuted.
if( control.IsBoolean ) {
BooleanMixerControl boolControl = (BooleanMixerControl)control;
boolControl.Value = isMuted;
set = true;
if( boolControl.Value != isMuted )
throw new ArgumentException("Could not set line muted value.");
}
}
return set;
}
private static Boolean GetLineMuted(MixerLine line) {
foreach(MixerControl control in line.Controls) {
if( control.IsBoolean ) {
BooleanMixerControl boolControl = (BooleanMixerControl)control;
return boolControl.Value;
}
}
return false;
}
}
I thought I’d take a look at NAudio’s BooleanMixerControl class, and I see this:
public bool Value {
get {
base.GetControlDetails();
return (this.boolDetails.fValue == 1);
}
set {
MmException.Try(MixerInterop.mixerSetControlDetails(base.mixerHandle, ref this.mixerControlDetails, base.mixerHandleType), "mixerSetControlDetails");
}
}
Interestingly it seems the value argument to the property setter is being ignored, the mixerSetControlDetails call therefore won’t do any useful work. Is this a bug in NAudio?
This feature is not implemented, and probably should be replaced with a
NotImplementedException. It was amongst the first code I ever wrote for NAudio back in 2002 when I was just beginning to learn about PInvoke and pinning, and if you look at the NAudio source you will see the code that uses the value is commented out, presumably because it caused some kind of memory exception when I originally tried it.The reason it has never been fixed is that I’ve never needed to use it myself, but if someone wants to contribute a fix I’d be glad to include it in NAudio.