/*-------------------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 }
int SDIFfile::readFile(const char *fileName) { SdifSignature mysig = SdifSignatureConst('1', 'T', 'R', 'C'); // char *sigstr = SdifSignatureToString(mysig); SdifFileT *file = SdifFOpen(fileName, eReadFile); if (file == NULL) return -1; SdifFReadGeneralHeader(file); SdifFReadAllASCIIChunks(file); int eof = 0; size_t bytesread = 0; while (!eof && SdifFLastError(file) == NULL) { bytesread += SdifFReadFrameHeader(file); while (!SdifFCurrFrameIsSelected(file) || SdifFCurrSignature(file) != mysig) { SdifFSkipFrameData(file); eof = SdifFGetSignature(file, &bytesread); if (eof == eEof) break; bytesread += SdifFReadFrameHeader(file); } if (!eof) { _checkFrameAlloc(); SdifSignature sig = SdifFCurrFrameSignature(file); SdifUInt4 nmatrix = SdifFCurrNbMatrix(file); SdifUInt4 streamid = SdifFCurrID(file); SdifFloat8 time = SdifFCurrTime(file); if (nmatrix != 1) { //FIXME: warn once? } for (SdifUInt4 m = 0; m < nmatrix; m++) { bytesread += SdifFReadMatrixHeader(file); if (SdifFCurrMatrixIsSelected(file)) { SdifSignature sig = SdifFCurrMatrixSignature(file); SdifDataTypeET type = SdifFCurrDataType(file); SdifInt4 nrows = SdifFCurrNbRow(file); SdifInt4 ncols = SdifFCurrNbCol(file); if (ncols < 4) { //FIXME: warn once } if (nrows > _maxSimultaneousPartials) _maxSimultaneousPartials = nrows; _frames[_numFrames] = new Frame(nrows); _frames[_numFrames]->time(time); // get direct access to partial node array PartialNode **partials = _frames[_numFrames]->partials(); _numFrames++; for (SdifInt4 row = 0; row < nrows; row++) { bytesread += SdifFReadOneRow(file); int id = (int) SdifFCurrOneRowCol(file, 1); float freq = (float) SdifFCurrOneRowCol(file, 2); float amp = (float) SdifFCurrOneRowCol(file, 3); float phase = (float) SdifFCurrOneRowCol(file, 4); for (SdifInt4 col = 5; col <= ncols; col++) SdifFCurrOneRowCol(file, col); // ignore any other cols //printf("[%ld] %f %f %f\n", index, freq, amp, phase); partials[row]->set(id, freq, amp, phase); if (id > _maxPartialID) _maxPartialID = id; } } else bytesread += SdifFSkipMatrixData(file); // ?? This is giving 4 bytes padding when the file doesn't have or need this. size_t padding = SdifFPaddingCalculate(file->Stream, bytesread); //printf("padding=%ld\n", padding); // bytesread += SdifFReadPadding(file, padding); //FIXME: this doesn't work if there is no padding. // bytesread += SdifFReadPadding(file, // SdifFPaddingCalculate(file->Stream, bytesread)); } eof = (SdifFGetSignature(file, &bytesread) == eEof); } } if (SdifFLastError(file)) { //FIXME: return -1; } SdifFClose(file); // Make a frame object for passing filtered frames to users of SDIFfile. _curFrame = new Frame(_maxSimultaneousPartials); // Make array that will cache filter decision for each partial. const int numTracks = maxPartialID() + 1; _filterPartials = new bool [numTracks]; for (int i = 0; i < numTracks; i++) _filterPartials[i] = false; _hasFile = true; return 0; }
/*in any order (accept :: is always first), allowing spaces*/ static void sdiflists_seek(t_sdiflists *x, t_floatarg gotoindex) { //check if file has been opened, -1 means first timetag was never set if(x->markers[0].timetag == -1) { post("sdiflists: open a file before seeking"); return; } x->index = (long)gotoindex; //check if index exists if( x->index >= x->nframes || gotoindex < 0) { #if DEBUG post("no index there."); #endif return; } #if DEBUG_MEM post ("index = %d , file pos = %d", x->index, x->markers[x->index].filepos); #endif t_colout *u; int i, selectedrow, selectedcol; unsigned int m; int nbrows =0; int nbcols =0; int xout = 0; int eof = 0; int col, row; float value = 0; size_t bytesread; int listsize[x->n_outs]; float timetag; //seek to index SdifFSetPos( x->file , &x->markers[x->index].filepos); //loop through while(!eof) { SdifFReadFrameHeader(x->file); timetag = SdifFCurrTime (x->file); //break once the current time has changed if( x->markers[x->index].timetag != timetag) break; outlet_float(x->timeout, timetag); /*Get the Data. If no selection is given when the file is opened, all rows/columns/matrices/frames will be obtained. */ if(SdifFCurrFrameIsSelected(x->file)) { #if DEBUG //fsig turns up 0 unless I close file and reopen in in the open function.. SdifSignature fsig = SdifFCurrFrameSignature (x->file); SdifUInt4 streamid = SdifFCurrID (x->file); post("\n frame header- Signature: '%s', timetag: %f, streamid: %d, nmatrices: %d", SdifSignatureToString(fsig),x->markers[x->index].timetag, streamid,SdifFCurrNbMatrix (x->file)); #endif //each frame can contain multiple matrices, so go through all of them for( m= 0; m < SdifFCurrNbMatrix (x->file) ; m++) { bytesread = SdifFReadMatrixHeader(x->file); //matrices not selected will be skipped, but they still need to be added to the bytesread tally if (SdifFCurrMatrixIsSelected (x->file)) { //get how many rows total, then check which ones are selected nbrows = SdifFCurrNbRow (x->file); nbcols = SdifFCurrNbCol (x->file); #if DEBUG //same problem with matrix signature as frame signature SdifDataTypeET type = SdifFCurrDataType(x->file); post("matrix header: Signature: '%s', nbrows: %d, nbcols %d, type %04x", SdifSignatureToString(SdifFCurrSignature(x->file)), nbrows, nbcols, type); #endif //go through every row and build vectors according to which column the row i sin for (selectedrow = 0, row = 0; row < nbrows; row++) { if(SdifFRowIsSelected (x->file, row)) { bytesread += SdifFReadOneRow (x->file); //put each member in the correct vector by accessing each one according to the current column //plus any previous columns from other matrices/frames for ( selectedcol= 0, col = 1, u = x->data + xout + selectedcol; col <= nbcols && xout + selectedcol < x->n_outs; col++, u++) { if(SdifFColumnIsSelected (x->file, col)) { value = SdifFCurrOneRowCol (x->file, col); SETFLOAT(u->outvec + selectedrow, value); selectedcol++; }//end if col is selected } //end for cols selectedrow++; }//end if row is selected }//end for rows //set how big each vector is that was just filled for(i = 0; i < SdifFNumColumnsSelected (x->file); i++) { listsize[xout + i] = SdifFNumRowsSelected (x->file); } //update number of columns xout += SdifFNumColumnsSelected (x->file); }//end if matrix is selected else bytesread += SdifFSkipMatrixData(x->file); SdifFReadPadding(x->file, SdifFPaddingCalculate(x->file->Stream, bytesread)); }//end for matrices in frame }// end if current frame is selected and not eof else SdifFSkipFrameData(x->file); eof = SdifFGetSignature (x->file, &bytesread) == eEof; } //end while not eof /* !! Once eof has been found, I have to close the file and reopen for the frame signatures to be found. someone please tell me why this is.. */ if(eof) { SdifFClose (x->file); x->file = SdifFOpen ( x->filename->s_name, eReadFile); SdifFReadGeneralHeader (x->file); SdifFReadAllASCIIChunks (x->file); eof = SdifFCurrSignature(x->file) == eEmptySignature; } #if DEBUG if (SdifFLastError (x->file)) { error(" in sdiflists_seek() "); } #endif // send out columns of rows, preserving right to left order for (i = x->n_outs, u = x->data + i; u--, i--;) outlet_list(u->out, gensym("list"),listsize[i], u->outvec); }