Subversion Repositories DIN Is Noise

Rev

Rev 1869 | Rev 2097 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
964 jag 1
/*
2
* curve_mixer.cc
2009 jag 3
* DIN Is Noise is copyright (c) 2006-2023 Jagannathan Sampath
1713 jag 4
* DIN Is Noise is released under GNU Public License 2.0
1479 jag 5
* For more information, please visit https://dinisnoise.org/
964 jag 6
*/
7
 
8
#include "curve_mixer.h"
9
#include "multi_curve.h"
10
#include "console.h"
11
#include "chrono.h"
12
#include <algorithm>
13
 
14
#include <string>
15
 
16
extern int SAMPLE_RATE;
1799 jag 17
extern char BUFFER [];
964 jag 18
 
19
curve_mixer::curve_mixer () {
20
  a = da = 0;
21
  x = dx = 0;
22
  active = 0;
23
}
24
 
25
curve_mixer::~curve_mixer () {
1869 jag 26
  if (active) cons << GREEN << "Finished mixing (@ " << name << ")" << eol;
964 jag 27
}
28
 
29
void curve_mixer::setup (multi_curve& crv, float ix, float idx) {
30
  a = 0.0f;
31
  da = 1./ (TIME * SAMPLE_RATE);
32
  x = ix;
33
  dx = idx;
34
  mix = crv;
35
  sol (&mix);
36
  active = 1;
37
  ELAPSEDT = 0;
38
}
39
 
40
void curve_mixer::gen_mix (float* out, int n) {
41
  sol (x, dx, n, out);
42
}
43
 
44
void curve_mixer::gen_mix (float* out, int n, xhandler& xmin, xhandler& xmax) {
45
  sol (x, dx, n, out, xmin, xmax);
46
}
47
 
48
void curve_mixer::gen_fm (float* out, int n, float* fm) {
49
  sol (x, dx, n, fm, out);
50
}
51
 
52
void curve_mixer::fill_alpha (float* ab, int n) {
53
  for (int i = 0; i < n; ++i) {
1799 jag 54
    ab[i] = a;
964 jag 55
    a += da;
56
  }
57
}
58
 
59
void curve_mixer::gen_alpha (float* ab, int n) {
60
  float fa = a + n * da;
61
  if (fa < 1.0) {
62
    fill_alpha (ab, n);
63
  } else {
64
    int n1 = (1 - a) / da;
65
    fill_alpha (ab, n1);
66
    int n2 = n - n1;
67
    for (int i = n1, j = n1 + n2; i < j; ++i) ab[i] = 1.0f;
68
    a = 1.0f;
69
    active = 0;
1869 jag 70
    sprintf (BUFFER, "Curve mixing complete (@ %s)", name.c_str());
1799 jag 71
    cons << GREEN << BUFFER << eol;
964 jag 72
  }
73
}
74
 
75
void curve_mixer::do_mix (float* out, float* mixb, float* mixa, int n) {
76
 
77
  gen_alpha (mixa, n);
78
 
79
  for (int i = 0; i < n; ++i) {
80
    float mv = mixb[i];
81
    out[i] = mv + mixa[i] * (out[i] - mv);
82
  }
83
 
1799 jag 84
  if (active && ui_clk() >= ELAPSEDT) {
1869 jag 85
    sprintf (BUFFER, "Curve mixing %d%% complete (@ %s)", int(a * 100), name.c_str());
1799 jag 86
    cons << YELLOW << BUFFER << eol;
964 jag 87
    ELAPSEDT = ui_clk () + std::min<double> (1.0, 0.05 * TIME);
88
  }
89
 
90
}