QA1016: オーディオ装置の音量変更

原文 Technical Q&A QA1016: Changing the volume of audio devices http://developer.apple.com/qa/qa2006/qa1016.html

回答: オーディオハードウェアの音量を変更するには、まずAudioDeviceを取得し、その装置が音量調節が出来るかどうかを確認しなければなりません。 AudioHardwareGetPropertyメソッドとkAudioHardwarePropertyDefaultOutputDevice / kAudioHardwarePropertyDefaultSystemOutputDevice定数を使うことで、デフォルト出力装置やシステム出力装置を得ることが出来ます。

全ての装置がマスターボリューム調節や各チャンネルごとの調節に対応しているとは限りません。

Listing 1: デフォルト出力装置を取得する

    UInt32 size;
    AudioDeviceID outputDevice;
    AudioHardwareGetProperty(kAudioHardwarePropertyDefaultOuputDevice,
                             &size,
                             &outputDevice);

装置を取得してから、その装置が音量コントロールを持っているか、そしてその内容を書き換えられるかどうか確認します。

Listing 2: 装置の能力を取得する

bool CAAudioHardwareDevice::HasProperty(
    UInt32 inChannel,
    CAAudioHardwareDeviceSectionID inSection,
    AudioHardwarePropertyID inPropertyID) const
{
    OSStatus theError = AudioDeviceGetPropertyInfo(mAudioDeviceID,
            inChannel, inSection, inPropertyID, NULL, NULL);
    return theError == 0;
}
 
bool CAAudioHardwareDevice::PropertyIsSettable(
    UInt32 inChannel,
    CAAudioHardwareDeviceSectionID inSection,
    AudioHardwarePropertyID inPropertyID) const
{
    Boolean isWritable = false;
    OSStatus theError = AudioDeviceGetPropertyInfo(mAudioDeviceID, inChannel,
        inSection, inPropertyID, NULL, &isWritable);
    ThrowIfError(theError, CAException(theError),
        "CAAudioHardwareDevice::PropertyIsSettable: "
        "プロパティ情報取得中にエラー発生");
    return isWritable != 0;
}
 
bool CAAudioHardwareDevice::HasVolumeControl(
    UInt32 inChannel,
    CAAudioHardwareDeviceSectionID inSection) const
{
    return HasProperty(inChannel, inSection, kAudioDevicePropertyVolumeScalar);
}
 
bool CAAudioHardwareDevice::VolumeControlIsSettable(
    UInt32 inChannel,
    CAAudioHardwareDeviceSectionID inSection) const
{
    return PropertyIsSettable(inChannel, inSection, kAudioDevicePropertyVolumeScalar);
}

kAudioDevicePropertyVolumeScalar定数を引数にAudioDeviceSetPropertyメソッドを呼ぶと、装置の音量を設定することが出来ます。 音量は0~1のFloat32値で表し、装置の音量値へとスケーリングされます。

Listing 3: マスターチャンネルの音量を設定する

AudioDeviceSetProperty(theDevice,
                       NULL, // タイムスタンプ(必要ありません)
                       0, // チャンネル0がマスターチャンネル
                          false,  // 出力装置用
                          kAudioDevicePropertyVolumeScalar,
                       sizeof(Float32),
                       &theValue );
Date Notes
2006-05-02 第1版

掲載日: 2006-05-02

  • translation/adc/audio/technical_qa/qa1016_changing_the_volume_of_audio_devices.txt
  • 最終更新: 2015-01-06 11:51
  • (外部編集)