示例#1
0
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;
}
示例#2
0
文件: sdiflists.c 项目: Angeldude/pd
/*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);
}