Rev 149 |
Blame |
Compare with Previous |
Last modification |
View Log
| RSS feed
/*
* sounder.cc
* DIN Is Noise is copyright (c) 2006-2017 Jagannathan Sampath
* For more information, please visit http://dinisnoise.org/
*/
#include "sounder.h"
#include "audio.h"
#include "console.h"
extern void hz2step (float& hz, float& step);
sounder::sounder (multi_curve* wav, const std::string& _name, float _hz, float _l, float _r, int _bias, float _fadet) {
name = _name;
l = r = 0.0f;
soll (wav);
solr (wav);
playl.set_wave (&soll);
playr.set_wave (&solr);
set_hz (_hz);
set_vol (_l, _r, _bias, _fadet);
}
void sounder::set_vol (float _l, float _r, int _b, float _t) {
lprev = l;
rprev = r;
l = _l;
r = _r;
bias = _b;
fading = 1;
}
void sounder::render (float* L, float* R, float* fdr_alphas) {
play* pl[] = {&playl, &playr};
float* out[] = {L, R};
float* outb[] = {aout.bufL, aout.bufR};
float v[] = {l, r};
float vprev[] = {lprev, rprev};
float* vb[] = {aout.fdrL, aout.fdrR};
for (int i = 0; i < 2; ++i) {
float* outi = out[i];
float* outbi = outb[i];
memset (outbi, 0, aout.samples_channel_size);
play* pli = pl[i];
if (fading) {
calc_fader (vb[i], fdr_alphas, aout.samples_per_channel, vprev[i], v[i]);
pli->gen_wav_mix (outbi, vb[i], aout.samples_per_channel);
} else pli->gen_wav_mix (outbi, v[i], aout.samples_per_channel);
for (int j = 0, k = aout.samples_per_channel; j < k; ++j) outi[j] += outbi[j];
}
}
void sounder::set_hz (float _hz) {
hz = _hz; hz2step (hz, step);
playl.fill_pitch (step);
playr.fill_pitch (step);
}
void sounder::eval_fade () {
int before = fdr.on, after;
if (before) {
after = fdr.eval ();
if (after != before) if (--NUM_FADERS < 0) NUM_FADERS = 0;
if (bias == LEFT) l = fdr.value (); else r = fdr.value ();
}
}
void sounder::calc_fader (float* out, float* alpha, int n, float v1, float v2) {
float dv = v2 - v1;
for (int i = 0; i < n; ++i) {
float ai = alpha[i];
out[i] = v1 + ai * dv;
}
}
void sounder::clear_fading () {
fading = 0;
}