int main(int argc, char *argv[]) { char *files[] = {"test51_0.tpl", "test51_1.tpl", "test51_2.tpl", "test51_3.tpl","test51_4.tpl", NULL}; char **f; char buf[FILE_BUFLEN]; int rc,fd; tpl_gather_t *gs=NULL; for (f = files; *f; f++) { if (DEBUG) printf("file is %s\n", *f); if ( ( fd = open(*f, O_RDONLY) ) == -1) { printf("error - can't open %s: %s\n", *f, strerror(errno)); exit(-1); } rc = read(fd,&buf,FILE_BUFLEN); /* read whole file (no points for style) */ if (rc == -1) { printf("error - can't read %s: %s\n", *f, strerror(errno)); exit(-1); } if (tpl_gather(TPL_GATHER_MEM,buf, rc, &gs, tpl_cb, NULL) <= 0) { printf("tpl_gather_mem returned <= 0, exiting\n"); exit(-1); } close(fd); } printf("num_tpls: %d, sum: %d\n", num_tpls, sum_tpls); return(0); }
int main() { FILE *f1,*f2; int fdflags,fd,fd1,fd2; int selrc, maxfd; tpl_gather_t *gs1=NULL,*gs2=NULL,**gs; struct timeval tv; fd_set rset; f1 = popen("cat test26_0.tpl;sleep 1; cat test26_1.tpl", "r"); fd1 = fileno(f1); fdflags = fcntl(fd1, F_GETFL, 0); fcntl( fd1, F_SETFL, fdflags | O_NONBLOCK); f2 = popen("cat test26_2.tpl;sleep 1; cat test26_3.tpl", "r"); fd2 = fileno(f2); fdflags = fcntl(fd2, F_GETFL, 0); fcntl( fd2, F_SETFL, fdflags | O_NONBLOCK); while (1) { FD_ZERO( &rset ); if (fd1 >= 0) FD_SET( fd1, &rset ); if (fd2 >= 0) FD_SET( fd2, &rset ); if (fd1 == -1 && fd2 == -1) { printf("%d tpls gathered.\n",num_tpls); printf("%d is their sum.\n",sum_tpls); return(0); } maxfd=0; if (fd1>maxfd) maxfd = fd1; if (fd2>maxfd) maxfd = fd2; tv.tv_sec = 5; tv.tv_usec = 0; selrc = select(maxfd+1, &rset, NULL, NULL, &tv ); if (selrc == -1) { perror("select()"); } else if (selrc) { for(fd=0;fd<maxfd+1;fd++) { if ( FD_ISSET(fd, &rset) ) { if (DEBUG) printf("fd %d readable\n", fd); gs = (fd1 == fd) ? &gs1 : &gs2; if (tpl_gather(TPL_GATHER_NONBLOCKING,fd,gs,tpl_cb,NULL) <= 0) { if (fd1 == fd) {pclose(f1); fd1 = -1; } if (fd2 == fd) {pclose(f2); fd2 = -1; } } else { if (DEBUG) printf("tpl_gather >0\n"); } } } } else { if (DEBUG) printf("timeout\n"); } } return(0); }
int main(int argc, char *argv[]) { int rc = -1; void *buf=NULL; size_t sz; if (tpl_gather(TPL_GATHER_BLOCKING, STDIN_FILENO, &buf, &sz) <= 0) goto done; /* peek into the saved image to see how many samples it has in it */ uint32_t num_fxlens, *fxlens; char *fmt = tpl_peek(TPL_MEM|TPL_FXLENS, buf, sz, &num_fxlens, &fxlens); if ((!fmt) || (num_fxlens<1)) {fprintf(stderr,"invalid buffer\n"); goto done;} cfg.nsamples = fxlens[0]; free(fxlens); /* make a buffer to load the PCM data into */ /* TODO assert cfg.resolution == cfg.resolution in the image */ size_t pcmlen = cfg.resolution * cfg.nsamples; int16_t *pcm; pcm = (int16_t*)malloc(pcmlen); if (!pcm) {fprintf(stderr,"out of memory\n"); goto done;} tpl_node *tn = tpl_map("iiij#", &cfg.sample_rate, &cfg.duration, &cfg.resolution, pcm, cfg.nsamples); tpl_load(tn, TPL_MEM, buf, sz); tpl_unpack(tn,0); tpl_free(tn); if (cfg.verbose) fprintf(stderr,"read the PCM file: " "duration %u s, sample rate %u hz, resolution %u bits\n", cfg.duration, cfg.sample_rate, cfg.resolution*8); play_pcm(argc, argv, pcm, pcmlen, cfg.sample_rate, cfg.verbose); /* TODO cycle if requested, reusing buf? */ rc = 0; done: if (buf) free(buf); return rc; }