⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stk.cpp

📁 Mobile STK for Symbian OS V0.1
💻 CPP
字号:
/***************************************************//*! \class Stk    \brief STK base class    Nearly all STK classes inherit from this class.    The global sample rate can be queried and    modified via Stk.  In addition, this class    provides error handling and byte-swapping    functions.    by Perry R. Cook and Gary P. Scavone, 1995 - 2005.*//***************************************************/#include "Stk.h"#if !defined(SYMBIAN)StkFloat Stk :: srate_ = (StkFloat) SRATE;std::string Stk :: rawwavepath_ = RAWWAVE_PATH;#endifconst Stk::StkFormat Stk :: STK_SINT8   = 0x1;const Stk::StkFormat Stk :: STK_SINT16  = 0x2;const Stk::StkFormat Stk :: STK_SINT24  = 0x4;const Stk::StkFormat Stk :: STK_SINT32  = 0x8;const Stk::StkFormat Stk :: STK_FLOAT32 = 0x10;const Stk::StkFormat Stk :: STK_FLOAT64 = 0x20;#if !defined(SYMBIAN)bool Stk :: showWarnings_ = false;bool Stk :: printErrors_ = true;#endif#if !defined(SYMBIAN)Stk :: Stk(void){}Stk :: ~Stk(void){}#endif#if !defined(SYMBIAN)void Stk :: setRawwavePath( std::string path ){  if ( !path.empty() )    rawwavepath_ = path;  // Make sure the path includes a "/"  if ( rawwavepath_[rawwavepath_.length()-1] != '/' )    rawwavepath_ += "/";}#endif//#if !defined(SYMBIAN)void Stk :: swap16(unsigned char *ptr){  register unsigned char val;  // Swap 1st and 2nd bytes  val = *(ptr);  *(ptr) = *(ptr+1);  *(ptr+1) = val;}void Stk :: swap32(unsigned char *ptr){  register unsigned char val;  // Swap 1st and 4th bytes  val = *(ptr);  *(ptr) = *(ptr+3);  *(ptr+3) = val;  //Swap 2nd and 3rd bytes  ptr += 1;  val = *(ptr);  *(ptr) = *(ptr+1);  *(ptr+1) = val;}void Stk :: swap64(unsigned char *ptr){  register unsigned char val;  // Swap 1st and 8th bytes  val = *(ptr);  *(ptr) = *(ptr+7);  *(ptr+7) = val;  // Swap 2nd and 7th bytes  ptr += 1;  val = *(ptr);  *(ptr) = *(ptr+5);  *(ptr+5) = val;  // Swap 3rd and 6th bytes  ptr += 1;  val = *(ptr);  *(ptr) = *(ptr+3);  *(ptr+3) = val;  // Swap 4th and 5th bytes  ptr += 1;  val = *(ptr);  *(ptr) = *(ptr+1);  *(ptr+1) = val;}//#endif#if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))  #include <unistd.h>#elif defined(__OS_WINDOWS__)  #include <windows.h>#endif#if !defined(SYMBIAN)void Stk :: sleep(unsigned long milliseconds){#if defined(__OS_WINDOWS__)  Sleep((DWORD) milliseconds);#elif (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))  usleep( (unsigned long) (milliseconds * 1000.0) );#endif}#endif#if !defined(SYMBIAN)void Stk :: handleError( StkError::Type type ){  handleError( errorString_.str(), type );  errorString_.str( std::string() ); // reset the ostringstream buffer}void Stk :: handleError( const char *message, StkError::Type type ){  std::string msg( message );  handleError( msg, type );}void Stk :: handleError( std::string message, StkError::Type type ){  if ( type == StkError::WARNING || type == StkError::STATUS ) {    if ( !showWarnings_ ) return;    std::cerr << '\n' << message << '\n' << std::endl;  }  else if (type == StkError::DEBUG_WARNING) {#if defined(_STK_DEBUG_)    std::cerr << '\n' << message << '\n' << std::endl;#endif  }  else {    if ( printErrors_ ) {      // Print error message before throwing.      std::cerr << '\n' << message << '\n' << std::endl;    }    throw StkError(message, type);  }}#endif//// StkFrames definitions//StkFrames :: StkFrames( unsigned int nFrames, unsigned int nChannels, bool interleaved )  : nFrames_( nFrames ), nChannels_( nChannels ), interleaved_( interleaved ){  size_ = nFrames_ * nChannels_;  bufferSize_ = size_;  if ( size_ > 0 ) {#if !defined(SYMBIAN)    data_ = (StkFloat *) calloc( size_, sizeof( StkFloat ) );#else	data_ = new (ELeave) StkFloat[size_];	for(int i=0; i< size_; i++)		data_[i]=0.0;#endif#if defined(_STK_DEBUG_)    if ( data_ == NULL ) {      std::string error = "StkFrames: memory allocation error in constructor!";      Stk::handleError( error, StkError::MEMORY_ALLOCATION );    }#endif  }  else data_ = 0;  dataRate_ = Stk::sampleRate();}StkFrames :: StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels, bool interleaved )  : nFrames_( nFrames ), nChannels_( nChannels ), interleaved_( interleaved ){  size_ = nFrames_ * nChannels_;  bufferSize_ = size_;  if ( size_ > 0 ) {#if !defined(SYMBIAN)    data_ = (StkFloat *) malloc( size_ * sizeof( StkFloat ) );#else	data_ = new (ELeave) StkFloat[size_];	for(int i=0; i< size_; i++)		data_[i]=0.0;#endif#if defined(_STK_DEBUG_)    if ( data_ == NULL ) {      std::string error = "StkFrames: memory allocation error in constructor!";      Stk::handleError( error, StkError::MEMORY_ALLOCATION );    }#endif    for ( long i=0; i<(long)size_; i++ ) data_[i] = value;  }  else data_ = 0;  dataRate_ = Stk::sampleRate();}StkFrames :: ~StkFrames(){#if !defined(SYMBIAN)  if ( data_ ) free( data_ );#else  if ( data_ ) delete [] data_ ;#endif}bool StkFrames :: empty() const{  if ( size_ > 0 ) return false;  else return true;}void StkFrames :: resize( size_t nFrames, unsigned int nChannels ){  nFrames_ = nFrames;  nChannels_ = nChannels;  size_ = nFrames_ * nChannels_;  if ( size_ > bufferSize_ ) {#if !defined(SYMBIAN)	if ( data_ ) free( data_ );#else	if ( data_ ) delete [] data_ ;#endif#if !defined(SYMBIAN)    data_ = (StkFloat *) malloc( size_ * sizeof( StkFloat ) );#else	data_ = new (ELeave) StkFloat[size_];	for(int i=0; i< size_; i++)		data_[i]=0.0;#endif#if defined(_STK_DEBUG_)    if ( data_ == NULL ) {      std::string error = "StkFrames::resize: memory allocation error!";      Stk::handleError( error, StkError::MEMORY_ALLOCATION );    }#endif    bufferSize_ = size_;  }}void StkFrames :: resize( size_t nFrames, unsigned int nChannels, StkFloat value ){  this->resize( nFrames, nChannels );  for ( size_t i=0; i<size_; i++ ) data_[i] = value;}StkFloat& StkFrames :: operator[] ( size_t n ){#if defined(_STK_DEBUG_)    if ( n >= size_ ) {      std::ostringstream error;      error << "StkFrames::operator[]: invalid index (" << n << ") value!";      Stk::handleError( error.str(), StkError::MEMORY_ACCESS );    }#endif  return data_[n];}StkFloat StkFrames :: operator[] ( size_t n ) const{#if defined(_STK_DEBUG_)    if ( n >= size_ ) {      std::ostringstream error;      error << "StkFrames::operator[]: invalid index (" << n << ") value!";      Stk::handleError( error.str(), StkError::MEMORY_ACCESS );    }#endif  return data_[n];}StkFloat& StkFrames :: operator() ( size_t frame, unsigned int channel ){#if defined(_STK_DEBUG_)    if ( frame >= nFrames_ || channel >= nChannels_ ) {      std::ostringstream error;      error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";      Stk::handleError( error.str(), StkError::MEMORY_ACCESS );    }#endif  if ( interleaved_ )    return data_[ frame * nChannels_ + channel ];  else    return data_[ channel * nFrames_ + frame ];}StkFloat StkFrames :: operator() ( size_t frame, unsigned int channel ) const{#if defined(_STK_DEBUG_)    if ( frame >= nFrames_ || channel >= nChannels_ ) {      std::ostringstream error;      error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";      Stk::handleError( error.str(), StkError::MEMORY_ACCESS );    }#endif  if ( interleaved_ )    return data_[ frame * nChannels_ + channel ];  else    return data_[ channel * nFrames_ + frame ];}StkFloat StkFrames :: interpolate( StkFloat frame, unsigned int channel ) const{#if defined(_STK_DEBUG_)    if ( frame >= (StkFloat) nFrames_ || channel >= nChannels_ ) {      std::ostringstream error;      error << "StkFrames::interpolate: invalid frame (" << frame << ") or channel (" << channel << ") value!";      Stk::handleError( error.str(), StkError::MEMORY_ACCESS );    }#endif  size_t iIndex = ( size_t ) frame;                    // integer part of index  StkFloat output, alpha = frame - (StkFloat) iIndex;  // fractional part of index  if ( interleaved_ ) {    iIndex = iIndex * nChannels_ + channel;    output = data_[ iIndex ];    output += ( alpha * ( data_[ iIndex + nChannels_ ] - output ) );  }  else {    iIndex += channel * nFrames_;    output = data_[ iIndex ];    output += ( alpha * ( data_[ iIndex++ ] - output ) );  }  return output;}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -