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