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_WARNING,
    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 void handleError( const char *message, StkError::Type type );
    00194
    00196   static void handleError( std::string message, StkError::Type type );
    00197
    00199   static void showWarnings( bool status ) { showWarnings_ = status; }
    00200
    00202   static void printErrors( bool status ) { printErrors_ = status; }
    00203
    00204 private:
    00205   static StkFloat srate_;
    00206   static std::string rawwavepath_;
    00207   static bool showWarnings_;
    00208   static bool printErrors_;
    00209   static std::vector<Stk *> alertList_;
    00210
    00211 protected:
    00212
    00213   std::ostringstream errorString_;
    00214   bool ignoreSampleRateChange_;
    00215
    00217   Stk( void );
    00218
    00220   virtual ~Stk( void );
    00221
    00223   virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
    00224
    00226   void addSampleRateAlert( Stk *ptr );
    00227
    00229   void removeSampleRateAlert( Stk *ptr );
    00230
    00232   void handleError( StkError::Type type );
    00233 };
    00234
    00235
    00236 /***************************************************/
    00259 /***************************************************/
    00260
    00261 class StkFrames
    00262 {
    00263 public:
    00264
    00266   StkFrames( unsigned int nFrames = 0, unsigned int nChannels = 0 );
    00267
    00269   StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels );
    00270
    00272   ~StkFrames();
    00273
    00274   // A copy constructor.
    00275   StkFrames( const StkFrames& f );
    00276
    00277   // Assignment operator that returns a reference to self.
    00278   StkFrames& operator= ( const StkFrames& f );
    00279
    00281
    00287   StkFloat& operator[] ( size_t n );
    00288
    00290
    00294   StkFloat operator[] ( size_t n ) const;
    00295
    00297
    00302   void operator+= ( StkFrames& f );
    00303
    00305
    00310   void operator*= ( StkFrames& f );
    00311
    00313
    00320   StkFloat& operator() ( size_t frame, unsigned int channel );
    00321
    00323
    00328   StkFloat operator() ( size_t frame, unsigned int channel ) const;
    00329
    00331
    00337   StkFloat interpolate( StkFloat frame, unsigned int channel = 0 ) const;
    00338
    00340   size_t size() const { return size_; };
    00341
    00343   bool empty() const;
    00344
    00346
    00353   void resize( size_t nFrames, unsigned int nChannels = 1 );
    00354
    00356
    00363   void resize( size_t nFrames, unsigned int nChannels, StkFloat value );
    00364
    00366   unsigned int channels( void ) const { return nChannels_; };
    00367
    00369   unsigned int frames( void ) const { return nFrames_; };
    00370
    00372
    00376   void setDataRate( StkFloat rate ) { dataRate_ = rate; };
    00377
    00379
    00383   StkFloat dataRate( void ) const { return dataRate_; };
    00384
    00385 private:
    00386
    00387   StkFloat *data_;
    00388   StkFloat dataRate_;
    00389   size_t nFrames_;
    00390   unsigned int nChannels_;
    00391   size_t size_;
    00392   size_t bufferSize_;
    00393
    00394 };
    00395
    00396 inline bool StkFrames :: empty() const
    00397 {
    00398   if ( size_ > 0 ) return false;
    00399   else return true;
    00400 }
    00401
    00402 inline StkFloat& StkFrames :: operator[] ( size_t n )
    00403 {
    00404 #if defined(_STK_DEBUG_)
    00405   if ( n >= size_ ) {
    00406     std::ostringstream error;
    00407     error << "StkFrames::operator[]: invalid index (" << n << ") value!";
    00408     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00409   }
    00410 #endif
    00411
    00412   return data_[n];
    00413 }
    00414
    00415 inline StkFloat StkFrames :: operator[] ( size_t n ) const
    00416 {
    00417 #if defined(_STK_DEBUG_)
    00418   if ( n >= size_ ) {
    00419     std::ostringstream error;
    00420     error << "StkFrames::operator[]: invalid index (" << n << ") value!";
    00421     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00422   }
    00423 #endif
    00424
    00425   return data_[n];
    00426 }
    00427
    00428 inline StkFloat& StkFrames :: operator() ( size_t frame, unsigned int channel )
    00429 {
    00430 #if defined(_STK_DEBUG_)
    00431   if ( frame >= nFrames_ || channel >= nChannels_ ) {
    00432     std::ostringstream error;
    00433     error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
    00434     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00435   }
    00436 #endif
    00437
    00438   return data_[ frame * nChannels_ + channel ];
    00439 }
    00440
    00441 inline StkFloat StkFrames :: operator() ( size_t frame, unsigned int channel ) const
    00442 {
    00443 #if defined(_STK_DEBUG_)
    00444   if ( frame >= nFrames_ || channel >= nChannels_ ) {
    00445     std::ostringstream error;
    00446     error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
    00447     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00448   }
    00449 #endif
    00450
    00451   return data_[ frame * nChannels_ + channel ];
    00452 }
    00453
    00454 inline void StkFrames :: operator+= ( StkFrames& f )
    00455 {
    00456 #if defined(_STK_DEBUG_)
    00457   if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
    00458     std::ostringstream error;
    00459     error << "StkFrames::operator+=: frames argument must be of equal dimensions!";
    00460     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00461   }
    00462 #endif
    00463
    00464   StkFloat *fptr = &f[0];
    00465   StkFloat *dptr = data_;
    00466   for ( unsigned int i=0; i<size_; i++ )
    00467     *dptr++ += *fptr++;
    00468 }
    00469
    00470 inline void StkFrames :: operator*= ( StkFrames& f )
    00471 {
    00472 #if defined(_STK_DEBUG_)
    00473   if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
    00474     std::ostringstream error;
    00475     error << "StkFrames::operator*=: frames argument must be of equal dimensions!";
    00476     Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
    00477   }
    00478 #endif
    00479
    00480   StkFloat *fptr = &f[0];
    00481   StkFloat *dptr = data_;
    00482   for ( unsigned int i=0; i<size_; i++ )
    00483     *dptr++ *= *fptr++;
    00484 }
    00485
    00486 // Here are a few other useful typedefs.
    00487 typedef unsigned short UINT16;
    00488 typedef unsigned int UINT32;
    00489 typedef signed short SINT16;
    00490 typedef signed int SINT32;
    00491 typedef float FLOAT32;
    00492 typedef double FLOAT64;
    00493
    00494 // The default sampling rate.
    00495 const StkFloat SRATE = 44100.0;
    00496
    00497 // The default real-time audio input and output buffer size.  If
    00498 // clicks are occuring in the input and/or output sound stream, a
    00499 // larger buffer size may help.  Larger buffer sizes, however, produce
    00500 // more latency.
    00501 const unsigned int RT_BUFFER_SIZE = 512;
    00502
    00503 // The default rawwave path value is set with the preprocessor
    00504 // definition RAWWAVE_PATH.  This can be specified as an argument to
    00505 // the configure script, in an integrated development environment, or
    00506 // below.  The global STK rawwave path variable can be dynamically set
    00507 // with the Stk::setRawwavePath() function.  This value is
    00508 // concatenated to the beginning of all references to rawwave files in
    00509 // the various STK core classes (ex. Clarinet.cpp).  If you wish to
    00510 // move the rawwaves directory to a different location in your file
    00511 // system, you will need to set this path definition appropriately.
    00512 #if !defined(RAWWAVE_PATH)
    00513   #define RAWWAVE_PATH "../../rawwaves/"
    00514 #endif
    00515
    00516 const StkFloat PI           = 3.14159265358979;
    00517 const StkFloat TWO_PI       = 2 * PI;
    00518 const StkFloat ONE_OVER_128 = 0.0078125;
    00519
    00520 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_MM__)
    00521   #define __OS_WINDOWS__
    00522   #define __STK_REALTIME__
    00523 #elif defined(__LINUX_OSS__) || defined(__LINUX_ALSA__) || defined(__UNIX_JACK__)
    00524   #define __OS_LINUX__
    00525   #define __STK_REALTIME__
    00526 #elif defined(__IRIX_AL__)
    00527   #define __OS_IRIX__
    00528 #elif defined(__MACOSX_CORE__) || defined(__UNIX_JACK__)
    00529   #define __OS_MACOSX__
    00530   #define __STK_REALTIME__
    00531 #endif
    00532
    00533 } // stk namespace
    00534
    00535 #endif

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