Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   Tutorial


Stk.h

00001 /***************************************************/
00013 /***************************************************/
00014 
00015 #ifndef STK_STK_H
00016 #define STK_STK_H
00017 
00018 #include <string>
00019 #include <iostream>
00020 #include <sstream>
00021 #include <valarray>
00022 
00023 // Most data in STK is passed and calculated with the
00024 // following user-definable floating-point type.  You
00025 // can change this to "float" if you prefer or perhaps
00026 // a "long double" in the future.
00027 typedef double StkFloat;
00028 
00029 // The "MY_FLOAT" type was deprecated in STK
00030 // versions higher than 4.1.3 and replaced with the variable
00031 // "StkFloat".  
00032 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__)
00033   typedef StkFloat MY_FLOAT;
00034   #pragma deprecated(MY_FLOAT)
00035 #elif defined(__GXX__) 
00036   typedef StkFloat MY_FLOAT __attribute__ ((deprecated));
00037 #else
00038   typedef StkFloat MY_FLOAT; // temporary
00039 #endif
00040 
00041 
00043 
00048 class StkError
00049 {
00050 public:
00051   enum Type {
00052     STATUS,
00053     WARNING,
00054     DEBUG_WARNING,
00055     FUNCTION_ARGUMENT,
00056     FILE_NOT_FOUND,
00057     FILE_UNKNOWN_FORMAT,
00058     FILE_ERROR,
00059     PROCESS_THREAD,
00060     PROCESS_SOCKET,
00061     PROCESS_SOCKET_IPADDR,
00062     AUDIO_SYSTEM,
00063     MIDI_SYSTEM,
00064     UNSPECIFIED
00065   };
00066 
00067 protected:
00068   std::string message_;
00069   Type type_;
00070 
00071 public:
00073   StkError(const std::string& message, Type type = StkError::UNSPECIFIED) : message_(message), type_(type) {}
00074 
00076   virtual ~StkError(void) {};
00077 
00079   virtual void printMessage(void) { std::cerr << '\n' << message_ << "\n\n"; }
00080 
00082   virtual const Type& getType(void) { return type_; }
00083 
00085   virtual const std::string& getMessage(void) { return message_; }
00086 
00088   virtual const char *getMessageCString(void) { return message_.c_str(); }
00089 };
00090 
00091 
00092 class Stk
00093 {
00094 public:
00095 
00096   typedef unsigned long StkFormat;
00097   static const StkFormat STK_SINT8;   
00098   static const StkFormat STK_SINT16;  
00099   static const StkFormat STK_SINT24;  
00100   static const StkFormat STK_SINT32;  
00101   static const StkFormat STK_FLOAT32; 
00102   static const StkFormat STK_FLOAT64; 
00104 
00105   static StkFloat sampleRate(void) { return srate_; }
00106 
00108 
00116   static void setSampleRate(StkFloat rate) { if (rate > 0.0) srate_ = rate; }
00117 
00119   static std::string rawwavePath(void) { return rawwavepath_; }
00120 
00122   static void setRawwavePath(std::string path);
00123 
00125   static void swap16(unsigned char *ptr);
00126 
00128   static void swap32(unsigned char *ptr);
00129 
00131   static void swap64(unsigned char *ptr);
00132 
00134   static void sleep(unsigned long milliseconds);
00135 
00137   static void handleError( const char *message, StkError::Type type );
00138 
00140   static void handleError( std::string message, StkError::Type type );
00141 
00142 private:
00143   static StkFloat srate_;
00144   static std::string rawwavepath_;
00145 
00146 protected:
00147 
00148   std::ostringstream errorString_;
00149 
00151   Stk(void);
00152 
00154   virtual ~Stk(void);
00155 
00157   void handleError( StkError::Type type );
00158 };
00159 
00160 
00161 /***************************************************/
00175 /***************************************************/
00176 
00177 class StkFrames
00178 {
00179 public:
00180 
00182   StkFrames( unsigned int nFrames = 0, unsigned int nChannels = 1, bool interleaved = true );
00183 
00185   StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels, bool interleaved = true );
00186 
00188 
00194   StkFloat& operator[]( size_t n ) { return data_[n]; };
00195 
00197 
00201   StkFloat operator[]( size_t n ) const { return data_[n]; };
00202 
00204   size_t size() const { return size_; }; 
00205 
00207 
00211   void resize( unsigned int nFrames, unsigned int nChannels = 1, StkFloat value = 0.0 );
00212 
00214   unsigned int channels( void ) const { return nChannels_; };
00215 
00217   unsigned int frames( void ) const { return nFrames_; };
00218 
00220   bool interleaved( void ) const { return interleaved_; };
00221 
00223   void setInterleaved( bool isInterleaved ) { interleaved_ = isInterleaved; };
00224 
00225 private:
00226   std::valarray<StkFloat> data_;
00227   unsigned int nFrames_;
00228   unsigned int nChannels_;
00229   size_t size_;
00230   bool interleaved_;
00231 
00232 };
00233 
00234 
00235 // Here are a few other useful typedefs.
00236 typedef signed short SINT16;
00237 typedef signed int SINT32;
00238 typedef float FLOAT32;
00239 typedef double FLOAT64;
00240 
00241 // The default sampling rate.
00242 const StkFloat SRATE = 44100.0;
00243 
00244 // The default real-time audio input and output buffer size.  If
00245 // clicks are occuring in the input and/or output sound stream, a
00246 // larger buffer size may help.  Larger buffer sizes, however, produce
00247 // more latency.
00248 const unsigned int RT_BUFFER_SIZE = 512;
00249 
00250 // The default rawwave path value is set with the preprocessor
00251 // definition RAWWAVE_PATH.  This can be specified as an argument to
00252 // the configure script, in an integrated development environment, or
00253 // below.  The global STK rawwave path variable can be dynamically set
00254 // with the Stk::setRawwavePath() function.  This value is
00255 // concatenated to the beginning of all references to rawwave files in
00256 // the various STK core classes (ex. Clarinet.cpp).  If you wish to
00257 // move the rawwaves directory to a different location in your file
00258 // system, you will need to set this path definition appropriately.
00259 #if !defined(RAWWAVE_PATH)
00260   #define RAWWAVE_PATH "../../rawwaves/"
00261 #endif
00262 
00263 const StkFloat PI           = 3.14159265359;
00264 const StkFloat TWO_PI       = 2 * PI;
00265 const StkFloat ONE_OVER_128 = 0.0078125;
00266 
00267 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__)
00268   #define __OS_WINDOWS__
00269   #define __STK_REALTIME__
00270 #elif defined(__LINUX_OSS__) || defined(__LINUX_ALSA__) || defined(__LINUX_JACK__)
00271   #define __OS_LINUX__
00272   #define __STK_REALTIME__
00273 #elif defined(__IRIX_AL__)
00274   #define __OS_IRIX__
00275   #define __STK_REALTIME__
00276 #elif defined(__MACOSX_CORE__)
00277   #define __OS_MACOSX__
00278   #define __STK_REALTIME__
00279 #endif
00280 
00281 //#define _STK_DEBUG_
00282 
00283 #endif

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