bool audio::orchestra::api::Core::open(uint32_t _device, audio::orchestra::mode _mode, uint32_t _channels, uint32_t _firstChannel, uint32_t _sampleRate, audio::format _format, uint32_t *_bufferSize, const audio::orchestra::StreamOptions& _options) { // Get device ID uint32_t nDevices = getDeviceCount(); if (nDevices == 0) { // This should not happen because a check is made before this function is called. ATA_ERROR("no devices found!"); return false; } if (_device >= nDevices) { // This should not happen because a check is made before this function is called. ATA_ERROR("device ID is invalid!"); return false; } AudioDeviceID deviceList[ nDevices/2 ]; uint32_t dataSize = sizeof(AudioDeviceID) * nDevices/2; AudioObjectPropertyAddress property = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; OSStatus result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &property, 0, nullptr, &dataSize, (void *) &deviceList); if (result != noErr) { ATA_ERROR("OS-X system error getting device IDs."); return false; } AudioDeviceID id = deviceList[ _device/2 ]; // Setup for stream mode. bool isInput = false; if (_mode == audio::orchestra::mode_input) { isInput = true; property.mScope = kAudioDevicePropertyScopeInput; } else { property.mScope = kAudioDevicePropertyScopeOutput; } // Get the stream "configuration". AudioBufferList *bufferList = nil; dataSize = 0; property.mSelector = kAudioDevicePropertyStreamConfiguration; result = AudioObjectGetPropertyDataSize(id, &property, 0, nullptr, &dataSize); if ( result != noErr || dataSize == 0) { ATA_ERROR("system error (" << getErrorCode(result) << ") getting stream configuration info for device (" << _device << ")."); return false; } // Allocate the AudioBufferList. bufferList = (AudioBufferList *) malloc(dataSize); if (bufferList == nullptr) { ATA_ERROR("memory error allocating AudioBufferList."); return false; } result = AudioObjectGetPropertyData(id, &property, 0, nullptr, &dataSize, bufferList); if ( result != noErr || dataSize == 0) { ATA_ERROR("system error (" << getErrorCode(result) << ") getting stream configuration for device (" << _device << ")."); return false; } // Search for one or more streams that contain the desired number of // channels. CoreAudio devices can have an arbitrary number of // streams and each stream can have an arbitrary number of channels. // For each stream, a single buffer of interleaved samples is // provided. orchestra prefers the use of one stream of interleaved // data or multiple consecutive single-channel streams. However, we // now support multiple consecutive multi-channel streams of // interleaved data as well. uint32_t iStream, offsetCounter = _firstChannel; uint32_t nStreams = bufferList->mNumberBuffers; bool monoMode = false; bool foundStream = false; // First check that the device supports the requested number of // channels. uint32_t deviceChannels = 0; for (iStream=0; iStream<nStreams; iStream++) { deviceChannels += bufferList->mBuffers[iStream].mNumberChannels; } if (deviceChannels < (_channels + _firstChannel)) { free(bufferList); ATA_ERROR("the device (" << _device << ") does not support the requested channel count."); return false; } // Look for a single stream meeting our needs. uint32_t firstStream, streamCount = 1, streamChannels = 0, channelOffset = 0; for (iStream=0; iStream<nStreams; iStream++) { streamChannels = bufferList->mBuffers[iStream].mNumberChannels; if (streamChannels >= _channels + offsetCounter) { firstStream = iStream; channelOffset = offsetCounter; foundStream = true; break; } if (streamChannels > offsetCounter) { break; } offsetCounter -= streamChannels; } // If we didn't find a single stream above, then we should be able // to meet the channel specification with multiple streams. if (foundStream == false) { monoMode = true; offsetCounter = _firstChannel; for (iStream=0; iStream<nStreams; iStream++) { streamChannels = bufferList->mBuffers[iStream].mNumberChannels; if (streamChannels > offsetCounter) { break; } offsetCounter -= streamChannels; } firstStream = iStream; channelOffset = offsetCounter; int32_t channelCounter = _channels + offsetCounter - streamChannels; if (streamChannels > 1) { monoMode = false; } while (channelCounter > 0) { streamChannels = bufferList->mBuffers[++iStream].mNumberChannels; if (streamChannels > 1) { monoMode = false; } channelCounter -= streamChannels; streamCount++; } } free(bufferList); // Determine the buffer size. AudioValueRange bufferRange; dataSize = sizeof(AudioValueRange); property.mSelector = kAudioDevicePropertyBufferFrameSizeRange; result = AudioObjectGetPropertyData(id, &property, 0, nullptr, &dataSize, &bufferRange); if (result != noErr) { ATA_ERROR("system error (" << getErrorCode(result) << ") getting buffer size range for device (" << _device << ")."); return false; } if (bufferRange.mMinimum > *_bufferSize) { *_bufferSize = (uint64_t) bufferRange.mMinimum; } else if (bufferRange.mMaximum < *_bufferSize) { *_bufferSize = (uint64_t) bufferRange.mMaximum; } if (_options.flags.m_minimizeLatency == true) { *_bufferSize = (uint64_t) bufferRange.mMinimum; } // Set the buffer size. For multiple streams, I'm assuming we only // need to make this setting for the master channel. uint32_t theSize = (uint32_t) *_bufferSize; dataSize = sizeof(uint32_t); property.mSelector = kAudioDevicePropertyBufferFrameSize; result = AudioObjectSetPropertyData(id, &property, 0, nullptr, dataSize, &theSize); if (result != noErr) { ATA_ERROR("system error (" << getErrorCode(result) << ") setting the buffer size for device (" << _device << ")."); return false; } // If attempting to setup a duplex stream, the bufferSize parameter // MUST be the same in both directions! *_bufferSize = theSize; if ( m_mode == audio::orchestra::mode_output && _mode == audio::orchestra::mode_input && *_bufferSize != m_bufferSize) { ATA_ERROR("system error setting buffer size for duplex stream on device (" << _device << ")."); return false; } m_bufferSize = *_bufferSize; m_nBuffers = 1; // Check and if necessary, change the sample rate for the device. double nominalRate; dataSize = sizeof(double); property.mSelector = kAudioDevicePropertyNominalSampleRate; result = AudioObjectGetPropertyData(id, &property, 0, nullptr, &dataSize, &nominalRate); if (result != noErr) { ATA_ERROR("system error (" << getErrorCode(result) << ") getting current sample rate."); return false; } // Only change the sample rate if off by more than 1 Hz. if (fabs(nominalRate - (double)_sampleRate) > 1.0) { // Set a property listener for the sample rate change double reportedRate = 0.0; AudioObjectPropertyAddress tmp = { kAudioDevicePropertyNominalSampleRate, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; result = AudioObjectAddPropertyListener(id, &tmp, &rateListener, (void *) &reportedRate); if (result != noErr) { ATA_ERROR("system error (" << getErrorCode(result) << ") setting sample rate property listener for device (" << _device << ")."); return false; } nominalRate = (double) _sampleRate; result = AudioObjectSetPropertyData(id, &property, 0, nullptr, dataSize, &nominalRate); if (result != noErr) { ATA_ERROR("system error (" << getErrorCode(result) << ") setting sample rate for device (" << _device << ")."); return false; } // Now wait until the reported nominal rate is what we just set. uint32_t microCounter = 0; while (reportedRate != nominalRate) { microCounter += 5000; if (microCounter > 5000000) { break; } std::this_thread::sleep_for(std::chrono::milliseconds(5)); } // Remove the property listener. AudioObjectRemovePropertyListener(id, &tmp, &rateListener, (void *) &reportedRate); if (microCounter > 5000000) { ATA_ERROR("timeout waiting for sample rate update for device (" << _device << ")."); return false; } } // Now set the stream format for all streams. Also, check the // physical format of the device and change that if necessary. AudioStreamBasicDescription description; dataSize = sizeof(AudioStreamBasicDescription); property.mSelector = kAudioStreamPropertyVirtualFormat; result = AudioObjectGetPropertyData(id, &property, 0, nullptr, &dataSize, &description); if (result != noErr) { ATA_ERROR("system error (" << getErrorCode(result) << ") getting stream format for device (" << _device << ")."); return false; } // Set the sample rate and data format id. However, only make the // change if the sample rate is not within 1.0 of the desired // rate and the format is not linear pcm. bool updateFormat = false; if (fabs(description.mSampleRate - (double)_sampleRate) > 1.0) { description.mSampleRate = (double) _sampleRate; updateFormat = true; } if (description.mFormatID != kAudioFormatLinearPCM) { description.mFormatID = kAudioFormatLinearPCM; updateFormat = true; } if (updateFormat) { result = AudioObjectSetPropertyData(id, &property, 0, nullptr, dataSize, &description); if (result != noErr) { ATA_ERROR("system error (" << getErrorCode(result) << ") setting sample rate or data format for device (" << _device << ")."); return false; } } // Now check the physical format. property.mSelector = kAudioStreamPropertyPhysicalFormat; result = AudioObjectGetPropertyData(id, &property, 0, nullptr, &dataSize, &description); if (result != noErr) { ATA_ERROR("system error (" << getErrorCode(result) << ") getting stream physical format for device (" << _device << ")."); return false; } //std::cout << "Current physical stream format:" << std::endl; //std::cout << " mBitsPerChan = " << description.mBitsPerChannel << std::endl; //std::cout << " aligned high = " << (description.mFormatFlags & kAudioFormatFlagIsAlignedHigh) << ", isPacked = " << (description.mFormatFlags & kAudioFormatFlagIsPacked) << std::endl; //std::cout << " bytesPerFrame = " << description.mBytesPerFrame << std::endl; //std::cout << " sample rate = " << description.mSampleRate << std::endl; if ( description.mFormatID != kAudioFormatLinearPCM || description.mBitsPerChannel < 16) { description.mFormatID = kAudioFormatLinearPCM; //description.mSampleRate = (double) sampleRate; AudioStreamBasicDescription testDescription = description; uint32_t formatFlags; // We'll try higher bit rates first and then work our way down. std::vector< std::pair<uint32_t, uint32_t> > physicalFormats; formatFlags = (description.mFormatFlags | kLinearPCMFormatFlagIsFloat) & ~kLinearPCMFormatFlagIsSignedInteger; physicalFormats.push_back(std::pair<float, uint32_t>(32, formatFlags)); formatFlags = (description.mFormatFlags | kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked) & ~kLinearPCMFormatFlagIsFloat; physicalFormats.push_back(std::pair<float, uint32_t>(32, formatFlags)); physicalFormats.push_back(std::pair<float, uint32_t>(24, formatFlags)); // 24-bit packed formatFlags &= ~(kAudioFormatFlagIsPacked | kAudioFormatFlagIsAlignedHigh); physicalFormats.push_back(std::pair<float, uint32_t>(24.2, formatFlags)); // 24-bit in 4 bytes, aligned low formatFlags |= kAudioFormatFlagIsAlignedHigh; physicalFormats.push_back(std::pair<float, uint32_t>(24.4, formatFlags)); // 24-bit in 4 bytes, aligned high formatFlags = (description.mFormatFlags | kLinearPCMFormatFlagIsSignedInteger | kAudioFormatFlagIsPacked) & ~kLinearPCMFormatFlagIsFloat; physicalFormats.push_back(std::pair<float, uint32_t>(16, formatFlags)); physicalFormats.push_back(std::pair<float, uint32_t>(8, formatFlags)); bool setPhysicalFormat = false; for(uint32_t i=0; i<physicalFormats.size(); i++) { testDescription = description; testDescription.mBitsPerChannel = (uint32_t) physicalFormats[i].first; testDescription.mFormatFlags = physicalFormats[i].second; if ( (24 == (uint32_t)physicalFormats[i].first) && ~(physicalFormats[i].second & kAudioFormatFlagIsPacked)) { testDescription.mBytesPerFrame = 4 * testDescription.mChannelsPerFrame; } else { testDescription.mBytesPerFrame = testDescription.mBitsPerChannel/8 * testDescription.mChannelsPerFrame; } testDescription.mBytesPerPacket = testDescription.mBytesPerFrame * testDescription.mFramesPerPacket; result = AudioObjectSetPropertyData(id, &property, 0, nullptr, dataSize, &testDescription); if (result == noErr) { setPhysicalFormat = true; //std::cout << "Updated physical stream format:" << std::endl; //std::cout << " mBitsPerChan = " << testDescription.mBitsPerChannel << std::endl; //std::cout << " aligned high = " << (testDescription.mFormatFlags & kAudioFormatFlagIsAlignedHigh) << ", isPacked = " << (testDescription.mFormatFlags & kAudioFormatFlagIsPacked) << std::endl; //std::cout << " bytesPerFrame = " << testDescription.mBytesPerFrame << std::endl; //std::cout << " sample rate = " << testDescription.mSampleRate << std::endl; break; } } if (!setPhysicalFormat) { ATA_ERROR("system error (" << getErrorCode(result) << ") setting physical data format for device (" << _device << ")."); return false; } } // done setting virtual/physical formats. // Get the stream / device latency. uint32_t latency; dataSize = sizeof(uint32_t); property.mSelector = kAudioDevicePropertyLatency; if (AudioObjectHasProperty(id, &property) == true) { result = AudioObjectGetPropertyData(id, &property, 0, nullptr, &dataSize, &latency); if (result == kAudioHardwareNoError) { m_latency[ _mode ] = latency; } else { ATA_ERROR("system error (" << getErrorCode(result) << ") getting device latency for device (" << _device << ")."); return false; } } // Byte-swapping: According to AudioHardware.h, the stream data will // always be presented in native-endian format, so we should never // need to byte swap. m_doByteSwap[modeToIdTable(_mode)] = false; // From the CoreAudio documentation, PCM data must be supplied as // 32-bit floats. m_userFormat = _format; m_deviceFormat[modeToIdTable(_mode)] = audio::format_float; if (streamCount == 1) { m_nDeviceChannels[modeToIdTable(_mode)] = description.mChannelsPerFrame; } else { // multiple streams m_nDeviceChannels[modeToIdTable(_mode)] = _channels; } m_nUserChannels[modeToIdTable(_mode)] = _channels; m_channelOffset[modeToIdTable(_mode)] = channelOffset; // offset within a CoreAudio stream m_deviceInterleaved[modeToIdTable(_mode)] = true; if (monoMode == true) { m_deviceInterleaved[modeToIdTable(_mode)] = false; } // Set flags for buffer conversion. m_doConvertBuffer[modeToIdTable(_mode)] = false; if (m_userFormat != m_deviceFormat[modeToIdTable(_mode)]) { m_doConvertBuffer[modeToIdTable(_mode)] = true; } if (m_nUserChannels[modeToIdTable(_mode)] < m_nDeviceChannels[modeToIdTable(_mode)]) { m_doConvertBuffer[modeToIdTable(_mode)] = true; } if (streamCount == 1) { if ( m_nUserChannels[modeToIdTable(_mode)] > 1 && m_deviceInterleaved[modeToIdTable(_mode)] == false) { m_doConvertBuffer[modeToIdTable(_mode)] = true; } } else if (monoMode) { m_doConvertBuffer[modeToIdTable(_mode)] = true; } m_private->iStream[modeToIdTable(_mode)] = firstStream; m_private->nStreams[modeToIdTable(_mode)] = streamCount; m_private->id[modeToIdTable(_mode)] = id; // Allocate necessary internal buffers. uint64_t bufferBytes; bufferBytes = m_nUserChannels[modeToIdTable(_mode)] * *_bufferSize * audio::getFormatBytes(m_userFormat); // m_userBuffer[modeToIdTable(_mode)] = (char *) calloc(bufferBytes, 1); m_userBuffer[modeToIdTable(_mode)].resize(bufferBytes, 0); if (m_userBuffer[modeToIdTable(_mode)].size() == 0) { ATA_ERROR("error allocating user buffer memory."); goto error; } // If possible, we will make use of the CoreAudio stream buffers as // "device buffers". However, we can't do this if using multiple // streams. if ( m_doConvertBuffer[modeToIdTable(_mode)] && m_private->nStreams[modeToIdTable(_mode)] > 1) { bool makeBuffer = true; bufferBytes = m_nDeviceChannels[modeToIdTable(_mode)] * audio::getFormatBytes(m_deviceFormat[modeToIdTable(_mode)]); if (_mode == audio::orchestra::mode_input) { if ( m_mode == audio::orchestra::mode_output && m_deviceBuffer) { uint64_t bytesOut = m_nDeviceChannels[0] * audio::getFormatBytes(m_deviceFormat[0]); if (bufferBytes <= bytesOut) { makeBuffer = false; } } } if (makeBuffer) { bufferBytes *= *_bufferSize; if (m_deviceBuffer) { free(m_deviceBuffer); m_deviceBuffer = nullptr; } m_deviceBuffer = (char *) calloc(bufferBytes, 1); if (m_deviceBuffer == nullptr) { ATA_ERROR("error allocating device buffer memory."); goto error; } } } m_sampleRate = _sampleRate; m_device[modeToIdTable(_mode)] = _device; m_state = audio::orchestra::state::stopped; ATA_VERBOSE("Set state as stopped"); // Setup the buffer conversion information structure. if (m_doConvertBuffer[modeToIdTable(_mode)]) { if (streamCount > 1) { setConvertInfo(_mode, 0); } else { setConvertInfo(_mode, channelOffset); } } if ( _mode == audio::orchestra::mode_input && m_mode == audio::orchestra::mode_output && m_device[0] == _device) { // Only one callback procedure per device. m_mode = audio::orchestra::mode_duplex; } else { #if defined(MAC_OS_X_VERSION_10_5) && (MAC_OS_X_VERSION_MIN_REQUIRED >= MAC_OS_X_VERSION_10_5) result = AudioDeviceCreateIOProcID(id, &audio::orchestra::api::Core::callbackEvent, this, &m_private->procId[modeToIdTable(_mode)]); #else // deprecated in favor of AudioDeviceCreateIOProcID() result = AudioDeviceAddIOProc(id, &audio::orchestra::api::Core::callbackEvent, this); #endif if (result != noErr) { ATA_ERROR("system error setting callback for device (" << _device << ")."); goto error; } if ( m_mode == audio::orchestra::mode_output && _mode == audio::orchestra::mode_input) { m_mode = audio::orchestra::mode_duplex; } else { m_mode = _mode; } } // Setup the device property listener for over/underload. property.mSelector = kAudioDeviceProcessorOverload; result = AudioObjectAddPropertyListener(id, &property, &audio::orchestra::api::Core::xrunListener, this); return true; error: m_userBuffer[0].clear(); m_userBuffer[1].clear(); if (m_deviceBuffer) { free(m_deviceBuffer); m_deviceBuffer = 0; } m_state = audio::orchestra::state::closed; ATA_VERBOSE("Set state as closed"); return false; }
bool CAUOutputDevice::GetPreferredChannelLayout(CCoreAudioChannelLayout& layout) { if (!m_DeviceId) return false; AudioObjectPropertyAddress propertyAddress; propertyAddress.mScope = kAudioDevicePropertyScopeOutput; propertyAddress.mElement = 0; propertyAddress.mSelector = kAudioDevicePropertyPreferredChannelLayout; if (!AudioObjectHasProperty(m_DeviceId, &propertyAddress)) return false; UInt32 propertySize = 0; OSStatus ret = AudioObjectGetPropertyDataSize(m_DeviceId, &propertyAddress, 0, NULL, &propertySize); if (ret != noErr) CLog::Log(LOGERROR, "CAUOutputDevice::GetPreferredChannelLayout: " "Unable to retrieve preferred channel layout size. Error = %s", GetError(ret).c_str()); void *pBuf = malloc(propertySize); ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &propertySize, pBuf); if (ret != noErr) CLog::Log(LOGERROR, "CAUOutputDevice::GetPreferredChannelLayout: " "Unable to retrieve preferred channel layout. Error = %s", GetError(ret).c_str()); else { // Copy the result into the caller's instance layout.CopyLayout(*((AudioChannelLayout*)pBuf)); } free(pBuf); return (ret == noErr); }
static inline gboolean _audio_device_set_mixing (AudioDeviceID device_id, gboolean enable_mix) { OSStatus status = noErr; UInt32 propertySize = 0, can_mix = enable_mix; Boolean writable = FALSE; gboolean res = FALSE; AudioObjectPropertyAddress audioDeviceSupportsMixingAddress = { kAudioDevicePropertySupportsMixing, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; if (AudioObjectHasProperty (device_id, &audioDeviceSupportsMixingAddress)) { /* Set mixable to false if we are allowed to */ status = AudioObjectIsPropertySettable (device_id, &audioDeviceSupportsMixingAddress, &writable); if (status) { GST_DEBUG ("AudioObjectIsPropertySettable: %d", (int) status); } status = AudioObjectGetPropertyDataSize (device_id, &audioDeviceSupportsMixingAddress, 0, NULL, &propertySize); if (status) { GST_DEBUG ("AudioObjectGetPropertyDataSize: %d", (int) status); } status = AudioObjectGetPropertyData (device_id, &audioDeviceSupportsMixingAddress, 0, NULL, &propertySize, &can_mix); if (status) { GST_DEBUG ("AudioObjectGetPropertyData: %d", (int) status); } if (status == noErr && writable) { can_mix = enable_mix; status = AudioObjectSetPropertyData (device_id, &audioDeviceSupportsMixingAddress, 0, NULL, propertySize, &can_mix); res = TRUE; } if (status != noErr) { GST_ERROR ("failed to set mixmode: %d", (int) status); } } else { GST_DEBUG ("property not found, mixing coudln't be changed"); } return res; }
static BOOL DeviceHasMute(AudioDeviceID deviceID, Boolean isInput) { Boolean writable = false; OSStatus err = noErr; AudioObjectPropertyAddress propertyAddress; propertyAddress.mSelector = kAudioDevicePropertyMute; propertyAddress.mScope = isInput ? kAudioDevicePropertyScopeInput : kAudioDevicePropertyScopeOutput; propertyAddress.mElement = 0; if (AudioObjectHasProperty(deviceID, &propertyAddress)) { /* check if we can set it */ err = AudioObjectIsPropertySettable(deviceID, &propertyAddress, &writable); if (err == noErr) return writable; } return FALSE; }
bool CCoreAudioDevice::GetMixingSupport() { if (!m_DeviceId) return false; UInt32 size; UInt32 mix = 0; Boolean writable = false; AudioObjectPropertyAddress propertyAddress; propertyAddress.mScope = kAudioDevicePropertyScopeOutput; propertyAddress.mElement = 0; propertyAddress.mSelector = kAudioDevicePropertySupportsMixing; if( AudioObjectHasProperty( m_DeviceId, &propertyAddress ) ) { OSStatus ret = AudioObjectIsPropertySettable(m_DeviceId, &propertyAddress, &writable); if (ret) { CLog::Log(LOGERROR, "CCoreAudioDevice::SupportsMixing: " "Unable to get propertyinfo mixing support. Error = %s", GetError(ret).c_str()); writable = false; } if (writable) { size = sizeof(mix); ret = AudioObjectGetPropertyData(m_DeviceId, &propertyAddress, 0, NULL, &size, &mix); if (ret != noErr) mix = 0; } } CLog::Log(LOGDEBUG, "CCoreAudioDevice::SupportsMixing: " "Device mixing support : %s.", mix ? "'Yes'" : "'No'"); return (mix > 0); }
static OSStatus ca_change_mixing(struct ao *ao, AudioDeviceID device, uint32_t val, bool *changed) { *changed = false; AudioObjectPropertyAddress p_addr = (AudioObjectPropertyAddress) { .mSelector = kAudioDevicePropertySupportsMixing, .mScope = kAudioObjectPropertyScopeGlobal, .mElement = kAudioObjectPropertyElementMaster, }; if (AudioObjectHasProperty(device, &p_addr)) { OSStatus err; Boolean writeable = 0; err = CA_SETTABLE(device, kAudioDevicePropertySupportsMixing, &writeable); if (!CHECK_CA_WARN("can't tell if mixing property is settable")) { return err; } if (!writeable) return noErr; err = CA_SET(device, kAudioDevicePropertySupportsMixing, &val); if (err != noErr) return err; if (!CHECK_CA_WARN("can't set mix mode")) { return err; } *changed = true; } return noErr; }
bool CAHALAudioObject::HasProperty(AudioObjectPropertyAddress& inAddress) const { return AudioObjectHasProperty(mObjectID, &inAddress); }
int main (int argc, const char * argv[]) { OSStatus result = noErr; Float32 theVolume = 0.0; UInt32 theMute = 0; Boolean doSetMute = false, doPrintDeviceList = true; const char* theNewDefaultDeviceString = NULL; Boolean didFindNewDevice = false; CFStringRef theNewDefaultDeviceName = 0; for (int i = 1; i < argc; ++i) { const char *arg = argv[i]; if (arg[0] != '-') { usage(); } else { arg += 1; if (arg[0] == 'v' || !strcmp(arg, "-volume")) { if (++i == argc) MissingArgument(); arg = argv[i]; sscanf(arg, "%f", &theVolume); } else if (arg[0] == 'm' || !strcmp(arg, "-mute")) { if (++i == argc) MissingArgument(); arg = argv[i]; doSetMute = true; sscanf(arg, "%d", &theMute); } else if (arg[0] == 'd' || !strcmp(arg, "-device")) { if (++i == argc) MissingArgument(); theNewDefaultDeviceString = argv[i]; theNewDefaultDeviceName = CFStringCreateWithCString(kCFAllocatorDefault, theNewDefaultDeviceString, CFStringGetSystemEncoding()); } else if (arg[0] == 'a' || !strcmp(arg, "-all")) { doPrintDeviceList = true; } else if (arg[0] == 'h' || !strcmp(arg, "-help")) { usage(); } else { fprintf(stderr, "unknown argument: %s\n\n", arg - 1); usage(); } } } UInt32 thePropSize; AudioDeviceID *theDeviceList = NULL; UInt32 theNumDevices = 0; if (doPrintDeviceList || theNewDefaultDeviceString) { // get the device list AudioObjectPropertyAddress thePropertyAddress = { kAudioHardwarePropertyDevices, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; result = AudioObjectGetPropertyDataSize(kAudioObjectSystemObject, &thePropertyAddress, 0, NULL, &thePropSize); if (result) { printf("Error in AudioObjectGetPropertyDataSize: %d\n", result); goto end; } // Find out how many devices are on the system theNumDevices = thePropSize / sizeof(AudioDeviceID); theDeviceList = (AudioDeviceID*)calloc(theNumDevices, sizeof(AudioDeviceID)); result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &thePropertyAddress, 0, NULL, &thePropSize, theDeviceList); if (result) { printf("Error in AudioObjectGetPropertyData: %d\n", result); goto end; } CFStringRef theDeviceName; for (UInt32 i=0; i < theNumDevices; i++) { // get the device name thePropSize = sizeof(CFStringRef); thePropertyAddress.mSelector = kAudioObjectPropertyName; thePropertyAddress.mScope = kAudioObjectPropertyScopeGlobal; thePropertyAddress.mElement = kAudioObjectPropertyElementMaster; result = AudioObjectGetPropertyData(theDeviceList[i], &thePropertyAddress, 0, NULL, &thePropSize, &theDeviceName); if (result) { printf("Error in AudioObjectGetPropertyData: %d\n", result); goto end; } if (doPrintDeviceList) CFShow(theDeviceName); if (theNewDefaultDeviceString) { if (CFStringCompare(theDeviceName, theNewDefaultDeviceName, 0) == kCFCompareEqualTo) { // we found the device, now it as the default output device thePropertyAddress.mSelector = kAudioHardwarePropertyDefaultOutputDevice; thePropertyAddress.mScope = kAudioObjectPropertyScopeGlobal; thePropertyAddress.mElement = kAudioObjectPropertyElementMaster; result = AudioObjectSetPropertyData(kAudioObjectSystemObject, &thePropertyAddress, 0, NULL, sizeof(AudioDeviceID), &theDeviceList[i]); if (result) { printf("Error in AudioObjectSetPropertyData: kAudioHardwarePropertyDefaultOutputDevice: %d\n", result); goto end; } else { didFindNewDevice = true; printf("\tSuccessfully changed default output device to %s\n", theNewDefaultDeviceString); if (!doPrintDeviceList) break; } } } CFRelease(theDeviceName); } if (theNewDefaultDeviceString) { if (!didFindNewDevice) printf("Error! Could not find specified device %s. Using current default output device\n", theNewDefaultDeviceString); CFRelease(theNewDefaultDeviceName); } } if (doSetMute || theVolume > 0.) { AudioDeviceID theDefaultOutputDeviceID; thePropSize = sizeof(theDefaultOutputDeviceID); CFStringRef theDefaultOutputDeviceName; AudioObjectPropertyAddress thePropertyAddress = { kAudioHardwarePropertyDefaultOutputDevice, kAudioObjectPropertyScopeGlobal, kAudioObjectPropertyElementMaster }; // get the ID of the default output device result = AudioObjectGetPropertyData(kAudioObjectSystemObject, &thePropertyAddress, 0, NULL, &thePropSize, &theDefaultOutputDeviceID); if (result) { printf("Error in AudioObjectGetPropertyData: %d\n", result); goto end; } thePropSize = sizeof(CFStringRef); thePropertyAddress.mSelector = kAudioObjectPropertyName; thePropertyAddress.mScope = kAudioObjectPropertyScopeGlobal; thePropertyAddress.mElement = kAudioObjectPropertyElementMaster; // get the name of the default output device result = AudioObjectGetPropertyData(theDefaultOutputDeviceID, &thePropertyAddress, 0, NULL, &thePropSize, &theDefaultOutputDeviceName); if (result) { printf("Error in AudioObjectGetPropertyData: %d\n", result); goto end; } const char* theDefaultOutputDeviceString = CFStringGetCStringPtr(theDefaultOutputDeviceName, CFStringGetSystemEncoding()); if (doSetMute) { thePropSize = sizeof(theMute); thePropertyAddress.mSelector = kAudioDevicePropertyMute; thePropertyAddress.mScope = kAudioDevicePropertyScopeOutput; thePropertyAddress.mElement = kAudioObjectPropertyElementMaster; // check if the device supports setting mute if (AudioObjectHasProperty(theDefaultOutputDeviceID, &thePropertyAddress)) { printf("\tSetting %s mute %s\n", theDefaultOutputDeviceString, (theMute) ? "on" : "off"); // if so then set it result = AudioObjectSetPropertyData(theDefaultOutputDeviceID, &thePropertyAddress, 0, NULL, thePropSize, &theMute); if (result) { printf("Error in AudioObjectSetPropertyData: %d\n", result); goto end; } } else { // if the device does not support master mute control, do nothing printf("Error: the default output device does not support mute control\n"); } } if (theVolume > 0.) { thePropSize = sizeof(theVolume); thePropertyAddress.mSelector = kAudioDevicePropertyVolumeScalar; thePropertyAddress.mScope = kAudioDevicePropertyScopeOutput; thePropertyAddress.mElement = kAudioObjectPropertyElementMaster; // see if the device supports volume control, if so, then set the user specified volume if (AudioObjectHasProperty(theDefaultOutputDeviceID, &thePropertyAddress)) { printf("\tSetting %s volume to %g\n", theDefaultOutputDeviceString, theVolume); result = AudioObjectSetPropertyData(theDefaultOutputDeviceID, &thePropertyAddress, 0, NULL, thePropSize, &theVolume); if (result) { printf("Error in AudioObjectSetPropertyData: %d\n", result); goto end; } } else { // if the device does not support master volume control, do nothing printf("Error: the default output device does not support volume control\n"); } } CFRelease(theDefaultOutputDeviceName); } end: if (theDeviceList) free(theDeviceList); return result; }