forked from marcel-licence/esp32_fm_synth
-
Notifications
You must be signed in to change notification settings - Fork 0
/
audio_module.ino
478 lines (403 loc) · 12 KB
/
audio_module.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
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
/*
* Copyright (c) 2022 Marcel Licence
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* Dieses Programm ist Freie Software: Sie können es unter den Bedingungen
* der GNU General Public License, wie von der Free Software Foundation,
* Version 3 der Lizenz oder (nach Ihrer Wahl) jeder neueren
* veröffentlichten Version, weiter verteilen und/oder modifizieren.
*
* Dieses Programm wird in der Hoffnung bereitgestellt, dass es nützlich sein wird, jedoch
* OHNE JEDE GEWÄHR,; sogar ohne die implizite
* Gewähr der MARKTFÄHIGKEIT oder EIGNUNG FÜR EINEN BESTIMMTEN ZWECK.
* Siehe die GNU General Public License für weitere Einzelheiten.
*
* Sie sollten eine Kopie der GNU General Public License zusammen mit diesem
* Programm erhalten haben. Wenn nicht, siehe <https://www.gnu.org/licenses/>.
*/
/**
* @file audio_module.ino
* @author Marcel Licence
* @date 16.12.2021
*
* @brief this file provides a general audio interface to make it easier working with different platforms
*/
#ifdef __CDT_PARSER__
#include <cdt.h>
#endif
#ifdef ESP8266
#include <ESP8266WiFi.h>
#include <I2S.h>
#include <i2s_reg.h>
#endif
#ifdef ESP32
#include <WiFi.h>
#endif
#ifdef TEENSYDUINO
#include <Audio.h>
#include <Wire.h>
#include <SPI.h>
#include <SD.h>
#include <SerialFlash.h>
#endif
#ifdef ARDUINO_DAISY_SEED
#include "DaisyDuino.h"
#endif
#if (defined ARDUINO_RASPBERRY_PI_PICO) || (defined ARDUINO_GENERIC_RP2040)
#include <I2S.h>
#endif
void Audio_Setup(void)
{
#if (defined ESP8266) || (defined ESP32)
WiFi.mode(WIFI_OFF);
btStop();
#endif
#if 0 //ndef ESP8266
btStop();
esp_wifi_deinit();
#endif
#ifdef ESP32_AUDIO_KIT
#ifdef ES8388_ENABLED
ES8388_Setup();
ES8388_SetIn2OoutVOL(0, 0);
#else
ac101_setup();
#endif
#endif
#ifdef ESP32
setup_i2s();
#endif
#ifdef ESP8266
#if 0
system_update_cpu_freq(160);
i2s_begin();
i2s_set_rate(SAMPLE_RATE);
#else
I2S_init();
#endif
pinMode(2, INPUT); //restore GPIOs taken by i2s
pinMode(15, INPUT);
#endif
#ifdef TEENSYDUINO
AudioMemory(4);
#endif
#ifdef ARDUINO_SEEED_XIAO_M0
SAMD21_Synth_Init();
pinMode(DAC0, OUTPUT);
#endif
#if (defined ARDUINO_RASPBERRY_PI_PICO) || (defined ARDUINO_GENERIC_RP2040)
if (!I2S.begin(SAMPLE_RATE))
{
Serial.println("Failed to initialize I2S!");
while (1); // do nothing
}
#endif
#if (defined ARDUINO_GENERIC_F407VGTX) || (defined ARDUINO_DISCO_F407VG)
STM32_AudioInit();
#endif /* (defined ARDUINO_GENERIC_F407VGTX) || (defined ARDUINO_DISCO_F407VG) */
}
#ifdef TEENSYDUINO
const int ledPin = LED_PIN; /* pin configured in config.h */
#if 1
AudioPlayQueue queue1;
AudioPlayQueue queue2;
AudioOutputI2S i2s1;
AudioConnection patchCord1(queue1, 0, i2s1, 0); /* left channel */
AudioConnection patchCord2(queue2, 0, i2s1, 1); /* right channel */
#else
// GUItool: begin automatically generated code
AudioInputUSB usb1; //xy=134,545
AudioPlayQueue queue1; //xy=258,380
AudioPlayQueue queue2; //xy=261,423
AudioRecordQueue queue6; //xy=265,557
AudioRecordQueue queue5; //xy=274,516
AudioPlayQueue queue4; //xy=352,622
AudioPlayQueue queue3; //xy=380,530
AudioOutputI2S i2s1; //xy=470,393
AudioOutputUSB usb2; //xy=494,544
AudioConnection patchCord1(usb1, 0, queue5, 0);
AudioConnection patchCord2(usb1, 1, queue6, 0);
AudioConnection patchCord3(queue1, 0, i2s1, 0);
AudioConnection patchCord4(queue2, 0, i2s1, 1);
AudioConnection patchCord5(queue3, 0, usb2, 0);
AudioConnection patchCord6(queue4, 0, usb2, 1);
// GUItool: end automatically generated code
#endif
static int16_t sampleBuffer[AUDIO_BLOCK_SAMPLES];
static int16_t sampleBuffer2[AUDIO_BLOCK_SAMPLES];
static int16_t *queueTransmitBuffer;
static int16_t *queueTransmitBuffer2;
void Teensy_Setup()
{
pinMode(ledPin, OUTPUT);
Midi_Setup();
}
#endif /* TEENSYDUINO */
#ifdef ARDUINO_DAISY_SEED
static DaisyHardware hw;
static size_t num_channels;
volatile static bool dataReady = false;
static float out_temp[2][48];
static float *outCh[2] = {out_temp[0], out_temp[1]};
void MyCallback(float **in, float **out, size_t size)
{
for (size_t i = 0; i < size; i++)
{
out[0][i] = out_temp[0][i];
out[1][i] = out_temp[1][i];
out_temp[0][i] = in[0][i];
out_temp[1][i] = in[1][i];
}
dataReady = true;
}
void DaisySeed_Setup(void)
{
float sample_rate;
// Initialize for Daisy pod at 48kHz
hw = DAISY.init(DAISY_SEED, AUDIO_SR_48K);
num_channels = hw.num_channels;
sample_rate = DAISY.get_samplerate();
DAISY.begin(MyCallback);
}
#endif /* ARDUINO_DAISY_SEED */
#ifdef ARDUINO_SEEED_XIAO_M0
static int32_t u32buf[SAMPLE_BUFFER_SIZE];
inline
void ProcessAudio(uint16_t *buff, size_t len)
{
/* convert from u16 to u10 */
for (size_t i = 0; i < len; i++)
{
const int32_t preDiv = 4194304; // 2 ^ (16 + 6)
buff[i] = (uint16_t)(0x200 + (u32buf[i] / (preDiv)));
}
}
#endif
void Audio_OutputMono(const int32_t *samples)
{
#ifdef ESP8266
for (int i = 0; i < SAMPLE_BUFFER_SIZE; i++)
{
int32_t sig = samples[i];
static uint16_t sig16 = 0;
sig *= 4;
sig16 = sig;
while (!I2S_isNotFull())
{
/* wait for buffer is not full */
}
writeDAC(0x8000 + sig16);
}
#endif
#ifdef ESP32
float mono[SAMPLE_BUFFER_SIZE];
for (int i = 0; i < SAMPLE_BUFFER_SIZE; i++)
{
float sigf = samples[i];
sigf /= INT16_MAX;
mono[i] = sigf;
}
i2s_write_stereo_samples_buff(mono, mono, SAMPLE_BUFFER_SIZE);
#endif /* ESP32 */
#ifdef TEENSYDUINO
{
#ifdef CYCLE_MODULE_ENABLED
calcCycleCountPre();
#endif
queueTransmitBuffer = queue1.getBuffer(); /* blocking? */
queueTransmitBuffer2 = queue2.getBuffer();
#ifdef CYCLE_MODULE_ENABLED
calcCycleCount();
#endif
if (queueTransmitBuffer)
{
{
for (size_t i = 0; i < AUDIO_BLOCK_SAMPLES; i++)
{
sampleBuffer[i] = (int16_t)((samples[i]));
}
memcpy(queueTransmitBuffer, sampleBuffer, AUDIO_BLOCK_SAMPLES * 2);
memcpy(queueTransmitBuffer2, sampleBuffer, AUDIO_BLOCK_SAMPLES * 2);
}
queue1.playBuffer();
queue2.playBuffer();
}
}
#endif /* TEENSYDUINO */
#ifdef ARDUINO_DAISY_SEED
float sig_f[SAMPLE_BUFFER_SIZE];
#ifdef CYCLE_MODULE_ENABLED
calcCycleCountPre();
#endif
while (!dataReady)
{
/* just do nothing */
}
#ifdef CYCLE_MODULE_ENABLED
calcCycleCount();
#endif
for (size_t i = 0; i < SAMPLE_BUFFER_SIZE; i++)
{
sig_f[i] = ((float)samples[i]) * (1.0f / ((float)INT16_MAX));
}
memcpy(out_temp[0], sig_f, sizeof(out_temp[0]));
memcpy(out_temp[1], sig_f, sizeof(out_temp[1]));
dataReady = false;
#endif /* ARDUINO_DAISY_SEED */
#ifdef ARDUINO_SEEED_XIAO_M0
#ifdef CYCLE_MODULE_ENABLED
calcCycleCountPre();
#endif
while (!SAMD21_Synth_Process(ProcessAudio))
{
/* just do nothing */
}
#ifdef CYCLE_MODULE_ENABLED
calcCycleCount();
#endif
memcpy(u32buf, samples, sizeof(int32_t)*SAMPLE_BUFFER_SIZE);
#endif /* ARDUINO_SEEED_XIAO_M0 */
#if (defined ARDUINO_RASPBERRY_PI_PICO) || (defined ARDUINO_GENERIC_RP2040)
/*
* @see https://arduino-pico.readthedocs.io/en/latest/i2s.html
* @see https://www.waveshare.com/pico-audio.htm for connections
*/
int16_t u16int[2 * SAMPLE_BUFFER_SIZE];
for (int i = 0; i < SAMPLE_BUFFER_SIZE; i++)
{
u16int[2 * i] = samples[i];
u16int[(2 * i) + 1] = samples[i];
}
#if 1
for (int i = 0; i < SAMPLE_BUFFER_SIZE * 2; i++)
{
I2S.write(u16int[i]);
}
#else
/* this does not work, I do not know why :-/ */
static int16_t u16int_buf[2 * SAMPLE_BUFFER_SIZE];
memcpy(u16int_buf, u16int, sizeof(u16int));
I2S.write(u16int_buf, sizeof(u16int));
#endif
#endif /* ARDUINO_RASPBERRY_PI_PICO, ARDUINO_GENERIC_RP2040 */
#ifdef ARDUINO_GENERIC_F407VGTX
/*
* Todo Implementation for the STM32F407VGT6
* Can be found on the ST Discovery Board
*/
#if 0
int16_t u16int[2 * SAMPLE_BUFFER_SIZE];
for (int i = 0; i < SAMPLE_BUFFER_SIZE; i++)
{
u16int[2 * i] = samples[i];
u16int[(2 * i) + 1] = samples[i];
}
for (int i = 0; i < SAMPLE_BUFFER_SIZE * 2; i++)
{
I2S.write(u16int[i]);
}
#endif
#endif /* ARDUINO_GENERIC_F407VGTX */
#ifdef ARDUINO_DISCO_F407VG
STM32_AudioWriteS16(samples);
#endif
}
#if (defined ESP32) || (defined TEENSYDUINO) || (defined ARDUINO_DAISY_SEED) || (defined ARDUINO_GENERIC_F407VGTX) || (defined ARDUINO_DISCO_F407VG) || (defined ARDUINO_BLACK_F407VE)
void Audio_Input(float *left, float *right)
{
#ifdef ESP32
i2s_read_stereo_samples_buff(left, right, SAMPLE_BUFFER_SIZE);
#endif /* ESP32 */
}
#ifdef OUTPUT_SAW_TEST
void Audio_Output(float *left, float *right)
#else
void Audio_Output(const float *left, const float *right)
#endif
{
#ifdef OUTPUT_SAW_TEST
/*
* base frequency: SAMPLE_FREQ / SAMPLE_BUFFER_SIZE
* for example: Fs : 44100Hz, Lsb = 48 -> Freq: 918.75 Hz
*/
for (int i = 0; i < SAMPLE_BUFFER_SIZE; i++)
{
left[i] = ((float)i * 2.0f) / ((float)SAMPLE_BUFFER_SIZE);
right[i] = ((float)i * 2.0f) / ((float)SAMPLE_BUFFER_SIZE);
left[i] -= 1.0f;
right[i] -= 1.0f;
//left[i] *= 0.125f;
//right[i] *= 0.125f;
}
#endif
#ifdef ESP32
i2s_write_stereo_samples_buff(left, right, SAMPLE_BUFFER_SIZE);
#endif /* ESP32 */
#ifdef TEENSYDUINO
{
#ifdef CYCLE_MODULE_ENABLED
calcCycleCountPre();
#endif
queueTransmitBuffer = queue1.getBuffer(); /* blocking? */
queueTransmitBuffer2 = queue2.getBuffer();
#ifdef CYCLE_MODULE_ENABLED
calcCycleCount();
#endif
if (queueTransmitBuffer)
{
{
for (size_t i = 0; i < AUDIO_BLOCK_SAMPLES; i++)
{
sampleBuffer[i] = (int16_t)(left[i] * INT16_MAX);
sampleBuffer2[i] = (int16_t)(right[i] * INT16_MAX);
}
memcpy(queueTransmitBuffer, sampleBuffer, AUDIO_BLOCK_SAMPLES * 2);
memcpy(queueTransmitBuffer2, sampleBuffer2, AUDIO_BLOCK_SAMPLES * 2);
}
queue1.playBuffer();
queue2.playBuffer();
}
}
#endif /* TEENSYDUINO */
#ifdef ARDUINO_DAISY_SEED
#ifdef CYCLE_MODULE_ENABLED
calcCycleCountPre();
#endif
while (!dataReady)
{
/* just do nothing */
}
#ifdef CYCLE_MODULE_ENABLED
calcCycleCount();
#endif
#if 0
for (int i = 0; i < SAMPLE_BUFFER_SIZE; i++)
{
left[i] = i * (1.0f / ((float)SAMPLE_BUFFER_SIZE));
right[i] = i * (1.0f / ((float)SAMPLE_BUFFER_SIZE));
}
#endif
memcpy(out_temp[0], left, sizeof(out_temp[0]));
memcpy(out_temp[1], right, sizeof(out_temp[1]));
dataReady = false;
#endif /* ARDUINO_DAISY_SEED */
#ifdef ARDUINO_GENERIC_F407VGTX
STM32_AudioWrite(left, right);
#endif /* ARDUINO_GENERIC_F407VGTX */
#ifdef ARDUINO_DISCO_F407VG
STM32_AudioWrite(left, right);
#endif /* ARDUINO_GENERIC_F407VGTX */
}
#endif /* (defined ESP32) || (defined TEENSYDUINO) || (defined ARDUINO_DAISY_SEED) || (defined ARDUINO_GENERIC_F407VGTX) || (defined ARDUINO_DISCO_F407VG) */