[{"data":1,"prerenderedAt":259},["ShallowReactive",2],{"example-page-synth":3},{"id":4,"info":5,"parts":6,"wired":25,"refs":26,"nets":27,"moreNets":252,"code":253},"synth","An ESP32 computes the sound sample by sample, 12 000 times a second: six voices, each a sawtooth, square, triangle or sine wave with an attack-decay-sustain-release envelope, summed into a resonant low-pass filter. FabGL's SoundGenerator hands the samples by DMA through I2S0 to the ESP32's 8-bit DAC on GPIO25; from there a volume potentiometer, a PAM8403 class-D amplifier board (24 dB, bridge output) and a 57 mm speaker. Turn on the sound (speaker button) and play with the keys A W S E D F T G Y H U J K (C4 to C5, like a piano: the black keys on the upper row). The five potentiometers shape the sound while you play: waveform, cutoff, resonance, attack, release; the OLED shows the settings, the notes held and the output wave. Put the oscilloscope on DAC and on the speaker to see the samples and what the amplifier makes of them. The sketch (synth) is in the code view: change the sound engine and compile.",[7,10,13,16,19,21,23],[8,9],"button",13,[11,12],"pot",6,[14,15],"resistor",2,[17,18],"esp32_devkitc",1,[20,18],"pam8403",[22,18],"speaker",[24,18],"ssd1306",false,{"A#4":8,"A1":17,"A2":20,"A4":8,"B4":8,"C#4":8,"C4":8,"C5":8,"D#4":8,"D4":8,"DS1":24,"E4":8,"F#4":8,"F4":8,"G#4":8,"G4":8,"RV1":11,"RV2":11,"RV3":11,"RV4":11,"RV5":11,"RV6":11,"R1":14,"R2":14,"LS1":22},[28,78,93,100,109,115,121,127,133,139,145,151,157,163,169,175,181,187,193,199,205,211,217,223,230,237,245],{"name":29,"pins":30},"0",[31,34,37,39,41,43,45,47,49,51,53,55,57,59,61,63,65,68,70,72,74,76],[32,33],"A#4","2",[35,36],"A1","GND",[38,36],"A2",[40,33],"A4",[42,33],"B4",[44,33],"C#4",[46,33],"C4",[48,33],"C5",[50,33],"D#4",[52,33],"D4",[54,36],"DS1",[56,33],"E4",[58,33],"F#4",[60,33],"F4",[62,33],"G#4",[64,33],"G4",[66,67],"RV1","1",[69,67],"RV2",[71,67],"RV3",[73,67],"RV4",[75,67],"RV5",[77,67],"RV6",{"name":79,"pins":80},"3V3",[81,82,84,86,88,89,90,91,92],[35,79],[54,83],"VCC",[85,67],"R1",[87,67],"R2",[66,33],[69,33],[71,33],[73,33],[75,33],{"name":94,"pins":95},"V5",[96,98],[35,97],"5V",[38,99],"+5V",{"name":101,"pins":102},"VOL",[103,105,107],[38,104],"L in",[38,106],"R in",[77,108],"W",{"name":110,"pins":111},"DAC",[112,114],[35,113],"GPIO25",[77,33],{"name":116,"pins":117},"KEY_A4",[118,120],[35,119],"GPIO23",[40,67],{"name":122,"pins":123},"KEY_AS4",[124,125],[32,67],[35,126],"GPIO26",{"name":128,"pins":129},"KEY_B4",[130,132],[35,131],"GPIO27",[42,67],{"name":134,"pins":135},"KEY_C4",[136,138],[35,137],"GPIO4",[46,67],{"name":140,"pins":141},"KEY_C5",[142,144],[35,143],"GPIO33",[48,67],{"name":146,"pins":147},"KEY_CS4",[148,150],[35,149],"GPIO5",[44,67],{"name":152,"pins":153},"KEY_D4",[154,156],[35,155],"GPIO13",[52,67],{"name":158,"pins":159},"KEY_DS4",[160,162],[35,161],"GPIO14",[50,67],{"name":164,"pins":165},"KEY_E4",[166,168],[35,167],"GPIO15",[56,67],{"name":170,"pins":171},"KEY_F4",[172,174],[35,173],"GPIO16",[60,67],{"name":176,"pins":177},"KEY_FS4",[178,180],[35,179],"GPIO17",[58,67],{"name":182,"pins":183},"KEY_G4",[184,186],[35,185],"GPIO18",[64,67],{"name":188,"pins":189},"KEY_GS4",[190,192],[35,191],"GPIO19",[62,67],{"name":194,"pins":195},"POT_ATT",[196,198],[35,197],"GPIO36\u002FVP",[73,108],{"name":200,"pins":201},"POT_CUT",[202,204],[35,203],"GPIO34",[69,108],{"name":206,"pins":207},"POT_REL",[208,210],[35,209],"GPIO39\u002FVN",[75,108],{"name":212,"pins":213},"POT_RES",[214,216],[35,215],"GPIO35",[71,108],{"name":218,"pins":219},"POT_WAVE",[220,222],[35,221],"GPIO32",[66,108],{"name":224,"pins":225},"SCL",[226,228,229],[35,227],"GPIO22",[54,224],[85,33],{"name":231,"pins":232},"SDA",[233,235,236],[35,234],"GPIO21",[54,231],[87,33],{"name":238,"pins":239},"SPK_N",[240,242],[38,241],"L−",[243,244],"LS1","−",{"name":246,"pins":247},"SPK_P",[248,250],[38,249],"L+",[243,251],"+",0,{"file":254,"lang":255,"text":256,"lines":257,"more":258},"sketch.ino","cpp","\u002F\u002F ESP32 synthesizer. 13 keys from C4 to C5 (buttons to GND on GPIO 4, 5, 13, 14, 15, 16, 17, 18,\n\u002F\u002F 19, 23, 26, 27, 33), six voices, four waveforms, an ADSR envelope and a resonant low-pass filter,\n\u002F\u002F computed sample by sample (12000 per second) inside FabGL's SoundGenerator. FabGL sends the samples\n\u002F\u002F by DMA through I2S0 to the 8-bit DAC on GPIO25: from there a volume potentiometer, a PAM8403\n\u002F\u002F amplifier and a speaker. Five potentiometers set the sound: waveform (GPIO32), cutoff (34),\n\u002F\u002F resonance (35), attack (36) and release (39). The OLED (I2C GPIO21 \u002F 22) shows the settings,\n\u002F\u002F the notes held and the output wave.\n#include \"fabgl.h\"\n#include \u003CWire.h>\n#include \u003CAdafruit_GFX.h>\n#include \u003CAdafruit_SSD1306.h>\n#include \"driver\u002Fadc.h\"   \u002F\u002F the legacy ADC API: FabGL's sound uses the legacy DAC driver, and ESP-IDF\n                           \u002F\u002F stops at boot when the legacy and the new driver (analogRead) meet\n\nconst int KEY_PINS[13] = { 4, 5, 13, 14, 15, 16, 17, 18, 19, 23, 26, 27, 33 };\nconst char *NOTE[13] = { \"C\", \"C#\", \"D\", \"D#\", \"E\", \"F\", \"F#\", \"G\", \"G#\", \"A\", \"A#\", \"B\", \"C\" };\nconst char *WAVE_NAME[4] = { \"Saw\", \"Square\", \"Triangle\", \"Sine\" };\n#define POT_WAVE    ADC1_CHANNEL_4   \u002F\u002F GPIO32\n#define POT_CUTOFF  ADC1_CHANNEL_6   \u002F\u002F GPIO34\n#define POT_RESO    ADC1_CHANNEL_7   \u002F\u002F GPIO35\n#define POT_ATTACK  ADC1_CHANNEL_0   \u002F\u002F GPIO36\n#define POT_RELEASE ADC1_CHANNEL_3   \u002F\u002F GPIO39\n\n#define RATE   12000      \u002F\u002F samples per second: the filter's cutoff stays below 2.4 kHz\n#define VOICES 6\n\nstruct Voice {\n  uint32_t phase, inc;\n  int32_t env;              \u002F\u002F envelope 0 .. 32767\n  uint8_t stage;            \u002F\u002F 0 off, 1 attack, 2 decay, 3 sustain, 4 release\n  int8_t key;\n};\n\nint16_t SINE[256];\n\n\u002F\u002F the sound engine: one FabGL waveform generator that mixes all voices and filters the sum\nclass SynthEngine : public WaveformGenerator {\npublic:\n  Voice v[VOICES];\n  volatile int wave = 0;\n  volatile int32_t att = 300, dec = 4, sus = 22000, rel = 10;   \u002F\u002F envelope steps per sample\n  volatile int32_t f = 8000, q = 30000;                       \u002F\u002F filter coefficients (Q15)\n  int32_t low = 0, band = 0;\n  int8_t scope[128];\n  volatile uint8_t scopePos = 0;\n  uint8_t scopeDiv = 0;\n\n  void setFrequency(int) override { }\n\n  int getSample() override {\n    int32_t mix = 0;\n    for (int i = 0; i \u003C VOICES; i++) {\n      Voice &x = v[i];\n      if (!x.stage) continue;\n      switch (x.stage) {\n        case 1: x.env += att; if (x.env >= 32767) { x.env = 32767; x.stage = 2; } break;\n        case 2: x.env -= dec; if (x.env \u003C= sus) { x.env = sus; x.stage = 3; } break;\n        case 4: x.env -= rel; if (x.env \u003C= 0) { x.env = 0; x.stage = 0; continue; } break;\n      }\n      x.phase += x.inc;\n      int32_t p = x.phase >> 20, s;                              \u002F\u002F phase 0 .. 4095\n      switch (wave) {\n        case 0:  s = p - 2048; break;                            \u002F\u002F saw\n        case 1:  s = p \u003C 2048 ? 2047 : -2048; break;             \u002F\u002F square\n        case 2:  s = p \u003C 2048 ? p * 2 - 2048 : 6143 - p * 2; break;   \u002F\u002F triangle\n        default: s = SINE[p >> 4]; break;                        \u002F\u002F sine\n      }\n      mix += (s * x.env) >> 15;\n    }\n    \u002F\u002F state-variable low-pass filter (Chamberlin)\n    int32_t in = mix >> 1;\n    low += (f * band) >> 15;\n    int32_t high = in - low - ((q * band) >> 15);\n    band += (f * high) >> 15;\n    if (low > 30000) low = 30000; else if (low \u003C -30000) low = -30000;\n    if (band > 30000) band = 30000; else if (band \u003C -30000) band = -30000;\n    int32_t out = (low * 13) >> 9;\n    if (out > 127) out = 127; else if (out \u003C -127) out = -127;\n    if ((scopeDiv++ & 3) == 0) scope[scopePos++ & 127] = out;\n    return out;\n  }\n\n  void noteOn(int key) {\n    float hz = 261.63f * powf(2.0f, key \u002F 12.0f);\n    int best = 0;\n    for (int i = 0; i \u003C VOICES; i++) {                           \u002F\u002F a free voice, else the quietest\n      if (!v[i].stage) { best = i; break; }\n      if (v[i].env \u003C v[best].env) best = i;\n    }\n    Voice &x = v[best];\n    x.inc = (uint32_t)(hz * 4294967296.0f \u002F RATE);\n    x.key = key;\n    x.stage = 1;\n  }\n  void noteOff(int key) {\n    for (int i = 0; i \u003C VOICES; i++) if (v[i].stage && v[i].stage != 4 && v[i].key == key) v[i].stage = 4;\n  }\n};\n\nfabgl::SoundGenerator sound(RATE);\nSynthEngine synth;\nAdafruit_SSD1306 oled(128, 64, &Wire, -1);\nbool held[13];\nfloat cutoffHz, resoQ, attackMs, releaseMs;\n\n\u002F\u002F a potentiometer as 0 .. 1\nfloat pot(adc1_channel_t ch) { return adc1_get_raw(ch) \u002F 4095.0f; }\n\nvoid readControls() {\n  synth.wave = min(3, (int)(pot(POT_WAVE) * 4));\n  cutoffHz = 80.0f * powf(30.0f, pot(POT_CUTOFF));                \u002F\u002F 80 Hz .. 2.4 kHz\n  synth.f = (int32_t)(2.0f * sinf(3.14159f * cutoffHz \u002F RATE) * 32768);\n  resoQ = 0.7f + pot(POT_RESO) * 7.3f;                             \u002F\u002F Q 0.7 .. 8\n  synth.q = (int32_t)(32768 \u002F resoQ);\n  attackMs = 2.0f * powf(500.0f, pot(POT_ATTACK));                 \u002F\u002F 2 ms .. 1 s\n  synth.att = max(1, (int)(32767 \u002F (attackMs * RATE \u002F 1000)));\n  releaseMs = 10.0f * powf(200.0f, pot(POT_RELEASE));              \u002F\u002F 10 ms .. 2 s\n  synth.rel = max(1, (int)(32767 \u002F (releaseMs * RATE \u002F 1000)));\n}\n",183,true,1796304400784]