~ubuntu-branches/ubuntu/utopic/sflphone/utopic-proposed

« back to all changes in this revision

Viewing changes to daemon/src/audio/delaydetection.h

  • Committer: Package Import Robot
  • Author(s): Mark Purcell
  • Date: 2014-01-28 18:23:36 UTC
  • mfrom: (4.3.4 sid)
  • Revision ID: package-import@ubuntu.com-20140128182336-jrsv0k9u6cawc068
Tags: 1.3.0-1
* New upstream release 
  - Fixes "New Upstream Release" (Closes: #735846)
  - Fixes "Ringtone does not stop" (Closes: #727164)
  - Fixes "[sflphone-kde] crash on startup" (Closes: #718178)
  - Fixes "sflphone GUI crashes when call is hung up" (Closes: #736583)
* Build-Depends: ensure GnuTLS 2.6
  - libucommon-dev (>= 6.0.7-1.1), libccrtp-dev (>= 2.0.6-3)
  - Fixes "FTBFS Build-Depends libgnutls{26,28}-dev" (Closes: #722040)
* Fix "boost 1.49 is going away" unversioned Build-Depends: (Closes: #736746)
* Add Build-Depends: libsndfile-dev, nepomuk-core-dev

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 *  Copyright (C) 2004-2012 Savoir-Faire Linux Inc.
3
 
 *  Author: Alexandre Savard <alexandre.savard@savoirfairelinux.com>
4
 
 *
5
 
 *  This program is free software; you can redistribute it and/or modify
6
 
 *  it under the terms of the GNU General Public License as published by
7
 
 *  the Free Software Foundation; either version 3 of the License, or
8
 
 *  (at your option) any later version.
9
 
 *
10
 
 *  This program is distributed in the hope that it will be useful,
11
 
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
12
 
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13
 
 *  GNU General Public License for more details.
14
 
 *
15
 
 *  You should have received a copy of the GNU General Public License
16
 
 *  along with this program; if not, write to the Free Software
17
 
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
18
 
 *
19
 
 *  Additional permission under GNU GPL version 3 section 7:
20
 
 *
21
 
 *  If you modify this program, or any covered work, by linking or
22
 
 *  combining it with the OpenSSL project's OpenSSL library (or a
23
 
 *  modified version of that library), containing parts covered by the
24
 
 *  terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc.
25
 
 *  grants you additional permission to convey the resulting work.
26
 
 *  Corresponding Source for a non-source form of such a combination
27
 
 *  shall include the source code for the parts of OpenSSL used as well
28
 
 *  as that of the covered work.
29
 
 */
30
 
 
31
 
 
32
 
#ifndef DELAYDETECTION_H
33
 
#define DELAYDETECTION_H
34
 
 
35
 
#include "sfl_types.h"
36
 
#include <vector>
37
 
 
38
 
// Template size in samples for correlation
39
 
#define WINDOW_SIZE 256
40
 
 
41
 
// Segment length in ms for correlation
42
 
#define MAX_DELAY 150
43
 
 
44
 
// Size of internal buffers in samples
45
 
#define  DELAY_BUFF_SIZE MAX_DELAY * 8000 / 1000
46
 
 
47
 
#define MAXFILTERSIZE 100
48
 
 
49
 
class FirFilter {
50
 
 
51
 
    public:
52
 
        FirFilter(const std::vector<double> &ir);
53
 
        /**
54
 
         * Perform filtering on one sample
55
 
         */
56
 
        float getOutputSample(float inputSample);
57
 
 
58
 
        void reset();
59
 
 
60
 
    private:
61
 
 
62
 
        /**
63
 
         * Length of the filter
64
 
         */
65
 
        int length_;
66
 
 
67
 
        /**
68
 
         * Coefficient of the filter
69
 
         */
70
 
        std::vector<double> impulseResponse_;
71
 
 
72
 
        /**
73
 
         * Circular buffer
74
 
         */
75
 
        double taps_[MAXFILTERSIZE];
76
 
        int counter_;
77
 
};
78
 
 
79
 
 
80
 
class DelayDetection {
81
 
    public:
82
 
 
83
 
        DelayDetection();
84
 
 
85
 
        ~DelayDetection();
86
 
 
87
 
        void putData(SFLDataFormat *inputData, int samples);
88
 
 
89
 
        void process(SFLDataFormat *inputData, int samples);
90
 
 
91
 
    private:
92
 
 
93
 
        enum State {
94
 
            WaitForSpeaker,
95
 
            WaitForMic,
96
 
            ComputeCorrelation
97
 
        };
98
 
 
99
 
 
100
 
        /**
101
 
         * Perform a normalized crosscorrelation between template and segment
102
 
         */
103
 
        void crossCorrelate(float *ref, float *seg, float *res, int refSize, int segSize);
104
 
 
105
 
        /**
106
 
         * Perform a correlation on specified signals (mac)
107
 
         */
108
 
        double correlate(float *sig1, float *sig2, short size);
109
 
 
110
 
        void convertInt16ToFloat32(SFLDataFormat *input, float *ouput, int nbSamples);
111
 
 
112
 
        void downsampleData(float *input, float *output, int nbSamples, int factor);
113
 
 
114
 
        void bandpassFilter(float *input, int nbSamples);
115
 
 
116
 
        static int getMaxIndex(float *data, int size);
117
 
 
118
 
        State internalState_;
119
 
 
120
 
        FirFilter decimationFilter_;
121
 
 
122
 
        FirFilter bandpassFilter_;
123
 
 
124
 
        /**
125
 
         * Segment size in samples for correlation
126
 
         */
127
 
        short segmentSize_;
128
 
 
129
 
        int downsamplingFactor_;
130
 
 
131
 
        float spkrReference_[WINDOW_SIZE*2];
132
 
 
133
 
        float capturedData_[DELAY_BUFF_SIZE*2];
134
 
 
135
 
        float spkrReferenceDown_[WINDOW_SIZE*2];
136
 
 
137
 
        float captureDataDown_[DELAY_BUFF_SIZE*2];
138
 
 
139
 
        float spkrReferenceFilter_[WINDOW_SIZE*2];
140
 
 
141
 
        float captureDataFilter_[DELAY_BUFF_SIZE*2];
142
 
 
143
 
        float correlationResult_[DELAY_BUFF_SIZE*2];
144
 
 
145
 
        int spkrDownSize_;
146
 
 
147
 
        int micDownSize_;
148
 
 
149
 
        int nbMicSampleStored_;
150
 
 
151
 
        int nbSpkrSampleStored_;
152
 
 
153
 
    public:
154
 
 
155
 
        friend class DelayDetectionTest;
156
 
};
157
 
 
158
 
#endif