3 #ifndef SPECTMORPH_MATH_HH 4 #define SPECTMORPH_MATH_HH 12 #include <xmmintrin.h> 34 sm_sincos (
double x,
double *s,
double *c)
72 template<
class Iterator,
int MODE>
74 internal_fast_vector_sin (
const VectorSinParams& params, Iterator begin, Iterator end)
76 g_return_if_fail (params.
mix_freq > 0 && params.
freq > 0 && params.
phase > -99 && params.
mag > 0);
78 const double phase_inc = params.
freq / params.
mix_freq * 2 * M_PI;
79 const double inc_re = cos (phase_inc);
80 const double inc_im = sin (phase_inc);
86 sm_sincos (params.
phase, &state_im, &state_re);
87 state_re *= params.
mag;
88 state_im *= params.
mag;
90 for (Iterator x = begin; x != end; x++)
96 if ((n++ & 255) == 255)
98 sm_sincos (phase_inc * n + params.
phase, &state_im, &state_re);
99 state_re *= params.
mag;
100 state_im *= params.
mag;
108 const double re = state_re * inc_re - state_im * inc_im;
109 const double im = state_re * inc_im + state_im * inc_re;
116 template<
class Iterator,
int MODE>
118 internal_fast_vector_sincos (
const VectorSinParams& params, Iterator sin_begin, Iterator sin_end, Iterator cos_begin)
120 g_return_if_fail (params.
mix_freq > 0 && params.
freq > 0 && params.
phase > -99 && params.
mag > 0);
122 const double phase_inc = params.
freq / params.
mix_freq * 2 * M_PI;
123 const double inc_re = cos (phase_inc);
124 const double inc_im = sin (phase_inc);
130 sm_sincos (params.
phase, &state_im, &state_re);
131 state_re *= params.
mag;
132 state_im *= params.
mag;
134 for (Iterator x = sin_begin, y = cos_begin; x != sin_end; x++, y++)
146 if ((n++ & 255) == 255)
148 sm_sincos (phase_inc * n + params.
phase, &state_im, &state_re);
149 state_re *= params.
mag;
150 state_im *= params.
mag;
158 const double re = state_re * inc_re - state_im * inc_im;
159 const double im = state_re * inc_im + state_im * inc_re;
166 template<
class Iterator>
168 fast_vector_sin (
const VectorSinParams& params, Iterator sin_begin, Iterator sin_end)
172 internal_fast_vector_sin<Iterator, VectorSinParams::ADD> (params, sin_begin, sin_end);
176 internal_fast_vector_sin<Iterator, VectorSinParams::REPLACE> (params, sin_begin, sin_end);
180 g_assert_not_reached();
184 template<
class Iterator>
186 fast_vector_sincos (
const VectorSinParams& params, Iterator sin_begin, Iterator sin_end, Iterator cos_begin)
190 internal_fast_vector_sincos<Iterator, VectorSinParams::ADD> (params, sin_begin, sin_end, cos_begin);
194 internal_fast_vector_sincos<Iterator, VectorSinParams::REPLACE> (params, sin_begin, sin_end, cos_begin);
198 g_assert_not_reached();
214 template<
bool NEED_COS,
int MODE>
216 internal_fast_vector_sincosf (
const VectorSinParams& params,
float *sin_begin,
float *sin_end,
float *cos_begin)
219 g_return_if_fail (params.
mix_freq > 0 && params.
freq > 0 && params.
phase > -99 && params.
mag > 0);
221 const int TABLE_SIZE = 32;
223 const double phase_inc = params.
freq / params.
mix_freq * 2 * M_PI;
224 const double inc_re16 = cos (phase_inc * TABLE_SIZE * 4);
225 const double inc_im16 = sin (phase_inc * TABLE_SIZE * 4);
231 sm_sincos (params.
phase, &state_im, &state_re);
232 state_re *= params.
mag;
233 state_im *= params.
mag;
235 F4Vector incf_re[TABLE_SIZE];
236 F4Vector incf_im[TABLE_SIZE];
240 table_params.
phase = 0;
241 table_params.
mag = 1;
243 fast_vector_sincos (table_params, incf_im[0].f, incf_im[0].f + (TABLE_SIZE * 4), incf_re[0].f);
246 int todo = sin_end - sin_begin;
247 while (todo >= 4 * TABLE_SIZE)
251 sf_re.f[0] = state_re;
252 sf_re.f[1] = state_re;
253 sf_re.f[2] = state_re;
254 sf_re.f[3] = state_re;
255 sf_im.f[0] = state_im;
256 sf_im.f[1] = state_im;
257 sf_im.f[2] = state_im;
258 sf_im.f[3] = state_im;
266 F4Vector *new_im =
reinterpret_cast<F4Vector *
> (sin_begin + n);
267 F4Vector *new_re =
reinterpret_cast<F4Vector *
> (cos_begin + n);
268 for (
int k = 0; k < TABLE_SIZE; k++)
274 new_re[k].v = _mm_add_ps (new_re[k].v, _mm_sub_ps (_mm_mul_ps (sf_re.v, incf_re[k].v),
275 _mm_mul_ps (sf_im.v, incf_im[k].v)));
277 new_im[k].v = _mm_add_ps (new_im[k].v, _mm_add_ps (_mm_mul_ps (sf_re.v, incf_im[k].v),
278 _mm_mul_ps (sf_im.v, incf_re[k].v)));
284 new_re[k].v = _mm_sub_ps (_mm_mul_ps (sf_re.v, incf_re[k].v),
285 _mm_mul_ps (sf_im.v, incf_im[k].v));
287 new_im[k].v = _mm_add_ps (_mm_mul_ps (sf_re.v, incf_im[k].v),
288 _mm_mul_ps (sf_im.v, incf_re[k].v));
298 const double re = state_re * inc_re16 - state_im * inc_im16;
299 const double im = state_re * inc_im16 + state_im * inc_re16;
303 todo -= 4 * TABLE_SIZE;
308 rest_params.
phase += n * phase_inc;
310 fast_vector_sincos (rest_params, sin_begin + n, sin_end, cos_begin + n);
312 fast_vector_sin (rest_params, sin_begin + n, sin_end);
315 fast_vector_sincos (params, sin_begin, sin_end, cos_begin);
317 fast_vector_sin (params, sin_begin, sin_end);
322 fast_vector_sincosf (
const VectorSinParams& params,
float *sin_begin,
float *sin_end,
float *cos_begin)
326 internal_fast_vector_sincosf<true, VectorSinParams::ADD> (params, sin_begin, sin_end, cos_begin);
330 internal_fast_vector_sincosf<true, VectorSinParams::REPLACE> (params, sin_begin, sin_end, cos_begin);
334 g_assert_not_reached();
339 fast_vector_sinf (
const VectorSinParams& params,
float *sin_begin,
float *sin_end)
343 internal_fast_vector_sincosf<false, VectorSinParams::ADD> (params, sin_begin, sin_end, NULL);
347 internal_fast_vector_sincosf<false, VectorSinParams::REPLACE> (params, sin_begin, sin_end, NULL);
351 g_assert_not_reached();
356 zero_float_block (
size_t n_values,
float *values)
358 memset (values, 0, n_values *
sizeof (
float));
364 extern float *int_sincos_table;
366 return int_sincos_table[i];
372 extern float *int_sincos_table;
375 return int_sincos_table[i];
381 extern float *int_sincos_table;
383 int_sincos_table = (
float *) malloc (
sizeof (
float) * 256);
384 for (
int i = 0; i < 256; i++)
385 int_sincos_table[i] = sin (
double (i / 256.0) * 2 * M_PI);
391 window_cos (
double x)
395 return 0.5 * cos (x * M_PI) + 0.5;
399 window_hamming (
double x)
404 return 0.54 + 0.46 * cos (M_PI * x);
408 window_blackman (
double x)
412 return 0.42 + 0.5 * cos (M_PI * x) + 0.08 * cos (2.0 * M_PI * x);
416 window_blackman_harris_92 (
double x)
421 const double a0 = 0.35875, a1 = 0.48829, a2 = 0.14128, a3 = 0.01168;
423 return a0 + a1 * cos (M_PI * x) + a2 * cos (2.0 * M_PI * x) + a3 * cos (3.0 * M_PI * x);
427 double db_to_factor (
double dB);
428 double db_from_factor (
double factor,
double min_dB);
430 #if defined (__i386__) && defined (__GNUC__) 431 static inline int G_GNUC_CONST
432 sm_ftoi (
register float f)
441 static inline int G_GNUC_CONST
442 sm_dtoi (
register double f)
452 sm_round_positive (
double d)
458 sm_round_positive (
float f)
464 sm_round_positive (
double d)
466 return int (d + 0.5);
470 sm_round_positive (
float f)
472 return int (f + 0.5);
476 int sm_fpu_okround();
480 static float idb2f_high[256];
481 static float idb2f_low[256];
483 static float ifreq2f_high[256];
484 static float ifreq2f_low[256];
487 #define SM_IDB_CONST_M96 uint16_t ((512 - 96) * 64) 489 int sm_factor2delta_idb (
double factor);
490 double sm_idb2factor_slow (uint16_t idb);
494 uint16_t sm_freq2ifreq (
double freq);
495 double sm_ifreq2freq_slow (uint16_t ifreq);
498 sm_idb2factor (uint16_t idb)
500 return MathTables::idb2f_high[idb >> 8] * MathTables::idb2f_low[idb & 0xff];
504 sm_ifreq2freq (uint16_t ifreq)
506 return MathTables::ifreq2f_high[ifreq >> 8] * MathTables::ifreq2f_low[ifreq & 0xff];
510 sm_factor2idb (
double factor)
518 const double db = 20 * log10 (std::max (factor, 1e-25));
520 return sm_round_positive (db * 64 + 512 * 64);
523 double sm_lowpass1_factor (
double mix_freq,
double freq);
524 double sm_xparam (
double x,
double slope);
525 double sm_xparam_inv (
double x,
double slope);
527 double sm_bessel_i0 (
double x);
528 double velocity_to_gain (
double velocity,
double vrange_db);
532 sm_bound (
const T& min_value,
const T& value,
const T& max_value)
534 return std::min (std::max (value, min_value), max_value);
Definition: smmath.hh:478
double freq
the frequency of the sin (and cos) wave to be created
Definition: smmath.hh:52
double phase
the start phase of the wave
Definition: smmath.hh:53
replace values in the output array with computed values
Definition: smmath.hh:59
double mag
the magnitude (amplitude) of the wave
Definition: smmath.hh:54
parameter structure for the various optimized vector sine functions
Definition: smmath.hh:48
Definition: smadsrenvelope.hh:8
double mix_freq
the mix freq (sampling rate) of the sin (and cos) wave to be created
Definition: smmath.hh:51
enum SpectMorph::VectorSinParams::@2 mode
whether to overwrite or add output
add computed values to the values that are in the output array
Definition: smmath.hh:58