Fix QAudioOutput volume on OSX.
authorMichael Goddard <michael.goddard@nokia.com>
Wed, 20 Jun 2012 05:53:39 +0000 (15:53 +1000)
committerQt by Nokia <qt-info@nokia.com>
Wed, 20 Jun 2012 07:11:28 +0000 (09:11 +0200)
Change-Id: I142ca638d3dc0839f7c5b103f03edac6715a0bb6
Reviewed-by: Ling Hu <ling.hu@nokia.com>
Reviewed-by: Dmytro Poplavskiy <dmytro.poplavskiy@nokia.com>
src/multimedia/audio/qaudiooutput_mac_p.cpp
src/multimedia/audio/qaudiooutput_mac_p.h

index 2d8f595..105f41f 100644 (file)
@@ -297,6 +297,8 @@ QAudioOutputPrivate::QAudioOutputPrivate(const QByteArray& device)
         stateCode = QAudio::StoppedState;
         audioThreadState.store(Stopped);
 
+        cachedVolume = (qreal)1.;
+
         intervalTimer = new QTimer(this);
         intervalTimer->setInterval(1000);
         connect(intervalTimer, SIGNAL(timeout()), SIGNAL(notify()));
@@ -408,6 +410,8 @@ bool QAudioOutputPrivate::open()
 
     isOpen = true;
 
+    setVolume(cachedVolume);
+
     return true;
 }
 
@@ -661,6 +665,27 @@ void QAudioOutputPrivate::stopTimers()
     intervalTimer->stop();
 }
 
+void QAudioOutputPrivate::setVolume(qreal v)
+{
+    const qreal normalizedVolume = qBound(qreal(0.0), v, qreal(1.0));
+    if (!isOpen) {
+        cachedVolume = normalizedVolume;
+        return;
+    }
+
+    if (AudioUnitSetParameter(audioUnit,
+                              kHALOutputParam_Volume,
+                              kAudioUnitScope_Global,
+                              0 /* bus */,
+                              (float)normalizedVolume,
+                              0) == noErr)
+        cachedVolume = normalizedVolume;
+}
+
+qreal QAudioOutputPrivate::volume() const
+{
+    return cachedVolume;
+}
 
 void QAudioOutputPrivate::deviceStopped()
 {
index 9535518..fd513c4 100644 (file)
@@ -106,6 +106,7 @@ public:
     QMutex          mutex;
     QTimer*         intervalTimer;
     QAbstractAudioDeviceInfo *audioDeviceInfo;
+    qreal           cachedVolume;
 
     QAudio::Error    errorCode;
     QAudio::State    stateCode;
@@ -152,6 +153,9 @@ public:
     void startTimers();
     void stopTimers();
 
+    void setVolume(qreal);
+    qreal volume() const;
+
 private slots:
     void deviceStopped();
     void inputReady();