Synth.cpp
 All Data Structures Namespaces Files Functions Variables Macros
voice.h
Go to the documentation of this file.
1 #ifndef VOICE_H_INCLUDED
2 #define VOICE_H_INCLUDED 1
3 
4 #include "synth.h"
5 
6 
7 class SimpleVoice : public IVoice {
8 public:
11 
13  osc1.setwaveform(1);
14  adsr.setA(0.1);
15  adsr.setD(0.4);
16  adsr.setS(0.2);
17  adsr.setR(0.1);
18  }
19  void step(void) {
20  osc1.step();
21  adsr.step();
22  }
23  void quiet(void) {
24  adsr.quiet();
25  }
26  bool idle(void) {
27  return adsr.state() == 0;
28  }
29  void setfreq(float f) {
30  osc1.setfreq(f);
31  }
32  void keydown(void) {
33  adsr.keydown();
34  }
35  void keyup(void) {
36  adsr.keyup();
37  }
38  int32_t output(void) {
39  return ((int32_t)adsr.output() * osc1.output()) >> 12;
40  }
41  void ioctl(uint32_t param, uint32_t value) {
42  // do nothing for now
43  }
44 };
45 
46 class TwoSquaresVoice : public IVoice {
47 public:
50 
52  osc1.setwaveform(2); // I <3 square waves
53  osc2.setwaveform(2);
54  adsr.setA(0.3);
55  adsr.setD(0.4);
56  adsr.setS(0.2);
57  adsr.setR(0.1);
58  adsr2.setA(0.01);
59  adsr2.setD(0.1);
60  adsr2.setS(0.1);
61  adsr2.setR(0.1);
62  }
63  void step(void) {
64  osc1.step();
65  osc2.step();
66  adsr.step();
67  adsr2.step();
68  }
69  void quiet(void) {
70  adsr.quiet();
71  adsr2.quiet();
72  }
73  bool idle(void) {
74  return adsr.state() == 0 && adsr2.state() == 0;
75  }
76  void setfreq(float f) {
77  osc1.setfreq(f);
78  osc2.setfreq(f / 2);
79  }
80  void keydown(void) {
81  adsr.keydown();
82  adsr2.keydown();
83  }
84  void keyup(void) {
85  adsr.keyup();
86  adsr2.keyup();
87  }
88  int32_t output(void) {
89  return (((int32_t)adsr.output() * osc1.output()) >> 14) +
90  (((int32_t)adsr2.output() * osc2.output()) >> 14);
91  }
92  void ioctl(uint32_t param, uint32_t value) {
93  // do nothing for now
94  }
95 };
96 
97 class NoisyVoice : public IVoice {
98 public:
102  uint32_t _f;
103 
105  filt.setQ(6);
106  osc1.setwaveform(1);
107  osc2.setwaveform(2);
108  osc3.setwaveform(2);
109  adsr.setA(0.03);
110  adsr.setD(0.7);
111  adsr.setS(0.4);
112  adsr.setR(0.1);
113  adsr2.setA(0.03);
114  adsr2.setD(0.6);
115  adsr2.setS(0.0);
116  adsr2.setR(0.6);
117  }
118  void step(void) {
119  uint32_t __f = _f;
120  osc1.step();
121  osc2.step();
122  osc3.step();
123  adsr.step();
124  adsr2.step();
125  filt.setF((__f * adsr2.output()) >> 12);
126  filt.step((osc1.output() + osc2.output() + osc3.output()) >> 1);
127  }
128  bool idle(void) {
129  return adsr.state() == 0;
130  }
131  void setfreq(float f) {
132  _f = f;
133  osc1.setfreq(f);
134  osc2.setfreq(f + small_random());
135  osc3.setfreq(f / 2 + 0.5 * small_random());
136  }
137  void quiet(void) {
138  adsr.quiet();
139  adsr2.quiet();
140  }
141  void keydown(void) {
142  adsr.keydown();
143  adsr2.keydown();
144  }
145  void keyup(void) {
146  adsr.keyup();
147  adsr2.keyup();
148  }
149  int32_t output(void) {
150  int32_t x;
151  x = filt.bandpass();
152  x += filt.highpass() >> 1;
153  return ((int32_t)adsr.output() * x) >> 13;
154  }
155  void ioctl(uint32_t param, uint32_t value) {
156  // do nothing for now
157  }
158 };
159 
160 #endif // VOICE_H_INCLUDED
void keydown(void)
Definition: synth.cpp:253
void keyup(void)
Definition: voice.h:35
Filter filt
Definition: voice.h:101
void keydown(void)
Definition: voice.h:80
Oscillator osc1
Definition: voice.h:48
NoisyVoice()
Definition: voice.h:104
void setfreq(float f)
Definition: synth.h:257
def f
Definition: test.py:35
int32_t output(void)
Definition: synth.cpp:270
void step(void)
Definition: synth.cpp:262
void quiet(void)
Definition: synth.h:198
void setR(float r)
Definition: synth.cpp:249
void step(void)
Definition: voice.h:19
Oscillator osc3
Definition: voice.h:99
void keydown(void)
Definition: voice.h:141
void step(void)
Definition: synth.h:265
ADSR adsr
Definition: voice.h:100
uint32_t _f
Definition: voice.h:102
float small_random()
Definition: synth.cpp:45
void ioctl(uint32_t param, uint32_t value)
Definition: voice.h:41
void ioctl(uint32_t param, uint32_t value)
Definition: voice.h:92
Definition: synth.h:212
bool idle(void)
Definition: voice.h:73
ADSR adsr
Definition: voice.h:10
void setA(float a)
Definition: synth.cpp:237
void quiet(void)
Definition: voice.h:23
void setS(float s)
Definition: synth.cpp:245
void setD(float d)
Definition: synth.cpp:241
ADSR adsr2
Definition: voice.h:100
int32_t output(void)
Definition: voice.h:149
int32_t bandpass(void)
Definition: synth.h:229
Definition: synth.h:45
void keyup(void)
Definition: synth.cpp:258
void keydown(void)
Definition: voice.h:32
void setF(uint32_t f)
Definition: synth.cpp:305
void quiet(void)
Definition: voice.h:69
uint32_t state()
Definition: synth.h:195
Oscillator osc1
Definition: voice.h:99
bool idle(void)
Definition: voice.h:26
void keyup(void)
Definition: voice.h:84
bool idle(void)
Definition: voice.h:128
void quiet(void)
Definition: voice.h:137
Oscillator osc2
Definition: voice.h:99
void step(int32_t x)
Definition: synth.cpp:321
TwoSquaresVoice()
Definition: voice.h:51
ADSR adsr2
Definition: voice.h:49
ADSR adsr
Definition: voice.h:49
void setfreq(float f)
Definition: voice.h:76
SimpleVoice()
Definition: voice.h:12
Definition: synth.h:176
void step(void)
Definition: voice.h:63
void setfreq(float f)
Definition: voice.h:131
void step(void)
Definition: voice.h:118
int32_t output(void)
Definition: voice.h:88
void setwaveform(int32_t x)
Definition: synth.h:260
Oscillator osc2
Definition: voice.h:48
void ioctl(uint32_t param, uint32_t value)
Definition: voice.h:155
int32_t output(void)
Definition: voice.h:38
int32_t highpass(void)
Definition: synth.h:226
Synthesizer modules and supporting functions.
Oscillator osc1
Definition: voice.h:9
int32_t output()
Definition: synth.h:203
void setfreq(float f)
Definition: voice.h:29
void setQ(float q)
Definition: synth.cpp:312
void keyup(void)
Definition: voice.h:145