Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add option to mute all devices #61

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 62 additions & 27 deletions audio_switch.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ void showUsage(const char * appName) {
" -f format : output format (cli/human/json). Defaults to human.\n"
" -t type : device type (input/output/system/all). Defaults to output.\n"
" -m mute : sets the mute status (mute/unmute/toggle). For input/output only.\n"
" -A all : Can be used with -m mute option above to set mute status for all devices, can't be used if toggle mute status is specified\n"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There is no parameter <all> to this, right? Would mute-all and unmute-all to -m be better?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zuckschwerdt updated to use mute-all and unmute-all with -M and -U flags respectively

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The new -M and -U again don't need a parameter. It should e.g. be
" -M : sets the mute status to mute for all devices of specified device type\n"
and not
" -M mute-all : sets the mute status to mute for all devices of specified device type\n"

But my suggestion -- just a suggestion really -- was to use:
" -m mute : sets the mute status (mute/mute-all/unmute/unmute-all/toggle). For input/output only.\n"
So we don't add new options.
But that is just my preference, your modified original is also a valid option (but could be confusing as you state in the text on when it applies)
" -A : Can be used with -m mute option above to set mute status for all devices, can't be used if toggle mute status is specified\n"

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see now what you meant, updated it

" -n : cycles the audio device to the next one\n"
" -i device_id : sets the audio device to the given device by id\n"
" -u device_uid : sets the audio device to the given device by uid or a substring of the uid\n"
Expand All @@ -52,6 +53,7 @@ int runAudioSwitch(int argc, const char * argv[]) {
ASDeviceType typeRequested = kAudioTypeUnknown;
ASOutputType outputRequested = kFormatHuman;
ASMuteType muteRequested = kToggleMute;
bool muteAll = false;
int function = 0;
int result = 0;

Expand Down Expand Up @@ -103,6 +105,10 @@ int runAudioSwitch(int argc, const char * argv[]) {
}
break;

case 'A':
muteAll = true;
break;

case 'n':
// cycle to the next audio device
function = kFunctionCycleNext;
Expand Down Expand Up @@ -206,23 +212,28 @@ int runAudioSwitch(int argc, const char * argv[]) {
OSStatus status;
bool anyStatusError = false;
if (typeRequested == kAudioTypeUnknown) typeRequested = kAudioTypeInput;


if (muteRequested == kToggleMute and muteAll) {
printf("can't mute all devices if toggle mute status is specified");
return 1;
}

switch(typeRequested) {
case kAudioTypeInput:
case kAudioTypeOutput:
status = setMute(typeRequested, muteRequested);
status = setMute(typeRequested, muteRequested, muteAll);
if(status != noErr) {
printf("Failed setting mute state. Error: %d (%s)", status, GetMacOSStatusErrorString(status));
return 1;
}
break;
case kAudioTypeAll:
status = setMute(kAudioTypeInput, muteRequested);
status = setMute(kAudioTypeInput, muteRequested, muteAll);
if(status != noErr) {
printf("Failed setting mute state for input. Error: %d (%s)", status, GetMacOSStatusErrorString(status));
anyStatusError = true;
}
status = setMute(kAudioTypeOutput, muteRequested);
status = setMute(kAudioTypeOutput, muteRequested, muteAll);
if(status != noErr) {
printf("Failed setting mute state for output. Error: %d (%s)", status, GetMacOSStatusErrorString(status));
anyStatusError = true;
Expand Down Expand Up @@ -691,12 +702,26 @@ int cycleNextForOneDevice(ASDeviceType typeRequested) {
}


OSStatus setMute(ASDeviceType typeRequested, ASMuteType muteRequested) {
OSStatus setMute(ASDeviceType typeRequested, ASMuteType muteRequested, bool muteAll) {
AudioDeviceID deviceIDs[64];
int numberOfDevices = 0;
AudioDeviceID currentDeviceID = kAudioDeviceUnknown;
char currentDeviceName[256];

if (muteAll) {
numberOfDevices = getAllDevices(typeRequested, &deviceIDs);
for (int i = 0; i < numberOfDevices; ++i) {
setMuteOne(typeRequested, muteRequested, deviceIDs[i]);
}
} else {
currentDeviceID = getCurrentlySelectedDeviceID(typeRequested);
setMuteOne(typeRequested, muteRequested, currentDeviceID);
}
}

OSStatus setMuteOne(ASDeviceType typeRequested, ASMuteType muteRequested, AudioDeviceID deviceID) {
char deviceName[256];

currentDeviceID = getCurrentlySelectedDeviceID(typeRequested);
getDeviceName(currentDeviceID, currentDeviceName);
getDeviceName(deviceID, deviceName);

UInt32 scope = kAudioObjectPropertyScopeInput;

Expand Down Expand Up @@ -724,29 +749,27 @@ OSStatus setMute(ASDeviceType typeRequested, ASMuteType muteRequested) {
OSStatus status;
if (muteRequested == kToggleMute) {
UInt32 dataSize;
status = AudioObjectGetPropertyDataSize(currentDeviceID, &propertyAddress, 0, NULL, &dataSize);
status = AudioObjectGetPropertyDataSize(deviceID, &propertyAddress, 0, NULL, &dataSize);
if (status != noErr) {
return status;
}
status = AudioObjectGetPropertyData(currentDeviceID, &propertyAddress, 0, NULL, &propertySize, &muted);
status = AudioObjectGetPropertyData(deviceID, &propertyAddress, 0, NULL, &propertySize, &muted);
if (status != noErr) {
return status;
}
muted = !muted;
}

printf("Setting device %s to %s\n", currentDeviceName, muted ? "muted": "unmuted");
printf("Setting device %s to %s\n", deviceName, muted ? "muted": "unmuted");

return AudioObjectSetPropertyData(currentDeviceID, &propertyAddress, 0, NULL, propertySize, &muted);
return AudioObjectSetPropertyData(deviceID, &propertyAddress, 0, NULL, propertySize, &muted);
}

void showAllDevices(ASDeviceType typeRequested, ASOutputType outputRequested) {
int getAllDevices(ASDeviceType typeRequested, char* deviceIDs) {
UInt32 propertySize;
AudioObjectPropertyAddress propertyAddress;
AudioDeviceID dev_array[64];
int numberOfDevices = 0;
ASDeviceType device_type;
char deviceName[256];

numberOfDevices = 0;

propertyAddress.mSelector = kAudioHardwarePropertyDevices;
propertyAddress.mScope = kAudioObjectPropertyScopeGlobal;
Expand All @@ -756,40 +779,52 @@ void showAllDevices(ASDeviceType typeRequested, ASOutputType outputRequested) {
AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize);
numberOfDevices = propertySize / sizeof(AudioDeviceID);

AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize, dev_array);
AudioObjectGetPropertyData(kAudioObjectSystemObject, &propertyAddress, 0, NULL, &propertySize, deviceIDs);

return numberOfDevices;
}

void showAllDevices(ASDeviceType typeRequested, ASOutputType outputRequested) {
AudioDeviceID deviceIDs[64];
int numberOfDevices = 0;

ASDeviceType deviceType;
char deviceName[256];

numberOfDevices = getAllDevices(typeRequested, &deviceIDs);

for (int i = 0; i < numberOfDevices; ++i) {
switch (typeRequested) {
case kAudioTypeInput:
if (!isAnInputDevice(dev_array[i]))
if (!isAnInputDevice(deviceIDs[i]))
continue;
device_type = kAudioTypeInput;
deviceType = kAudioTypeInput;
break;
case kAudioTypeOutput:
if (!isAnOutputDevice(dev_array[i]))
if (!isAnOutputDevice(deviceIDs[i]))
continue;
device_type = kAudioTypeOutput;
deviceType = kAudioTypeOutput;
break;
case kAudioTypeSystemOutput:
device_type = getDeviceType(dev_array[i]);
if (device_type != kAudioTypeOutput)
deviceType = getDeviceType(deviceIDs[i]);
if (deviceType != kAudioTypeOutput)
continue;
break;
default:
break;
}

getDeviceName(dev_array[i], deviceName);
getDeviceName(deviceIDs[i], deviceName);

switch (outputRequested) {
case kFormatHuman:
printf("%s\n", deviceName);
break;
case kFormatCLI:
printf("%s,%s,%u,%s\n", deviceName, deviceTypeName(device_type), dev_array[i], getDeviceUID(dev_array[i]));
printf("%s,%s,%u,%s\n", deviceName, deviceTypeName(device_type), deviceIDs[i], getDeviceUID(deviceIDs[i]));
break;
case kFormatJSON:
printf("{\"name\": \"%s\", \"type\": \"%s\", \"id\": \"%u\", \"uid\": \"%s\"}\n", deviceName, deviceTypeName(device_type), dev_array[i], getDeviceUID(dev_array[i]));
printf("{\"name\": \"%s\", \"type\": \"%s\", \"id\": \"%u\", \"uid\": \"%s\"}\n", deviceName, deviceTypeName(deviceType), deviceIDs[i], getDeviceUID(deviceIDs[i]));
break;
default:
break;
Expand Down