|
1 /* This file is part of the KDE project. |
|
2 |
|
3 Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies). |
|
4 |
|
5 This library is free software: you can redistribute it and/or modify |
|
6 it under the terms of the GNU Lesser General Public License as published by |
|
7 the Free Software Foundation, either version 2.1 or 3 of the License. |
|
8 |
|
9 This library is distributed in the hope that it will be useful, |
|
10 but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 GNU Lesser General Public License for more details. |
|
13 |
|
14 You should have received a copy of the GNU Lesser General Public License |
|
15 along with this library. If not, see <http://www.gnu.org/licenses/>. |
|
16 */ |
|
17 |
|
18 #include "audiomixer.h" |
|
19 |
|
20 QT_BEGIN_NAMESPACE |
|
21 |
|
22 namespace Phonon |
|
23 { |
|
24 namespace QT7 |
|
25 { |
|
26 |
|
27 AudioMixerAudioNode::AudioMixerAudioNode() : AudioNode(30, 1) |
|
28 { |
|
29 m_numberOfBusses = 2; |
|
30 m_volume = 1.0f; |
|
31 } |
|
32 |
|
33 ComponentDescription AudioMixerAudioNode::getAudioNodeDescription() const |
|
34 { |
|
35 ComponentDescription description; |
|
36 description.componentType = kAudioUnitType_Mixer; |
|
37 description.componentSubType = kAudioUnitSubType_StereoMixer; |
|
38 description.componentManufacturer = kAudioUnitManufacturer_Apple; |
|
39 description.componentFlags = 0; |
|
40 description.componentFlagsMask = 0; |
|
41 return description; |
|
42 } |
|
43 |
|
44 void AudioMixerAudioNode::initializeAudioUnit() |
|
45 { |
|
46 // Set bus count: |
|
47 OSStatus err = AudioUnitSetProperty(m_audioUnit, |
|
48 kAudioUnitProperty_BusCount, kAudioUnitScope_Input, 0, &m_numberOfBusses, sizeof(int)); |
|
49 BACKEND_ASSERT2(err == noErr, "Could not set number of busses on audio mixer node.", FATAL_ERROR) |
|
50 } |
|
51 |
|
52 void AudioMixerAudioNode::setVolume(float volume, int bus) |
|
53 { |
|
54 if (volume < 0) |
|
55 m_volume = 0; |
|
56 else if (volume > 1) |
|
57 m_volume = 1; |
|
58 else |
|
59 m_volume = volume; |
|
60 |
|
61 if (m_audioUnit){ |
|
62 // Float32 db = Float32(volume);//Float32(20.0 * log10(volume)); // convert to db |
|
63 Float32 db = Float32(volume); |
|
64 OSStatus err = AudioUnitSetParameter(m_audioUnit, kStereoMixerParam_Volume, kAudioUnitScope_Input, bus, db, 0); |
|
65 BACKEND_ASSERT2(err == noErr, "Could not set volume on audio mixer node.", NORMAL_ERROR) |
|
66 } |
|
67 } |
|
68 |
|
69 float AudioMixerAudioNode::volume(int bus) |
|
70 { |
|
71 if (!m_audioUnit) |
|
72 return 0; |
|
73 |
|
74 Float32 db; |
|
75 OSStatus err = AudioUnitGetParameter(m_audioUnit, kStereoMixerParam_Volume, kAudioUnitScope_Input, bus, &db); |
|
76 BACKEND_ASSERT3(err == noErr, "Could not get volume on audio mixer node.", NORMAL_ERROR, 0) |
|
77 return float(db); |
|
78 } |
|
79 |
|
80 /////////////////////////////////////////////////////////////////////// |
|
81 |
|
82 AudioMixer::AudioMixer(QObject *parent) : MediaNode(AudioSink | AudioSource, 0, parent) |
|
83 { |
|
84 m_audioNode = new AudioMixerAudioNode(); |
|
85 setAudioNode(m_audioNode); |
|
86 m_fadeCurve = Phonon::VolumeFaderEffect::Fade3Decibel; |
|
87 m_fadeTimer = 0; |
|
88 m_fadeDuration = 0; |
|
89 m_fadeFromVolume = 0; |
|
90 m_fadeToVolume = 0; |
|
91 } |
|
92 |
|
93 AudioMixer::~AudioMixer() |
|
94 { |
|
95 if (m_fadeTimer) |
|
96 killTimer(m_fadeTimer); |
|
97 } |
|
98 |
|
99 QList<Phonon::EffectParameter> AudioMixer::parameters() const |
|
100 { |
|
101 QList<Phonon::EffectParameter> ret; |
|
102 return ret; |
|
103 } |
|
104 |
|
105 QVariant AudioMixer::parameterValue(const Phonon::EffectParameter &value) const |
|
106 { |
|
107 NOT_IMPLEMENTED; |
|
108 Q_UNUSED(value); |
|
109 return QVariant(); |
|
110 } |
|
111 |
|
112 void AudioMixer::setParameterValue(const Phonon::EffectParameter ¶meter, const QVariant &newValue) |
|
113 { |
|
114 NOT_IMPLEMENTED; |
|
115 Q_UNUSED(parameter); |
|
116 Q_UNUSED(newValue); |
|
117 } |
|
118 |
|
119 float AudioMixer::volume() const |
|
120 { |
|
121 return m_audioNode->volume(0); |
|
122 } |
|
123 |
|
124 void AudioMixer::setVolume(float volume) |
|
125 { |
|
126 m_audioNode->setVolume(volume, 0); |
|
127 } |
|
128 |
|
129 Phonon::VolumeFaderEffect::FadeCurve AudioMixer::fadeCurve() const |
|
130 { |
|
131 return m_fadeCurve; |
|
132 } |
|
133 |
|
134 void AudioMixer::setFadeCurve(Phonon::VolumeFaderEffect::FadeCurve fadeCurve) |
|
135 { |
|
136 m_fadeCurve = fadeCurve; |
|
137 } |
|
138 |
|
139 void AudioMixer::fadeTo(float volume, int fadeTime) |
|
140 { |
|
141 m_fadeToVolume = volume; |
|
142 m_fadeDuration = fadeTime; |
|
143 m_fadeFromVolume = m_audioNode->volume(0); |
|
144 |
|
145 m_fadeStartTime.start(); |
|
146 if (m_fadeTimer) |
|
147 killTimer(m_fadeTimer); |
|
148 m_fadeTimer = startTimer(100); |
|
149 } |
|
150 |
|
151 void AudioMixer::updateFade() |
|
152 { |
|
153 float step = float(m_fadeStartTime.elapsed()) / float(m_fadeDuration); |
|
154 if (step > 1){ |
|
155 step = 1; |
|
156 if (m_fadeTimer) |
|
157 killTimer(m_fadeTimer); |
|
158 } |
|
159 float volume = m_fadeFromVolume + ((m_fadeToVolume - m_fadeFromVolume) * step); |
|
160 m_audioNode->setVolume(volume, 0); |
|
161 } |
|
162 |
|
163 bool AudioMixer::event(QEvent *event) |
|
164 { |
|
165 switch (event->type()){ |
|
166 case QEvent::Timer:{ |
|
167 QTimerEvent *timerEvent = static_cast<QTimerEvent *>(event); |
|
168 if (timerEvent->timerId() == m_fadeTimer) |
|
169 updateFade(); |
|
170 break; } |
|
171 default: |
|
172 break; |
|
173 } |
|
174 return MediaNode::event(event); |
|
175 } |
|
176 |
|
177 }} //namespace Phonon::QT7 |
|
178 |
|
179 QT_END_NAMESPACE |
|
180 |
|
181 #include "moc_audiomixer.cpp" |