[PD-cvs] pd/src d_soundfile.c, 1.4.4.11.2.10.2.9, 1.4.4.11.2.10.2.10
Mathieu Bouchard
matju at users.sourceforge.net
Wed Jul 4 22:25:45 CEST 2007
Update of /cvsroot/pure-data/pd/src
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv17992
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.9
retrieving revision 1.4.4.11.2.10.2.10
diff -C2 -d -r1.4.4.11.2.10.2.9 -r1.4.4.11.2.10.2.10
*** d_soundfile.c 3 Jul 2007 04:34:59 -0000 1.4.4.11.2.10.2.9
--- d_soundfile.c 4 Jul 2007 20:25:43 -0000 1.4.4.11.2.10.2.10
***************
*** 40,43 ****
--- 40,45 ----
#endif
+ static bool debug=0;
+
/***************** soundfile header structures ************************/
***************
*** 52,56 ****
struct t_nextstep {
! char ns_fileid[4]; /* magic number '.snd' if file is big-endian */
uint32 onset; /* byte offset of first sample */
uint32 length; /* length of sound in bytes */
--- 54,58 ----
struct t_nextstep {
! char fileid[4]; /* magic number '.snd' if file is big-endian */
uint32 onset; /* byte offset of first sample */
uint32 length; /* length of sound in bytes */
***************
*** 271,276 ****
/* read chunks in loop until we get to the data chunk */
while (strncmp(((t_datachunk *)buf)->id, "SSND", 4)) {
! long chunksize = swap4(((t_datachunk *)buf)->size, swap),
! seekto = headersize + chunksize + 8, seekout;
if (!strncmp(((t_datachunk *)buf)->id, "COMM", 4)) {
long commblockonset = headersize + 8;
--- 273,277 ----
/* read chunks in loop until we get to the data chunk */
while (strncmp(((t_datachunk *)buf)->id, "SSND", 4)) {
! long chunksize = swap4(((t_datachunk *)buf)->size, swap), seekto = headersize + chunksize + 8, seekout;
if (!strncmp(((t_datachunk *)buf)->id, "COMM", 4)) {
long commblockonset = headersize + 8;
***************
*** 318,322 ****
int fd = open_via_path(dirname, filename, "", buf, &bufptr, MAXPDSTRING, 1);
if (fd < 0) return -1;
! return (open_soundfile_via_fd(fd, headersize, p_bytespersamp, p_bigendian, p_nchannels, p_bytelimit, skipframes));
}
--- 319,323 ----
int fd = open_via_path(dirname, filename, "", buf, &bufptr, MAXPDSTRING, 1);
if (fd < 0) return -1;
! return open_soundfile_via_fd(fd, headersize, p_bytespersamp, p_bigendian, p_nchannels, p_bytelimit, skipframes);
}
***************
*** 329,365 ****
int fd = canvas_open(canvas, filename, "", buf, &bufptr, MAXPDSTRING, 1);
if (fd < 0) return -1;
! return (open_soundfile_via_fd(fd, headersize, p_bytespersamp, p_bigendian, p_nchannels, p_bytelimit, skipframes));
}
static void soundfile_xferin(int sfchannels, int nvecs, float **vecs,
long itemsread, unsigned char *buf, int nitems, int bytespersamp, int bigendian) {
- int i, j;
unsigned char *sp, *sp2;
float *fp;
int nchannels = (sfchannels < nvecs ? sfchannels : nvecs);
int bytesperframe = bytespersamp * sfchannels;
! for (i = 0, sp = buf; i < nchannels; i++, sp += bytespersamp) {
if (bytespersamp == 2) {
if (bigendian) {
! for (j = 0, sp2 = sp, fp=vecs[i] + itemsread; j < nitems; j++, sp2 += bytesperframe, fp++)
*fp = SCALE * ((sp2[0] << 24) | (sp2[1] << 16));
} else {
! for (j = 0, sp2 = sp, fp=vecs[i] + itemsread; j < nitems; j++, sp2 += bytesperframe, fp++)
*fp = SCALE * ((sp2[1] << 24) | (sp2[0] << 16));
}
} else if (bytespersamp == 3) {
if (bigendian) {
! for (j = 0, sp2 = sp, fp=vecs[i] + itemsread; j < nitems; j++, sp2 += bytesperframe, fp++)
*fp = SCALE * ((sp2[0] << 24) | (sp2[1] << 16) | (sp2[2] << 8));
} else {
! for (j = 0, sp2 = sp, fp=vecs[i] + itemsread; j < nitems; j++, sp2 += bytesperframe, fp++)
*fp = SCALE * ((sp2[2] << 24) | (sp2[1] << 16) | (sp2[0] << 8));
}
} else if (bytespersamp == 4) {
if (bigendian) {
! for (j = 0, sp2 = sp, fp=vecs[i] + itemsread; j < nitems; j++, sp2 += bytesperframe, fp++)
*(long *)fp = ((sp2[0] << 24) | (sp2[1] << 16) | (sp2[2] << 8) | sp2[3]);
} else {
! for (j = 0, sp2 = sp, fp=vecs[i] + itemsread; j < nitems; j++, sp2 += bytesperframe, fp++)
*(long *)fp = ((sp2[3] << 24) | (sp2[2] << 16) | (sp2[1] << 8) | sp2[0]);
}
--- 330,369 ----
int fd = canvas_open(canvas, filename, "", buf, &bufptr, MAXPDSTRING, 1);
if (fd < 0) return -1;
! return open_soundfile_via_fd(fd, headersize, p_bytespersamp, p_bigendian, p_nchannels, p_bytelimit, skipframes);
}
static void soundfile_xferin(int sfchannels, int nvecs, float **vecs,
long itemsread, unsigned char *buf, int nitems, int bytespersamp, int bigendian) {
unsigned char *sp, *sp2;
float *fp;
int nchannels = (sfchannels < nvecs ? sfchannels : nvecs);
int bytesperframe = bytespersamp * sfchannels;
! sp = buf;
! for (int i=0; i < nchannels; i++, sp += bytespersamp) {
! int j;
! sp2=sp;
! fp=vecs[i] + itemsread;
if (bytespersamp == 2) {
if (bigendian) {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++)
*fp = SCALE * ((sp2[0] << 24) | (sp2[1] << 16));
} else {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++)
*fp = SCALE * ((sp2[1] << 24) | (sp2[0] << 16));
}
} else if (bytespersamp == 3) {
if (bigendian) {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++)
*fp = SCALE * ((sp2[0] << 24) | (sp2[1] << 16) | (sp2[2] << 8));
} else {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++)
*fp = SCALE * ((sp2[2] << 24) | (sp2[1] << 16) | (sp2[0] << 8));
}
} else if (bytespersamp == 4) {
if (bigendian) {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++)
*(long *)fp = ((sp2[0] << 24) | (sp2[1] << 16) | (sp2[2] << 8) | sp2[3]);
} else {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++)
*(long *)fp = ((sp2[3] << 24) | (sp2[2] << 16) | (sp2[1] << 8) | sp2[0]);
}
***************
*** 367,373 ****
}
/* zero out other outputs */
! for (i = sfchannels; i < nvecs; i++)
! for (j = nitems, fp = vecs[i]; j--; )
! *fp++ = 0;
}
--- 371,378 ----
}
/* zero out other outputs */
! for (int i=sfchannels; i < nvecs; i++) {
! fp=vecs[i];
! for (int j=nitems; j--; ) *fp++ = 0;
! }
}
***************
*** 481,485 ****
if (filetype == FORMAT_NEXT) {
if (strcasecmp(filenamebuf + strlen(filenamebuf)-4, ".snd")) strcat(filenamebuf, ".snd");
! if (bigendian) strncpy(nexthdr->ns_fileid, bigendian?".snd":"dns.", 4);
nexthdr->onset = swap4(sizeof(*nexthdr), swap);
nexthdr->length = 0;
--- 486,490 ----
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);
nexthdr->length = 0;
***************
*** 590,597 ****
long xx;
for (i = 0, sp = buf; i < nchannels; i++, sp += bytespersamp) {
if (bytespersamp == 2) {
float ff = normalfactor * 32768.;
if (bigendian) {
! for (j = 0, sp2 = sp, fp = vecs[i] + onset; j < nitems; j++, sp2 += bytesperframe, fp++) {
int xx = int(32768. + (*fp * ff)) - 0x8000;
if (xx < -0x7fff) xx = -0x7fff;
--- 595,603 ----
long xx;
for (i = 0, sp = buf; i < nchannels; i++, sp += bytespersamp) {
+ sp2 = sp; fp = vecs[i] + onset;
if (bytespersamp == 2) {
float ff = normalfactor * 32768.;
if (bigendian) {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) {
int xx = int(32768. + (*fp * ff)) - 0x8000;
if (xx < -0x7fff) xx = -0x7fff;
***************
*** 600,604 ****
}
} else {
! for (j = 0, sp2 = sp, fp=vecs[i] + onset; j < nitems; j++, sp2 += bytesperframe, fp++) {
int xx = int(32768. + (*fp * ff)) - 0x8000;
if (xx < -0x7fff) xx = -0x7fff;
--- 606,610 ----
}
} else {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) {
int xx = int(32768. + (*fp * ff)) - 0x8000;
if (xx < -0x7fff) xx = -0x7fff;
***************
*** 610,614 ****
float ff = normalfactor * 8388608.;
if (bigendian) {
! for (j = 0, sp2 = sp, fp=vecs[i] + onset; j < nitems; j++, sp2 += bytesperframe, fp++) {
int xx = int(8388608. + (*fp * ff)) - 0x800000;
if (xx < -0x7fffff) xx = -0x7fffff;
--- 616,620 ----
float ff = normalfactor * 8388608.;
if (bigendian) {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) {
int xx = int(8388608. + (*fp * ff)) - 0x800000;
if (xx < -0x7fffff) xx = -0x7fffff;
***************
*** 617,621 ****
}
} else {
! for (j = 0, sp2 = sp, fp=vecs[i] + onset; j < nitems; j++, sp2 += bytesperframe, fp++) {
int xx = int(8388608. + (*fp * ff)) - 0x800000;
if (xx < -0x7fffff) xx = -0x7fffff;
--- 623,627 ----
}
} else {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) {
int xx = int(8388608. + (*fp * ff)) - 0x800000;
if (xx < -0x7fffff) xx = -0x7fffff;
***************
*** 626,630 ****
} else if (bytespersamp == 4) {
if (bigendian) {
! for (j = 0, sp2 = sp, fp=vecs[i] + onset; j < nitems; j++, sp2 += bytesperframe, fp++) {
float f2 = *fp * normalfactor;
xx = *(long *)&f2;
--- 632,636 ----
} else if (bytespersamp == 4) {
if (bigendian) {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) {
float f2 = *fp * normalfactor;
xx = *(long *)&f2;
***************
*** 632,636 ****
}
} else {
! for (j = 0, sp2 = sp, fp=vecs[i] + onset; j < nitems; j++, sp2 += bytesperframe, fp++) {
float f2 = *fp * normalfactor;
xx = *(long *)&f2;
--- 638,642 ----
}
} else {
! for (j = 0; j < nitems; j++, sp2 += bytesperframe, fp++) {
float f2 = *fp * normalfactor;
xx = *(long *)&f2;
***************
*** 642,661 ****
}
-
/* ------- soundfiler - reads and writes soundfiles to/from "garrays" ---- */
! #ifndef THREADED_SF
! #define DEFMAXSIZE 4000000 /* default maximum 16 MB per channel */
! #else
! #define DEFMAXSIZE 40000000 /* threaded we should be able to hande a bit more */
! #endif
#define SAMPBUFSIZE 1024
-
static t_class *soundfiler_class;
! struct t_soundfiler : t_object {
! t_canvas *canvas;
! };
#ifdef THREADED_SF
--- 648,659 ----
}
/* ------- soundfiler - reads and writes soundfiles to/from "garrays" ---- */
! #define DEFMAXSIZE (16*1024*1024*4) /* default maximum 16 million floats per channel */
#define SAMPBUFSIZE 1024
static t_class *soundfiler_class;
! struct t_soundfiler : t_object {t_canvas *canvas;};
#ifdef THREADED_SF
***************
*** 684,694 ****
};
! static t_sfqueue * soundfiler_queue;
!
! #ifdef DEBUG
! #define SFDEBUG
! #endif
!
! #define SFDEBUG
/* we fill the queue */
--- 682,686 ----
};
! static t_sfqueue *soundfiler_queue;
/* we fill the queue */
***************
*** 696,702 ****
/* preparing entry */
t_sfprocess * last_entry = (t_sfprocess*)getbytes(sizeof(t_sfprocess));
! #ifdef SFDEBUG
! post("adding process to queue");
! #endif
pthread_mutex_init(&(last_entry->mutex), NULL);
pthread_mutex_lock(&(last_entry->mutex));
--- 688,692 ----
/* preparing entry */
t_sfprocess * last_entry = (t_sfprocess*)getbytes(sizeof(t_sfprocess));
! if (debug) post("adding process to queue");
pthread_mutex_init(&(last_entry->mutex), NULL);
pthread_mutex_lock(&(last_entry->mutex));
***************
*** 719,732 ****
}
if ( soundfiler_queue->begin == soundfiler_queue->end ) {
! #ifdef DEBUG
! post("signaling");
! #endif
pthread_mutex_unlock(&(soundfiler_queue->mutex));
/* and signal the helper thread */
pthread_cond_signal(&(soundfiler_queue->cond));
} else {
! #ifdef DEBUG
! post("not signaling");
! #endif
pthread_mutex_unlock(&(soundfiler_queue->mutex));
}
--- 709,718 ----
}
if ( soundfiler_queue->begin == soundfiler_queue->end ) {
! if (debug) post("signaling");
pthread_mutex_unlock(&(soundfiler_queue->mutex));
/* and signal the helper thread */
pthread_cond_signal(&(soundfiler_queue->cond));
} else {
! if (debug) post("not signaling");
pthread_mutex_unlock(&(soundfiler_queue->mutex));
}
***************
*** 738,752 ****
t_sfprocess *me;
t_sfprocess *next;
! #ifdef DEBUG
! post("soundfiler_thread ID: %d", pthread_self());
! #endif
while (1) {
! #ifdef DEBUG
! post("Soundfiler sleeping");
! #endif
pthread_cond_wait(&soundfiler_queue->cond, &soundfiler_queue->mutex);
! #ifdef DEBUG
! post("Soundfiler awake");
! #endif
/* work on the queue */
while (soundfiler_queue->begin!=NULL) {
--- 724,732 ----
t_sfprocess *me;
t_sfprocess *next;
! if (debug) post("soundfiler_thread ID: %d", pthread_self());
while (1) {
! if (debug) post("Soundfiler sleeping");
pthread_cond_wait(&soundfiler_queue->cond, &soundfiler_queue->mutex);
! if (debug) post("Soundfiler awake");
/* work on the queue */
while (soundfiler_queue->begin!=NULL) {
***************
*** 757,768 ****
pthread_mutex_unlock(&(me->mutex));
pthread_mutex_unlock(&(soundfiler_queue->mutex));
! #ifdef DEBUG
! post("soundfiler: mutex unlocked, running process");
! #endif
/* running the specific function */
me->process(me->x, NULL, me->argc, me->argv);
! #ifdef DEBUG
! post("soundfiler: process done, locking mutex");
! #endif
pthread_mutex_lock(&(soundfiler_queue->mutex));
pthread_mutex_lock(&(me->mutex));
--- 737,744 ----
pthread_mutex_unlock(&(me->mutex));
pthread_mutex_unlock(&(soundfiler_queue->mutex));
! if (debug) post("soundfiler: mutex unlocked, running process");
/* running the specific function */
me->process(me->x, NULL, me->argc, me->argv);
! if (debug) post("soundfiler: process done, locking mutex");
pthread_mutex_lock(&(soundfiler_queue->mutex));
pthread_mutex_lock(&(me->mutex));
***************
*** 931,938 ****
fp = fdopen(fd, "rb");
bufframes = SAMPBUFSIZE / (channels * bytespersamp);
! #ifdef SFDEBUG
! post("buffers: %d", argc);
! post("channels: %d", channels);
! #endif
#if (_POSIX_MEMLOCK - 0) >= 200112L
munlockall();
--- 907,914 ----
fp = fdopen(fd, "rb");
bufframes = SAMPBUFSIZE / (channels * bytespersamp);
! if (debug) {
! post("buffers: %d", argc);
! post("channels: %d", channels);
! }
#if (_POSIX_MEMLOCK - 0) >= 200112L
munlockall();
***************
*** 965,971 ****
if(i > channels) memset(nvecs[i],0,vecsize[i] * sizeof(t_float));
}
! #ifdef SFDEBUG
! post("transfer soundfile");
! #endif
for (itemsread = 0; itemsread < finalsize; ) {
int thisread = finalsize - itemsread;
--- 941,945 ----
if(i > channels) memset(nvecs[i],0,vecsize[i] * sizeof(t_float));
}
! if (debug) post("transfer soundfile");
for (itemsread = 0; itemsread < finalsize; ) {
int thisread = finalsize - itemsread;
***************
*** 977,983 ****
itemsread += nitems;
}
! #ifdef SFDEBUG
! post("zeroing remaining elements");
! #endif
/* zero out remaining elements of vectors */
for (i = 0; i < argc; i++) {
--- 951,955 ----
itemsread += nitems;
}
! if (debug) post("zeroing remaining elements");
/* zero out remaining elements of vectors */
for (i = 0; i < argc; i++) {
***************
*** 985,991 ****
}
/* set idle callback to switch pointers */
! #ifdef SFDEBUG
! post("locked");
! #endif
for (i = 0; i < argc; i++) {
t_int* w = (t_int*)getbytes(4*sizeof(t_int));
--- 957,961 ----
}
/* set idle callback to switch pointers */
! if (debug) post("locked");
for (i = 0; i < argc; i++) {
t_int* w = (t_int*)getbytes(4*sizeof(t_int));
***************
*** 997,1003 ****
pthread_cond_wait(&resume_after_callback, &resume_after_callback_mutex);
}
! #ifdef SFDEBUG
! post("unlocked, doing graphics updates");
! #endif
/* do all graphics updates. run this in the main thread via callback */
for (i = 0; i < argc; i++) {
--- 967,971 ----
pthread_cond_wait(&resume_after_callback, &resume_after_callback_mutex);
}
! if (debug) post("unlocked, doing graphics updates");
/* do all graphics updates. run this in the main thread via callback */
for (i = 0; i < argc; i++) {
***************
*** 1034,1038 ****
t_int nvec = w[1];
t_int finalsize = w[2];
! pthread_cond_t * conditional = (pthread_cond_t*) w[3];
garray->array.vec = (char *) nvec;
garray->array.n = finalsize;
--- 1002,1006 ----
t_int nvec = w[1];
t_int finalsize = w[2];
! pthread_cond_t *conditional = (pthread_cond_t*) w[3];
garray->array.vec = (char *) nvec;
garray->array.n = finalsize;
***************
*** 1048,1054 ****
int n = w[1];
/* if this is the only array in the graph, reset the graph's coordinates */
! #ifdef SFDEBUG
! post("redraw array %p", garray);
! #endif
gl = garray->canvas;
if (gl->list == garray && !garray->next) {
--- 1016,1020 ----
int n = w[1];
/* if this is the only array in the graph, reset the graph's coordinates */
! if (debug) post("redraw array %p", garray);
gl = garray->canvas;
if (gl->list == garray && !garray->next) {
***************
*** 1062,1068 ****
t_outlet* outlet = (t_outlet*) w[0];
float itemsread = (float) w[1];
! #ifdef SFDEBUG
! post("bang %p", outlet);
! #endif
outlet_float (outlet, itemsread);
return 0;
--- 1028,1032 ----
t_outlet* outlet = (t_outlet*) w[0];
float itemsread = (float) w[1];
! if (debug) post("bang %p", outlet);
outlet_float (outlet, itemsread);
return 0;
***************
*** 1168,1177 ****
static void soundfiler_t_resize(t_soundfiler *y, t_symbol *s, int argc, t_atom *argv) {
int was, elemsize; /* array contains was elements of size elemsize */
! t_float * vec; /* old array */
t_canvas *gl;
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)) {
pd_error(y, "%s: no such table", argv[0].a_symbol->s_name);
--- 1132,1141 ----
static void soundfiler_t_resize(t_soundfiler *y, t_symbol *s, int argc, t_atom *argv) {
int was, elemsize; /* array contains was elements of size elemsize */
! t_float *vec; /* old array */
t_canvas *gl;
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)) {
pd_error(y, "%s: no such table", argv[0].a_symbol->s_name);
***************
*** 1237,1241 ****
static void soundfiler_t_const(t_soundfiler *y, t_symbol *s, int argc, t_atom *argv) {
int size, elemsize; /* array contains was elements of size elemsize */
! t_float * vec; /* old array */
t_canvas *gl;
int val; /* value */
--- 1201,1205 ----
static void soundfiler_t_const(t_soundfiler *y, t_symbol *s, int argc, t_atom *argv) {
int size, elemsize; /* array contains was elements of size elemsize */
! t_float *vec; /* old array */
t_canvas *gl;
int val; /* value */
***************
*** 1299,1303 ****
post("warning: threaded soundfiler is not synchronous");
#endif /* THREADED_SF */
! return (x);
}
--- 1263,1267 ----
post("warning: threaded soundfiler is not synchronous");
#endif /* THREADED_SF */
! return x;
}
***************
*** 1801,1805 ****
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"));
--- 1765,1769 ----
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"));
More information about the Pd-cvs
mailing list