Home   Information   Classes   Download   Usage   Mail List   Requirements   Links   FAQ   Tutorial


Stk.h
1 #ifndef STK_STK_H
2 #define STK_STK_H
3 
4 #include <string>
5 #include <cstring>
6 #include <iostream>
7 #include <sstream>
8 #include <vector>
9 //#include <cstdlib>
10 
17 namespace stk {
18 
19 /***************************************************/
69 /***************************************************/
70 
71 //#define _STK_DEBUG_
72 
73 // Most data in STK is passed and calculated with the
74 // following user-definable floating-point type. You
75 // can change this to "float" if you prefer or perhaps
76 // a "long double" in the future.
77 typedef double StkFloat;
78 
80 
85 class StkError
86 {
87 public:
88  enum Type {
89  STATUS,
90  WARNING,
91  DEBUG_PRINT,
92  MEMORY_ALLOCATION,
93  MEMORY_ACCESS,
94  FUNCTION_ARGUMENT,
95  FILE_NOT_FOUND,
96  FILE_UNKNOWN_FORMAT,
97  FILE_ERROR,
98  PROCESS_THREAD,
99  PROCESS_SOCKET,
100  PROCESS_SOCKET_IPADDR,
101  AUDIO_SYSTEM,
102  MIDI_SYSTEM,
103  UNSPECIFIED
104  };
105 
106 protected:
107  std::string message_;
108  Type type_;
109 
110 public:
112  StkError(const std::string& message, Type type = StkError::UNSPECIFIED)
113  : message_(message), type_(type) {}
114 
116  virtual ~StkError(void) {};
117 
119  virtual void printMessage(void) { std::cerr << '\n' << message_ << "\n\n"; }
120 
122  virtual const Type& getType(void) { return type_; }
123 
125  virtual const std::string& getMessage(void) { return message_; }
126 
128  virtual const char *getMessageCString(void) { return message_.c_str(); }
129 };
130 
131 
132 class Stk
133 {
134 public:
135 
136  typedef unsigned long StkFormat;
137  static const StkFormat STK_SINT8;
138  static const StkFormat STK_SINT16;
139  static const StkFormat STK_SINT24;
140  static const StkFormat STK_SINT32;
141  static const StkFormat STK_FLOAT32;
142  static const StkFormat STK_FLOAT64;
144 
145  static StkFloat sampleRate( void ) { return srate_; }
146 
148 
165  static void setSampleRate( StkFloat rate );
166 
168 
173  void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
174 
176  static std::string rawwavePath(void) { return rawwavepath_; }
177 
179  static void setRawwavePath( std::string path );
180 
182  static void swap16( unsigned char *ptr );
183 
185  static void swap32( unsigned char *ptr );
186 
188  static void swap64( unsigned char *ptr );
189 
191  static void sleep( unsigned long milliseconds );
192 
194  static bool inRange( StkFloat value, StkFloat min, StkFloat max ) {
195  if ( value < min ) return false;
196  else if ( value > max ) return false;
197  else return true;
198  }
199 
201  static void handleError( const char *message, StkError::Type type );
202 
204  static void handleError( std::string message, StkError::Type type );
205 
207  static void showWarnings( bool status ) { showWarnings_ = status; }
208 
210  static void printErrors( bool status ) { printErrors_ = status; }
211 
212 private:
213  static StkFloat srate_;
214  static std::string rawwavepath_;
215  static bool showWarnings_;
216  static bool printErrors_;
217  static std::vector<Stk *> alertList_;
218 
219 protected:
220 
221  static std::ostringstream oStream_;
222  bool ignoreSampleRateChange_;
223 
225  Stk( void );
226 
228  virtual ~Stk( void );
229 
231  virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
232 
234  void addSampleRateAlert( Stk *ptr );
235 
237  void removeSampleRateAlert( Stk *ptr );
238 
240  void handleError( StkError::Type type ) const;
241 };
242 
243 
244 /***************************************************/
270 /***************************************************/
271 
273 {
274 public:
275 
277  StkFrames( unsigned int nFrames = 0, unsigned int nChannels = 0 );
278 
280  StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels );
281 
283  ~StkFrames();
284 
285  // A copy constructor.
286  StkFrames( const StkFrames& f );
287 
288  // Assignment operator that returns a reference to self.
289  StkFrames& operator= ( const StkFrames& f );
290 
292 
298  StkFloat& operator[] ( size_t n );
299 
301 
305  StkFloat operator[] ( size_t n ) const;
306 
308 
313  void operator+= ( StkFrames& f );
314 
316 
321  void operator*= ( StkFrames& f );
322 
324 
331  StkFloat& operator() ( size_t frame, unsigned int channel );
332 
334 
339  StkFloat operator() ( size_t frame, unsigned int channel ) const;
340 
342 
348  StkFloat interpolate( StkFloat frame, unsigned int channel = 0 ) const;
349 
351  size_t size() const { return size_; };
352 
354  bool empty() const;
355 
357 
364  void resize( size_t nFrames, unsigned int nChannels = 1 );
365 
367 
374  void resize( size_t nFrames, unsigned int nChannels, StkFloat value );
375 
377  unsigned int channels( void ) const { return nChannels_; };
378 
380  unsigned int frames( void ) const { return (unsigned int)nFrames_; };
381 
383 
387  void setDataRate( StkFloat rate ) { dataRate_ = rate; };
388 
390 
394  StkFloat dataRate( void ) const { return dataRate_; };
395 
396 private:
397 
398  StkFloat *data_;
399  StkFloat dataRate_;
400  size_t nFrames_;
401  unsigned int nChannels_;
402  size_t size_;
403  size_t bufferSize_;
404 
405 };
406 
407 inline bool StkFrames :: empty() const
408 {
409  if ( size_ > 0 ) return false;
410  else return true;
411 }
412 
413 inline StkFloat& StkFrames :: operator[] ( size_t n )
414 {
415 #if defined(_STK_DEBUG_)
416  if ( n >= size_ ) {
417  std::ostringstream error;
418  error << "StkFrames::operator[]: invalid index (" << n << ") value!";
419  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
420  }
421 #endif
422 
423  return data_[n];
424 }
425 
426 inline StkFloat StkFrames :: operator[] ( size_t n ) const
427 {
428 #if defined(_STK_DEBUG_)
429  if ( n >= size_ ) {
430  std::ostringstream error;
431  error << "StkFrames::operator[]: invalid index (" << n << ") value!";
432  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
433  }
434 #endif
435 
436  return data_[n];
437 }
438 
439 inline StkFloat& StkFrames :: operator() ( size_t frame, unsigned int channel )
440 {
441 #if defined(_STK_DEBUG_)
442  if ( frame >= nFrames_ || channel >= nChannels_ ) {
443  std::ostringstream error;
444  error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
445  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
446  }
447 #endif
448 
449  return data_[ frame * nChannels_ + channel ];
450 }
451 
452 inline StkFloat StkFrames :: operator() ( size_t frame, unsigned int channel ) const
453 {
454 #if defined(_STK_DEBUG_)
455  if ( frame >= nFrames_ || channel >= nChannels_ ) {
456  std::ostringstream error;
457  error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
458  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
459  }
460 #endif
461 
462  return data_[ frame * nChannels_ + channel ];
463 }
464 
466 {
467 #if defined(_STK_DEBUG_)
468  if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
469  std::ostringstream error;
470  error << "StkFrames::operator+=: frames argument must be of equal dimensions!";
471  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
472  }
473 #endif
474 
475  StkFloat *fptr = &f[0];
476  StkFloat *dptr = data_;
477  for ( unsigned int i=0; i<size_; i++ )
478  *dptr++ += *fptr++;
479 }
480 
482 {
483 #if defined(_STK_DEBUG_)
484  if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
485  std::ostringstream error;
486  error << "StkFrames::operator*=: frames argument must be of equal dimensions!";
487  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
488  }
489 #endif
490 
491  StkFloat *fptr = &f[0];
492  StkFloat *dptr = data_;
493  for ( unsigned int i=0; i<size_; i++ )
494  *dptr++ *= *fptr++;
495 }
496 
497 // Here are a few other useful typedefs.
498 typedef unsigned short UINT16;
499 typedef unsigned int UINT32;
500 typedef signed short SINT16;
501 typedef signed int SINT32;
502 typedef float FLOAT32;
503 typedef double FLOAT64;
504 
505 // The default sampling rate.
506 const StkFloat SRATE = 44100.0;
507 
508 // The default real-time audio input and output buffer size. If
509 // clicks are occuring in the input and/or output sound stream, a
510 // larger buffer size may help. Larger buffer sizes, however, produce
511 // more latency.
512 const unsigned int RT_BUFFER_SIZE = 512;
513 
514 // The default rawwave path value is set with the preprocessor
515 // definition RAWWAVE_PATH. This can be specified as an argument to
516 // the configure script, in an integrated development environment, or
517 // below. The global STK rawwave path variable can be dynamically set
518 // with the Stk::setRawwavePath() function. This value is
519 // concatenated to the beginning of all references to rawwave files in
520 // the various STK core classes (ex. Clarinet.cpp). If you wish to
521 // move the rawwaves directory to a different location in your file
522 // system, you will need to set this path definition appropriately.
523 #if !defined(RAWWAVE_PATH)
524  #define RAWWAVE_PATH "../../rawwaves/"
525 #endif
526 
527 const StkFloat PI = 3.14159265358979;
528 const StkFloat TWO_PI = 2 * PI;
529 const StkFloat ONE_OVER_128 = 0.0078125;
530 
531 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_MM__)
532  #define __OS_WINDOWS__
533  #define __STK_REALTIME__
534 #elif defined(__LINUX_OSS__) || defined(__LINUX_ALSA__) || defined(__UNIX_JACK__)
535  #define __OS_LINUX__
536  #define __STK_REALTIME__
537 #elif defined(__IRIX_AL__)
538  #define __OS_IRIX__
539 #elif defined(__MACOSX_CORE__) || defined(__UNIX_JACK__)
540  #define __OS_MACOSX__
541  #define __STK_REALTIME__
542 #endif
543 
544 } // stk namespace
545 
546 #endif

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