-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAS-1_FMsynth.ino
More file actions
181 lines (158 loc) · 5.9 KB
/
AS-1_FMsynth.ino
File metadata and controls
181 lines (158 loc) · 5.9 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
/*
_____/\\\\\\\\\________/\\\\\\\\\\\______________________/\\\_
___/\\\\\\\\\\\\\____/\\\/////////\\\________________/\\\\\\\_
__/\\\/////////\\\__\//\\\______\///________________\/////\\\_
_\/\\\_______\/\\\___\////\\\__________/\\\\\\\\\\\_____\/\\\_
_\/\\\\\\\\\\\\\\\______\////\\\______\///////////______\/\\\_
_\/\\\/////////\\\_________\////\\\_____________________\/\\\_
_\/\\\_______\/\\\__/\\\______\//\\\____________________\/\\\_
_\/\\\_______\/\\\_\///\\\\\\\\\\\/_____________________\/\\\_
_\///________\///____\///////////_______________________\///_
5 Knob FM Synth
POT 1 = OSCILLATOR FREQUENCY
POT 2 = MODULATOR FREQUENCY
POT 3 = FREQUENCY SHIFT
POT 4 = MODULATION INTENSITY 1x
POT 5 = MODULATION INTENSITY 10x
______________________________
/ ____ ____ \
| / \ / \ |
| ( POT1 ) ( POT4 ) |
| \____/ \____/ |
| ____ |
| / \ |
| ( POT3 ) |
| \____/ |
| ____ ____ |
| / \ / \ |
| ( POT2 ) ( POT5 ) |
| \____/ \____/ |
| |
\ _______________________________/
Based on Mozzi example
Knob_LightLevel_x2_FMsynth
Tim Barrass 2013, CC by-nc-sa
http://sensorium.github.com/Mozzi/
Andrew Buckie 2018, CC by-nc-sa
*/
#include <MozziGuts.h>
#include <Oscil.h> // oscillator
#include <tables/cos2048_int8.h> // table for Oscils to play
#include <Smooth.h>
#include <AutoMap.h> // maps unpredictable inputs to a range
#include <mozzi_fixmath.h>
#include "SynthBoxConfig.h"
// Declare mapping variables
int CarrierFreq;
const int MinCarrierFreq = 22;
const int MaxCarrierFreq = 440;
float ModSpeed;
const int MinModSpeed = 1;
const int MaxModSpeed = 10000;
int FreqShift;
const int MinFreqShift = 1;
const int MaxFreqShift = 10;
int Intensity;
const int MinIntensity = 10;
const int MaxIntensity = 500;
int IntensityMultiplier;
const int MinIntensityMultiplier = 1;
const int MaxIntensityMultiplier = 10;
Oscil<COS2048_NUM_CELLS, AUDIO_RATE> aCarrier(COS2048_DATA);
Oscil<COS2048_NUM_CELLS, AUDIO_RATE> aModulator(COS2048_DATA);
Oscil<COS2048_NUM_CELLS, CONTROL_RATE> kIntensityMod(COS2048_DATA);
int mod_ratio = 5; // brightness (harmonics)
long fm_intensity; // carries control info from updateControl to updateAudio
// smoothing for intensity to remove clicks on transitions
float smoothness = 0.95f;
Smooth <long> aSmoothIntensity(smoothness);
void setup(){
startMozzi();
Serial.begin(115200); // For Debugging of values
pinMode(DIP_1, INPUT_PULLUP); // Configure D2 as input with pull-up resistor
pinMode(DIP_2, INPUT_PULLUP); // Configure D3 as input with pull-up resistor
pinMode(DIP_3, INPUT_PULLUP); // Configure D4 as input with pull-up resistor
pinMode(DIP_4, INPUT_PULLUP); // Configure D5 as input with pull-up resistor
pinMode(DIP_5, INPUT_PULLUP); // Configure D6 as input with pull-up resistor
pinMode(ExpPlug, INPUT_PULLUP); // Configure D7 as input with pull-up resistor
}
void updateControl(){
// Read digital inputs
ExpPot1 = digitalRead(DIP_1); // DIP switch 1 - 1 = Off / 0 = On
ExpPot2 = digitalRead(DIP_2); // DIP switch 2 - 1 = Off / 0 = On
ExpPot3 = digitalRead(DIP_3); // DIP switch 3 - 1 = Off / 0 = On
ExpPot4 = digitalRead(DIP_4); // DIP switch 4 - 1 = Off / 0 = On
ExpPot5 = digitalRead(DIP_5); // DIP switch 5 - 1 = Off / 0 = On
ExpDetct = digitalRead(ExpPlug); // 1 = Expression pedal plugged in / 0 = Expression pedal
// Map to expression pedal if selected
if (ExpPot1 == 0 & ExpDetct ==1){
Pot1 = Exp;
}
else{
Pot1 = Pot1Default;
}
if (ExpPot2 == 0 & ExpDetct ==1){
Pot2 = Exp;
}
else{
Pot2 = Pot2Default;
}
if (ExpPot3 == 0 & ExpDetct ==1){
Pot3 = Exp;
}
else{
Pot3 = Pot3Default;
}
if (ExpPot4 == 0 & ExpDetct ==1){
Pot4 = Exp;
}
else{
Pot4 = Pot4Default;
}
if (ExpPot5 == 0 & ExpDetct ==1){
Pot5 = Exp;
}
else{
Pot5 = Pot5Default;
}
// Read all the analog inputs
Pot1Val = mozziAnalogRead(Pot1); // value is 0-1023
Pot2Val = mozziAnalogRead(Pot2); // value is 0-1023
Pot3Val = mozziAnalogRead(Pot3); // value is 0-1023
Pot4Val = mozziAnalogRead(Pot4); // value is 0-1023
Pot5Val = mozziAnalogRead(Pot5); // value is 0-1023
ExpVal = mozziAnalogRead(Exp); // value is 0-1023
// Map Values
CarrierFreq = map(Pot1Val, 0, 1023, MinCarrierFreq, MaxCarrierFreq);
ModSpeed = (float)map(Pot2Val, 0, 1023, MinModSpeed, MaxModSpeed)/1000; // Use a float for the LFO sub 1hz frequencies
FreqShift = map(Pot3Val, 0, 1023, MinFreqShift, MaxFreqShift);
Intensity = map(Pot4Val, 0, 1023, MinIntensity, MaxIntensity);
IntensityMultiplier = map(Pot5Val, 0, 1023, MinIntensityMultiplier, MaxIntensityMultiplier);
// Print Debug values
Serial.print("CarrierFreq = ");
Serial.print(CarrierFreq);
Serial.print(" ModSpeed = ");
Serial.print(ModSpeed);
Serial.print(" FreqShift = ");
Serial.print(FreqShift);
Serial.print(" Intensity = ");
Serial.print(Intensity);
Serial.print(" IntensityMultiplier = ");
Serial.print(IntensityMultiplier);
Serial.println(); // Carriage return
//calculate the modulation frequency to stay in ratio
int mod_freq = CarrierFreq * mod_ratio * FreqShift;
// set the FM oscillator frequencies
aCarrier.setFreq(CarrierFreq);
aModulator.setFreq(mod_freq);
// calculate the fm_intensity
fm_intensity = ((long)Intensity * IntensityMultiplier * (kIntensityMod.next()+128))>>8; // shift back to range after 8 bit multiply
kIntensityMod.setFreq(ModSpeed);
}
int updateAudio(){
long modulation = aSmoothIntensity.next(fm_intensity) * aModulator.next();
return aCarrier.phMod(modulation);
}
void loop(){
audioHook();
}