Update of /cvsroot/pure-data/pd/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv15588
Modified Files: Tag: desiredata d_soundfile.c Log Message: cleanup
Index: d_soundfile.c =================================================================== RCS file: /cvsroot/pure-data/pd/src/d_soundfile.c,v retrieving revision 1.4.4.11.2.10.2.16 retrieving revision 1.4.4.11.2.10.2.17 diff -C2 -d -r1.4.4.11.2.10.2.16 -r1.4.4.11.2.10.2.17 *** d_soundfile.c 19 Jul 2007 01:30:16 -0000 1.4.4.11.2.10.2.16 --- d_soundfile.c 19 Jul 2007 02:43:33 -0000 1.4.4.11.2.10.2.17 *************** *** 403,412 **** if (argc < 2 || argv[1].a_type != A_FLOAT || ((bytespersamp = (int) argv[1].a_float) < 2) || bytespersamp > 4) goto usage; argc -= 2; argv += 2; ! } else if (!strcmp(flag, "normalize")) {normalize = 1; argc -= 1; argv += 1; ! } else if (!strcmp(flag, "wave")) {filetype = FORMAT_WAVE; argc -= 1; argv += 1; ! } else if (!strcmp(flag, "nextstep")) {filetype = FORMAT_NEXT; argc -= 1; argv += 1; ! } else if (!strcmp(flag, "aiff")) {filetype = FORMAT_AIFF; argc -= 1; argv += 1; ! } else if (!strcmp(flag, "big")) {endianness = 1; argc -= 1; argv += 1; ! } else if (!strcmp(flag, "little")) {endianness = 0; argc -= 1; argv += 1; } else if (!strcmp(flag, "r") || !strcmp(flag, "rate")) { if (argc < 2 || argv[1].a_type != A_FLOAT || ((rate = argv[1].a_float) <= 0)) goto usage; --- 403,412 ---- if (argc < 2 || argv[1].a_type != A_FLOAT || ((bytespersamp = (int) argv[1].a_float) < 2) || bytespersamp > 4) goto usage; argc -= 2; argv += 2; ! } else if (!strcmp(flag, "normalize")) {normalize = 1; argc--; argv++; ! } else if (!strcmp(flag, "wave")) {filetype = FORMAT_WAVE; argc--; argv++; ! } else if (!strcmp(flag, "nextstep")) {filetype = FORMAT_NEXT; argc--; argv++; ! } else if (!strcmp(flag, "aiff")) {filetype = FORMAT_AIFF; argc--; argv++; ! } else if (!strcmp(flag, "big")) {endianness = 1; argc--; argv++; ! } else if (!strcmp(flag, "little")) {endianness = 0; argc--; argv++; } else if (!strcmp(flag, "r") || !strcmp(flag, "rate")) { if (argc < 2 || argv[1].a_type != A_FLOAT || ((rate = argv[1].a_float) <= 0)) goto usage; *************** *** 460,475 **** }
static int create_soundfile(t_canvas *canvas, const char *filename, int filetype, int nframes, int bytespersamp, int bigendian, int nchannels, int swap, float samplerate) { char filenamebuf[MAXPDSTRING]; char headerbuf[WRITEHDRSIZE]; - t_wave *wavehdr = (t_wave *)headerbuf; - t_nextstep *nexthdr = (t_nextstep *)headerbuf; - t_aiff *aiffhdr = (t_aiff *)headerbuf; int fd, headersize = 0; strncpy(filenamebuf, filename, MAXPDSTRING-10); filenamebuf[MAXPDSTRING-10] = 0; if (filetype == FORMAT_NEXT) { ! if (strcasecmp(filenamebuf + strlen(filenamebuf)-4, ".snd")) strcat(filenamebuf, ".snd"); if (bigendian) strncpy(nexthdr->fileid, bigendian?".snd":"dns.", 4); nexthdr->onset = swap4(sizeof(*nexthdr), swap); --- 460,475 ---- }
+ static bool strcaseends(const char *a, const char *b) {return strcasecmp(a+strlen(a)-strlen(b),b)==0;} + static int create_soundfile(t_canvas *canvas, const char *filename, int filetype, int nframes, int bytespersamp, int bigendian, int nchannels, int swap, float samplerate) { char filenamebuf[MAXPDSTRING]; char headerbuf[WRITEHDRSIZE]; int fd, headersize = 0; strncpy(filenamebuf, filename, MAXPDSTRING-10); filenamebuf[MAXPDSTRING-10] = 0; if (filetype == FORMAT_NEXT) { ! t_nextstep *nexthdr = (t_nextstep *)headerbuf; ! if (!strcaseends(filenamebuf,".snd")) strcat(filenamebuf, ".snd"); if (bigendian) strncpy(nexthdr->fileid, bigendian?".snd":"dns.", 4); nexthdr->onset = swap4(sizeof(*nexthdr), swap); *************** *** 485,490 **** long longtmp; static unsigned char dogdoo[] = {0x40, 0x0e, 0xac, 0x44, 0, 0, 0, 0, 0, 0, 'S', 'S', 'N', 'D'}; ! if (strcasecmp(filenamebuf + strlen(filenamebuf)-4, ".aif") && ! strcasecmp(filenamebuf + strlen(filenamebuf)-5, ".aiff")) strcat(filenamebuf, ".aif"); strncpy(aiffhdr->fileid, "FORM", 4); --- 485,490 ---- long longtmp; static unsigned char dogdoo[] = {0x40, 0x0e, 0xac, 0x44, 0, 0, 0, 0, 0, 0, 'S', 'S', 'N', 'D'}; ! t_aiff *aiffhdr = (t_aiff *)headerbuf; ! if (!strcaseends(filenamebuf,".aif") && !strcaseends(filenamebuf,".aiff")) strcat(filenamebuf, ".aif"); strncpy(aiffhdr->fileid, "FORM", 4); *************** *** 502,506 **** memset(aiffhdr->samprate + sizeof(dogdoo) + 4, 0, 8); headersize = AIFFPLUS; - /* fix by matju for häfeli, 2007.07.04, but really, dogdoo should be removed */ while (samplerate >= 0x10000) {aiffhdr->samprate[1]++; samplerate/=2;} --- 502,505 ---- *************** *** 509,513 **** } else { /* WAVE format */ long datasize = nframes * nchannels * bytespersamp; ! if (strcasecmp(filenamebuf + strlen(filenamebuf)-4, ".wav")) strcat(filenamebuf, ".wav"); strncpy(wavehdr->fileid, "RIFF", 4); wavehdr->chunksize = swap4(datasize + sizeof(*wavehdr) - 8, swap); --- 508,513 ---- } else { /* WAVE format */ long datasize = nframes * nchannels * bytespersamp; ! if (!strcaseends(filenamebuf,".wav")) strcat(filenamebuf, ".wav"); ! t_wave *wavehdr = (t_wave *)headerbuf; strncpy(wavehdr->fileid, "RIFF", 4); wavehdr->chunksize = swap4(datasize + sizeof(*wavehdr) - 8, swap); *************** *** 525,529 **** headersize = sizeof(t_wave); } - char *buf2 = canvas_makefilename(canvas, filenamebuf,0,0); sys_bashfilename(buf2,buf2); --- 525,528 ---- *************** *** 581,585 **** float *fp; int bytesperframe = bytespersamp * nchannels; - long xx; for (i = 0, sp = buf; i < nchannels; i++, sp += bytespersamp) { sp2 = sp; fp = vecs[i] + onset; --- 580,583 ---- *************** *** 588,601 **** if (bigendian) { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! int xx = int(32768. + (*fp * ff)) - 0x8000; ! if (xx < -0x7fff) xx = -0x7fff; ! if (xx > +0x7fff) xx = +0x7fff; sp2[0] = xx>>8; sp2[1] = xx; } } else { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! int xx = int(32768. + (*fp * ff)) - 0x8000; ! if (xx < -0x7fff) xx = -0x7fff; ! if (xx > +0x7fff) xx = +0x7fff; sp2[1] = xx>>8; sp2[0] = xx; } --- 586,595 ---- if (bigendian) { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! int xx = clip(int(32768. + (*fp * ff)) - 0x8000,-0x7fff,+0x7fff); sp2[0] = xx>>8; sp2[1] = xx; } } else { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! int xx = clip(int(32768. + (*fp * ff)) - 0x8000,-0x7fff,+0x7fff); sp2[1] = xx>>8; sp2[0] = xx; } *************** *** 605,618 **** if (bigendian) { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! int xx = int(8388608. + (*fp * ff)) - 0x800000; ! if (xx < -0x7fffff) xx = -0x7fffff; ! if (xx > +0x7fffff) xx = +0x7fffff; sp2[0] = xx>>16; sp2[1] = xx>>8; sp2[2] = xx; } } else { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! int xx = int(8388608. + (*fp * ff)) - 0x800000; ! if (xx < -0x7fffff) xx = -0x7fffff; ! if (xx > +0x7fffff) xx = +0x7fffff; sp2[2] = xx>>16; sp2[1] = xx>>8; sp2[0] = xx; } --- 599,608 ---- if (bigendian) { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! int xx = clip(int(8388608. + (*fp * ff)) - 0x800000,-0x7fffff,+0x7fffff); sp2[0] = xx>>16; sp2[1] = xx>>8; sp2[2] = xx; } } else { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! int xx = clip(int(8388608. + (*fp * ff)) - 0x800000,-0x7fffff,+0x7fffff); sp2[2] = xx>>16; sp2[1] = xx>>8; sp2[0] = xx; } *************** *** 621,632 **** if (bigendian) { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! float f2 = *fp * normalfactor; ! xx = *(long *)&f2; sp2[0] = xx >> 24; sp2[1] = xx >> 16; sp2[2] = xx >> 8; sp2[3] = xx; } } else { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! float f2 = *fp * normalfactor; ! xx = *(long *)&f2; sp2[3] = xx >> 24; sp2[2] = xx >> 16; sp2[1] = xx >> 8; sp2[0] = xx; } --- 611,620 ---- if (bigendian) { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! float f2 = *fp * normalfactor; long xx = *(long *)&f2; sp2[0] = xx >> 24; sp2[1] = xx >> 16; sp2[2] = xx >> 8; sp2[3] = xx; } } else { for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) { ! float f2 = *fp * normalfactor; long xx = *(long *)&f2; sp2[3] = xx >> 24; sp2[2] = xx >> 16; sp2[1] = xx >> 8; sp2[0] = xx; } *************** *** 801,805 **** static t_int soundfiler_read_output(t_int * w);
- static void soundfiler_t_read(t_soundfiler *x, t_symbol *s, int argc, t_atom *argv) { int headersize = -1, channels = 0, bytespersamp = 0, bigendian = 0, resize = 0, i, j; --- 789,792 ---- *************** *** 833,838 **** (bytespersamp > 4) || argv[4].a_type != A_SYMBOL || ! ((endianness = argv[4].a_symbol->name[0]) != 'b' ! && endianness != 'l' && endianness != 'n')) goto usage; if (endianness == 'b') bigendian = 1; --- 820,824 ---- (bytespersamp > 4) || argv[4].a_type != A_SYMBOL || ! ((endianness = argv[4].a_symbol->name[0]) != 'b' && endianness != 'l' && endianness != 'n')) goto usage; if (endianness == 'b') bigendian = 1; *************** *** 871,875 **** goto done; } - if (resize) { /* figure out what to resize to */ --- 857,860 ---- *************** *** 911,916 **** goto done; } - /* zero samples */ - if(i > channels) memset(nvecs[i],0,vecsize[i] * sizeof(t_float)); } else --- 896,899 ---- *************** *** 924,930 **** goto done; } - /* zero samples */ - if(i > channels) memset(nvecs[i],0,vecsize[i] * sizeof(t_float)); } if (debug) post("transfer soundfile"); for (itemsread = 0; itemsread < finalsize; ) { --- 907,912 ---- goto done; } } + if(i > channels) memset(nvecs[i],0,vecsize[i] * sizeof(t_float)); if (debug) post("transfer soundfile"); for (itemsread = 0; itemsread < finalsize; ) { *************** *** 1018,1023 **** }
! /* this is broken out from soundfiler_write below so garray_write can ! call it too... not done yet though. */ long soundfiler_t_dowrite(void *obj, t_canvas *canvas, int argc, t_atom *argv) { int bytespersamp, bigendian, swap, filetype, normalize, i, j, nchannels; --- 1000,1004 ---- }
! /* this is broken out from soundfiler_write below so garray_write can call it too... not done yet though. */ long soundfiler_t_dowrite(void *obj, t_canvas *canvas, int argc, t_atom *argv) { int bytespersamp, bigendian, swap, filetype, normalize, i, j, nchannels; *************** *** 1117,1121 **** int n; /* resize of n elements */ char *nvec; /* new array */ - t_garray *x = (t_garray *)pd_findbyclass(argv[0].a_symbol, garray_class); if (!(x)) { --- 1098,1101 ---- *************** *** 1621,1625 **** fd = open_soundfile(dirname, filename, skipheaderbytes, &bytespersample, &bigendian, &sfchannels, &bytelimit, onsetframes); pthread_mutex_lock(&x->mutex); - /* copy back into the instance structure. */ x->bytespersample = bytespersample; --- 1601,1604 ---- *************** *** 1633,1637 **** goto lost; } ! /* check if another request has been made; if so, field it */ if (x->requestcode != REQUEST_BUSY) goto lost; x->fifohead = 0; --- 1612,1616 ---- goto lost; } ! /* check if another request has been made; if so, field it */ if (x->requestcode != REQUEST_BUSY) goto lost; x->fifohead = 0; *************** *** 1643,1657 **** x->sigcountdown = x->sigperiod = x->fifosize / (16 * x->bytespersample * x->sfchannels * x->vecsize); /* in a loop, wait for the fifo to get hungry and feed it */ - while (x->requestcode == REQUEST_BUSY) { int fifosize = x->fifosize; if (x->eof) break; if (x->fifohead >= x->fifotail) { ! /* if the head is >= the tail, we can immediately read ! to the end of the fifo. Unless, that is, we would ! read all the way to the end of the buffer and the ! "tail" is zero; this would fill the buffer completely ! which isn't allowed because you can't tell a completely ! full buffer from an empty one. */ if (x->fifotail || (fifosize - x->fifohead > READSIZE)) { wantbytes = fifosize - x->fifohead; --- 1622,1632 ---- x->sigcountdown = x->sigperiod = x->fifosize / (16 * x->bytespersample * x->sfchannels * x->vecsize); /* in a loop, wait for the fifo to get hungry and feed it */ while (x->requestcode == REQUEST_BUSY) { int fifosize = x->fifosize; if (x->eof) break; if (x->fifohead >= x->fifotail) { ! /* if the head is >= the tail, we can immediately read to the end of the fifo. Unless, that is, we ! would read all the way to the end of the buffer and the "tail" is zero; this would fill the buffer completely ! which isn't allowed because you can't tell a completely full buffer from an empty one. */ if (x->fifotail || (fifosize - x->fifohead > READSIZE)) { wantbytes = fifosize - x->fifohead; *************** *** 1666,1671 **** } } else { ! /* otherwise check if there are at least READSIZE ! bytes to read. If not, wait and loop back. */ wantbytes = x->fifotail - x->fifohead - 1; if (wantbytes < READSIZE) { --- 1641,1645 ---- } } else { ! /* otherwise check if there are at least READSIZE bytes to read. If not, wait and loop back. */ wantbytes = x->fifotail - x->fifohead - 1; if (wantbytes < READSIZE) { *************** *** 1728,1734 ****
static void *readsf_new(t_floatarg fnchannels, t_floatarg fbufsize) { ! t_readsf *x; ! int nchannels = int(fnchannels), bufsize = int(fbufsize), i; ! char *buf; if (nchannels < 1) nchannels = 1; else if (nchannels > MAXSFCHANS) nchannels = MAXSFCHANS; --- 1702,1706 ----
static void *readsf_new(t_floatarg fnchannels, t_floatarg fbufsize) { ! int nchannels = int(fnchannels), bufsize = int(fbufsize); if (nchannels < 1) nchannels = 1; else if (nchannels > MAXSFCHANS) nchannels = MAXSFCHANS; *************** *** 1736,1743 **** else if (bufsize < MINBUFSIZE) bufsize = MINBUFSIZE; else if (bufsize > MAXBUFSIZE) bufsize = MAXBUFSIZE; ! buf = (char *)getbytes(bufsize); if (!buf) return 0; ! x = (t_readsf *)pd_new(readsf_class); ! for (i = 0; i < nchannels; i++) outlet_new(x,gensym("signal")); x->noutlets = nchannels; x->bangout = outlet_new(x,&s_bang); --- 1708,1715 ---- else if (bufsize < MINBUFSIZE) bufsize = MINBUFSIZE; else if (bufsize > MAXBUFSIZE) bufsize = MAXBUFSIZE; ! char *buf = (char *)getbytes(bufsize); if (!buf) return 0; ! t_readsf *x = (t_readsf *)pd_new(readsf_class); ! for (int i=0; i<nchannels; i++) outlet_new(x,gensym("signal")); x->noutlets = nchannels; x->bangout = outlet_new(x,&s_bang); *************** *** 1774,1778 **** } if (x->eof && x->fifohead >= x->fifotail && x->fifohead < x->fifotail + wantbytes-1) { - int xfersize; if (x->fileerror) { pd_error(x, "dsp: %s: %s", x->filename, --- 1746,1749 ---- *************** *** 1782,1786 **** x->state = STATE_IDLE; /* if there's a partial buffer left, copy it out. */ ! xfersize = (x->fifohead - x->fifotail + 1) / (sfchannels * bytespersample); if (xfersize) { soundfile_xferin(sfchannels, noutlets, x->outvec, 0, --- 1753,1757 ---- x->state = STATE_IDLE; /* if there's a partial buffer left, copy it out. */ ! int xfersize = (x->fifohead - x->fifotail + 1) / (sfchannels * bytespersample); if (xfersize) { soundfile_xferin(sfchannels, noutlets, x->outvec, 0,