diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index af49f62..18fc33b 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -22,6 +22,10 @@ set(COMPONENT_SRCS "weasel.c fifostream.c pdmain.c \ ../pd/src/d_resample.c \ ../pd/src/d_soundfile.c \ ../pd/src/d_global.c \ +../pd/src/d_osc.c \ +../pd/src/d_ctl.c \ +../pd/src/d_arithmetic.c \ +../pd/src/d_dac.c \ ../pd/src/x_arithmetic.c \ ../pd/src/x_connective.c \ ../pd/src/x_interface.c \ diff --git a/main/fifostream.c b/main/fifostream.c index b99b9c8..283a45b 100644 --- a/main/fifostream.c +++ b/main/fifostream.c @@ -10,7 +10,12 @@ #include "audio_type_def.h" static const char *TAG = "fifostream"; -#define BUF_SIZE (100) +#define CHANS 2 +#define INCHANS CHANS +#define OUTCHANS CHANS +#define BLKSIZE 64 +#define BLK_BYTES (BLKSIZE*CHANS*sizeof(short)) + extern void pdmain_tick( void); typedef struct fifostream { @@ -40,12 +45,12 @@ static esp_err_t fifostream_open(audio_element_handle_t self) fifostream->channel = info.channels; } fifostream->at_eof = 0; - fifostream->s_buf = (short *)calloc(sizeof(short), BUF_SIZE/2); + fifostream->s_buf = (short *)calloc(sizeof(short), BLK_BYTES/sizeof(short)); if (fifostream->s_buf == NULL) { ESP_LOGE(TAG, "calloc buffer failed. (line %d)", __LINE__); return ESP_ERR_NO_MEM; } - memset(fifostream->s_buf, 0, BUF_SIZE); + memset(fifostream->s_buf, 0, BLK_BYTES); return ESP_OK; } @@ -61,13 +66,54 @@ static esp_err_t fifostream_close(audio_element_handle_t self) return ESP_OK; } -int phaseinc = 123; -#define INCHANS 2 -#define OUTCHANS 2 -#define BLKSIZE 64 - float soundin[OUTCHANS * BLKSIZE], soundout[OUTCHANS * BLKSIZE]; +#if 0 /* better one that doesn't work for some reason */ +static int fifostream_process(audio_element_handle_t self, char *in_buffer, + int in_len) +{ + static int cumsamps = 0; + fifostream_t *fifostream = (fifostream_t *)audio_element_getdata(self); + int ret = 0; + int r_size = (fifostream->at_eof ? 0 : + audio_element_input(self, (char *)fifostream->s_buf, BLK_BYTES)); + if (r_size >= BLK_BYTES) + { + int i, nsamps = BLKSIZE; + fifostream->byte_num += BLK_BYTES; + + for (i = 0; i < BLKSIZE; i+= 2) + { + soundin[i] = fifostream->s_buf[i]/32768;; + soundin[BLKSIZE+i] = fifostream->s_buf[i+1]/32768; + } + pdmain_tick(); + for (i = 0; i < BLKSIZE; i+= 2) + { + int ch1 = 32767*soundout[i], ch2 = 32767*soundout[BLKSIZE+i]; + if (ch1 > 32767) + ch1 = 32767; + else if (ch1 < -32768) + ch1 = -32768; + if (ch2 > 32767) + ch2 = 32767; + else if (ch2 < -32768) + ch2 = -32768; + fifostream->s_buf[i] = (short)ch1; + fifostream->s_buf[i+1] = (short)ch2; + } + ret = audio_element_output(self, (char *)fifostream->s_buf, BLK_BYTES); + } + else + { + fifostream->at_eof = 1; + ret = 0; + } + return ret; +} +#endif + +#if 1 static int fifostream_process(audio_element_handle_t self, char *in_buffer, int in_len) { static int cumsamps = 0; @@ -75,35 +121,48 @@ static int fifostream_process(audio_element_handle_t self, char *in_buffer, int int ret = 0; int r_size = 0; if (fifostream->at_eof == 0) { - r_size = audio_element_input(self, (char *)fifostream->s_buf, BUF_SIZE); + r_size = audio_element_input(self, (char *)fifostream->s_buf, BLK_BYTES); } if (r_size > 0) { - if (r_size != BUF_SIZE) { + if (r_size != BLK_BYTES) { fifostream->at_eof = 1; } fifostream->byte_num += r_size; { - int i, nsamps = r_size/sizeof(short); + int i, nsamps = r_size/(2*sizeof(short)); static int phase; + if (nsamps * 2 * sizeof(short) != r_size) + ESP_LOGE("WOMBAT", "odd sample frame size %d", r_size); for (i = 0; i < nsamps; i++) { - phase += phaseinc ; - fifostream->s_buf[i] = (phase >> 3); - } - - cumsamps += nsamps; - while (cumsamps >= 128) - { - pdmain_tick(); - cumsamps -= 128; + int ch1 = 32767*soundout[i], ch2 = 32767*soundout[BLKSIZE+i]; + if (ch1 > 32767) + ch1 = 32767; + else if (ch1 < -32768) + ch1 = -32768; + if (ch2 > 32767) + ch2 = 32767; + else if (ch2 < -32768) + ch2 = -32768; + soundin[phase] = fifostream->s_buf[2*i]/32768; + fifostream->s_buf[2*i] = ch1; + soundin[BLKSIZE+phase] = fifostream->s_buf[2*i+1]/32768; + fifostream->s_buf[2*i+1] = ch2; + phase++; + if (phase == BLKSIZE) + { + pdmain_tick(); + phase = 0; + } } } - ret = audio_element_output(self, (char *)fifostream->s_buf, BUF_SIZE); + ret = audio_element_output(self, (char *)fifostream->s_buf, BLK_BYTES); } else { ret = r_size; } return ret; } +#endif audio_element_handle_t fifostream_init(fifostream_cfg_t *config) { diff --git a/main/pdmain.c b/main/pdmain.c index 1386cf4..ebd4eeb 100644 --- a/main/pdmain.c +++ b/main/pdmain.c @@ -17,14 +17,30 @@ extern t_printhook sys_printhook; void pdmain_print( const char *s); +#if 0 static const char patchfile[] = "\ canvas 274 279 752 643 12;\n\ #X obj 123 146 loadbang;\n\ -#X obj 123 171 metro 10000;\n\ +#X obj 123 171 metro 1000;\n\ #X obj 123 196 print poodle;\n\ #X connect 0 0 1 0;\n\ #X connect 1 0 2 0;\n\ "; +#endif + +static const char patchfile[] = "\ +canvas 0 50 604 482 12;\n\ +#X obj 179 99 r foo;\n\ +#X obj 179 124 osc~ 880;\n\ +#X obj 179 149 dac~ 1;\n\ +#X obj 257 127 osc~ 880;\n\ +#X obj 257 102 r foo2;\n\ +#X obj 257 151 dac~ 2;\n\ +#X connect 0 0 1 0;\n\ +#X connect 1 0 2 0;\n\ +#X connect 3 0 5 0;\n\ +#X connect 4 0 3 0;\n\ +"; static void trymem(int foo) { @@ -70,16 +86,19 @@ void pd_poll_bt( void) } } +extern float soundin[], soundout[]; + void pdmain_init( void) { t_binbuf *b; sys_printhook = pdmain_print; trymem(1); // 111 - trymem(2); pd_init(); - trymem(100); // 47 + trymem(2); // 47 STUFF->st_dacsr = sys_getsr(); + STUFF->st_soundout = soundout; + STUFF->st_soundin = soundin; b = binbuf_new(); @@ -102,6 +121,7 @@ void pdmain_tick( void) pdmain_init(); initted = 1; } + memset(soundout, 0, 128*sizeof(float)); pd_poll_bt(); sched_tick(); } @@ -110,13 +130,11 @@ void pdmain_tick( void) t_class *glob_pdobject; -extern int phaseinc; - static void glob_foo(void *dummy, t_floatarg f) { post("foo %f", f); - phaseinc = f; } + void glob_dsp(void *dummy, t_symbol *s, int argc, t_atom *argv); void glob_init( void) @@ -207,15 +225,15 @@ void conf_init(void) d_global_setup(); d_soundfile_setup(); d_ugen_setup(); + d_dac_setup(); + d_ctl_setup(); + d_osc_setup(); trymem(11); } /* - d_osc_setup(); d_arithmetic_setup(); d_array_setup(); - d_ctl_setup(); - d_dac_setup(); d_delay_setup(); d_filter_setup(); d_math_setup(); @@ -226,7 +244,7 @@ void conf_init(void) /* ------- STUBS that do nothing ------------- */ int sys_get_outchannels(void) {return(2); } int sys_get_inchannels(void) {return(2); } -float sys_getsr( void) {return (48000);} +float sys_getsr( void) {return (44100);} int sys_getblksize(void) { return (DEFDACBLKSIZE); } int pd_compatibilitylevel = 100; @@ -956,7 +974,8 @@ double sys_getrealtime(void) (1./1000000.) * (now.tv_usec - then.tv_usec)); } -/* -------------- DSP basics --------------- */ +#if 0 +/* -------------- DSP basics - LATER move to d_ugen.c --------------- */ t_int *plus_perform(t_int *w) { @@ -1080,6 +1099,7 @@ void dsp_add_scalarcopy(t_float *in, t_sample *out, int n) else dsp_add(sig_tilde_perf8, 3, in, out, (t_int)n); } +#endif /* ------------------ g_traversal.c --------------- */ diff --git a/main/weasel.c b/main/weasel.c index 09e0964..1d43a67 100644 --- a/main/weasel.c +++ b/main/weasel.c @@ -330,7 +330,6 @@ void app_main() esp_bt_pin_code_t pin_code; esp_bt_gap_set_pin(pin_type, 0, pin_code); - /* pdmain_init(); */ audio_main(); }