The Synthesis ToolKit in C++ (STK)

Home Information Classes Download Usage Mail List Requirements Links FAQ Tutorial

# Stk.h

    00001 #ifndef STK_STK_H
    00002 #define STK_STK_H
    00003
    00004 #include <string>
    00005 #include <iostream>
    00006 #include <sstream>
    00007 #include <vector>
    00008 //#include <cstdlib>
    00009
    00016 namespace stk {
    00017
    00018 /***************************************************/
    00068 /***************************************************/
    00069
    00070 //#define _STK_DEBUG_
    00071
    00072 // Most data in STK is passed and calculated with the
    00073 // following user-definable floating-point type.  You
    00074 // can change this to "float" if you prefer or perhaps
    00075 // a "long double" in the future.
    00076 typedef double StkFloat;
    00077
    00079
    00084 class StkError
    00085 {
    00086 public:
    00087   enum Type {
    00088     STATUS,
    00089     WARNING,
    00090     DEBUG_PRINT,
    00091     MEMORY_ALLOCATION,
    00092     MEMORY_ACCESS,
    00093     FUNCTION_ARGUMENT,
    00094     FILE_NOT_FOUND,
    00095     FILE_UNKNOWN_FORMAT,
    00096     FILE_ERROR,
    00097     PROCESS_THREAD,
    00098     PROCESS_SOCKET,
    00099     PROCESS_SOCKET_IPADDR,
    00100     AUDIO_SYSTEM,
    00101     MIDI_SYSTEM,
    00102     UNSPECIFIED
    00103   };
    00104
    00105 protected:
    00106   std::string message_;
    00107   Type type_;
    00108
    00109 public:
    00111   StkError(const std::string& message, Type type = StkError::UNSPECIFIED)
    00112     : message_(message), type_(type) {}
    00113
    00115   virtual ~StkError(void) {};
    00116
    00118   virtual void printMessage(void) { std::cerr << '\n' << message_ << "\n\n"; }
    00119
    00121   virtual const Type& getType(void) { return type_; }
    00122
    00124   virtual const std::string& getMessage(void) { return message_; }
    00125
    00127   virtual const char *getMessageCString(void) { return message_.c_str(); }
    00128 };
    00129
    00130
    00131 class Stk
    00132 {
    00133 public:
    00134
    00135   typedef unsigned long StkFormat;
    00136   static const StkFormat STK_SINT8;
    00137   static const StkFormat STK_SINT16;
    00138   static const StkFormat STK_SINT24;
    00139   static const StkFormat STK_SINT32;
    00140   static const StkFormat STK_FLOAT32;
    00141   static const StkFormat STK_FLOAT64;
    00143
    00144   static StkFloat sampleRate( void ) { return srate_; }
    00145
    00147
    00164   static void setSampleRate( StkFloat rate );
    00165
    00167
    00172   void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
    00173
    00175   static std::string rawwavePath(void) { return rawwavepath_; }
    00176
    00178   static void setRawwavePath( std::string path );
    00179
    00181   static void swap16( unsigned char *ptr );
    00182
    00184   static void swap32( unsigned char *ptr );
    00185
    00187   static void swap64( unsigned char *ptr );
    00188
    00190   static void sleep( unsigned long milliseconds );
    00191
    00193   static bool inRange( StkFloat value, StkFloat min, StkFloat max ) {
    00194     if ( value < min ) return false;
    00195     else if ( value > max ) return false;
    00196     else return true;
    00197   }
    00198
    00200   static void handleError( const char *message, StkError::Type type );
    00201
    00203   static void handleError( std::string message, StkError::Type type );
    00204
    00206   static void showWarnings( bool status ) { showWarnings_ = status; }
    00207
    00209   static void printErrors( bool status ) { printErrors_ = status; }
    00210
    00211 private:
    00212   static StkFloat srate_;
    00213   static std::string rawwavepath_;
    00214   static bool showWarnings_;
    00215   static bool printErrors_;
    00216   static std::vector<Stk *> alertList_;
    00217
    00218 protected:
    00219
    00220   static std::ostringstream oStream_;
    00221   bool ignoreSampleRateChange_;
    00222
    00224   Stk( void );
    00225
    00227   virtual ~Stk( void );
    00228
    00230   virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
    00231
    00233   void addSampleRateAlert( Stk *ptr );
    00234
    00236   void removeSampleRateAlert( Stk *ptr );
    00237
    00239   void handleError( StkError::Type type );
    00240 };
    00241
    00242
    00243 /***************************************************/
    00269 /***************************************************/
    00270
    00271 class StkFrames
    00272 {
    00273 public:
    00274
    00276   StkFrames( unsigned int nFrames = 0, unsigned int nChannels = 0 );
    00277
    00279   StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels );
    00280
    00282   ~StkFrames();
    00283
    00284   // A copy constructor.
    00285   StkFrames( const StkFrames& f );
    00286
    00287   // Assignment operator that returns a reference to self.
    00288   StkFrames& operator= ( const StkFrames& f );
    00289
    00291
    00297   StkFloat& operator[] ( size_t n );
    00298
    00300
    00304   StkFloat operator[] ( size_t n ) const;
    00305
    00307
    00312   void operator+= ( StkFrames& f );
    00313
    00315
    00320   void operator*= ( StkFrames& f );
    00321
    00323
    00330   StkFloat& operator() ( size_t frame, unsigned int channel );
    00331
    00333
    00338   StkFloat operator() ( size_t frame, unsigned int channel ) const;
    00339
    00341
    00347   StkFloat interpolate( StkFloat frame, unsigned int channel = 0 ) const;
    00348
    00350   size_t size() const { return size_; };
    00351
    00353   bool empty() const;
    00354
    00356
    00363   void resize( size_t nFrames, unsigned int nChannels = 1 );
    00364
    00366
    00373   void resize( size_t nFrames, unsigned int nChannels, StkFloat value );
    00374
    00376   unsigned int channels( void ) const { return nChannels_; };
    00377
    00379   unsigned int frames( void ) const { return nFrames_; };
    00380
    00382
    00386   void setDataRate( StkFloat rate ) { dataRate_ = rate; };
    00387
    00389
    00393   StkFloat dataRate( void ) const { return dataRate_; };
    00394
    00395 private:
    00396
    00397   StkFloat *data_;
    00398   StkFloat dataRate_;
    00399   size_t nFrames_;
    00400   unsigned int nChannels_;
    00401   size_t size_;
    00402   size_t bufferSize_;
    00403
    00404 };
    00405
    00406 inline bool StkFrames :: empty() const
    00407 {
    00408   if ( size_ > 0 ) return false;
    00409   else return true;
    00410 }
    00411
    00412 inline StkFloat& StkFrames :: operator[] ( size_t n )
    00413 {
    00414 #if defined(_STK_DEBUG_)
    00415   if ( n >= size_ ) {
    00416     std::ostringstream error;
    00417     error << "StkFrames::operator[]: invalid index (" << n << ") value!";
    00418     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00419   }
    00420 #endif
    00421
    00422   return data_[n];
    00423 }
    00424
    00425 inline StkFloat StkFrames :: operator[] ( size_t n ) const
    00426 {
    00427 #if defined(_STK_DEBUG_)
    00428   if ( n >= size_ ) {
    00429     std::ostringstream error;
    00430     error << "StkFrames::operator[]: invalid index (" << n << ") value!";
    00431     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00432   }
    00433 #endif
    00434
    00435   return data_[n];
    00436 }
    00437
    00438 inline StkFloat& StkFrames :: operator() ( size_t frame, unsigned int channel )
    00439 {
    00440 #if defined(_STK_DEBUG_)
    00441   if ( frame >= nFrames_ || channel >= nChannels_ ) {
    00442     std::ostringstream error;
    00443     error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
    00444     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00445   }
    00446 #endif
    00447
    00448   return data_[ frame * nChannels_ + channel ];
    00449 }
    00450
    00451 inline StkFloat StkFrames :: operator() ( size_t frame, unsigned int channel ) const
    00452 {
    00453 #if defined(_STK_DEBUG_)
    00454   if ( frame >= nFrames_ || channel >= nChannels_ ) {
    00455     std::ostringstream error;
    00456     error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
    00457     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00458   }
    00459 #endif
    00460
    00461   return data_[ frame * nChannels_ + channel ];
    00462 }
    00463
    00464 inline void StkFrames :: operator+= ( StkFrames& f )
    00465 {
    00466 #if defined(_STK_DEBUG_)
    00467   if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
    00468     std::ostringstream error;
    00469     error << "StkFrames::operator+=: frames argument must be of equal dimensions!";
    00470     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00471   }
    00472 #endif
    00473
    00474   StkFloat *fptr = &f[0];
    00475   StkFloat *dptr = data_;
    00476   for ( unsigned int i=0; i<size_; i++ )
    00477     *dptr++ += *fptr++;
    00478 }
    00479
    00480 inline void StkFrames :: operator*= ( StkFrames& f )
    00481 {
    00482 #if defined(_STK_DEBUG_)
    00483   if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
    00484     std::ostringstream error;
    00485     error << "StkFrames::operator*=: frames argument must be of equal dimensions!";
    00486     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00487   }
    00488 #endif
    00489
    00490   StkFloat *fptr = &f[0];
    00491   StkFloat *dptr = data_;
    00492   for ( unsigned int i=0; i<size_; i++ )
    00493     *dptr++ *= *fptr++;
    00494 }
    00495
    00496 // Here are a few other useful typedefs.
    00497 typedef unsigned short UINT16;
    00498 typedef unsigned int UINT32;
    00499 typedef signed short SINT16;
    00500 typedef signed int SINT32;
    00501 typedef float FLOAT32;
    00502 typedef double FLOAT64;
    00503
    00504 // The default sampling rate.
    00505 const StkFloat SRATE = 44100.0;
    00506
    00507 // The default real-time audio input and output buffer size.  If
    00508 // clicks are occuring in the input and/or output sound stream, a
    00509 // larger buffer size may help.  Larger buffer sizes, however, produce
    00510 // more latency.
    00511 const unsigned int RT_BUFFER_SIZE = 512;
    00512
    00513 // The default rawwave path value is set with the preprocessor
    00514 // definition RAWWAVE_PATH.  This can be specified as an argument to
    00515 // the configure script, in an integrated development environment, or
    00516 // below.  The global STK rawwave path variable can be dynamically set
    00517 // with the Stk::setRawwavePath() function.  This value is
    00518 // concatenated to the beginning of all references to rawwave files in
    00519 // the various STK core classes (ex. Clarinet.cpp).  If you wish to
    00520 // move the rawwaves directory to a different location in your file
    00521 // system, you will need to set this path definition appropriately.
    00522 #if !defined(RAWWAVE_PATH)
    00523   #define RAWWAVE_PATH "../../rawwaves/"
    00524 #endif
    00525
    00526 const StkFloat PI           = 3.14159265358979;
    00527 const StkFloat TWO_PI       = 2 * PI;
    00528 const StkFloat ONE_OVER_128 = 0.0078125;
    00529
    00530 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_MM__)
    00531   #define __OS_WINDOWS__
    00532   #define __STK_REALTIME__
    00533 #elif defined(__LINUX_OSS__) || defined(__LINUX_ALSA__) || defined(__UNIX_JACK__)
    00534   #define __OS_LINUX__
    00535   #define __STK_REALTIME__
    00536 #elif defined(__IRIX_AL__)
    00537   #define __OS_IRIX__
    00538 #elif defined(__MACOSX_CORE__) || defined(__UNIX_JACK__)
    00539   #define __OS_MACOSX__
    00540   #define __STK_REALTIME__
    00541 #endif
    00542
    00543 } // stk namespace
    00544
    00545 #endif

The Synthesis ToolKit in C++ (STK)
©1995-2012 Perry R. Cook and Gary P. Scavone. All Rights Reserved.