static void blackboard_image(t_blackboard *x, t_symbol *s, int argc, t_atom *argv) { int fd; char path[MAXPDSTRING]; char name[MAXPDSTRING]; char *nameptr; if(x->f_ninstructions >= _blackboard::maxcmd) { pd_error(x, "%s too many drawing commands.", eobj_getclassname(x)->s_name); return; } if(argc > 2 && argv) { if(atom_gettype(argv) == A_FLOAT && atom_gettype(argv+1) == A_FLOAT && atom_gettype(argv+2) == A_SYMBOL) { sprintf(path, "%s",atom_getsymbol(argv+2)->s_name); if(access(path, O_RDONLY) != -1) { sprintf(x->f_instructions[x->f_ninstructions], "create image %d %d -anchor nw -image [image create photo -file %s]", (int)atom_getfloat(argv), (int)atom_getfloat(argv+1), path); x->f_ninstructions++; ebox_redraw((t_ebox *)x); return; } sprintf(path, "%s/%s", canvas_getdir(x->j_box.b_obj.o_canvas)->s_name, atom_getsymbol(argv+2)->s_name); if(access(path, O_RDONLY) != -1) { sprintf(x->f_instructions[x->f_ninstructions], "create image %d %d -anchor nw -image [image create photo -file %s]", (int)atom_getfloat(argv), (int)atom_getfloat(argv+1), path); x->f_ninstructions++; ebox_redraw((t_ebox *)x); return; } sprintf(name, "%s", atom_getsymbol(argv+2)->s_name); if(!strncmp(name+strlen(name)-4, ".gif", 4)) { strncpy(name+strlen(name)-4, "\0", 4); } fd = open_via_path(canvas_getdir(x->j_box.b_obj.o_canvas)->s_name, name, ".gif", path, &nameptr, MAXPDSTRING, 0); if(fd >= 0) { sprintf(x->f_instructions[x->f_ninstructions], "create image %d %d -anchor nw -image [image create photo -file %s/%s.gif]", (int)atom_getfloat(argv), (int)atom_getfloat(argv+1), path, name); x->f_ninstructions++; ebox_redraw((t_ebox *)x); return; } } } }
void eobj_write(t_eobj* x, t_symbol* s, int argc, t_atom *argv) { char buf[MAXPDSTRING]; char* pch; t_atom av[1]; t_eclass* c = eobj_getclass(x); // The file name is defined if(argc && argv && atom_gettype(argv) == A_SYMBOL) { pch = strpbrk(atom_getsymbol(argv)->s_name, "/\""); // The folder seems defined if(pch != NULL) { atom_setsym(av, atom_getsymbol(argv)); if(c->c_widget.w_write) c->c_widget.w_write(x, s, 1, av); return; } // The folder isn't defined so write it in the canvas folder else { sprintf(buf, "%s/%s", canvas_getdir(x->o_canvas)->s_name, atom_getsymbol(argv)->s_name); atom_setsym(av, gensym(buf)); if(c->c_widget.w_write) c->c_widget.w_write(x, s, 1, av); return; } } // The file name is not defined so we popup a window else { sys_vgui("eobj_saveas %s nothing nothing\n", x->o_id->s_name); } }
static void mtr_write(t_mtr *x, t_symbol *s) { if (s && s != &s_) mtr_dowrite(x, 0, s); else /* CHECKED no default */ hammerpanel_save(x->x_filehandle, canvas_getdir(x->x_glist), 0); }
FILE *fileread_open(char *filename, t_canvas *cv, int textmode) { int fd; char path[MAXPDSTRING+2], *nameptr; t_symbol *dirsym = (cv ? canvas_getdir(cv) : 0); /* path arg is returned unbashed (system-independent) */ if ((fd = open_via_path((dirsym ? dirsym->s_name : ""), filename, "", path, &nameptr, MAXPDSTRING, 1)) < 0) return (0); /* Closing/reopening dance. This is unnecessary under linux, and we could have tried to convert fd to fp, but under windows open_via_path() returns what seems to be an invalid fd. LATER try to understand what is going on here... */ close(fd); if (path != nameptr) { char *slashpos = path + strlen(path); *slashpos++ = '/'; /* try not to be dependent on current open_via_path() implementation */ if (nameptr != slashpos) strcpy(slashpos, nameptr); } sys_bashfilename(path, path); return (fopen(path, (textmode ? "r" : "rb"))); }
static void plustot_env_evalfile(t_plustot_env *x, t_symbol *fname) { char buf1[MAXPDSTRING], buf2[MAXPDSTRING], *nameptr, *dir; int fd; dir = canvas_getdir(x->x_glist)->s_name; if ((fd = open_via_path(dir, fname->s_name, "", buf1, &nameptr, MAXPDSTRING, 0)) < 0) { loud_error((t_pd *)x, "file '%s' not found", fname->s_name); } else { Tcl_Interp *interp = plustin_getinterp(x->x_tin); FILE *fp; close(fd); strcpy(buf2, buf1); strcat(buf2, "/"); strcat(buf2, nameptr); sys_bashfilename(buf2, buf2); Tcl_Preserve(interp); if (Tcl_EvalFile(interp, buf2) != TCL_OK) { strcpy(buf1, "evaluation failed ("); strncat(buf1, buf2, MAXPDSTRING - strlen(buf1) - 2); strcat(buf1, ")"); plusloud_tclerror((t_pd *)x, interp, buf1); } Tcl_Release(interp); } }
static void normalize_path(t_folder_list* x, char *normalized, const char *original) { char buf[FILENAME_MAX]; t_symbol *cwd = canvas_getdir(x->x_canvas); #ifdef _WIN32 sys_unbashfilename(original, buf); #else strncpy(buf, original, FILENAME_MAX); #endif if(sys_isabsolutepath(buf)) { strncpy(normalized, buf, FILENAME_MAX); return; } strncpy(normalized, cwd->s_name, FILENAME_MAX); if(normalized[(strlen(normalized)-1)] != '/') { strncat(normalized, "/", 1); } if(buf[0] == '.') { if(buf[1] == '/') { strncat(normalized, buf + 2, FILENAME_MAX - strlen(normalized)); } else if(buf[1] == '.' && buf[2] == '/') { strncat(normalized, buf, FILENAME_MAX - strlen(normalized)); } } else if(buf[0] != '/') { strncat(normalized, buf, FILENAME_MAX - strlen(normalized)); } else { strncpy(normalized, buf, FILENAME_MAX); } }
static void mtrack_write(t_mtrack *tp, t_symbol *s) { if (s && s != &s_) mtr_dowrite(tp->tr_owner, tp, s); else /* CHECKED no default */ hammerpanel_save(tp->tr_filehandle, canvas_getdir(tp->tr_owner->x_glist), 0); }
/* CHECKED symbol arg not allowed -- a bug? but CHECKME other classes (cf seq's filetype dilemma) */ static void funbuff_write(t_funbuff *x, t_symbol *s) { if (s && s != &s_) funbuff_dowrite(x, s); else /* CHECKME default name */ hammerpanel_save(x->x_filehandle, canvas_getdir(x->x_canvas), x->x_defname); }
static const char* imagebang_get_filename(t_imagebang *x,char *file) { static char fname[MAXPDSTRING]; char *bufptr; int fd; fd=open_via_path(canvas_getdir(glist_getcanvas(x->glist))->s_name, file, "",fname, &bufptr, MAXPDSTRING, 1); if(fd>0){ fname[strlen(fname)]='/'; DEBUG(post("image file: %s",fname);)
static void xeq_mfread(t_xeq *x, t_symbol *s, int ac, t_atom *av) { t_symbol *filename, *tts = &s_; if (!ac || av->a_type != A_SYMBOL) return; filename = av->a_w.w_symbol; if (ac > 1 && !(tts = squtt_makesymbol(av + 1))) return; if (mfbb_read(x->x_binbuf, filename->s_name, canvas_getdir(x->x_canvas)->s_name, tts)) error("%s: read failed", filename->s_name); xeq_rewind(x); hyphen_forallfriends((t_hyphen *)x, xeqhook_multicast_rewind, 0); }
void flext_obj::GetCanvasDir(char *buf,size_t bufsz) const { #if FLEXT_SYS == FLEXT_SYS_PD const char *c = GetString(canvas_getdir(thisCanvas())); strncpy(buf,c,bufsz); #elif FLEXT_SYS == FLEXT_SYS_MAX short path = patcher_myvol(thisCanvas()); // \TODO dangerous!! no check for path length (got to be long enough... like 1024 chars) path_topathname(path,NULL,buf); #else #error Not implemented #endif }
const char *image_get_filename(t_image *x,char *file) { static char fname[MAXPDSTRING]; char *bufptr; int fd; fd=open_via_path(canvas_getdir(glist_getcanvas(x->x_glist))->s_name, file, "",fname, &bufptr, MAXPDSTRING, 1); if(fd>0) { fname[strlen(fname)]='/'; close(fd); return fname; } else return 0; }
static void freadln_open (t_freadln *x, t_symbol *s, t_symbol*type) { char filenamebuf[MAXPDSTRING], *filenamebufptr; char*dirname=canvas_getdir(x->x_canvas)->s_name; int fd, len; freadln_close(x); /* if(type!=gensym("cr")) { pd_error(x, "currently only 'cr' type files are implemented!"); return; } */ if (type==gensym("cr")) strcpy(x->linebreak_chr,"\n"); else strcpy(x->linebreak_chr,";\n"); /* directory, filename, extension, dirresult, nameresult, unsigned int size, int bin */ if ((fd=open_via_path(dirname, s->s_name,"", filenamebuf, &filenamebufptr, MAXPDSTRING,0)) < 0 ) { pd_error(x, "%s: failed to open %s", s->s_name, filenamebuf); return; } close(fd); len=strlen(filenamebuf); if (!(x->x_filename=(char*)getbytes(sizeof(char)*(len+strlen(s->s_name)+2)))) { pd_error(x, "out of memory"); freadln_close(x); return; } strcpy(x->x_filename,filenamebuf); strcpy(x->x_filename+len,"/"); strcpy(x->x_filename+len+1,s->s_name); if (!(x->x_file=fopen(x->x_filename, "r"))) { pd_error(x, "freadln: failed to open %128s",filenamebuf); return; } if (!(x->x_textbuf = (char *) getbytes (MIN_FREADLN_LENGTH * sizeof(char)))) { pd_error(x, "out of memory"); freadln_close(x); return; } x->x_textbuf_length=MIN_FREADLN_LENGTH; }
static void helplink_doclick(t_helplink *x) { char* objectname = x->x_ulink->s_name; char dirbuf[MAXPDSTRING], *nameptr; int fd = canvas_open(x->x_glist, objectname, "-help.pd", dirbuf, &nameptr, MAXPDSTRING, 0); if (fd < 0) { /* if canvas_open() failed try open_via_helppath() */ open_via_helppath(objectname, canvas_getdir(x->x_glist)->s_name); } else { /* if canvas_open() gave us a filehandle, then we have a helppatch to * open in dirbuf and nameptr, but we don't need the filehandle */ close(fd); glob_evalfile(NULL, gensym(nameptr), gensym(dirbuf)); } }
static void sdiflists_types(t_sdiflists *x, t_symbol *types) { if(x->markers[0].filepos != -1 ) { t_colout *u; int j; SdifFClose(x->file); for(j=0; j < x->nframes; j++) x->markers[j].timetag = x->markers[j].filepos = 0; x->markers[0].timetag = -1; for ( j=0, u = x->data; j < x->n_outs; u++, j++) freebytes( u->outvec, x->max_vec * sizeof(t_atom)); #if DEBUG post("sdiflists::types: closed previous file"); #endif x->nframes = IFRAMES; } //prepend the full path onto the filename char fullfilename[MAXPDSTRING], namebuf[MAXPDSTRING]; char buf[MAXPDSTRING], *bufptr, *dirname; int fd = 0; dirname=canvas_getdir(x->canvas)->s_name; t_binbuf *bbuf = binbuf_new(); fd = open_via_path(dirname, types->s_name,"", buf, &bufptr, MAXPDSTRING, 0); if(fd > 0) { namebuf[0] = 0; if (*buf) strcat(namebuf, buf), strcat(namebuf, "/"); strcat(namebuf, bufptr); sys_bashfilename(namebuf, fullfilename); SdifGenKill (); // have to close it to read a new .STYP SdifGenInitCond (types->s_name); post("sdiflists: types-declaration file: %s", types->s_name); } else post ("sdiflists: types file %s could not be opened", types->s_name); }
static void matrix_read(t_matrix *x, t_symbol *filename) { t_binbuf *bbuf = binbuf_new(); t_atom *ap; int n; if (binbuf_read_via_path(bbuf, filename->s_name, canvas_getdir(x->x_canvas)->s_name, 0)) pd_error(x,"matrix: failed to read %128s", filename->s_name); ap=binbuf_getvec(bbuf); n =binbuf_getnatom(bbuf)-1; if ((ap->a_type == A_SYMBOL) && (!strcmp(ap->a_w.w_symbol->s_name,"matrix") || !strcmp(ap->a_w.w_symbol->s_name,"#matrix")) ){ matrix_matrix2(x, gensym("matrix"), n, ap+1); } binbuf_free(bbuf); }
void hammerpanel_setopendir(t_hammerfile *f, t_symbol *dir) { if (f->f_currentdir && f->f_currentdir != &s_) { if (dir && dir != &s_) { int length; if (length = ospath_length(dir->s_name, f->f_currentdir->s_name)) { char *path = getbytes(length + 1); if (ospath_absolute(dir->s_name, f->f_currentdir->s_name, path)) /* LATER stat (think how to report a failure) */ f->f_currentdir = gensym(path); freebytes(path, length + 1); } } else if (f->f_canvas) f->f_currentdir = canvas_getdir(f->f_canvas); } else bug("hammerpanel_setopendir"); }
static void patcherinfos_output(t_patcherinfos *x) { t_atom av[4]; if(x->f_canvas) { outlet_symbol(x->f_out_name, x->f_canvas->gl_name); outlet_symbol(x->f_out_path, canvas_getdir(x->f_canvas)); atom_setfloat(av, x->f_canvas->gl_screenx1); atom_setfloat(av+1, x->f_canvas->gl_screeny1); atom_setfloat(av+2, x->f_canvas->gl_screenx2); atom_setfloat(av+3, x->f_canvas->gl_screeny2); outlet_anything(x->f_out_coords, gensym("windowsize"), 4, av); atom_setfloat(av, x->f_canvas->gl_xmargin); atom_setfloat(av+1, x->f_canvas->gl_ymargin); atom_setfloat(av+2, x->f_canvas->gl_pixwidth); atom_setfloat(av+3, x->f_canvas->gl_pixheight); outlet_anything(x->f_out_coords, gensym("canvassize"), 4, av); } }
std::string CPPExtern::findFile(const std::string f, const std::string e) const { char buf[MAXPDSTRING], buf2[MAXPDSTRING]; char*bufptr=0; std::string result=""; int fd=-1; t_canvas*canvas=const_cast<t_canvas*>(getCanvas()); char*filename=const_cast<char*>(f.c_str()); char*ext=const_cast<char*>(e.c_str()); if ((fd=open_via_path(canvas_getdir(canvas)->s_name, filename, ext, buf2, &bufptr, MAXPDSTRING, 1))>=0) { sys_close(fd); result=buf2; result+="/"; result+=bufptr; } else { canvas_makefilename(canvas, filename, buf, MAXPDSTRING); result=buf; } return result; }
static void xeq_read(t_xeq *x, t_symbol *s, int ac, t_atom *av) { char *format, *filename; int fid = 0; if (!ac || av->a_type != A_SYMBOL) return; format = av->a_w.w_symbol->s_name; if (!strcmp(format, "cr")) fid = 1; else if (!strcmp(format, "mf")) fid = 2; if (fid && (!--ac || (++av)->a_type != A_SYMBOL)) return; filename = av->a_w.w_symbol->s_name; if (fid == 2) xeq_mfread(x, s, ac, av); else { if (binbuf_read_via_path(x->x_binbuf, filename, canvas_getdir(x->x_canvas)->s_name, fid)) error("%s: read failed", filename); xeq_rewind(x); hyphen_forallfriends((t_hyphen *)x, xeqhook_multicast_rewind, 0); } }
void eobj_read(t_eobj* x, t_symbol* s, int argc, t_atom *argv) { char buf[MAXPDSTRING]; char* pch; t_atom av[1]; t_namelist* var; t_eclass* c = eobj_getclass(x); // Name if(argc && argv && atom_gettype(argv) == A_SYMBOL) { // Valid path if((access(atom_getsymbol(argv)->s_name, O_RDONLY) != -1)) { if(c->c_widget.w_read) c->c_widget.w_read(x, s, 1, argv); } // Invalid path or no path else { // Wrong path but we don't care pch = strpbrk(atom_getsymbol(argv)->s_name, "/\""); if(pch != NULL) { if(c->c_widget.w_read) c->c_widget.w_read(x, s, 1, argv); } else { // Look in the canvas folder sprintf(buf, "%s/%s", canvas_getdir(x->o_canvas)->s_name, atom_getsymbol(argv)->s_name); if((access(buf, O_RDONLY) != -1)) { atom_setsym(av, gensym(buf)); if(c->c_widget.w_read) c->c_widget.w_read(x, s, 1, av); return; } // Look in the search path var = sys_searchpath; while (var) { sprintf(buf, "%s/%s", var->nl_string, atom_getsymbol(argv)->s_name); if((access(buf, O_RDONLY) != -1)) { atom_setsym(av, gensym(buf)); if(c->c_widget.w_read) c->c_widget.w_read(x, s, 1, av); return; } var = var->nl_next; } // Nothing work but we don't care if(c->c_widget.w_read) c->c_widget.w_read(x, s, 1, av); return; } } } // No name so we popup a window else { sys_vgui("eobj_openfrom %s\n", x->o_id->s_name); } }
static void *readsfx_child_main(void *zz) { t_readsfx *x = zz; void * reader_obj = sfxreader_new(); #ifdef DEBUG_SOUNDFILE pute("1\n"); #endif pthread_mutex_lock(&x->x_mutex); int fd, fifohead; char *buf; while (1) { #ifdef DEBUG_SOUNDFILE pute("0\n"); #endif if (x->x_requestcode == REQUEST_NOTHING) { #ifdef DEBUG_SOUNDFILE pute("wait 2\n"); #endif sfread_cond_signal(&x->x_answercondition); sfread_cond_wait(&x->x_requestcondition, &x->x_mutex); #ifdef DEBUG_SOUNDFILE pute("3\n"); #endif } else if (x->x_requestcode == REQUEST_OPEN) { #ifdef DEBUG_SOUNDFILE char boo[80]; #endif int sysrtn, wantbytes; //---------START OPEN SOUNDFILE------------------- /* copy file stuff out of the data structure so we can relinquish the mutex while we're in open_soundfile(). */ long onsetframes = x->x_onsetframes; long bytelimit = 0x7fffffff; int bytespersample = x->x_bytespersample; int sfchannels = x->x_sfchannels; int bigendian = x->x_bigendian; //void* reader_obj = x->x_reader_obj; char *filename = x->x_filename; char *dirname = canvas_getdir(x->x_canvas)->s_name; #ifdef DEBUG_SOUNDFILE pute("4\n"); #endif /* alter the request code so that an ensuing "open" will get noticed. */ x->x_requestcode = REQUEST_BUSY; x->x_fileerror = 0; /* if there's already a file open, close it */ if (x->x_fd >= 0) { fd = x->x_fd; pthread_mutex_unlock(&x->x_mutex); sfxreader_close(reader_obj); pthread_mutex_lock(&x->x_mutex); x->x_fd = -1; if (x->x_requestcode != REQUEST_BUSY) goto lost; } /* open the soundfile with the mutex unlocked */ pthread_mutex_unlock(&x->x_mutex); #ifdef DEBUG_SOUNDFILE pute("Pre-open\n"); #endif fd = sfxreader_open(reader_obj, dirname, filename, &bytespersample, &bigendian, &sfchannels, &bytelimit, onsetframes); pthread_mutex_lock(&x->x_mutex); #ifdef DEBUG_SOUNDFILE pute("5\n"); #endif /* copy back into the instance structure. */ x->x_bytespersample = bytespersample; x->x_sfchannels = sfchannels; x->x_bigendian = bigendian; x->x_fd = fd; x->x_bytelimit = bytelimit; if (fd < 0) { x->x_fileerror = errno; x->x_eof = 1; #ifdef DEBUG_SOUNDFILE pute("open failed\n"); pute(filename); pute(dirname); #endif goto lost; } sfxreader_info(dirname, filename, &(x->x_data)); //---------END OPEN SOUNDFILE------------------- /* check if another request has been made; if so, field it */ if (x->x_requestcode != REQUEST_BUSY) goto lost; #ifdef DEBUG_SOUNDFILE pute("6\n"); #endif x->x_fifohead = 0; /* set fifosize from bufsize. fifosize must be a multiple of the number of bytes eaten for each DSP tick. We pessimistically assume MAXVECSIZE samples per tick since that could change. There could be a problem here if the vector size increases while a soundfile is being played... */ x->x_fifosize = x->x_bufsize - (x->x_bufsize % (x->x_bytespersample * x->x_sfchannels * MAXVECSIZE)); /* arrange for the "request" condition to be signalled 16 times per buffer */ #ifdef DEBUG_SOUNDFILE sprintf(boo, "fifosize %d\n", x->x_fifosize); pute(boo); #endif x->x_sigcountdown = x->x_sigperiod = (x->x_fifosize / (16 * x->x_bytespersample * x->x_sfchannels * x->x_vecsize)); /* in a loop, wait for the fifo to get hungry and feed it */ while (x->x_requestcode == REQUEST_BUSY) { int fifosize = x->x_fifosize; #ifdef DEBUG_SOUNDFILE pute("77\n"); #endif if (x->x_eof) break; if (x->x_fifohead >= x->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->x_fifotail || (fifosize - x->x_fifohead > READSIZE)) { wantbytes = fifosize - x->x_fifohead; if (wantbytes > READSIZE) wantbytes = READSIZE; if (wantbytes > x->x_bytelimit) wantbytes = (int)x->x_bytelimit; #ifdef DEBUG_SOUNDFILE sprintf(boo, "head %d, tail %d, size %d\n", x->x_fifohead, x->x_fifotail, wantbytes); pute(boo); #endif } else { #ifdef DEBUG_SOUNDFILE pute("wait 7a ...\n"); #endif sfread_cond_signal(&x->x_answercondition); #ifdef DEBUG_SOUNDFILE pute("signalled\n"); #endif sfread_cond_wait(&x->x_requestcondition, &x->x_mutex); #ifdef DEBUG_SOUNDFILE pute("7a done\n"); #endif continue; } } else { /* otherwise check if there are at least READSIZE bytes to read. If not, wait and loop back. */ wantbytes = x->x_fifotail - x->x_fifohead - 1; if (wantbytes < READSIZE) { #ifdef DEBUG_SOUNDFILE pute("wait 7...\n"); #endif sfread_cond_signal(&x->x_answercondition); sfread_cond_wait(&x->x_requestcondition, &x->x_mutex); #ifdef DEBUG_SOUNDFILE pute("7 done\n"); #endif continue; } else wantbytes = READSIZE; if (wantbytes > x->x_bytelimit) wantbytes = (int)x->x_bytelimit; } #ifdef DEBUG_SOUNDFILE pute("8\n"); #endif fd = x->x_fd; buf = x->x_buf; fifohead = x->x_fifohead; pthread_mutex_unlock(&x->x_mutex); //Read from our file reader sysrtn = sfxreader_read(reader_obj, buf + fifohead, wantbytes); pthread_mutex_lock(&x->x_mutex); if (x->x_requestcode != REQUEST_BUSY) break; if (sysrtn < 0) { #ifdef DEBUG_SOUNDFILE pute("fileerror\n"); #endif x->x_fileerror = errno; break; } else if (sysrtn == 0) { x->x_eof = 1; break; } else { x->x_fifohead += sysrtn; x->x_bytelimit -= sysrtn; if (x->x_fifohead == fifosize) x->x_fifohead = 0; if (x->x_bytelimit <= 0) { x->x_eof = 1; break; } } #ifdef DEBUG_SOUNDFILE sprintf(boo, "after: head %d, tail %d\n", x->x_fifohead, x->x_fifotail); pute(boo); #endif /* signal parent in case it's waiting for data */ sfread_cond_signal(&x->x_answercondition); } lost: if (x->x_requestcode == REQUEST_BUSY) x->x_requestcode = REQUEST_NOTHING; /* fell out of read loop: close file if necessary, set EOF and signal once more */ if (x->x_fd >= 0) { fd = x->x_fd; pthread_mutex_unlock(&x->x_mutex); close (fd); pthread_mutex_lock(&x->x_mutex); x->x_fd = -1; } sfread_cond_signal(&x->x_answercondition); } else if (x->x_requestcode == REQUEST_CLOSE) { if (x->x_fd >= 0) { // void* reader_obj = x->x_reader_obj; fd = x->x_fd; pthread_mutex_unlock(&x->x_mutex); sfxreader_close(reader_obj); pthread_mutex_lock(&x->x_mutex); x->x_fd = -1; } if (x->x_requestcode == REQUEST_CLOSE) x->x_requestcode = REQUEST_NOTHING; sfread_cond_signal(&x->x_answercondition); } else if (x->x_requestcode == REQUEST_QUIT) { if (x->x_fd >= 0) { // void* reader_obj = x->x_reader_obj; fd = x->x_fd; pthread_mutex_unlock(&x->x_mutex); sfxreader_free(reader_obj); pthread_mutex_lock(&x->x_mutex); x->x_fd = -1; } x->x_requestcode = REQUEST_NOTHING; sfread_cond_signal(&x->x_answercondition); break; } else { #ifdef DEBUG_SOUNDFILE pute("13\n"); #endif } } #ifdef DEBUG_SOUNDFILE pute("thread exit\n"); #endif pthread_mutex_unlock(&x->x_mutex); return (0); }
static t_symbol *iem_image_calc_size(t_iem_image *x) { char dirbuf[MAXPDSTRING], *namebufptr; char namebuf[MAXPDSTRING]; unsigned char buf[222]; unsigned int i; char *c; int fd; FILE *fh; size_t items; if(!x->x_gifsym || !x->x_gifsym->s_name) { post("iem_image-ERROR: no gifname"); x->x_gifsym = (t_symbol *)0; return((t_symbol *)0); } fd = open_via_path(canvas_getdir(glist_getcanvas(x->x_gui.x_glist))->s_name, x->x_gifsym->s_name, "", dirbuf, &namebufptr, MAXPDSTRING, 1); if (fd < 0) { post("iem_image-ERROR: cannot open %s first time", x->x_gifsym->s_name); x->x_gifsym = (t_symbol *)0; return((t_symbol *)0); } else { if(fd >= 0) close(fd); strcpy(namebuf, dirbuf); strcat(namebuf, "/"); strcat(namebuf, namebufptr); fh = fopen(namebuf, "r"); if(fh == NULL) { post("iem_image-ERROR: cannot open %s second time", namebuf); x->x_gifsym = (t_symbol *)0; return((t_symbol *)0); } else { items=fread(buf, 22, sizeof(unsigned char), fh); if((items < 1)||(strlen((char*)buf)<7)) { post("iem_image-ERROR: can not read header in %s, only %d items read: %s.", namebuf, strlen((char*)buf), (char*) buf); x->x_gifsym = (t_symbol *)0; return((t_symbol *)0); }; fclose(fh); c = (char *)buf; if((c[0] != 'G')||(c[1] != 'I')||(c[2] != 'F')) { post("iem_image-ERROR: %s is not a GIF-file", namebuf); x->x_gifsym = (t_symbol *)0; return((t_symbol *)0); } i = 256*(unsigned int)buf[7]; i += (unsigned int)buf[6]; x->x_gui.x_w = (int)i; i = 256*(unsigned int)buf[9]; i += (unsigned int)buf[8]; x->x_gui.x_h = (int)i; SETFLOAT(x->x_at_out, (t_float)x->x_gui.x_w); SETFLOAT(x->x_at_out+1, (t_float)x->x_gui.x_h); outlet_list(x->x_gui.x_obj.ob_outlet, &s_list, 2, x->x_at_out); if(x->x_gui.x_fsf.x_snd_able && x->x_gui.x_snd->s_thing) pd_list(x->x_gui.x_snd->s_thing, &s_list, 2, x->x_at_out); return(gensym(namebuf)); } } }
static void rootibang(t_rooti *x) { outlet_float(x->f_out3, x->level); outlet_symbol(x->s_out2, x->canvas->gl_name); outlet_symbol(x->s_out1, canvas_getdir(x->canvas)); }
t_hammerfile *hammerfile_new(t_pd *master, t_hammerembedfn embedfn, t_hammerfilefn readfn, t_hammerfilefn writefn, t_hammerfilefn updatefn) { t_hammerfile *result = (t_hammerfile *)pd_new(hammerfile_class); result->f_master = master; result->f_next = hammerfile_proxies; hammerfile_proxies = result; if (!(result->f_canvas = canvas_getcurrent())) { bug("hammerfile_new: out of context"); return (result); } /* 1. embedding */ if (result->f_embedfn = embedfn) { /* just in case of missing 'restore' */ hammerembed_gc(master, ps__C, 0); if (hammerfile_isloading(result) || hammerfile_ispasting(result)) pd_bind(master, ps__C); } /* 2. the panels */ if (readfn || writefn) { t_hammerfile *f; char buf[64]; sprintf(buf, "miXed.%x", (int)result); result->f_bindname = gensym(buf); pd_bind((t_pd *)result, result->f_bindname); result->f_currentdir = result->f_inidir = canvas_getdir(result->f_canvas); result->f_panelfn = readfn; result->f_panelclock = clock_new(result, (t_method)hammerpanel_tick); f = (t_hammerfile *)pd_new(hammerfile_class); f->f_master = master; f->f_canvas = result->f_canvas; sprintf(buf, "miXed.%x", (int)f); f->f_bindname = gensym(buf); pd_bind((t_pd *)f, f->f_bindname); f->f_currentdir = f->f_inidir = result->f_currentdir; f->f_panelfn = writefn; f->f_panelclock = clock_new(f, (t_method)hammerpanel_tick); result->f_savepanel = f; } else result->f_savepanel = 0; /* 3. editor */ if (result->f_editorfn = updatefn) { result->f_editorclock = clock_new(result, (t_method)hammereditor_tick); if (!result->f_bindname) { char buf[64]; sprintf(buf, "miXed.%x", (int)result); result->f_bindname = gensym(buf); pd_bind((t_pd *)result, result->f_bindname); } } return (result); }
/*-------------------open files, STYP first if given, then SDIF-----------------*/ static void sdiflists_open(t_sdiflists *x, t_symbol *s, int argcount, t_atom *argvec) { int i,j; unsigned int StreamSpecified = 0; for (i = 0; i < argcount; i++) { if (argvec[i].a_type == A_FLOAT) {//TODO: check if this was given after file, cuz that would be useless x->streamid = (int) argvec[i].a_w.w_float; StreamSpecified = 1; post("stream specified:#%d", x->streamid); } if (argvec[i].a_type == A_SYMBOL) { char *sym = argvec[i].a_w.w_symbol->s_name; if(!strcmp( sym + strlen(sym)-5 , ".sdif" ) || !strcmp( sym + strlen(sym)-5 , ".SDIF" )) { t_colout *u; if(x->markers[0].timetag != -1) { post("closing file... "); SdifFClose(x->file); for(j=0; j < x->nframes; j++) x->markers[j].timetag = x->markers[j].filepos = 0; for(j=0, u = x->data; j < x->n_outs; u++, j++) freebytes( u->outvec, x->max_vec * sizeof(t_atom)); x->markers[0].timetag = -1; x->index = -1; x->nframes = IFRAMES; #if DEBUG post("sdiflists::open: closed previous file"); #endif }// end if x->file exists unsigned int rows = 0; unsigned int cols = 0; t_int eof, m, updatepos; t_int firstframe = 1; t_int timepos = 0; size_t bytesread = 0; SdiffPosT currpos; float currtime = 0; /*method for opening file in canvas directory. Based on zexy's [msgfile], which is based on Pd's [textfile]*/ char filnam[MAXPDSTRING], namebuf[MAXPDSTRING]; char buf[MAXPDSTRING], *bufptr, *readbuf; int fd; // used to check if file exists char *dirname; dirname = canvas_getdir(x->canvas)->s_name; t_binbuf *bbuf = binbuf_new(); fd = open_via_path(dirname, sym,"", buf, &bufptr, MAXPDSTRING, 0); if(fd < 0) { error("sdiflists-open: %s cannot be found", sym); return; } namebuf[0] = 0; if (*buf) strcat(namebuf, buf), strcat(namebuf, "/"); strcat(namebuf, bufptr); // open and get length sys_bashfilename(namebuf, filnam); //this is hopefully a readable file #if DEBUG post("(open_via_path) dirname: %s, filename->s_name: %s, buf: %s, bufptr: %s", dirname, sym, buf, bufptr); post("AFTER bashfilename: namebuf: %s, filnam: %s ", namebuf, filnam); #endif x->filename = gensym( namebuf ); /* Check if the file is a good SDIF file, skip function if not */ if (SdifCheckFileFormat (x->filename->s_name)) { post("sdiflists: reading %s", x->filename->s_name); x->file = SdifFOpen ( x->filename->s_name, eReadFile); bytesread += SdifFReadGeneralHeader (x->file); bytesread += SdifFReadAllASCIIChunks (x->file); eof = SdifFCurrSignature(x->file) == eEmptySignature; int err; while (!eof) //frame loop { /*The frame positions must be indexed before the frameheader is read, then check if it is a selected frame. If not, skip the frame and overwrite the marker.*/ err = SdifFGetPos(x->file, &currpos); if(err==-1) error("error SdifFGetPos"); /* Read frame header. Current signature has already been read by SdifFReadAllASCIIChunks or the last loop.) */ bytesread += SdifFReadFrameHeader (x->file); if(!StreamSpecified) { x->streamid = SdifSelectGetFirstInt(x->file->Selection->stream, SdifFCurrID (x->file)); StreamSpecified = 1; post("first stream used: #%d", x->streamid); } //PROBLEM: (maybe not...check)the last frame in the file is always acceptable... fix by using sel spec while (!SdifFCurrFrameIsSelected (x->file) || SdifFCurrID (x->file) != x->streamid ) { // post("frame skipped"); SdifFSkipFrameData (x->file); if ((eof = SdifFGetSignature(x->file, &bytesread) == eEof)) break; SdifFGetPos(x->file, &currpos); bytesread += SdifFReadFrameHeader(x->file); } if(eof) //have to check again...since it might have skipped to the end break; //check if this is a new time so successive frames don't overwrite filepos currtime = SdifFCurrTime (x->file); if( !timepos || x->markers[timepos-1].timetag != currtime ) { x->markers[timepos].filepos = currpos; x->markers[timepos].timetag = currtime; timepos++; if( timepos >= x->nframes ) { x->markers = (t_pos *)resizebytes( x->markers, x->nframes * sizeof(t_pos), (x->nframes + IFRAMES) * sizeof(t_pos) ); x->nframes = x->nframes + IFRAMES; } } /*matrices loop */ for ( m = 0; (unsigned int)m < SdifFCurrNbMatrix (x->file); m++) { bytesread += SdifFReadMatrixHeader (x->file); if( SdifFCurrNbRow (x->file) > rows) { rows = SdifFCurrNbRow (x->file); //get matrix stats cols = SdifFCurrNbCol (x->file);//should stay the same } //skip the actual matrices bytesread += SdifFSkipMatrixData (x->file); }// end for matrices eof = SdifFGetSignature (x->file, &bytesread) == eEof; }// end while no eof x->seconds = currtime; x->nframes= timepos; //last timepos was the eof #if DEBUG post(" rows: %d , cols: %d , frames: %d , seconds: %f", rows, cols, x->nframes, x->seconds); post("sdiflists: %s opened. ", x->filename->s_name); #endif x->max_vec = rows; //needed to free memory //the following lines are a bug work-around... if the eof is reached, you cannot seek //until the fle is closed, opened, and re-initialized... i posted about this on the sdif list. SdifFClose (x->file); x->file = SdifFOpen ( x->filename->s_name, eReadFile); SdifFReadGeneralHeader (x->file); SdifFReadAllASCIIChunks (x->file); eof = SdifFCurrSignature(x->file) == eEmptySignature; //make room for the row lists for ( i=0, u = x->data; i < x->n_outs; u++, i++) u->outvec = (t_atom *)getbytes( x->max_vec * sizeof(t_atom)); } /* end if filetype check */ } } //end if Symbol }//end for arguments }
static void *pddplink_new(t_symbol *s, int ac, t_atom *av) { t_pddplink xgen, *x; int skip; xgen.x_isboxed = 0; xgen.x_isgopvisible = 0; xgen.x_vistext = 0; xgen.x_vissize = 0; if ((xgen.x_ulink = pddplink_nextsymbol(ac, av, 0, &skip))) { t_symbol *opt; ac -= skip; av += skip; while ((opt = pddplink_nextsymbol(ac, av, 1, &skip))) { ac -= skip; av += skip; if (opt == gensym("-box")) xgen.x_isboxed = 1; else if (opt == gensym("-gop")) xgen.x_isgopvisible = 1; else if (opt == gensym("-text")) { t_symbol *nextsym = pddplink_nextsymbol(ac, av, 1, &skip); int natoms = (nextsym ? skip - 1 : ac); if (natoms) xgen.x_vistext = pddplink_optext(&xgen.x_vissize, natoms, av); } } } x = (t_pddplink *) pd_new(xgen.x_isboxed ? pddplinkbox_class : pddplink_class); x->x_glist = canvas_getcurrent(); x->x_dirsym = canvas_getdir(x->x_glist); /* FIXME */ x->x_isboxed = xgen.x_isboxed; x->x_isgopvisible = xgen.x_isgopvisible; x->x_vistext = xgen.x_vistext; x->x_vissize = xgen.x_vissize; x->x_vislength = (x->x_vistext ? strlen(x->x_vistext) : 0); x->x_rtextactive = 0; if (xgen.x_ulink) x->x_ulink = xgen.x_ulink; else x->x_ulink = gensym("Untitled"); SETSYMBOL(&x->x_openargs[0], x->x_ulink); SETSYMBOL(&x->x_openargs[1], x->x_dirsym); x->x_ishit = 0; if (x->x_isboxed) outlet_new((t_object *)x, &s_anything); else { /* do we need to set ((t_text *)x)->te_type = T_TEXT; ? */ if (!x->x_vistext) { x->x_vislength = strlen(x->x_ulink->s_name); x->x_vissize = x->x_vislength + 1; x->x_vistext = getbytes(x->x_vissize); strcpy(x->x_vistext, x->x_ulink->s_name); } } return (x); }
static void soundfile_info_read(t_soundfile_info *x, t_symbol *filename) { char completefilename[400]; int i, n, n2, n4, filesize, read_chars, header_size=0, ch, bps, sr; FILE *fh; t_atom *at; char *cvec; long ll; short ss; if(filename->s_name[0] == '/')/*make complete path + filename*/ { strcpy(completefilename, filename->s_name); } else if(((filename->s_name[0] >= 'A')&&(filename->s_name[0] <= 'Z')|| (filename->s_name[0] >= 'a')&&(filename->s_name[0] <= 'z'))&& (filename->s_name[1] == ':')&&(filename->s_name[2] == '/')) { strcpy(completefilename, filename->s_name); } else { strcpy(completefilename, canvas_getdir(x->x_canvas)->s_name); strcat(completefilename, "/"); strcat(completefilename, filename->s_name); } fh = fopen(completefilename,"rb"); if(!fh) { post("soundfile_info_read: cannot open %s !!\n", completefilename); } else { n = x->x_size; n2 = sizeof(short) * x->x_size; n4 = sizeof(long) * x->x_size; fseek(fh, 0, SEEK_END); filesize = ftell(fh); fseek(fh,0,SEEK_SET); read_chars = (int)fread(x->x_begmem, sizeof(char), n4, fh) /2; fclose(fh); // post("read chars = %d", read_chars); cvec = (char *)x->x_begmem; if(read_chars > 4) { if(strncmp(cvec, "RIFF", 4)) { post("soundfile_info_read-error: %s is no RIFF-WAVE-file", completefilename); goto soundfile_info_end; } header_size += 8; cvec += 8; if(strncmp(cvec, "WAVE", 4)) { post("soundfile_info_read-error: %s is no RIFF-WAVE-file", completefilename); goto soundfile_info_end; } header_size += 4; cvec += 4; for(i=header_size/2; i<read_chars; i++) { if(!strncmp(cvec, "fmt ", 4)) goto soundfile_info_fmt; header_size += 2; cvec += 2; } post("soundfile_info_read-error: %s has at begin no format-chunk", completefilename); goto soundfile_info_end; soundfile_info_fmt: header_size += 4; cvec += 4; ll = soundfile_info_str2long(cvec); if(ll != 16) { post("soundfile_info_read-error: %s has a format-chunk not equal to 16", completefilename); goto soundfile_info_end; } header_size += 4; cvec += 4; ss = soundfile_info_str2short(cvec); /* format */ if(ss != 1) /* PCM = 1 */ { post("soundfile_info_read-error: %s is not PCM-format coded", completefilename); goto soundfile_info_end; } header_size += 2; cvec += 2; ss = soundfile_info_str2short(cvec); /* channels */ if((ss < 1) || (ss > 100)) { post("soundfile_info_read-error: %s has no common channel-number", completefilename); goto soundfile_info_end; } SETFLOAT(x->x_atheader+SFI_HEADER_CHANNELS, (t_float)ss); ch = ss; header_size += 2; cvec += 2; ll = soundfile_info_str2long(cvec); /* samplerate */ if((ll > 400000) || (ll < 200)) { post("soundfile_info_read-error: %s has no common samplerate", completefilename); goto soundfile_info_end; } SETFLOAT(x->x_atheader+SFI_HEADER_SAMPLERATE, (t_float)ll); sr = ll; header_size += 4; cvec += 4; header_size += 4; /* bytes_per_sec */ cvec += 4; ss = soundfile_info_str2short(cvec); /* bytes_per_sample */ if((ss < 1) || (ss > 100)) { post("soundfile_info_read-error: %s has no common number of bytes per sample", completefilename); goto soundfile_info_end; } SETFLOAT(x->x_atheader+SFI_HEADER_BYTES_PER_SAMPLE, (t_float)(ss/ch)); bps = ss; header_size += 2; cvec += 2; header_size += 2; /* bits_per_sample */ cvec += 2; for(i=header_size/2; i<read_chars; i++) { if(!strncmp(cvec, "data", 4)) goto soundfile_info_data; header_size += 2; cvec += 2; } post("soundfile_info_read-error: %s has at begin no data-chunk", completefilename); goto soundfile_info_end; soundfile_info_data: header_size += 8; cvec += 8; SETFLOAT(x->x_atheader+SFI_HEADER_HEADERBYTES, (t_float)header_size); filesize -= header_size; filesize /= bps; SETFLOAT(x->x_atheader+SFI_HEADER_MULTICHANNEL_FILE_LENGTH, (t_float)filesize); SETSYMBOL(x->x_atheader+SFI_HEADER_ENDINESS, gensym("l")); SETSYMBOL(x->x_atheader+SFI_HEADER_FILENAME, gensym(completefilename)); /* post("ch = %d", ss); post("sr = %d", ll); post("bps = %d", ss/ch); post("head = %d", header_size); post("len = %d", filesize);*/ outlet_list(x->x_list_out, &s_list, SFI_HEADER_SIZE, x->x_atheader); soundfile_info_end: ; } } }