~noskcaj/ubuntu/saucy/sflphone/merge-1.2.3-2

« back to all changes in this revision

Viewing changes to daemon/src/audio/sound/tone.cpp

  • Committer: Package Import Robot
  • Author(s): Francois Marier
  • Date: 2012-02-18 21:47:09 UTC
  • mfrom: (1.1.6)
  • Revision ID: package-import@ubuntu.com-20120218214709-6362d71gqdsdkrj5
Tags: 1.0.2-1
* New upstream release
  - remove logging patch (applied upstream)
  - update s390 patch since it was partially applied upstream
* Include the Evolution plugin as a separate binary package

* Fix compilation issues on SH4 (closes: #658987)
* Merge Ubuntu's binutils-gold linking fix

Show diffs side-by-side

added added

removed removed

Lines of Context:
36
36
 */
37
37
#include "tone.h"
38
38
#include <cmath>
 
39
#include <cassert>
39
40
#include <cstdlib>
40
41
#include <cstring>
41
42
 
42
 
static const double TWOPI = 2.0 * M_PI;
43
 
 
44
43
Tone::Tone(const std::string& definition, unsigned int sampleRate) :
45
44
    sampleRate_(sampleRate), xhigher_(0.0), xlower_(0.0)
46
45
{
121
120
        posStart = posEnd + 1;
122
121
    } while (posStart < deflen);
123
122
 
 
123
    assert(!buffer_);
124
124
    buffer_ = new SFLDataFormat[size_];
125
125
 
126
126
    memcpy(buffer_, buffer, size_ * sizeof(SFLDataFormat)); // copy char, not SFLDataFormat.
131
131
void
132
132
Tone::fillWavetable()
133
133
{
134
 
    double tableSize = (double) TABLE_LENGTH;
 
134
    double tableSize = TABLE_LENGTH;
 
135
    static const double TWO_PI = 2.0 * M_PI;
135
136
 
136
137
    for (int i = 0; i < TABLE_LENGTH; ++i)
137
 
        wavetable_[i] = sin((static_cast<double>(i) / (tableSize - 1.0)) * TWOPI);
 
138
        wavetable_[i] = sin((i / (tableSize - 1.0)) * TWO_PI);
138
139
}
139
140
 
140
141
double
141
142
Tone::interpolate(double x)
142
143
{
143
 
    int xi_0, xi_1;
144
 
    double yi_0, yi_1, A, B;
145
 
 
146
 
    xi_0 = (int) x;
147
 
    xi_1 = xi_0 + 1;
148
 
 
149
 
    yi_0 = wavetable_[xi_0];
150
 
    yi_1 =  wavetable_[xi_1];
151
 
 
152
 
    A = (x - xi_0);
153
 
    B = 1.0 - A;
 
144
    int xi_0 = x;
 
145
    int xi_1 = xi_0 + 1;
 
146
 
 
147
    double yi_0 = wavetable_[xi_0];
 
148
    double yi_1 =  wavetable_[xi_1];
 
149
 
 
150
    double A = (x - xi_0);
 
151
    double B = 1.0 - A;
154
152
 
155
153
    return (A * yi_0) + (B * yi_1);
156
154
}
161
159
    xhigher_ = 0.0;
162
160
    xlower_ = 0.0;
163
161
 
164
 
    double sr = (double) sampleRate_;
165
 
    double tableSize = (double) TABLE_LENGTH;
 
162
    double sr = sampleRate_;
 
163
    double tableSize = TABLE_LENGTH;
166
164
 
167
 
    double N_h = sr / (double)(frequency1);
168
 
    double N_l = sr / (double)(frequency2);
 
165
    double N_h = sr / frequency1;
 
166
    double N_l = sr / frequency2;
169
167
 
170
168
    double dx_h = tableSize / N_h;
171
169
    double dx_l = tableSize / N_l;
177
175
    double amp =  DATA_AMPLITUDE;
178
176
 
179
177
    for (int t = 0; t < nb; t ++) {
180
 
        buffer[t] = static_cast<SFLDataFormat>(amp * (interpolate(x_h) + interpolate(x_l)));
 
178
        buffer[t] = amp * (interpolate(x_h) + interpolate(x_l));
181
179
        x_h += dx_h;
182
180
        x_l += dx_l;
183
181