void showAllDevices(ASDeviceType typeRequested) { UInt32 propertySize; AudioDeviceID dev_array[64]; int numberOfDevices = 0; ASDeviceType device_type; char deviceName[256]; AudioHardwareGetPropertyInfo(kAudioHardwarePropertyDevices, &propertySize, NULL); AudioHardwareGetProperty(kAudioHardwarePropertyDevices, &propertySize, dev_array); numberOfDevices = (propertySize / sizeof(AudioDeviceID)); for(int i = 0; i < numberOfDevices; ++i) { device_type = getDeviceType(dev_array[i]); switch(typeRequested) { case kAudioTypeInput: case kAudioTypeOutput: if (device_type != typeRequested) continue; break; case kAudioTypeSystemOutput: if (device_type != kAudioTypeOutput) continue; break; } getDeviceName(dev_array[i], deviceName); printf("%s (%s)\n",deviceName,deviceTypeName(device_type)); } }
void setDevice(AudioDeviceID newDeviceID, ASDeviceType typeRequested) { UInt32 propertySize = sizeof(UInt32); switch(typeRequested) { case kAudioTypeInput: AudioHardwareSetProperty(kAudioHardwarePropertyDefaultInputDevice, propertySize, &newDeviceID); break; case kAudioTypeOutput: AudioHardwareSetProperty(kAudioHardwarePropertyDefaultOutputDevice, propertySize, &newDeviceID); break; case kAudioTypeSystemOutput: AudioHardwareSetProperty(kAudioHardwarePropertyDefaultSystemOutputDevice, propertySize, &newDeviceID); break; } printf("%s audio device set to \"%s\"\n", deviceTypeName(typeRequested), getCurrentlySelectedDeviceName(typeRequested)); }
int runAudioSwitch(int argc, const char * argv[]) { char requestedDeviceName[256]; AudioDeviceID chosenDeviceID = kAudioDeviceUnknown; ASDeviceType typeRequested = kAudioTypeUnknown; int function = 0; int c; while ((c = getopt(argc, (char **)argv, "hacnt:s:")) != -1) { switch (c) { case 'a': // show all function = kFunctionShowAll; break; case 'c': // get current device function = kFunctionShowCurrent; break; case 'h': // show help function = kFunctionShowHelp; break; case 'n': // cycle to the next audio device function = kFunctionCycleNext; break; case 's': // set the requestedDeviceName function = kFunctionSetDevice; strcpy(requestedDeviceName, optarg); break; case 't': // set the requestedDeviceName if (strcmp(optarg, "input") == 0) { typeRequested = kAudioTypeInput; } else if (strcmp(optarg, "output") == 0) { typeRequested = kAudioTypeOutput; } else if (strcmp(optarg, "system") == 0) { typeRequested = kAudioTypeSystemOutput; } else { printf("Invalid device type \"%s\" specified.\n",optarg); showUsage(argv[0]); return 1; } break; } } if (function == kFunctionShowAll) { switch(typeRequested) { case kAudioTypeInput: case kAudioTypeOutput: showAllDevices(typeRequested); break; case kAudioTypeSystemOutput: showAllDevices(kAudioTypeOutput); break; default: showAllDevices(kAudioTypeInput); showAllDevices(kAudioTypeOutput); } return 0; } if (function == kFunctionShowHelp) { showUsage(argv[0]); return 0; } if (function == kFunctionShowCurrent) { if (typeRequested == kAudioTypeUnknown) typeRequested = kAudioTypeOutput; showCurrentlySelectedDeviceID(typeRequested); return 0; } if (typeRequested == kAudioTypeUnknown) typeRequested = kAudioTypeOutput; if (function == kFunctionCycleNext) { // get current device of requested type chosenDeviceID = getCurrentlySelectedDeviceID(typeRequested); if (chosenDeviceID == kAudioDeviceUnknown) { printf("Could not find current audio device of type %s. Nothing was changed.\n", deviceTypeName(typeRequested)); return 1; } // find next device to current device chosenDeviceID = getNextDeviceID(chosenDeviceID, typeRequested); if (chosenDeviceID == kAudioDeviceUnknown) { printf("Could not find next audio device of type %s. Nothing was changed.\n", deviceTypeName(typeRequested)); return 1; } // choose the requested audio device setDevice(chosenDeviceID, typeRequested); getDeviceName(chosenDeviceID, requestedDeviceName); printf("%s audio device set to \"%s\"\n", deviceTypeName(typeRequested), requestedDeviceName); return 0; } if (function != kFunctionSetDevice) { printf("Please specify audio device.\n"); showUsage(argv[0]); return 1; } // find the id of the requested device chosenDeviceID = getRequestedDeviceID(requestedDeviceName, typeRequested); if (chosenDeviceID == kAudioDeviceUnknown) { printf("Could not find an audio device named \"%s\" of type %s. Nothing was changed.\n",requestedDeviceName, deviceTypeName(typeRequested)); return 1; } // choose the requested audio device setDevice(chosenDeviceID, typeRequested); printf("%s audio device set to \"%s\"\n", deviceTypeName(typeRequested), requestedDeviceName); return 0; }