| 1 | /* |
| 2 | Copyright (C) 2010 Colin Guthrie <cguthrie@mandriva.org> |
| 3 | Copyright (C) 2013 Harald Sitter <sitter@kde.org> |
| 4 | |
| 5 | This library is free software; you can redistribute it and/or |
| 6 | modify it under the terms of the GNU Lesser General Public |
| 7 | License as published by the Free Software Foundation; either |
| 8 | version 2.1 of the License, or (at your option) version 3, or any |
| 9 | later version accepted by the membership of KDE e.V. (or its |
| 10 | successor approved by the membership of KDE e.V.), Nokia Corporation |
| 11 | (or its successors, if any) and the KDE Free Qt Foundation, which shall |
| 12 | act as a proxy defined in Section 6 of version 3 of the license. |
| 13 | |
| 14 | This library is distributed in the hope that it will be useful, |
| 15 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 17 | Lesser General Public License for more details. |
| 18 | |
| 19 | You should have received a copy of the GNU Lesser General Public |
| 20 | License along with this library. If not, see <http://www.gnu.org/licenses/>. |
| 21 | */ |
| 22 | |
| 23 | #include "pulsestream_p.h" |
| 24 | |
| 25 | #include <qmath.h> |
| 26 | |
| 27 | #include "pulsesupport.h" |
| 28 | |
| 29 | namespace Phonon |
| 30 | { |
| 31 | |
| 32 | PulseStream::PulseStream(QString streamUuid, QString role) |
| 33 | : QObject() |
| 34 | , mStreamUuid(streamUuid) |
| 35 | , mIndex(PA_INVALID_INDEX) |
| 36 | , mDevice(-1) |
| 37 | , mMute(false) |
| 38 | , mCachedVolume(-1) |
| 39 | , mRole(role) |
| 40 | { |
| 41 | pa_cvolume_init(a: &mVolume); |
| 42 | } |
| 43 | |
| 44 | PulseStream::~PulseStream() |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | QString PulseStream::uuid() const |
| 49 | { |
| 50 | return mStreamUuid; |
| 51 | } |
| 52 | |
| 53 | uint32_t PulseStream::index() const |
| 54 | { |
| 55 | return mIndex; |
| 56 | } |
| 57 | |
| 58 | void PulseStream::setIndex(uint32_t index) |
| 59 | { |
| 60 | mIndex = index; |
| 61 | } |
| 62 | |
| 63 | uint8_t PulseStream::channels() const |
| 64 | { |
| 65 | return mVolume.channels; |
| 66 | } |
| 67 | |
| 68 | void PulseStream::setDevice(int device) |
| 69 | { |
| 70 | if (mDevice != device) { |
| 71 | mDevice = device; |
| 72 | emit usingDevice(device); |
| 73 | } |
| 74 | } |
| 75 | |
| 76 | // Copied from AudioOutput |
| 77 | static const qreal LOUDNESS_TO_VOLTAGE_EXPONENT = qreal(0.67); |
| 78 | static const qreal VOLTAGE_TO_LOUDNESS_EXPONENT = qreal(1.0/LOUDNESS_TO_VOLTAGE_EXPONENT); |
| 79 | |
| 80 | void PulseStream::setVolume(const pa_cvolume *volume) |
| 81 | { |
| 82 | if (mCachedVolume != -1) |
| 83 | QMetaObject::invokeMethod(obj: this, member: "applyCachedVolume" , c: Qt::QueuedConnection); |
| 84 | if (pa_cvolume_equal(a: &mVolume, b: volume) == 0) { |
| 85 | memcpy(dest: &mVolume, src: volume, n: sizeof(mVolume)); |
| 86 | qreal vol = (qreal)pa_cvolume_avg(a: volume) / PA_VOLUME_NORM; |
| 87 | // AudioOutput expects the "backend" to supply values that have been |
| 88 | // adjusted for Stephens' law, so we need to fudge them accordingly |
| 89 | // so that the %ages match up in KMix/the application's own slider. |
| 90 | emit volumeChanged(volume: qPow(x: vol, y: VOLTAGE_TO_LOUDNESS_EXPONENT)); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | void PulseStream::setMute(bool mute) |
| 95 | { |
| 96 | if (mMute != mute) { |
| 97 | mMute = mute; |
| 98 | emit muteChanged(mute: mMute); |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | qreal PulseStream::cachedVolume() const |
| 103 | { |
| 104 | return mCachedVolume; |
| 105 | } |
| 106 | |
| 107 | void PulseStream::setCachedVolume(qreal volume) |
| 108 | { |
| 109 | mCachedVolume = volume; |
| 110 | } |
| 111 | |
| 112 | QString PulseStream::role() const |
| 113 | { |
| 114 | return mRole; |
| 115 | } |
| 116 | |
| 117 | void PulseStream::applyCachedVolume() |
| 118 | { |
| 119 | if (mCachedVolume == -1) |
| 120 | return; |
| 121 | PulseSupport::getInstance()->setOutputVolume(streamUuid: mStreamUuid, volume: mCachedVolume); |
| 122 | mCachedVolume = -1; |
| 123 | } |
| 124 | |
| 125 | } // namespace Phonon |
| 126 | |
| 127 | #include "moc_pulsestream_p.cpp" |
| 128 | |
| 129 | // vim: sw=4 ts=4 |
| 130 | |