static void runawhile(int num_ticks) { int ticksleft = num_ticks*1000; while (ticksleft > 0) { // fill inbuf here libpd_process_float(1, inbuf, outbuf); // use outbuf here sys_pollgui(); usleep(1451); /* 1 tick is about 1.45 msec */ ticksleft -= 1; } }
void sched_audio_callbackfn(void) { sys_setmiditimediff(0, 1e-6 * sys_schedadvance); sys_addhist(1); sched_tick(sys_time + sys_time_per_dsp_tick); sys_addhist(2); sys_pollmidiqueue(); sys_addhist(3); sys_pollgui(); sys_addhist(5); sched_pollformeters(); sys_addhist(0); }
/* take the scheduler forward one DSP tick, also handling clock timeouts */ void sched_tick(double next_sys_time) { int countdown = 5000; while (clock_setlist && clock_setlist->c_settime < next_sys_time) { t_clock *c = clock_setlist; sys_time = c->c_settime; clock_unset(clock_setlist); outlet_setstacklim(); (*c->c_fn)(c->c_owner); if (!countdown--) { countdown = 5000; sys_pollgui(); } if (sys_quit) return; } sys_time = next_sys_time; dsp_tick(); sched_diddsp++; }
static void m_callbackscheduler(void) { sys_initmidiqueue(); while (!sys_quit) { double timewas = pd_this->pd_systime; #ifdef _WIN32 Sleep(1000); #else sleep(1); #endif if (pd_this->pd_systime == timewas) { sys_lock(); sys_pollgui(); sched_tick(); sys_unlock(); } if (sys_idlehook) sys_idlehook(); } }
static void m_callbackscheduler(void) { sys_initmidiqueue(); while (!sys_quit) { double timewas = sys_time; #ifdef MSW Sleep(1000); #else sleep(1); #endif if (sys_time == timewas) { sys_lock(); sys_pollgui(); sched_tick(sys_time + sys_time_per_dsp_tick); sys_unlock(); } if (sys_idlehook) sys_idlehook(); } }
/* take the scheduler forward one DSP tick, also handling clock timeouts */ void sched_tick( void) { double next_sys_time = pd_this->pd_systime + sys_time_per_dsp_tick; int countdown = 5000; while (pd_this->pd_clock_setlist && pd_this->pd_clock_setlist->c_settime < next_sys_time) { t_clock *c = pd_this->pd_clock_setlist; pd_this->pd_systime = c->c_settime; clock_unset(pd_this->pd_clock_setlist); outlet_setstacklim(); (*c->c_fn)(c->c_owner); if (!countdown--) { countdown = 5000; sys_pollgui(); } if (sys_quit) return; } pd_this->pd_systime = next_sys_time; dsp_tick(); sched_diddsp++; }
static void m_pollingscheduler( void) { int idlecount = 0; sys_time_per_dsp_tick = (TIMEUNITPERSEC) * ((double)sys_schedblocksize) / sys_dacsr; #ifdef THREAD_LOCKING sys_lock(); #endif sys_clearhist(); if (sys_sleepgrain < 100) sys_sleepgrain = sys_schedadvance/4; if (sys_sleepgrain < 100) sys_sleepgrain = 100; else if (sys_sleepgrain > 5000) sys_sleepgrain = 5000; sys_initmidiqueue(); while (!sys_quit) { int didsomething = 0; int timeforward; sys_addhist(0); waitfortick: if (sched_useaudio != SCHED_AUDIO_NONE) { #ifdef THREAD_LOCKING /* T.Grill - send_dacs may sleep -> unlock thread lock make that time available - could messaging do any harm while sys_send_dacs is running? */ sys_unlock(); #endif timeforward = sys_send_dacs(); #ifdef THREAD_LOCKING /* T.Grill - done */ sys_unlock(); #endif /* if dacs remain "idle" for 1 sec, they're hung up. */ if (timeforward != 0) idlecount = 0; else { idlecount++; if (!(idlecount & 31)) { static double idletime; if (sched_useaudio != SCHED_AUDIO_POLL) { bug("m_pollingscheduler\n"); return; } /* on 32nd idle, start a clock watch; every 32 ensuing idles, check it */ if (idlecount == 32) idletime = sys_getrealtime(); else if (sys_getrealtime() - idletime > 1.) { post("audio I/O stuck... closing audio\n"); sys_close_audio(); sched_set_using_audio(SCHED_AUDIO_NONE); goto waitfortick; } } } } else { if (1000. * (sys_getrealtime() - sched_referencerealtime) > clock_gettimesince(sched_referencelogicaltime)) timeforward = SENDDACS_YES; else timeforward = SENDDACS_NO; } sys_setmiditimediff(0, 1e-6 * sys_schedadvance); sys_addhist(1); if (timeforward != SENDDACS_NO) sched_tick(sys_time + sys_time_per_dsp_tick); if (timeforward == SENDDACS_YES) didsomething = 1; sys_addhist(2); sys_pollmidiqueue(); if (sys_pollgui()) { if (!didsomething) sched_didpoll++; didsomething = 1; } sys_addhist(3); /* test for idle; if so, do graphics updates. */ if (!didsomething) { sched_pollformeters(); sys_reportidle(); #ifdef THREAD_LOCKING sys_unlock(); /* unlock while we idle */ #endif /* call externally installed idle function if any. */ if (!sys_idlehook || !sys_idlehook()) { /* if even that had nothing to do, sleep. */ if (timeforward != SENDDACS_SLEPT) sys_microsleep(sys_sleepgrain); } #ifdef THREAD_LOCKING sys_lock(); #endif sys_addhist(5); sched_didnothing++; } } #ifdef THREAD_LOCKING sys_unlock(); #endif }
int pd_extern_sched(char *flags) { int naudioindev, audioindev[MAXAUDIOINDEV], chindev[MAXAUDIOINDEV]; int naudiooutdev, audiooutdev[MAXAUDIOOUTDEV], choutdev[MAXAUDIOOUTDEV]; int i, j, rate, advance, callback, chin, chout, fill = 0, c, blocksize; t_binbuf *b = binbuf_new(); sys_get_audio_params(&naudioindev, audioindev, chindev, &naudiooutdev, audiooutdev, choutdev, &rate, &advance, &callback, &blocksize); chin = (naudioindev < 1 ? 0 : chindev[0]); chout = (naudiooutdev < 1 ? 0 : choutdev[0]); /* fprintf(stderr, "Pd plug-in scheduler called, chans %d %d, sr %d\n", chin, chout, (int)rate); */ sys_setchsr(chin, chout, rate); sys_audioapi = API_NONE; while ((c = getchar()) != EOF) { if (c == ';') { int n; t_atom *ap; binbuf_text(b, inbuf, fill); n = binbuf_getnatom(b); ap = binbuf_getvec(b); fill = 0; if (n > 0 && ap[0].a_type == A_FLOAT) { /* a list -- take it as incoming signals. */ int chan, nchan = n/DEFDACBLKSIZE; t_sample *fp; for (i = chan = 0, fp = sys_soundin; chan < nchan; chan++) for (j = 0; j < DEFDACBLKSIZE; j++) *fp++ = atom_getfloat(ap++); for (; chan < chin; chan++) for (j = 0; j < DEFDACBLKSIZE; j++) *fp++ = 0; sched_tick(sys_time+sys_time_per_dsp_tick); sys_pollgui(); #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__) || defined(__GNU__) pollwatchdog(); #endif printf(";\n"); for (i = chout*DEFDACBLKSIZE, fp = sys_soundout; i--; fp++) { printf("%g\n", *fp); *fp = 0; } printf(";\n"); fflush(stdout); } else if (n > 1 && ap[0].a_type == A_SYMBOL) { t_pd *whom = ap[0].a_w.w_symbol->s_thing; if (!whom) error("%s: no such object", ap[0].a_w.w_symbol->s_name); else if (ap[1].a_type == A_SYMBOL) typedmess(whom, ap[1].a_w.w_symbol, n-2, ap+2); else pd_list(whom, 0, n-1, ap+1); } } else if (fill < BUFSIZE) inbuf[fill++] = c; else if (fill == BUFSIZE) fprintf(stderr, "pd-extern: input buffer overflow\n"); } return (0); }
int pd_extern_sched(char *flags) { int naudioindev, audioindev[MAXAUDIOINDEV], chindev[MAXAUDIOINDEV]; int naudiooutdev, audiooutdev[MAXAUDIOOUTDEV], choutdev[MAXAUDIOOUTDEV]; int i, j, rate, advance, callback, chin, chout, fill = 0, c, blocksize, useascii = 0; t_binbuf *b = binbuf_new(); sys_get_audio_params(&naudioindev, audioindev, chindev, &naudiooutdev, audiooutdev, choutdev, &rate, &advance, &callback, &blocksize); chin = (naudioindev < 1 ? 0 : chindev[0]); chout = (naudiooutdev < 1 ? 0 : choutdev[0]); if (!flags || flags[0] != 'a') { /* signal to stdout object to do binary by attaching an object to an obscure symbol name */ pd_ambinary_class = class_new(gensym("pd~"), 0, 0, sizeof(t_pd), CLASS_PD, 0); pd_bind(&pd_ambinary_class, gensym("#pd_binary_stdio")); /* On Windows, set stdin and out to "binary" mode */ #ifdef _WIN32 setmode(fileno(stdout),O_BINARY); setmode(fileno(stdin),O_BINARY); #endif } else { if (!(ascii_inbuf = getbytes(BUFSIZE))) return (1); useascii = 1; } /* fprintf(stderr, "Pd plug-in scheduler called, chans %d %d, sr %d\n", chin, chout, (int)rate); */ sys_setchsr(chin, chout, rate); sys_audioapi = API_NONE; while (useascii ? readasciimessage(b) : readbinmessage(b) ) { t_atom *ap = binbuf_getvec(b); int n = binbuf_getnatom(b); if (n > 0 && ap[0].a_type == A_FLOAT) { /* a list -- take it as incoming signals. */ int chan, nchan = n/DEFDACBLKSIZE; t_sample *fp; for (i = chan = 0, fp = STUFF->st_soundin; chan < nchan; chan++) for (j = 0; j < DEFDACBLKSIZE; j++) *fp++ = atom_getfloat(ap++); for (; chan < chin; chan++) for (j = 0; j < DEFDACBLKSIZE; j++) *fp++ = 0; sched_tick(); sys_pollgui(); #if defined(__linux__) || defined(__FreeBSD__) || defined(__FreeBSD_kernel__)\ || defined(__GNU__) pollwatchdog(); #endif if (useascii) printf(";\n"); else putchar(A_SEMI); for (i = chout*DEFDACBLKSIZE, fp = STUFF->st_soundout; i--; fp++) { if (useascii) printf("%g\n", *fp); else pd_tilde_putfloat(*fp, stdout); *fp = 0; } if (useascii) printf(";\n"); else putchar(A_SEMI); fflush(stdout); } else if (n > 1 && ap[0].a_type == A_SYMBOL) { t_pd *whom = ap[0].a_w.w_symbol->s_thing; if (!whom) error("%s: no such object", ap[0].a_w.w_symbol->s_name); else if (ap[1].a_type == A_SYMBOL) typedmess(whom, ap[1].a_w.w_symbol, n-2, ap+2); else pd_list(whom, 0, n-1, ap+1); } } binbuf_free(b); return (0); }