From 06f96cd1146dc4a471a70c925d4c42deb0082a8f Mon Sep 17 00:00:00 2001 From: Miller Puckette Date: Wed, 9 Oct 2024 09:48:55 -0500 Subject: [PATCH] updates, sort of working for non-lyrat esp32s --- .gitignore | 1 + main/espd.c | 101 ++++++++++++++++++++++++---- main/espd.h | 13 +++- main/locale.h | 5 -- main/testpatch.c | 138 ++++++++++++++++++++++++++++++++++++--- test-patch/esp-patch.pd | 42 +++++------- test-patch/host-patch.pd | 67 ++++++++++--------- 7 files changed, 282 insertions(+), 85 deletions(-) delete mode 100644 main/locale.h diff --git a/.gitignore b/.gitignore index a7d6859..61c8b7f 100644 --- a/.gitignore +++ b/.gitignore @@ -1,3 +1,4 @@ build dependencies.lock managed_components +main/locale.h diff --git a/main/espd.c b/main/espd.c index 3a6afd2..b54404e 100644 --- a/main/espd.c +++ b/main/espd.c @@ -15,7 +15,13 @@ #ifdef PD_LYRAT #include "board.h" #endif /* PD_LYRAT */ +#ifdef OBSOLETEAPI #include "driver/i2s.h" +#else /* OBSOLETEAPI */ +#include "driver/i2s_std.h" +#include "driver/gpio.h" +#endif /* OBSOLETEAPI */ + #include "esp_log.h" #include "nvs.h" #include "nvs_flash.h" @@ -32,18 +38,23 @@ void pdmain_init( void); void sd_init( void); -/* #define USEADC */ +static i2s_chan_handle_t tx_handle; +#ifdef USEADC +static i2s_chan_handle_t rx_handle; +#endif + #define BLKSIZE 64 float soundin[IOCHANS * BLKSIZE], soundout[IOCHANS * BLKSIZE]; +static int espd_printdacs; void senddacs( void) { - int i, ret; + int i, j, ret; static int count; size_t transferred; short poodle[IOCHANS * BLKSIZE]; - for (i = 0; i < BLKSIZE; i += IOCHANS) + for (i = j = 0; i < BLKSIZE; i++, j += IOCHANS) { int ch1 = floor(0.5 + 32768.*soundout[i]); #if IOCHANS > 1 @@ -61,42 +72,57 @@ void senddacs( void) ch2 = -32768; ch2 &= 0xffff; #endif - poodle[i] = ch1; + poodle[j] = ch1; soundout[i] = 0; #if IOCHANS > 1 - poodle[BLKSIZE+i] = ch2; + poodle[j+1] = ch2; soundout[i+BLKSIZE] = 0; #endif } - if (count++ > 2000) + if (espd_printdacs && count++ > 250) { ESP_LOGI(TAG, "sample %lx", poodle[0]); count = 0; } +#ifdef OBSOLETEAPI ret = i2s_write(I2S_NUM_0, poodle, sizeof(poodle), &transferred, portMAX_DELAY); if (ret != ESP_OK) ESP_LOGE(TAG, "error writing"); +#else + i2s_channel_write(tx_handle, poodle, sizeof(poodle), &transferred, + portMAX_DELAY); +#endif + #ifdef USEADC +#ifdef OBSOLETEAPI ret = i2s_read(I2S_NUM_0, poodle, sizeof(poodle), &transferred, portMAX_DELAY); if (ret != ESP_OK) ESP_LOGE(TAG, "error reading"); +#else + i2s_channel_read(rx_handle, poodle, sizeof(poodle), &transferred, + portMAX_DELAY); +#endif - for (i = 0; i < BLKSIZE; i++) + for (i = j = 0; i < BLKSIZE; i++, j += IOCHANS) { - uint32_t ch1 = poodle[i] & 0xffff, ch2 = (poodle[i]>>16) & 0xffff ; - if (ch1 & 0x8000) - soundin[i] = (ch1*(1./32768.)) - 2; - else soundin[i] = (ch1*(1./32768.)); + uint32_t ch1 = poodle[j] & 0xffff; +#if IOCHANS > 1 + uint32_t ch2 = poodle[j+1] & 0xffff; if (ch2 & 0x8000) soundin[i+BLKSIZE] = (ch2*(1./32768.)) - 2; else soundin[i+BLKSIZE] = (ch2*(1./32768.)); +#endif + if (ch1 & 0x8000) + soundin[i] = (ch1*(1./32768.)) - 2; + else soundin[i] = (ch1*(1./32768.)); } #endif /* USEADC */ } +#if OBSOLETEAPI /* allow deprecated form if new one unavailable */ #ifndef I2S_COMM_FORMAT_STAND_I2S #define I2S_COMM_FORMAT_STAND_I2S I2S_COMM_FORMAT_I2S @@ -166,6 +192,53 @@ static void initdacs( void) i2s_set_pin(I2S_NUM_0, &i2s_pin_cfg); } } +#else /* OBSOLETEAPI */ +static void initdacs( void) +{ + + /* Get the default channel configuration by the helper macro. + * This helper macro is defined in `i2s_common.h` and shared by all the I2S + communication modes. */ + i2s_chan_config_t chan_cfg = I2S_CHANNEL_DEFAULT_CONFIG(I2S_NUM_0, + I2S_ROLE_MASTER); + /* Allocate a new TX channel and get the handle of this channel */ +#ifdef USEADC + i2s_new_channel(&chan_cfg, &tx_handle, &rx_handle); +#else + i2s_new_channel(&chan_cfg, &tx_handle, NULL); +#endif + /* Setting the configurations, the slot configuration and clock configuration + can be generated by the macros defined in `i2s_std.h` which can only be + used in STD mode. They can help to specify the slot and clock + configurations for initialization or updating */ + i2s_std_config_t std_cfg = { + .clk_cfg = I2S_STD_CLK_DEFAULT_CONFIG(48000), + .slot_cfg = I2S_STD_MSB_SLOT_DEFAULT_CONFIG(I2S_DATA_BIT_WIDTH_16BIT, + I2S_SLOT_MODE_STEREO), + .gpio_cfg = { + .mclk = I2S_GPIO_UNUSED, + .bclk = 13, + .ws = 33, + .dout = 32, + .din = 35, + .invert_flags = { + .mclk_inv = false, + .bclk_inv = false, + .ws_inv = false, + }, + }, + }; + /* Initialize the channel */ + i2s_channel_init_std_mode(tx_handle, &std_cfg); + + /* Before writing data, start the TX channel first */ + i2s_channel_enable(tx_handle); +#ifdef USEADC + i2s_channel_init_std_mode(rx_handle, &std_cfg); + i2s_channel_enable(rx_handle); +#endif +} +#endif /* OBSOLETEAPI */ static int audiostate; @@ -353,8 +426,12 @@ static void espd_printtimediff( void) void glob_foo(void *dummy, t_floatarg f) { if (f == 0) - trymem(0); + espd_printdacs = 0; else if (f == 1) + espd_printdacs = 1; + else if (f == 2) + trymem(2); + else if (f == 3) espd_printtimediff(); } diff --git a/main/espd.h b/main/espd.h index b88dff3..1c13313 100644 --- a/main/espd.h +++ b/main/espd.h @@ -1,17 +1,24 @@ /* #define PD_USE_BLUETOOTH */ /* messages to Pd over bluetooth */ -/* #define PD_USE_WIFI */ /* messages to/from Pd over wifi TCP */ +#define PD_USE_WIFI /* messages to/from Pd over wifi TCP */ #define PD_USE_CONSOLE /* messages to Pd over "console" (USB serial) */ -#define PD_INCLUDEPATCH /* load the patch defined in "testpatch.c" */ +/* #define PD_INCLUDEPATCH */ /* load the patch defined in "testpatch.c" */ /* #define PD_LYRAT */ /* using LyraT or LyraT mini board */ +#define USEADC /* enable audio input (output always enabled) */ /* task priorities */ #define PRIORITY_WIFI 2 + /* if WIFI is enabled we need to define the WIFI name and password, + the peer machine, and the send and receive ports. This can be done + using a "locale.h" or by defining CONFIG_ESP_WIFI_SSID, etc., in + some other way, such as in the sdkconfig file. */ #if defined(CONFIG_LOCALE_FILE) #include CONFIG_LOCALE_FILE #else +#if defined(PD_USE_WIFI) && !defined(CONFIG_ESP_WIFI_SSID) #include "locale.h" #endif +#endif #include void pd_sendmsg(char *buf, int bufsize); @@ -23,7 +30,7 @@ void bt_init( void); void pd_bt_writeback(unsigned char *s, int length); #endif -#define IOCHANS 1 +#define IOCHANS 2 #ifdef PD_USE_WIFI void wifi_init(void); /* wifi.c - manage 802.11 connection */ diff --git a/main/locale.h b/main/locale.h deleted file mode 100644 index 6dc5e17..0000000 --- a/main/locale.h +++ /dev/null @@ -1,5 +0,0 @@ -#define CONFIG_ESP_WIFI_SSID "rock_lobster" -#define CONFIG_ESP_WIFI_PASSWORD "g3tUr0wnw1f1" -#define CONFIG_ESP_WIFI_SENDADDR "10.0.0.215" -#define CONFIG_ESP_WIFI_SENDPORT 4498 -#define CONFIG_ESP_WIFI_LISTENPORT 4499 diff --git a/main/testpatch.c b/main/testpatch.c index e27f3ae..adc40b4 100644 --- a/main/testpatch.c +++ b/main/testpatch.c @@ -1,11 +1,129 @@ -static const char patchfile[] = "\ -canvas 0 0 450 300 12;\n\ -#X obj 190 104 loadbang;\n\ -#X msg 190 129 \; pd dsp 1;\n\ -#X obj 119 158 dac~ 1;\n\ -#X obj 118 98 osc~ 440;\n\ -#X obj 119 126 *~ 0.1;\n\ -#X connect 0 0 1 0;\n\ -#X connect 3 0 4 0;\n\ -#X connect 4 0 2 0;\n\ +static const char patchfile[] = "\\ +canvas 208 66 1338 642 12;\ +#X obj 696 339 print;\ +#X obj 381 596 dac~;\ +#X obj 1057 187 adc~;\ +#X obj 985 242 print~;\ +#X obj 97 41 r key;\ +#X msg 758 321 \; pd dsp 1;\ +#X obj 382 494 *~ 0;\ +#X obj 389 361 -~ 0.5;\ +#X obj 283 411 *~ 1e+20;\ +#X obj 283 437 clip~ 0 1;\ +#X obj 382 520 -~ 0;\ +#X obj 381 557 *~ 0;\ +#X obj 449 553 *~ 0;\ +#X obj 521 536 t b f;\ +#X obj 521 562 1;\ +#X obj 521 588 -;\ +#X obj 1046 245 print~;\ +#X msg 116 120 0;\ +#X msg 161 120 1;\ +#X msg 196 120 2;\ +#X msg 233 121 4;\ +#X msg 276 121 8;\ +#X msg 351 120 16;\ +#X msg 423 120 32;\ +#X msg 482 119 64;\ +#X msg 736 133 440;\ +#X msg 788 136 0;\ +#X msg 828 138 1;\ +#X obj 452 285 t f f;\ +#X obj 555 154 t b f;\ +#X obj 695 164 t b f;\ +#X obj 787 175 t b f;\ +#X obj 281 469 -~;\ +#X obj 281 495 *~ 0;\ +#X obj 516 349 unpack 0 0 0 4 0, f 20;\ +#X obj 520 315 pack 0 0 0 0 0;\ +#X msg 895 143 0;\ +#X msg 935 145 1;\ +#X obj 894 182 t b f;\ +#X obj 383 468 +~;\ +#X obj 388 336 phasor~ 2;\ +#X obj 105 75 sel 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116, f 157;\ +#X msg 1089 109 foo 1;\ +#X msg 1146 107 foo 0;\ +#X obj 1100 151 s pd;\ +#X msg 696 131 0.1;\ +#X obj 452 259 / 32, f 8;\ +#X msg 558 121 32;\ +#X msg 598 123 4096;\ +#X connect 2 0 3 0;\ +#X connect 2 1 16 0;\ +#X connect 4 0 41 0;\ +#X connect 6 0 10 0;\ +#X connect 7 0 8 0;\ +#X connect 7 0 32 1;\ +#X connect 7 0 39 1;\ +#X connect 8 0 9 0;\ +#X connect 9 0 32 0;\ +#X connect 10 0 11 0;\ +#X connect 10 0 12 0;\ +#X connect 11 0 1 0;\ +#X connect 12 0 1 1;\ +#X connect 13 0 14 0;\ +#X connect 13 1 15 1;\ +#X connect 14 0 15 0;\ +#X connect 15 0 11 1;\ +#X connect 17 0 46 0;\ +#X connect 18 0 46 0;\ +#X connect 19 0 46 0;\ +#X connect 20 0 46 0;\ +#X connect 21 0 46 0;\ +#X connect 22 0 46 0;\ +#X connect 23 0 46 0;\ +#X connect 24 0 46 0;\ +#X connect 25 0 30 0;\ +#X connect 26 0 31 0;\ +#X connect 27 0 31 0;\ +#X connect 28 0 35 0;\ +#X connect 29 0 46 0;\ +#X connect 29 1 46 1;\ +#X connect 30 0 46 0;\ +#X connect 30 1 35 3;\ +#X connect 31 0 46 0;\ +#X connect 31 1 35 2;\ +#X connect 32 0 33 0;\ +#X connect 33 0 39 0;\ +#X connect 34 0 6 1;\ +#X connect 34 2 13 0;\ +#X connect 34 2 12 1;\ +#X connect 34 3 40 0;\ +#X connect 34 4 33 1;\ +#X connect 35 0 0 0;\ +#X connect 35 0 34 0;\ +#X connect 35 0 5 0;\ +#X connect 36 0 38 0;\ +#X connect 37 0 38 0;\ +#X connect 38 0 46 0;\ +#X connect 38 1 35 4;\ +#X connect 39 0 6 0;\ +#X connect 40 0 7 0;\ +#X connect 41 0 17 0;\ +#X connect 41 1 18 0;\ +#X connect 41 2 19 0;\ +#X connect 41 3 20 0;\ +#X connect 41 4 21 0;\ +#X connect 41 5 22 0;\ +#X connect 41 6 23 0;\ +#X connect 41 7 24 0;\ +#X connect 41 8 47 0;\ +#X connect 41 9 48 0;\ +#X connect 41 10 45 0;\ +#X connect 41 11 25 0;\ +#X connect 41 12 26 0;\ +#X connect 41 13 27 0;\ +#X connect 41 14 36 0;\ +#X connect 41 15 37 0;\ +#X connect 41 16 3 0;\ +#X connect 41 17 16 0;\ +#X connect 41 18 42 0;\ +#X connect 41 19 43 0;\ +#X connect 42 0 44 0;\ +#X connect 43 0 44 0;\ +#X connect 45 0 30 0;\ +#X connect 46 0 28 0;\ +#X connect 47 0 29 0;\ +#X connect 48 0 29 0;\ "; diff --git a/test-patch/esp-patch.pd b/test-patch/esp-patch.pd index 6f163e4..8143be0 100644 --- a/test-patch/esp-patch.pd +++ b/test-patch/esp-patch.pd @@ -1,33 +1,25 @@ -#N canvas 332 97 393 697 12; +#N canvas 52 117 393 697 12; #X obj 53 386 dac~ 1; -#X obj 44 130 r a; -#X obj 52 326 *~; +#X obj 119 294 r a; #X obj 158 87 loadbang; -#X msg 103 130 50; -#X obj 49 178 line~; -#X obj 49 203 *~; -#X obj 49 228 *~ 0.0001; #X msg 156 123 dsp 1; #X obj 156 147 s pd; -#X obj 72 268 r f; +#X obj 46 160 r f; #X obj 144 352 r b; #X obj 54 354 +~; -#X obj 144 378 f; -#X obj 144 404 / 100; -#X obj 72 295 phasor~ 440; -#X connect 1 0 5 0; -#X connect 2 0 12 0; -#X connect 3 0 8 0; +#X obj 46 187 phasor~ 440; +#X obj 46 213 -~ 0.5; +#X obj 46 239 *~ 1e+15; +#X obj 46 265 clip~ 0 1; +#X obj 52 326 *~ 0.03; +#X connect 1 0 12 1; +#X connect 2 0 3 0; #X connect 3 0 4 0; -#X connect 4 0 5 0; -#X connect 5 0 6 0; -#X connect 5 0 6 1; -#X connect 6 0 7 0; -#X connect 7 0 2 0; +#X connect 5 0 8 0; +#X connect 6 0 7 1; +#X connect 7 0 0 0; #X connect 8 0 9 0; -#X connect 10 0 15 0; -#X connect 11 0 13 0; -#X connect 12 0 0 0; -#X connect 13 0 14 0; -#X connect 14 0 12 1; -#X connect 15 0 2 1; +#X connect 9 0 10 0; +#X connect 10 0 11 0; +#X connect 11 0 12 0; +#X connect 12 0 7 0; diff --git a/test-patch/host-patch.pd b/test-patch/host-patch.pd index fcb2113..f9ed34a 100644 --- a/test-patch/host-patch.pd +++ b/test-patch/host-patch.pd @@ -1,4 +1,4 @@ -#N canvas 110 26 1262 545 12; +#N canvas 687 140 1262 545 12; #X msg 373 294 listen 4498; #X obj 374 331 netreceive -u -f; #X listbox 481 365 26 0 0 0 - - - 0; @@ -33,7 +33,7 @@ #X obj 95 121 r amp; #X msg 93 146 send a \$1; #X msg 476 198 set \$1; -#X floatatom 589 180 11 0 100 1 bias bias-set bias 0; +#X floatatom 589 180 11 -100 100 1 bias bias-set bias 0; #X obj 476 172 r bias; #X obj 476 224 s bias-set; #X obj 181 129 r bias; @@ -56,11 +56,10 @@ #X obj 599 106 s amp; #X floatatom 821 349 5 0 1000 0 - - - 0; #X obj 821 400 / 32768; -#X obj 821 426 * 100; #X obj 821 374 + 0.25; -#X obj 821 479 s bias; -#X floatatom 832 452 10 0 0 0 - - - 0; -#X obj 817 312 r int-bias; +#X obj 820 453 s bias; +#X floatatom 837 427 10 0 0 0 - - - 0; +#X obj 821 314 r int-bias; #X msg 941 34 \; int-bias 223 \; amp 0 \; freq 0; #X msg 929 279 \; int-bias \$1 \; amp 0 \; freq 0; #X msg 905 151 15; @@ -74,11 +73,16 @@ #X msg 1089 155 255; #X msg 847 204 255; #X msg 1048 154 127; -#X obj 1107 106 + 255; -#X floatatom 1112 69 5 0 0 0 - - - 0; +#X obj 857 145 + 255; +#X floatatom 858 117 5 0 0 0 - - - 0; #X msg 1166 155 639; -#X msg 983 417 \; int-bias 255 \; amp 0 \; freq 0; +#X msg 830 31 \; int-bias 255 \; amp 0 \; freq 0; #X msg 1134 153 510; +#X msg 210 255 send pd foo 0; +#X msg 250 225 send pd foo 2; +#X floatatom 1053 395 11 0 0 0 - - - 0; +#X obj 1053 446 s amp; +#X obj 1053 420 / 200; #X connect 0 0 1 0; #X connect 1 0 4 0; #X connect 1 1 2 0; @@ -124,25 +128,28 @@ #X connect 51 0 52 0; #X connect 52 0 53 0; #X connect 52 0 54 0; -#X connect 55 0 58 0; -#X connect 56 0 57 0; -#X connect 57 0 59 0; -#X connect 57 0 60 0; -#X connect 58 0 56 0; -#X connect 61 0 55 0; -#X connect 64 0 66 0; -#X connect 65 0 66 0; -#X connect 66 0 70 0; -#X connect 66 1 67 0; -#X connect 67 0 63 0; -#X connect 68 0 66 0; -#X connect 69 0 66 0; -#X connect 70 0 73 0; -#X connect 71 0 66 0; +#X connect 55 0 57 0; +#X connect 56 0 59 0; +#X connect 56 0 58 0; +#X connect 57 0 56 0; +#X connect 60 0 55 0; +#X connect 63 0 65 0; +#X connect 64 0 65 0; +#X connect 65 0 69 0; +#X connect 65 1 66 0; +#X connect 66 0 62 0; +#X connect 67 0 65 0; +#X connect 68 0 65 0; +#X connect 69 0 72 0; +#X connect 70 0 65 0; +#X connect 71 0 65 0; #X connect 72 0 66 0; -#X connect 73 0 67 0; -#X connect 74 0 66 0; -#X connect 75 0 66 0; -#X connect 76 0 75 0; -#X connect 77 0 66 0; -#X connect 79 0 66 0; +#X connect 73 0 65 0; +#X connect 74 0 65 0; +#X connect 75 0 74 0; +#X connect 76 0 65 0; +#X connect 78 0 65 0; +#X connect 79 0 16 0; +#X connect 80 0 16 0; +#X connect 81 0 83 0; +#X connect 83 0 82 0;