Exemple #1
0
void FileRead :: open( std::string fileName, bool typeRaw, unsigned int nChannels,
                       StkFormat format, StkFloat rate )
{
  // If another file is open, close it.
  close();

  // Try to open the file.
  fd_ = fopen( fileName.c_str(), "rb" );
  if ( !fd_ ) {
    oStream_ << "FileRead::open: could not open or find file (" << fileName << ")!";
    handleError( StkError::FILE_NOT_FOUND );
  }

  // Attempt to determine file type from header (unless RAW).
  bool result = false;
  if ( typeRaw )
    result = getRawInfo( fileName.c_str(), nChannels, format, rate );
  else {
    char header[12];
    if ( fread( &header, 4, 3, fd_ ) != 3 ) goto error;
    if ( !strncmp( header, "RIFF", 4 ) &&
         !strncmp( &header[8], "WAVE", 4 ) )
      result = getWavInfo( fileName.c_str() );
    else if ( !strncmp( header, ".snd", 4 ) )
      result = getSndInfo( fileName.c_str() );
    else if ( !strncmp( header, "FORM", 4 ) &&
              ( !strncmp( &header[8], "AIFF", 4 ) || !strncmp(&header[8], "AIFC", 4) ) )
      result = getAifInfo( fileName.c_str() );
    else {
      if ( fseek( fd_, 126, SEEK_SET ) == -1 ) goto error;
      if ( fread( &header, 2, 1, fd_ ) != 1 ) goto error;
      if ( !strncmp( header, "MI", 2 ) ||
           !strncmp( header, "IM", 2 ) )
        result = getMatInfo( fileName.c_str() );
      else {
        oStream_ << "FileRead::open: file (" << fileName << ") format unknown.";
        handleError( StkError::FILE_UNKNOWN_FORMAT );
      }
    }
  }

  // If here, we had a file type candidate but something else went wrong.
  if ( result == false )
    handleError( StkError::FILE_ERROR );

  // Check for empty files.
  if ( fileSize_ == 0 ) {
    oStream_ << "FileRead::open: file (" << fileName << ") data size is zero!";
    handleError( StkError::FILE_ERROR );
  }

  return;

 error:
  oStream_ << "FileRead::open: error reading file (" << fileName << ")!";
  handleError( StkError::FILE_ERROR );
}
Exemple #2
0
void WvIn :: openFile( std::string fileName, bool raw, bool doNormalize )
{
  closeFile();

  // Try to open the file.
  fd_ = fopen(fileName.c_str(), "rb");
  if (!fd_) {
    errorString_ << "WvIn::openFile: could not open or find file (" << fileName << ")!";
    handleError( StkError::FILE_NOT_FOUND );
  }

  unsigned long lastChannels = channels_;
  unsigned long samples, lastSamples = (bufferSize_+1)*channels_;
  bool result = false;
  if ( raw )
    result = getRawInfo( fileName.c_str() );
  else {
    char header[12];
    if ( fread(&header, 4, 3, fd_) != 3 ) goto error;
    if ( !strncmp(header, "RIFF", 4) &&
         !strncmp(&header[8], "WAVE", 4) )
      result = getWavInfo( fileName.c_str() );
    else if ( !strncmp(header, ".snd", 4) )
      result = getSndInfo( fileName.c_str() );
    else if ( !strncmp(header, "FORM", 4) &&
              (!strncmp(&header[8], "AIFF", 4) || !strncmp(&header[8], "AIFC", 4) ) )
      result = getAifInfo( fileName.c_str() );
    else {
      if ( fseek(fd_, 126, SEEK_SET) == -1 ) goto error;
      if ( fread(&header, 2, 1, fd_) != 1 ) goto error;
      if (!strncmp(header, "MI", 2) ||
          !strncmp(header, "IM", 2) )
        result = getMatInfo( fileName.c_str() );
      else {
        errorString_ << "WvIn::openFile: file (" << fileName << ") format unknown.";
        handleError( StkError::FILE_UNKNOWN_FORMAT );
      }
    }
  }

  if ( result == false )
    handleError( StkError::FILE_ERROR );

  if ( fileSize_ == 0 ) {
    errorString_ << "WvIn::openFile: file (" << fileName << ") data size is zero!";
    handleError( StkError::FILE_ERROR );
  }

  // Allocate new memory if necessary.
  samples = (bufferSize_+1)*channels_;
  if ( lastSamples < samples ) {
    if ( data_ ) delete [] data_;
    data_ = (StkFloat *) new StkFloat[samples];
  }
  if ( lastChannels < channels_ ) {
    if ( lastOutputs_ ) delete [] lastOutputs_;
    lastOutputs_ = (StkFloat *) new StkFloat[channels_];
  }

  if ( fmod(rate_, 1.0) != 0.0 ) interpolate_ = true;
  chunkPointer_ = 0;
  reset();
  readData( 0 );  // Load file data.
  if ( doNormalize ) normalize();
  finished_ = false;
  return;

 error:
  errorString_ << "WvIn::openFile: error reading file (" << fileName << ")!";
  handleError( StkError::FILE_ERROR );
}