~ubuntu-branches/ubuntu/wily/sflphone/wily

« back to all changes in this revision

Viewing changes to daemon/src/audio/dsp.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2015-01-07 14:51:16 UTC
  • mfrom: (4.3.5 sid)
  • Revision ID: package-import@ubuntu.com-20150107145116-yxnafinf4lrdvrmx
Tags: 1.4.1-0.1ubuntu1
* Merge with Debian, remaining changes:
 - Drop soprano, nepomuk build-dep
* Drop ubuntu patches, now upstream

Show diffs side-by-side

added added

removed removed

Lines of Context:
32
32
#include "dsp.h"
33
33
#include "audiobuffer.h"
34
34
 
 
35
void
 
36
DSP::speexStateDeleter(SpeexPreprocessState *state)
 
37
{
 
38
    speex_preprocess_state_destroy(state);
 
39
}
 
40
 
35
41
DSP::DSP(int smplPerFrame, int channels, int samplingRate) :
36
42
    smplPerFrame_(smplPerFrame),
37
 
    noiseStates_(channels, nullptr)
 
43
    dspStates_()
38
44
{
39
 
    for (auto &state : noiseStates_)
40
 
        state = speex_preprocess_state_init(smplPerFrame_, samplingRate);
 
45
    for (int c = 0; c < channels; ++c)
 
46
        dspStates_.push_back(
 
47
                {speex_preprocess_state_init(smplPerFrame_, samplingRate),
 
48
                 speexStateDeleter});
41
49
}
42
50
 
43
51
void DSP::enableAGC()
44
52
{
45
53
    // automatic gain control, range [1-32768]
46
 
    for (auto &state : noiseStates_) {
 
54
    for (const auto &state : dspStates_) {
47
55
        int enable = 1;
48
 
        speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_AGC, &enable);
 
56
        speex_preprocess_ctl(state.get(), SPEEX_PREPROCESS_SET_AGC, &enable);
49
57
        int target = 16000;
50
 
        speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_AGC_TARGET, &target);
 
58
        speex_preprocess_ctl(state.get(), SPEEX_PREPROCESS_SET_AGC_TARGET, &target);
51
59
    }
52
60
}
53
61
 
54
62
void DSP::disableAGC()
55
63
{
56
 
    for (auto &state : noiseStates_) {
 
64
    for (const auto &state : dspStates_) {
57
65
        int enable = 0;
58
 
        speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_AGC, &enable);
 
66
        speex_preprocess_ctl(state.get(), SPEEX_PREPROCESS_SET_AGC, &enable);
59
67
    }
60
68
}
61
69
 
62
70
void DSP::enableDenoise()
63
71
{
64
 
    for (auto &state : noiseStates_) {
 
72
    for (const auto &state : dspStates_) {
65
73
        int enable = 1;
66
 
        speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_DENOISE, &enable);
 
74
        speex_preprocess_ctl(state.get(), SPEEX_PREPROCESS_SET_DENOISE, &enable);
67
75
    }
68
76
}
69
77
 
70
78
void DSP::disableDenoise()
71
79
{
72
 
    for (auto &state : noiseStates_) {
 
80
    for (const auto &state : dspStates_) {
73
81
        int enable = 0;
74
 
        speex_preprocess_ctl(state, SPEEX_PREPROCESS_SET_DENOISE, &enable);
 
82
        speex_preprocess_ctl(state.get(), SPEEX_PREPROCESS_SET_DENOISE, &enable);
75
83
    }
76
84
}
77
85
 
78
 
DSP::~DSP()
79
 
{
80
 
    for (auto state : noiseStates_)
81
 
        speex_preprocess_state_destroy(state);
82
 
}
83
 
 
84
86
void DSP::process(AudioBuffer& buff, int samples)
85
87
{
86
88
    if (samples != smplPerFrame_) {
91
93
    auto &channelData = buff.getData();
92
94
    size_t index = 0;
93
95
    for (auto &c : channelData) {
94
 
        if (index < noiseStates_.size() and noiseStates_[index])
95
 
            speex_preprocess_run(noiseStates_[index], c.data());
 
96
        if (index < dspStates_.size() and dspStates_[index].get())
 
97
            speex_preprocess_run(dspStates_[index].get(), c.data());
96
98
        ++index;
97
99
    }
98
100
}