Пример #1
0
char *getCurrentlySelectedDeviceName(ASDeviceType typeRequested) {
	 AudioDeviceID currentDeviceID = kAudioDeviceUnknown;
	 char *currentDeviceName = malloc(256 * sizeof(char));
	 currentDeviceID = getCurrentlySelectedDeviceID(typeRequested);
	 getDeviceName(currentDeviceID, currentDeviceName);
	 return currentDeviceName;
}
void showCurrentlySelectedDeviceID(ASDeviceType typeRequested) {
	AudioDeviceID currentDeviceID = kAudioDeviceUnknown;
	char currentDeviceName[256];
	
	currentDeviceID = getCurrentlySelectedDeviceID(typeRequested);
	getDeviceName(currentDeviceID, currentDeviceName);
	printf("%s\n",currentDeviceName);
}
Пример #3
0
void setDeviceWithFallback(AudioDeviceID newDeviceID, AudioDeviceID fallbackDeviceID, ASDeviceType typeRequested) {
	UInt32 propertySize = sizeof(UInt32);

	if (getCurrentlySelectedDeviceID(typeRequested) == newDeviceID) {
      setDevice(fallbackDeviceID, typeRequested);
	} else {
	  setDevice(newDeviceID, 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;
	
}