summaryrefslogtreecommitdiffstats
path: root/akode/lib/volumefilter.cpp
blob: c0534531c29c1d1ecf147e6d7a479410d693f55c (plain)
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
/*  aKode Volume-Filter

    Copyright (C) 2004 Allan Sandfeld Jensen <kde@carewolf.com>

    This library is free software; you can redistribute it and/or
    modify it under the terms of the GNU Library General Public
    License as published by the Free Software Foundation; either
    version 2 of the License, or (at your option) any later version.

    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
    Library General Public License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to
    the Free Software Foundation, Inc., 51 Franklin Steet, Fifth Floor,
    Boston, MA 02110-1301, USA.
*/

#include "audioframe.h"
#include "arithmetic.h"
#include "volumefilter.h"

#define VM_FIDELITY (1<<14)

namespace aKode {

VolumeFilter::VolumeFilter() : m_volume(0) {}

// T is the input/output type, S is the fast arithmetics type, Arithm is a division definition
template<typename T, typename S, template<typename S> class Arithm>
static bool _doFrame(AudioFrame* in, AudioFrame* out, int volume)
{
    T** indata = (T**)in->data;
    T** outdata = (T**)out->data;

    long length = in->length;
    if (in->channels != out->channels) return false;
    if (in->sample_width != out->sample_width) return false;

    S smax = Arithm<S>::max(in->sample_width);

    for(int i=0; i<in->channels; i++) {
        for(int j=0; j<length; j++) {
            S signal = Arithm<S>::muldiv(outdata[i][j], volume, VM_FIDELITY);

            if (signal > smax) signal = smax;
            else
            if (signal < -smax) signal = -smax;

            indata[i][j] = (T)(signal);
        }
    }
    return true;
}


bool VolumeFilter::doFrame(AudioFrame* in, AudioFrame* out)
{
    if (!out) out = in;

    int volint = (int)(m_volume*VM_FIDELITY+0.5);

    if (in->sample_width < -32) {
        return _doFrame<double, double, Arithm_FP>(in, out, volint);
    } else
    if (in->sample_width < 0) {
        return _doFrame<float,  float,  Arithm_FP>(in, out, volint);
    } else
    if (in->sample_width <= 8) {
        return _doFrame<int8_t, int32_t, Arithm_Int>(in, out, volint);
    } else
    if (in->sample_width <= 16) {
        return _doFrame<int16_t, int32_t, Arithm_Int>(in, out, volint);
    } else
    if (in->sample_width <= 24) {
        return _doFrame<int32_t, int32_t, Arithm_Int>(in, out, volint);
    } else
        return _doFrame<int32_t, int64_t, Arithm_Int>(in, out, volint);
}

void VolumeFilter::setVolume(float volume) {
    m_volume = volume;
}

float VolumeFilter::volume() const {
    return m_volume;
}

} // namespace