The Tooba
 All Data Structures Namespaces Files Functions Variables Macros Pages
voice.h
Go to the documentation of this file.
1 #ifndef VOICE_H_INCLUDED
2 #define VOICE_H_INCLUDED 1
3 
4 #include <stdio.h>
5 #include <math.h>
6 
7 #define UNIT 0x100000000LL
8 
9 /* 1 / (1 - 1/e), because exponential */
10 #define BIGGER ((uint64_t) (1.5819767 * UNIT))
11 
12 class ADSR {
13  uint32_t _state, _value; // adsr
14  uint32_t attack, decay, sustain, release;
15  uint64_t gap;
16 
17 public:
18  ADSR() {
19  _state = _value = 0;
20  }
21  void setA(float a) {
22  if (a < 0.01) a = 0.01;
23  // attack = (int32_t)(UNIT * exp(-DT / a));
24  // Switching to LINEAR attack
25  attack = (uint32_t) (DT * UNIT / a);
26  }
27  void setD(float d) {
28  if (d < 0.01) d = 0.01;
29  decay = (uint32_t)(UNIT * exp(-DT / d));
30  }
31  void setS(float s) {
32  sustain = (uint32_t) (UNIT * s);
33  }
34  void setR(float r) {
35  if (r < 0.01) r = 0.01;
36  release = (uint32_t)(UNIT * exp(-DT / r));
37  }
38 
39  uint32_t state() {
40  return _state;
41  }
42  uint32_t output() {
43  return _value;
44  }
45  void keydown(uint32_t down) {
46  if (down) {
47  _state = 1;
48  } else {
49  _state = 0;
50  }
51  }
52  void step(void) {
53  uint64_t x;
54  if (_state == 1) {
55  // attack
56  x = ((uint64_t) _value) + attack;
57  if (x >= UNIT) {
58  _state = 2;
59  _value = UNIT - 1;
60  gap = _value - sustain;
61  } else {
62  _value = x;
63  }
64  }
65  else if (_state == 2) {
66  // decay
67  _value = gap + sustain;
68  x = gap;
69  gap = (x * decay) >> 32;
70  }
71  else if (_state == 0) {
72  // release
73  x = _value;
74  if (_value < (1 << 18))
75  // fix for a fixed-point bug where notes
76  // never really completely end
77  _value = 0;
78  else
79  _value = (x * release) >> 32;
80  }
81  }
82 };
83 
84 class Oscillator {
85  uint32_t phase, dphase, waveform; // oscillator
86 
87 public:
89  waveform = 1;
90  }
91 
92  void setfreq(float f) {
93  dphase = (int32_t)(UNIT * f / SAMPLING_RATE);
94  }
95  void setwaveform(int32_t x) { // 0 for ramp, 1 for triangle
96  waveform = x;
97  }
98 
99  void step(void) {
100  phase += dphase;
101  }
102  int32_t output(void) {
103  int64_t x = 0;
104  switch (waveform) {
105  default:
106  case 0:
107  // ramp
108  x = phase;
109  return x - 0x80000000;
110  break;
111  case 1:
112  // triangle
113  if (phase >= 0x80000000) {
114  x = ~phase;
115  } else {
116  x = phase;
117  }
118  return (x << 1) - 0x80000000;
119  break;
120  case 2:
121  // square
122  if (phase >= 0x80000000) {
123  return 0x7fffffff;
124  } else {
125  return -0x80000000;
126  }
127  break;
128  }
129  }
130 };
131 
132 class Voice {
133 public:
136 
137  Voice() {
138  osc1.setwaveform(1);
139  osc2.setwaveform(1);
140  osc3.setwaveform(1);
141  adsr.setA(0.03);
142  adsr.setD(0.3);
143  adsr.setS(0.4);
144  adsr.setR(0.1);
145  }
146  void step(void) {
147  osc1.step();
148  osc2.step();
149  osc3.step();
150  adsr.step();
151  }
152  void setfreq(float f) {
153  osc1.setfreq(f);
154  osc2.setfreq(f + 3);
155  osc3.setfreq(f - 5);
156  }
157  void keydown(uint32_t down) {
158  adsr.keydown(down);
159  }
160  int32_t output(void) {
161  int64_t x = osc1.output();
162  x += osc2.output();
163  x += osc3.output();
164  return mult_unsigned_signed(adsr.output(), x >> 2);
165  }
166 };
167 
168 #endif // VOICE_H_INCLUDED
void setfreq(float f)
Definition: voice.h:152
void setfreq(float f)
Definition: voice.h:92
int32_t output(void)
Definition: voice.h:102
void step(void)
Definition: voice.h:52
uint32_t _value
Definition: voice.h:13
void setR(float r)
Definition: voice.h:34
uint32_t dphase
Definition: voice.h:85
Oscillator()
Definition: voice.h:88
void step(void)
Definition: voice.h:99
uint32_t output()
Definition: voice.h:42
Oscillator osc1
Definition: voice.h:134
Definition: voice.h:132
Oscillator osc2
Definition: voice.h:134
ADSR adsr
Definition: voice.h:135
void setA(float a)
Definition: voice.h:21
void setS(float s)
Definition: voice.h:31
void setD(float d)
Definition: voice.h:27
Oscillator osc3
Definition: voice.h:134
uint32_t state()
Definition: voice.h:39
def f
Definition: hack.py:34
void keydown(uint32_t down)
Definition: voice.h:45
uint64_t gap
Definition: voice.h:15
uint32_t waveform
Definition: voice.h:85
uint32_t release
Definition: voice.h:14
ADSR()
Definition: voice.h:18
int32_t output(void)
Definition: voice.h:160
uint32_t attack
Definition: voice.h:14
#define SAMPLING_RATE
Definition: common.h:27
Definition: voice.h:12
void setwaveform(int32_t x)
Definition: voice.h:95
uint32_t sustain
Definition: voice.h:14
uint32_t phase
Definition: voice.h:85
void step(void)
Definition: voice.h:146
int32_t mult_unsigned_signed(uint32_t x, int32_t y)
Definition: common.cpp:13
uint32_t decay
Definition: voice.h:14
tuple x
Definition: testit.py:14
void keydown(uint32_t down)
Definition: voice.h:157
uint32_t _state
Definition: voice.h:13
Voice()
Definition: voice.h:137
#define UNIT
Definition: voice.h:7
#define DT
Definition: common.h:28