-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathsynth1.cpp
More file actions
183 lines (150 loc) · 5.99 KB
/
synth1.cpp
File metadata and controls
183 lines (150 loc) · 5.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/* Gamma - Generic processing library
See COPYRIGHT file for authors and license information
Example:
Description:
*/
#include <cstdio> // for printing to stdout
#define GAMMA_H_INC_ALL // define this to include all header files
#define GAMMA_H_NO_IO // define this to avoid bringing AudioIO from Gamma
#include "Gamma/Gamma.h"
#include "al/core/app/al_App.hpp"
#include "al/core/graphics/al_Shapes.hpp"
#include "al/util/ui/al_Parameter.hpp"
#include "al/util/scene/al_PolySynth.hpp"
#include "al/util/scene/al_SynthSequencer.hpp"
#include "al/util/ui/al_ControlGUI.hpp"
//using namespace gam;
using namespace al;
// This is the same SineEnv class defined in graphics/synth1.cpp
// It inclludes drawing code
class SineEnv : public SynthVoice {
public:
// Unit generators
gam::Pan<> mPan;
gam::Sine<> mOsc;
gam::Env<3> mAmpEnv;
gam::EnvFollow<> mEnvFollow; // envelope follower to connect audio output to graphics
// Additional members
Mesh mMesh;
// Initialize voice. This function will nly be called once per voice
virtual void init() {
// Intialize envelope
mAmpEnv.curve(0); // make segments lines
mAmpEnv.levels(0,1,1,0);
mAmpEnv.sustainPoint(2); // Make point 2 sustain until a release is issued
// We have the mesh be a sphere
addDisc(mMesh, 1.0, 30);
createInternalTriggerParameter("amplitude", 0.3, 0.0, 1.0);
createInternalTriggerParameter("frequency", 60, 20, 5000);
createInternalTriggerParameter("attackTime", 1.0, 0.01, 3.0);
createInternalTriggerParameter("releaseTime", 3.0, 0.1, 10.0);
createInternalTriggerParameter("pan", 0.0, -1.0, 1.0);
}
//
virtual void onProcess(AudioIOData& io) override {
// Parameters will update values once per audio callback
mOsc.freq(getInternalParameterValue("frequency"));
mAmpEnv.lengths()[0] = getInternalParameterValue("attackTime");
mAmpEnv.lengths()[2] = getInternalParameterValue("releaseTime");
mPan.pos(getInternalParameterValue("pan"));
while(io()){
float s1 = mOsc() * mAmpEnv() * getInternalParameterValue("amplitude");
float s2;
mEnvFollow(s1);
mPan(s1, s1,s2);
io.out(0) += s1;
io.out(1) += s2;
}
// We need to let the synth know that this voice is done
// by calling the free(). This takes the voice out of the
// rendering chain
if(mAmpEnv.done() && (mEnvFollow.value() < 0.001f)) free();
}
virtual void onProcess(Graphics &g) {
float frequency = getInternalParameterValue("frequency");
float amplitude = getInternalParameterValue("amplitude");
g.pushMatrix();
g.translate(frequency/200 - 3, amplitude, -8);
g.scale(1- amplitude, amplitude, 1);
g.color(mEnvFollow.value(), frequency/1000, mEnvFollow.value()* 10, 0.4);
g.draw(mMesh);
g.popMatrix();
}
virtual void onTriggerOn() override {
mAmpEnv.reset();
}
virtual void onTriggerOff() override {
mAmpEnv.release();
}
};
// We make an app.
class MyApp : public App
{
public:
// GUI manager for SineEnv voices
// The name provided determines the name of the directory
// where the presets and sequences are stored
SynthGUIManager<SineEnv> synthManager {"synth1"};
// This function is called right after the window is created
// It provides a grphics context to initialize ParameterGUI
// It's also a good place to put things that should
// happen once at startup.
virtual void onCreate() override {
ParameterGUI::initialize();
// Play example sequence. Comment this line to start from scratch
synthManager.synthSequencer().playSequence("synth1.synthSequence");
synthManager.synthRecorder().verbose(true);
}
// The audio callback function. Called when audio hardware requires data
virtual void onSound(AudioIOData &io) override {
synthManager.render(io); // Render audio
}
// The graphics callback function.
virtual void onDraw(Graphics &g) override {
g.clear();
synthManager.render(g);
// Draw GUI
ParameterGUI::beginDraw();
synthManager.drawSynthControlPanel();
ParameterGUI::endDraw();
}
// Whenever a key is pressed, this function is called
virtual void onKeyDown(Keyboard const& k) override {
if (ParameterGUI::usingKeyboard()) { // Ignore keys if GUI is using keyboard
return;
}
if (k.shift()) {
// If shift pressed then keyboard sets preset
int presetNumber = asciiToIndex(k.key());
synthManager.recallPreset(presetNumber);
} else {
// Otherwise trigger note for polyphonic synth
int midiNote = asciiToMIDI(k.key());
if (midiNote > 0) {
synthManager.voice()->setInternalParameterValue("frequency", ::pow(2.f, (midiNote - 69.f)/12.f) * 432.f);
synthManager.triggerOn(midiNote);
}
}
}
// Whenever a key is released this function is called
virtual void onKeyUp(Keyboard const& k) override {
int midiNote = asciiToMIDI(k.key());
if (midiNote > 0) {
synthManager.triggerOff(midiNote);
}
}
void onExit() override {
ParameterGUI::cleanup();
}
};
int main(){ // Create app instance
MyApp app;
app.navControl().active(false); // Disable navigation via keyboard, since we will be using keyboard for note triggering
// Set up audio
app.initAudio(48000., 256, 2, 0);
// Set sampling rate for Gamma objects from app's audio
gam::sampleRate(app.audioIO().framesPerSecond());
app.audioIO().print();
app.start();
return 0;
}