~ubuntu-branches/ubuntu/saucy/sflphone/saucy

« back to all changes in this revision

Viewing changes to sflphone-common/src/audio/sound/tone.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Francois Marier
  • Date: 2010-12-24 16:33:55 UTC
  • mfrom: (1.1.2 upstream)
  • Revision ID: james.westby@ubuntu.com-20101224163355-tkvvikqxbrbav6up
Tags: 0.9.11-1
* New upstream release
* Add new build dependencies on libwebkit-dev and libyaml-dev

* Bump Standards-Version up to 3.9.1
* Bump debhelper compatibility to 8
* Patch another typo in the upstream code (lintian notice)

Show diffs side-by-side

added added

removed removed

Lines of Context:
42
42
#define TABLE_LENGTH 4096
43
43
double TWOPI = 2 * M_PI;
44
44
 
45
 
Tone::Tone (const std::string& definition, unsigned int sampleRate) : AudioLoop(), _sampleRate (sampleRate), _xhigher(0.0), _xlower(0.0)
 
45
Tone::Tone (const std::string& definition, unsigned int sampleRate) : AudioLoop(), _sampleRate (sampleRate), _xhigher (0.0), _xlower (0.0)
46
46
{
47
 
        fillWavetable();
 
47
    fillWavetable();
48
48
    genBuffer (definition); // allocate memory with definition parameter
49
49
}
50
50
 
116
116
            }
117
117
 
118
118
            // Generate SAMPLING_RATE samples of sinus, buffer is the result
119
 
            _debug("genSin(%d, %d)", freq1, freq2);
 
119
            _debug ("genSin(%d, %d)", freq1, freq2);
120
120
            genSin (bufferPos, freq1, freq2, count);
121
121
 
122
122
            // To concatenate the different buffers for each section.
143
143
void
144
144
Tone::fillWavetable()
145
145
{
146
 
        double tableSize = (double)TABLE_LENGTH;
 
146
    double tableSize = (double) TABLE_LENGTH;
147
147
 
148
 
        for(int i = 0; i < TABLE_LENGTH; i++) {
149
 
                _wavetable[i] = sin( ((double)i / (tableSize - 1.0)) * TWOPI );
150
 
        }
 
148
    for (int i = 0; i < TABLE_LENGTH; i++) {
 
149
        _wavetable[i] = sin ( ( (double) i / (tableSize - 1.0)) * TWOPI);
 
150
    }
151
151
}
152
152
 
153
153
double
154
 
Tone::interpolate(double x)
 
154
Tone::interpolate (double x)
155
155
{
156
 
        int xi_0, xi_1;
157
 
        double yi_0, yi_1, A, B;
158
 
 
159
 
        xi_0 = (int)x;
160
 
        xi_1 = xi_0+1;
161
 
 
162
 
        yi_0  =_wavetable[xi_0];
163
 
        yi_1 = _wavetable[xi_1];
164
 
 
165
 
        A = (x - xi_0);
166
 
        B = 1.0 - A;
167
 
 
168
 
        return A*yi_0 + B*yi_1;
 
156
    int xi_0, xi_1;
 
157
    double yi_0, yi_1, A, B;
 
158
 
 
159
    xi_0 = (int) x;
 
160
    xi_1 = xi_0+1;
 
161
 
 
162
    yi_0  =_wavetable[xi_0];
 
163
    yi_1 = _wavetable[xi_1];
 
164
 
 
165
    A = (x - xi_0);
 
166
    B = 1.0 - A;
 
167
 
 
168
    return A*yi_0 + B*yi_1;
169
169
}
170
170
 
171
171
void
172
172
Tone::genSin (SFLDataFormat* buffer, int frequency1, int frequency2, int nb)
173
173
{
174
 
        _xhigher = 0.0;
175
 
        _xlower = 0.0;
176
 
 
177
 
        double sr = (double)_sampleRate;
178
 
        double tableSize = (double)TABLE_LENGTH;
179
 
 
180
 
         double N_h = sr / (double) (frequency1);
181
 
         double N_l = sr / (double)  (frequency2);
182
 
 
183
 
         double dx_h = tableSize / N_h;
184
 
         double dx_l = tableSize / N_l;
185
 
 
186
 
         double x_h = _xhigher;
187
 
         double x_l = _xlower;
188
 
 
189
 
         double amp = (double)SFLDataAmplitude;
190
 
 
191
 
         for (int t = 0; t < nb; t ++) {
192
 
                 buffer[t] = (int16)(amp*(interpolate(x_h) + interpolate(x_l)));
193
 
                 x_h += dx_h;
194
 
                 x_l += dx_l;
195
 
 
196
 
                 if(x_h > tableSize) {
197
 
                         x_h -= tableSize;
198
 
                }
199
 
 
200
 
                 if(x_l > tableSize) {
201
 
                         x_l -= tableSize;
202
 
                }
203
 
         }
204
 
 
205
 
         _xhigher = x_h;
206
 
         _xlower = x_l;
 
174
    _xhigher = 0.0;
 
175
    _xlower = 0.0;
 
176
 
 
177
    double sr = (double) _sampleRate;
 
178
    double tableSize = (double) TABLE_LENGTH;
 
179
 
 
180
    double N_h = sr / (double) (frequency1);
 
181
    double N_l = sr / (double) (frequency2);
 
182
 
 
183
    double dx_h = tableSize / N_h;
 
184
    double dx_l = tableSize / N_l;
 
185
 
 
186
    double x_h = _xhigher;
 
187
    double x_l = _xlower;
 
188
 
 
189
    double amp = (double) SFLDataAmplitude;
 
190
 
 
191
    for (int t = 0; t < nb; t ++) {
 
192
        buffer[t] = (int16) (amp* (interpolate (x_h) + interpolate (x_l)));
 
193
        x_h += dx_h;
 
194
        x_l += dx_l;
 
195
 
 
196
        if (x_h > tableSize) {
 
197
            x_h -= tableSize;
 
198
        }
 
199
 
 
200
        if (x_l > tableSize) {
 
201
            x_l -= tableSize;
 
202
        }
 
203
    }
 
204
 
 
205
    _xhigher = x_h;
 
206
    _xlower = x_l;
207
207
 
208
208
}
209
209