Ejemplo n.º 1
0
// static
OSStatus    BGMDeviceControlSync::AHSSetPropertyData(AudioObjectID inObjectID, const AudioObjectPropertyAddress* inAddress, UInt32 inDataSize, const void* inData)
{
    // See the explanation about these pragmas in AHSGetPropertyData
    #pragma clang diagnostic push
    #pragma clang diagnostic ignored "-Wnonnull"
    return AudioHardwareServiceSetPropertyData(inObjectID, inAddress, 0, NULL, inDataSize, inData);
    #pragma clang diagnostic pop
}
Ejemplo n.º 2
0
    bool setMuted (bool mute)
    {
        if (outputDeviceID != kAudioObjectUnknown && canSetVolume())
        {
            UInt32 newMute = mute ? 1 : 0;
            UInt32 size = sizeof (newMute);

            return AudioHardwareServiceSetPropertyData (outputDeviceID, &addr, 0, nullptr,
                                                        size, &newMute) == noErr;
        }

        return false;
    }
Ejemplo n.º 3
0
    bool setGain (float gain)
    {
        if (outputDeviceID != kAudioObjectUnknown && canSetVolume())
        {
            Float32 newVolume = gain;
            UInt32 size = sizeof (newVolume);

            return AudioHardwareServiceSetPropertyData (outputDeviceID, &addr, 0, nullptr,
                                                        size, &newVolume) == noErr;
        }

        return false;
    }
Ejemplo n.º 4
0
// setting system volume. Mutes if under threshhold.
// Courtesy: https://gist.github.com/atr000/205140
int setVolume(float newVolume) {
  if (newVolume < 0.0 || newVolume > 1.0) {
    fprintf(stderr, "ERROR: Requested volume out of range (%.2f)\n", newVolume);
    return 1;
  }

  // get output device
  UInt32 propertySize = 0;
  OSStatus status = noErr;
  AudioObjectPropertyAddress propertyAOPA;
  propertyAOPA.mElement = kAudioObjectPropertyElementMaster;
  propertyAOPA.mScope = kAudioDevicePropertyScopeOutput;

  if (newVolume < 0.001) {
    propertyAOPA.mSelector = kAudioDevicePropertyMute;
  } else {
    propertyAOPA.mSelector = kAudioHardwareServiceDeviceProperty_VirtualMasterVolume;
  }

  AudioDeviceID outputDeviceID = defaultOutputDeviceID();

  if (outputDeviceID == kAudioObjectUnknown) {
    fprintf(stderr, "ERROR: Unknown device\n");
    return 2;
  }

  if (!AudioHardwareServiceHasProperty(outputDeviceID, &propertyAOPA)) {
    fprintf(stderr, "ERROR: Device 0x%0x does not support volume control\n", outputDeviceID);
    return 3;
  }

  Boolean canSetVolume = false;
  status = AudioHardwareServiceIsPropertySettable(outputDeviceID, &propertyAOPA, &canSetVolume);

  if (status || !canSetVolume) {
    fprintf(stderr, "ERROR: Device 0x%0x does not support volume control\n", outputDeviceID);
    return 4;
  }

  if (propertyAOPA.mSelector == kAudioDevicePropertyMute) {
    propertySize = sizeof(UInt32);
    UInt32 mute = 1;
    status = AudioHardwareServiceSetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, propertySize, &mute);
  } else {
    propertySize = sizeof(Float32);
    status = AudioHardwareServiceSetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, propertySize, &newVolume);

    if (status) {
      fprintf(stderr, "ERROR: Unable to set volume for device 0x%0x\n", outputDeviceID);
      return 5;
    }

    // make sure we're not muted
    propertyAOPA.mSelector = kAudioDevicePropertyMute;
    propertySize = sizeof(UInt32);
    UInt32 mute = 0;

    if (!AudioHardwareServiceHasProperty(outputDeviceID, &propertyAOPA)) {
      fprintf(stderr, "ERROR: Device 0x%0x does not support muting\n", outputDeviceID);
      return 6;
    }

    Boolean canSetMute = false;

    status = AudioHardwareServiceIsPropertySettable(outputDeviceID, &propertyAOPA, &canSetMute);

    if (status || !canSetMute) {
      fprintf(stderr, "ERROR: Device 0x%0x does not support muting\n", outputDeviceID);
      return 7;
    }

    status = AudioHardwareServiceSetPropertyData(outputDeviceID, &propertyAOPA, 0, NULL, propertySize, &mute);
  }

  if (status) {
    fprintf(stderr, "ERROR: Unable to set volume for device 0x%0x\n", outputDeviceID);
    return 8;
  }

  return 0;
}