-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathsynthmodule86.ino
executable file
·335 lines (263 loc) · 7.56 KB
/
synthmodule86.ino
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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
#include <Arduino.h>
#include <Ticker.h>
#include <pgmspace.h>
#include <i2s.h>
#include <i2s_reg.h>
#include <NeoPixelBus.h>
#include <ESP8266WiFi.h>
#include "AudioOutputI2S.h"
#include "AnalogMultiplexer.h"
#include "NeoLibCore.h"
extern "C" {
#include "user_interface.h"
}
//DEFINE SYNTH PATCH
#include "synth/8bitmixtape.h"
SynthTest mysynth;
// ----------------------- START CONFIG -----------------------------
//#define ENABLE_OTA
//#define ENABLE_APPLEMIDI
// note: clicking sound when not conncted to wifi (strange)
//#define ENABLE_WIFI
//#define ENABLE_WIFI_AP
//#define USE_PDM
//#define ENABLE_SERIAL
#define ENABLE_NEO_PIXEL
#define MULTIPLEXED_ANALOG_INPUT A0
#define MUX_A D1
#define MUX_B D2
#define MUX_C D3
//NOTE: IF EVRYTHING STARTED TO ACT WEIRD ERASE FLASH AND SKETCH FROM IDE
// (1000000 us/44100) × 5 = 113
//#define USE_AUDIOBLOCK
#define NON_AUDIOBLOCK_RATE 113
#define AUDIOBLOCK_RATE 1450
#define AUDIOBLOCK_SIZE 65
#define POT_SAMPLE_RATE_MS 20
// How many leds in your strip?
#define NEO_NUM_LEDS 8
#define NEO_DATA_PIN D7
#define NEO_SAMPLE_RATE_MS 100
#define WIFI_SSID "RUMAH"
#define WIFI_PASSWORD "rumah4321"
#define WIFI_AP_SSID "8BITMIXTAPEWIFI"
#define WIFI_AP_PASSWORD "thereisnospoon"
// ----------------------- END CONFIG -----------------------------
#ifdef ENABLE_WIFI
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#endif
#ifdef ENABLE_OTA
#include <ArduinoOTA.h>
#endif
#ifdef ENABLE_APPLEMIDI
#include "AppleMidi.h"
#endif
#ifdef ENABLE_NEO_PIXEL
NeoPixelBus<NeoRgbFeature, NeoEsp8266BitBang400KbpsMethod> neoPixel(NEO_NUM_LEDS, NEO_DATA_PIN);
RgbColor red(128, 0, 0);
RgbColor green(0, 128, 0);
RgbColor black(0);
#endif
AudioOutputI2S *soundOut;
AnalogMultiplexerPin multiplexer;
Ticker potTimer;
const char *ssid = WIFI_AP_SSID;
const char *password = WIFI_AP_PASSWORD;
uint16_t DAC=0x8000;
int16_t sample[2];
int16_t cycle = 0;
uint16_t potc[] = {1,1,1,1,1,1,1,1};
// Non-blocking I2S write for left and right 16-bit PCM
bool ICACHE_FLASH_ATTR i2s_write_lr_nb(int16_t left, int16_t right){
int sample = right & 0xFFFF;
sample = sample << 16;
sample |= left & 0xFFFF;
return i2s_write_sample_nb(sample);
}
#ifdef USE_PDM
//PDM From Jan Ostman
uint32_t i2sACC;
uint16_t err;
void writeDAC(uint16_t DAC) {
for (uint8_t i=0;i<32;i++) {
i2sACC=i2sACC<<1;
if(DAC >= err) {
i2sACC|=1;
err += 0xFFFF-DAC;
} else {
err -= DAC;
}
}
bool flag=i2s_write_sample(i2sACC);
}
#endif
//Forward declaration
void ICACHE_RAM_ATTR onTimerISR();
void onUpdateControl();
#ifdef ENABLE_NEO_PIXEL
Ticker potNEO;
void onUpdateNEO();
#endif
//Applemidi
#ifdef ENABLE_APPLEMIDI
APPLEMIDI_CREATE_INSTANCE(WiFiUDP, AppleMIDI); // see definition in AppleMidi_Defs.h
void OnAppleMidiControlChange(byte channel, byte note, byte value);
#endif
void setup() {
spi_flash_erase_sector(0x7E);
#ifdef ENABLE_WIFI
#ifdef ENABLE_WIFI_AP
WiFi.softAP(ssid, password);
#else
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
}
#endif
#else
wifi_set_sleep_type(MODEM_SLEEP_T);
WiFi.disconnect(true);
WiFi.softAPdisconnect(true);
delay(1000);
#endif
//160MHZ clock speed
system_update_cpu_freq(160);
#ifdef ENABLE_OTA
ArduinoOTA.begin();
#endif
#ifdef ENABLE_APPLEMIDI
AppleMIDI.begin("ESP909"); // 'ESP909' will show up as the session name
AppleMIDI.OnReceiveControlChange(OnAppleMidiControlChange);
#endif
multiplexer.setup(MUX_A, MUX_B, MUX_C, MULTIPLEXED_ANALOG_INPUT);
//Soundcard settings
soundOut = new AudioOutputI2S();
soundOut->SetRate(44100);
soundOut->SetBitsPerSample(16);
soundOut->SetChannels(2);
soundOut->begin();
// i2s_begin(); //Start the i2s DMA engine
// i2s_set_rate(44100); //Set sample rate
//Soundcard timer
timer1_attachInterrupt(onTimerISR); //Attach our sampling ISR
timer1_enable(TIM_DIV16, TIM_EDGE, TIM_LOOP);
#ifdef USE_AUDIOBLOCK
timer1_write(AUDIOBLOCK_RATE); //Read potentio control at AUDIOBLOCK_RATE interval
#else
timer1_write(NON_AUDIOBLOCK_RATE); //Read potentio control at AUDIOBLOCK_RATE interval
#endif
//Control timer (update pots)
potTimer.attach_ms(POT_SAMPLE_RATE_MS, onUpdateControl); //Read potentio control at 20ms interval
#ifdef ENABLE_NEO_PIXEL
potNEO.attach_ms(NEO_SAMPLE_RATE_MS, onUpdateNEO); //Read potentio control at 20ms interval
#endif
#ifdef ENABLE_NEO_PIXEL
neoPixel.Begin();
neoPixel.Show();
#endif
#ifdef USE_PDM
pinMode(2, INPUT); //restore GPIOs taken by i2s
pinMode(15, INPUT);
#endif
#ifdef ENABLE_SERIAL
Serial.begin(115200);
#endif
}
#ifdef ENABLE_NEO_PIXEL
void onUpdateNEO() {
for(uint8_t j =0; j < 8; j++)
{
if (j < 8) neoPixel.SetPixelColor(j, black);
}
for(uint8_t i =0; i < ((( (DAC) >>12) - 4)<<1) ; i++)
{
if (i < 8) neoPixel.SetPixelColor(i, green);
}
neoPixel.Show();
}
#endif
void onUpdateControl() {
potc[0] = multiplexer.read(0,10) >> 0;
potc[1] = multiplexer.read(1,8) >> 0;
potc[2] = multiplexer.read(2,8) >> 0;
//potc[3] = multiplexer.read(3,10) >> 0;
//potc[4] = multiplexer.read(4,10) >> 0;
//potc[5] = multiplexer.read(5,10) >> 0;
//potc[6] = multiplexer.read(6,10) >> 0;
//potc[7] = multiplexer.read(7,10) >> 0;
if (potc[0] < 360){
mysynth.param[1].setValue(potc[1]);
mysynth.param[2].setValue(potc[2]);
}else{
mysynth.param[3].setValue(potc[1]);
mysynth.param[4].setValue(potc[2]);
}
//mysynth.param[3].setValue(potc[3]);
//mysynth.param[4].setValue(potc[4]);
//mysynth.param[5].setValue(potc[5]);
//mysynth.param[6].setValue(potc[6]);
//mysynth.param[7].setValue(potc[7]);
#ifdef ENABLE_SERIAL
uint8_t btn = wasButtonPressed(potc[0]);
if (btn == BUTTON_LEFT) Serial.println("LEFT");
if (btn == BUTTON_RIGHT) Serial.println("RIGHT");
if (btn == BUTTON_LEFT+BUTTON_RIGHT) Serial.println("BOTH");
// Serial.print(potc[0]);
// Serial.print(" -> ");
// Serial.println(btn);
#endif
}
#ifdef ENABLE_APPLEMIDI
void OnAppleMidiControlChange(byte channel, byte note, byte value) {
if (channel==10) {
if (note < 5) potc[note] = value;
}
}
#endif
bool toggle = false;
void ICACHE_RAM_ATTR onTimerISR() {
#ifdef USE_AUDIOBLOCK
for(uint8_t i = 0; i < AUDIOBLOCK_SIZE; i++) //Bigger Audioblock, more latency
{
if(!i2s_is_full())
{
DAC = mysynth.run(i);
#ifdef USE_PDM
// Jan Ostman PDM..
writeDAC(DAC^0x8000);
#else
sample[0] = (DAC-0x8000); //normalize
sample[1] = sample[0];
soundOut->ConsumeSample(sample); //more overhead
//i2s_write_lr_nb( DAC^0x8000, DAC^0x8000); //nicer
#endif
}
}
timer1_write(AUDIOBLOCK_RATE); // Render next block in... ms..
#else
//We are using realtime clock 44100 Hz
if(!i2s_is_full())
{
DAC = mysynth.run(cycle++);
#ifdef USE_PDM
// Jan Ostman PDM..
writeDAC(DAC^0x8000);
#else
sample[0] = (DAC-0x8000); //normalize
sample[1] = sample[0];
soundOut->ConsumeSample(sample); //more overhead
//i2s_write_lr_nb( DAC^0x8000, DAC^0x8000); //nicer
#endif
}
#endif
//i2s_write_lr_nb((((((DAC)<<8) ^ 0x8000))),0); //for one liner
}
void loop() {
#ifdef ENABLE_OTA
ArduinoOTA.handle();
#endif
#ifdef ENABLE_APPLEMIDI
AppleMIDI.run();
#endif
}