Update of /cvsroot/pure-data/externals/tb/sndfiler/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv2331/src
Modified Files: sndfiler.c Log Message: new flags: -resize and -skip
Index: sndfiler.c =================================================================== RCS file: /cvsroot/pure-data/externals/tb/sndfiler/src/sndfiler.c,v retrieving revision 1.1 retrieving revision 1.2 diff -C2 -d -r1.1 -r1.2 *** sndfiler.c 20 Nov 2005 21:54:41 -0000 1.1 --- sndfiler.c 28 Nov 2005 22:27:20 -0000 1.2 *************** *** 237,240 **** --- 237,242 ---- int channel_count; t_float** helper_arrays; + int resize = 0; + int seek = 0, arraysize = 0, writesize;
t_symbol* file; *************** *** 243,252 **** SNDFILE* sndfile; SF_INFO info; ! ! if (argc < 2) { ! pd_error(x, "usage: read soundfile array1 array2 ..."); ! return; }
file = atom_getsymbolarg(0, argc, argv); --- 245,271 ---- SNDFILE* sndfile; SF_INFO info; ! ! // parse flags ! while (argc > 0 && argv->a_type == A_SYMBOL && ! *argv->a_w.w_symbol->s_name == '-') { ! char *flag = argv->a_w.w_symbol->s_name + 1; ! if (!strcmp(flag, "resize")) ! { ! resize = 1; ! argc -= 1; argv += 1; ! } ! else if (!strcmp(flag, "skip")) ! { ! if (argc < 2 || argv[1].a_type != A_FLOAT || ! ((seek = argv[1].a_w.w_float) == 0)) ! goto usage; ! argc -= 2; argv += 2; ! } ! else goto usage; } + + if (argc < 2) + goto usage;
file = atom_getsymbolarg(0, argc, argv); *************** *** 258,279 **** for (i = 0; i != channel_count; ++i) { ! t_garray * array = (t_garray *)pd_findbyclass(atom_getsymbolarg(i+1, ! argc, argv), garray_class); ! ! if (array) ! arrays[i] = array; ! else ! { ! pd_error(x, "%s: no such array", atom_getsymbolarg(i+1, argc, argv)->s_name); ! return; ! } }
- post("sndfiler: loading file ..."); - sndfile = sf_open(file->s_name, SFM_READ, &info);
if (sndfile) { int maxchannels = (channel_count < info.channels) ? channel_count : info.channels; --- 277,314 ---- for (i = 0; i != channel_count; ++i) { ! t_float *dummy; ! int size; ! t_garray *array; ! ! if(!(array = (t_garray *)pd_findbyclass( ! atom_getsymbolarg(i+1, argc, argv), garray_class))) ! { ! pd_error(x, "%s: no such array", atom_getsymbolarg(i+1, ! argc, argv)->s_name); ! return; ! } ! ! if(garray_getfloatarray(array, &size, &dummy)) ! arrays[i] = array; ! else ! { ! pd_error(x, "%s: bad template for sndfiler", atom_getsymbolarg(i+1, ! argc, argv)->s_name); ! return; ! } ! ! // in multichannel mode: check if arrays have different length ! if (arraysize && arraysize != size && !resize) ! { ! post("sndfiler: arrays have different lengths, resizing to last one ..."); ! } ! arraysize = size; }
sndfile = sf_open(file->s_name, SFM_READ, &info);
if (sndfile) { + int pos = 0; int maxchannels = (channel_count < info.channels) ? channel_count : info.channels; *************** *** 282,286 **** --- 317,347 ----
t_int ** syncdata = getbytes(sizeof(t_int*) * 5); + + // negative seek: offset from the end of the file + if(seek<0) + { + pos = sf_seek(sndfile, seek, SEEK_END); + } + if(seek>0) + { + pos = sf_seek(sndfile, seek, SEEK_SET); + } + if(pos == -1) + { + pd_error(x, "invalid seek in soundfile"); + return; + } + + if(resize) + { + writesize = (info.frames-pos); + arraysize = writesize; + } + else + writesize = (arraysize>(info.frames-pos)) ? + info.frames-pos : arraysize;
+ post("sndfiler: loading file ..."); + #if (_POSIX_MEMLOCK - 0) >= 200112L munlockall(); *************** *** 289,296 **** for (i = 0; i != channel_count; ++i) { ! helper_arrays[i] = getalignedbytes(info.frames * sizeof(t_float)); }
! for (i = 0; i != info.frames; ++i) { sf_read_float(sndfile, item, info.channels); --- 350,357 ---- for (i = 0; i != channel_count; ++i) { ! helper_arrays[i] = getalignedbytes(arraysize * sizeof(t_float)); }
! for (i = 0; i != writesize; ++i) { sf_read_float(sndfile, item, info.channels); *************** *** 304,307 **** --- 365,383 ---- } } + + // fill remaining elements with zero + if(!resize && (arraysize>(info.frames-pos))) + { + for (i = writesize; i != arraysize; ++i) + { + for (j = 0; j != info.channels; ++j) + { + if (j < channel_count) + { + helper_arrays[j][i] = 0; + } + } + } + }
#if (_POSIX_MEMLOCK - 0) >= 200112L *************** *** 314,324 **** syncdata[1] = (t_int*)helper_arrays; syncdata[2] = (t_int*)channel_count; ! syncdata[3] = (t_int*)(long)info.frames; syncdata[4] = (t_int*)x;
sys_callback(sndfiler_synchonize, (t_int*)syncdata, 5); } else pd_error(x, "Error opening file"); }
--- 390,408 ---- syncdata[1] = (t_int*)helper_arrays; syncdata[2] = (t_int*)channel_count; ! syncdata[3] = (t_int*)arraysize; syncdata[4] = (t_int*)x;
sys_callback(sndfiler_synchonize, (t_int*)syncdata, 5); + return; } else + { pd_error(x, "Error opening file"); + return; + } + + usage: + pd_error(x, "usage: read [flags] filename array1 array2 ..."); + post("flags: -skip <n> -resize "); }