Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


Stk.h

00001 /***************************************************/
00051 /***************************************************/
00052 
00053 #ifndef STK_STK_H
00054 #define STK_STK_H
00055 
00056 #include <string>
00057 #include <iostream>
00058 #include <sstream>
00059 #include <vector>
00060 
00061 // Most data in STK is passed and calculated with the
00062 // following user-definable floating-point type.  You
00063 // can change this to "float" if you prefer or perhaps
00064 // a "long double" in the future.
00065 typedef double StkFloat;
00066 
00068 
00073 class StkError
00074 {
00075 public:
00076   enum Type {
00077     STATUS,
00078     WARNING,
00079     DEBUG_WARNING,
00080     MEMORY_ALLOCATION,
00081     MEMORY_ACCESS,
00082     FUNCTION_ARGUMENT,
00083     FILE_NOT_FOUND,
00084     FILE_UNKNOWN_FORMAT,
00085     FILE_ERROR,
00086     PROCESS_THREAD,
00087     PROCESS_SOCKET,
00088     PROCESS_SOCKET_IPADDR,
00089     AUDIO_SYSTEM,
00090     MIDI_SYSTEM,
00091     UNSPECIFIED
00092   };
00093 
00094 protected:
00095   std::string message_;
00096   Type type_;
00097 
00098 public:
00100   StkError(const std::string& message, Type type = StkError::UNSPECIFIED)
00101     : message_(message), type_(type) {}
00102 
00104   virtual ~StkError(void) {};
00105 
00107   virtual void printMessage(void) { std::cerr << '\n' << message_ << "\n\n"; }
00108 
00110   virtual const Type& getType(void) { return type_; }
00111 
00113   virtual const std::string& getMessage(void) { return message_; }
00114 
00116   virtual const char *getMessageCString(void) { return message_.c_str(); }
00117 };
00118 
00119 
00120 class Stk
00121 {
00122 public:
00123 
00124   typedef unsigned long StkFormat;
00125   static const StkFormat STK_SINT8;   
00126   static const StkFormat STK_SINT16;  
00127   static const StkFormat STK_SINT24;  
00128   static const StkFormat STK_SINT32;  
00129   static const StkFormat STK_FLOAT32; 
00130   static const StkFormat STK_FLOAT64; 
00132 
00133   static StkFloat sampleRate( void ) { return srate_; }
00134 
00136 
00153   static void setSampleRate( StkFloat rate );
00154 
00156 
00161   void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
00162 
00164   static std::string rawwavePath(void) { return rawwavepath_; }
00165 
00167   static void setRawwavePath( std::string path );
00168 
00170   static void swap16( unsigned char *ptr );
00171 
00173   static void swap32( unsigned char *ptr );
00174 
00176   static void swap64( unsigned char *ptr );
00177 
00179   static void sleep( unsigned long milliseconds );
00180 
00182   static void handleError( const char *message, StkError::Type type );
00183 
00185   static void handleError( std::string message, StkError::Type type );
00186 
00188   static void showWarnings( bool status ) { showWarnings_ = status; }
00189 
00191   static void printErrors( bool status ) { printErrors_ = status; }
00192 
00193 private:
00194   static StkFloat srate_;
00195   static std::string rawwavepath_;
00196   static bool showWarnings_;
00197   static bool printErrors_;
00198   static std::vector<Stk *> alertList_;
00199 
00200 protected:
00201 
00202   std::ostringstream errorString_;
00203   bool ignoreSampleRateChange_;
00204 
00206   Stk( void );
00207 
00209   virtual ~Stk( void );
00210 
00212   virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
00213 
00215   void addSampleRateAlert( Stk *ptr );
00216 
00218   void removeSampleRateAlert( Stk *ptr );
00219 
00221   void handleError( StkError::Type type );
00222 };
00223 
00224 
00225 /***************************************************/
00240 /***************************************************/
00241 
00242 class StkFrames
00243 {
00244 public:
00245 
00247   StkFrames( unsigned int nFrames = 0, unsigned int nChannels = 0, bool interleaved = true );
00248 
00250   StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels, bool interleaved = true );
00251 
00253   ~StkFrames();
00254 
00256 
00262   StkFloat& operator[] ( size_t n );
00263 
00265 
00269   StkFloat operator[] ( size_t n ) const;
00270 
00272 
00279   StkFloat& operator() ( size_t frame, unsigned int channel );
00280 
00282 
00287   StkFloat operator() ( size_t frame, unsigned int channel ) const;
00288 
00290 
00296   StkFloat interpolate( StkFloat frame, unsigned int channel = 0 ) const;
00297 
00299   size_t size() const { return size_; }; 
00300 
00302   bool empty() const;
00303 
00305 
00312   void resize( size_t nFrames, unsigned int nChannels = 1 );
00313 
00315 
00322   void resize( size_t nFrames, unsigned int nChannels, StkFloat value );
00323 
00325   unsigned int channels( void ) const { return nChannels_; };
00326 
00328   unsigned int frames( void ) const { return nFrames_; };
00329 
00331 
00335   void setDataRate( StkFloat rate ) { dataRate_ = rate; };
00336 
00338 
00342   StkFloat dataRate( void ) const { return dataRate_; };
00343 
00345   bool interleaved( void ) const { return interleaved_; };
00346 
00348 
00353   void setInterleaved( bool isInterleaved ) { interleaved_ = isInterleaved; };
00354 
00355 private:
00356 
00357   StkFloat *data_;
00358   StkFloat dataRate_;
00359   size_t nFrames_;
00360   unsigned int nChannels_;
00361   size_t size_;
00362   size_t bufferSize_;
00363   bool interleaved_;
00364 
00365 };
00366 
00367 
00368 // Here are a few other useful typedefs.
00369 typedef unsigned short UINT16;
00370 typedef unsigned int UINT32;
00371 typedef signed short SINT16;
00372 typedef signed int SINT32;
00373 typedef float FLOAT32;
00374 typedef double FLOAT64;
00375 
00376 // The default sampling rate.
00377 const StkFloat SRATE = 44100.0;
00378 
00379 // The default real-time audio input and output buffer size.  If
00380 // clicks are occuring in the input and/or output sound stream, a
00381 // larger buffer size may help.  Larger buffer sizes, however, produce
00382 // more latency.
00383 const unsigned int RT_BUFFER_SIZE = 512;
00384 
00385 // The default rawwave path value is set with the preprocessor
00386 // definition RAWWAVE_PATH.  This can be specified as an argument to
00387 // the configure script, in an integrated development environment, or
00388 // below.  The global STK rawwave path variable can be dynamically set
00389 // with the Stk::setRawwavePath() function.  This value is
00390 // concatenated to the beginning of all references to rawwave files in
00391 // the various STK core classes (ex. Clarinet.cpp).  If you wish to
00392 // move the rawwaves directory to a different location in your file
00393 // system, you will need to set this path definition appropriately.
00394 #if !defined(RAWWAVE_PATH)
00395   #define RAWWAVE_PATH "../../rawwaves/"
00396 #endif
00397 
00398 const StkFloat PI           = 3.14159265358979;
00399 const StkFloat TWO_PI       = 2 * PI;
00400 const StkFloat ONE_OVER_128 = 0.0078125;
00401 
00402 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_MM__)
00403   #define __OS_WINDOWS__
00404   #define __STK_REALTIME__
00405 #elif defined(__LINUX_OSS__) || defined(__LINUX_ALSA__) || defined(__UNIX_JACK__)
00406   #define __OS_LINUX__
00407   #define __STK_REALTIME__
00408 #elif defined(__IRIX_AL__)
00409   #define __OS_IRIX__
00410 #elif defined(__MACOSX_CORE__) || defined(__UNIX_JACK__)
00411   #define __OS_MACOSX__
00412   #define __STK_REALTIME__
00413 #endif
00414 
00415 //#define _STK_DEBUG_
00416 
00417 #endif

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