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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
|
/* 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 "volumeeffect.h"
#include "qbasefilter.h"
#include "qmeminputpin.h"
#include <QtCore/qmath.h> //for sqrt
QT_BEGIN_NAMESPACE
#ifndef QT_NO_PHONON_VOLUMEFADEREFFECT
namespace Phonon
{
namespace DS9
{
/**************************************************************************
* curve functions
*************************************************************************/
static qreal curveValueFadeIn3dB(const qreal fadeStart, const qreal fadeDiff, const qreal completed)
{
return (fadeStart + fadeDiff * sqrt(completed));
}
static qreal curveValueFadeOut3dB(const qreal fadeStart, const qreal fadeDiff, const qreal completed)
{
return (fadeStart + fadeDiff * (1.0 - sqrt(1.0 - completed)));
}
// in == out for a linear fade
static qreal curveValueFade6dB(const qreal fadeStart, const qreal fadeDiff, const qreal completed)
{
return (fadeStart + fadeDiff * completed);
}
static qreal curveValueFadeIn9dB(const qreal fadeStart, const qreal fadeDiff, const qreal completed)
{
return (fadeStart + fadeDiff * pow(completed, 1.5));
}
static qreal curveValueFadeOut9dB(const qreal fadeStart, const qreal fadeDiff, const qreal completed)
{
return (fadeStart + fadeDiff * (1.0 - pow(1.0 - completed, 1.5)));
}
static qreal curveValueFadeIn12dB(const qreal fadeStart, const qreal fadeDiff, const qreal completed)
{
return (fadeStart + fadeDiff * completed * completed);
}
static qreal curveValueFadeOut12dB(const qreal fadeStart, const qreal fadeDiff, const qreal completed)
{
const qreal x = 1.0 - completed;
return (fadeStart + fadeDiff * (1.0 - x * x));
}
static const QVector<AM_MEDIA_TYPE> audioMediaType()
{
QVector<AM_MEDIA_TYPE> ret;
AM_MEDIA_TYPE mt = { MEDIATYPE_Audio, MEDIASUBTYPE_PCM, 1, 0, 1, GUID_NULL, 0, 0, 0};
ret << mt;
return ret;
}
class VolumeMemInputPin : public QMemInputPin
{
public:
VolumeMemInputPin(QBaseFilter *parent, const QVector<AM_MEDIA_TYPE> &mt, QPin *output) : QMemInputPin(parent, mt, true /*transform*/, output)
{
}
~VolumeMemInputPin()
{
}
STDMETHODIMP NotifyAllocator(IMemAllocator *alloc, BOOL b)
{
ALLOCATOR_PROPERTIES prop;
HRESULT hr = alloc->GetProperties(&prop);
if (SUCCEEDED(hr) && prop.cBuffers > 1) {
//this allows to reduce the latency for sound
//the problem is that too low numbers makes the whole thing fail...
ALLOCATOR_PROPERTIES actual;
prop.cBuffers = 1;
alloc->SetProperties(&prop, &actual);
}
return QMemInputPin::NotifyAllocator(alloc, b);
}
};
class VolumeMemOutputPin : public QPin
{
public:
VolumeMemOutputPin(QBaseFilter *parent, const QVector<AM_MEDIA_TYPE> &mt) : QPin(parent, PINDIR_OUTPUT, mt)
{
}
~VolumeMemOutputPin()
{
}
};
class VolumeEffectFilter : public QBaseFilter
{
public:
VolumeEffectFilter(VolumeEffect *);
//reimplementation
virtual HRESULT processSample(IMediaSample *);
private:
void treatOneSamplePerChannel(BYTE **buffer, int sampleSize, int channelCount, int frequency);
QMemInputPin *m_input;
QPin *m_output;
VolumeEffect *m_volumeEffect;
};
VolumeEffectFilter::VolumeEffectFilter(VolumeEffect *ve) : QBaseFilter(CLSID_NULL),
m_volumeEffect(ve)
{
QVector<AM_MEDIA_TYPE> mt;
//creating the output
m_output = new VolumeMemOutputPin(this, mt);
//then creating the input
mt << audioMediaType();
m_input = new VolumeMemInputPin(this, mt, m_output);
}
void VolumeEffectFilter::treatOneSamplePerChannel(BYTE **buffer, int sampleSize, int channelCount, int frequency)
{
float volume = m_volumeEffect->volume();
if (m_volumeEffect->m_fading) {
const qreal lastSample = m_volumeEffect->m_fadeDuration * frequency / 1000;
const qreal completed = qreal(m_volumeEffect->m_fadeSamplePosition++) / lastSample;
if (qFuzzyCompare(completed, qreal(1.))) {
m_volumeEffect->setVolume(m_volumeEffect->m_targetVolume);
m_volumeEffect->m_fading = false; //we end the fading effect
} else {
volume = m_volumeEffect->m_fadeCurveFn(m_volumeEffect->m_initialVolume,
m_volumeEffect->m_targetVolume - m_volumeEffect->m_initialVolume,
completed);
}
}
for(int c = 0; c < channelCount; ++c) {
switch (sampleSize)
{
case 2:
{
short *shortBuffer = reinterpret_cast<short*>(*buffer);
*shortBuffer *= qRound(volume);
}
break;
case 1:
**buffer *= qRound(volume);
break;
default:
break;
}
*buffer += sampleSize;
}
}
HRESULT VolumeEffectFilter::processSample(IMediaSample * ms)
{
BYTE *buffer = 0;
ms->GetPointer(&buffer);
if (buffer) {
const AM_MEDIA_TYPE &mt = m_output->connectedType();
if (mt.formattype != FORMAT_WaveFormatEx) {
return VFW_E_INVALIDMEDIATYPE;
}
WAVEFORMATEX *format = reinterpret_cast<WAVEFORMATEX*>(mt.pbFormat);
const int channelCount = format->nChannels;
const int sampleSize = format->wBitsPerSample / 8; //...in bytes
const BYTE *end = buffer + ms->GetActualDataLength();
while (buffer < end) {
treatOneSamplePerChannel(&buffer, sampleSize, channelCount, format->nSamplesPerSec);
}
}
return S_OK;
}
VolumeEffect::VolumeEffect(QObject *parent) : Effect(parent),
m_volume(1), m_fadeCurve(Phonon::VolumeFaderEffect::Fade3Decibel),
m_fading(false), m_initialVolume(0), m_targetVolume(0), m_fadeDuration(0),
m_fadeSamplePosition(0)
{
//creation of the effects
for(int i = 0; i < FILTER_COUNT; ++i) {
VolumeEffectFilter *f = new VolumeEffectFilter(this);
m_filters[i] = Filter(f);
}
}
float VolumeEffect::volume() const
{
return m_volume;
}
void VolumeEffect::setVolume(float newVolume)
{
m_volume = newVolume;
}
Phonon::VolumeFaderEffect::FadeCurve VolumeEffect::fadeCurve() const
{
return m_fadeCurve;
}
void VolumeEffect::setFadeCurve(Phonon::VolumeFaderEffect::FadeCurve curve)
{
m_fadeCurve = curve;
}
void VolumeEffect::fadeTo(float vol, int duration)
{
m_fading = true; //will be set back to false when fading is finished
m_targetVolume = vol;
m_fadeSamplePosition = 0;
m_initialVolume = m_volume;
m_fadeDuration = duration;
//in or out?
const bool in = vol > m_volume;
switch(m_fadeCurve)
{
case Phonon::VolumeFaderEffect::Fade6Decibel:
m_fadeCurveFn = curveValueFade6dB;
break;
case Phonon::VolumeFaderEffect::Fade9Decibel:
if (in) {
m_fadeCurveFn = curveValueFadeIn9dB;
} else {
m_fadeCurveFn = curveValueFadeOut9dB;
}
break;
case Phonon::VolumeFaderEffect::Fade12Decibel:
if (in) {
m_fadeCurveFn = curveValueFadeIn12dB;
} else {
m_fadeCurveFn = curveValueFadeOut12dB;
}
break;
case Phonon::VolumeFaderEffect::Fade3Decibel:
default:
if (in) {
m_fadeCurveFn = curveValueFadeIn3dB;
} else {
m_fadeCurveFn = curveValueFadeOut3dB;
}
break;
}
}
}
}
#endif //QT_NO_PHONON_VOLUMEFADEREFFECT
QT_END_NAMESPACE
#include "moc_volumeeffect.cpp"
|