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 <stdexcept>
10 //#include <cstdlib>
11 
18 namespace stk {
19 
20 /***************************************************/
70 /***************************************************/
71 
72 //#define _STK_DEBUG_
73 
74 // Most data in STK is passed and calculated with the
75 // following user-definable floating-point type. You
76 // can change this to "float" if you prefer or perhaps
77 // a "long double" in the future.
78 typedef double StkFloat;
79 
81 
86 class StkError : public std::exception
87 {
88 public:
89  enum Type {
90  STATUS,
91  WARNING,
92  DEBUG_PRINT,
93  MEMORY_ALLOCATION,
94  MEMORY_ACCESS,
95  FUNCTION_ARGUMENT,
96  FILE_NOT_FOUND,
97  FILE_UNKNOWN_FORMAT,
98  FILE_ERROR,
99  PROCESS_THREAD,
100  PROCESS_SOCKET,
101  PROCESS_SOCKET_IPADDR,
102  AUDIO_SYSTEM,
103  MIDI_SYSTEM,
104  UNSPECIFIED
105  };
106 
107 protected:
108  std::string message_;
109  Type type_;
110 
111 public:
113  StkError(const std::string& message, Type type = StkError::UNSPECIFIED)
114  : std::exception(), message_(message), type_(type) {}
115 
117  virtual ~StkError(void) throw() {};
118 
120  virtual void printMessage(void) { std::cerr << '\n' << message_ << "\n\n"; }
121 
123  virtual const Type& getType(void) { return type_; }
124 
126  virtual const std::string& getMessage(void) { return message_; }
127 
129  virtual const char *getMessageCString(void) { return message_.c_str(); }
130 
131  virtual const char *what(void) const throw() { return message_.c_str(); }
132 };
133 
134 
135 class Stk
136 {
137 public:
138 
139  typedef unsigned long StkFormat;
140  static const StkFormat STK_SINT8;
141  static const StkFormat STK_SINT16;
142  static const StkFormat STK_SINT24;
143  static const StkFormat STK_SINT32;
144  static const StkFormat STK_FLOAT32;
145  static const StkFormat STK_FLOAT64;
147 
148  static StkFloat sampleRate( void ) { return srate_; }
149 
151 
168  static void setSampleRate( StkFloat rate );
169 
171 
176  void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
177 
179  static void clear_alertList(){std::vector<Stk *>().swap(alertList_);};
180 
182  static std::string rawwavePath(void) { return rawwavepath_; }
183 
185  static void setRawwavePath( std::string path );
186 
188  static void swap16( unsigned char *ptr );
189 
191  static void swap32( unsigned char *ptr );
192 
194  static void swap64( unsigned char *ptr );
195 
197  static void sleep( unsigned long milliseconds );
198 
200  static bool inRange( StkFloat value, StkFloat min, StkFloat max ) {
201  if ( value < min ) return false;
202  else if ( value > max ) return false;
203  else return true;
204  }
205 
207  static void handleError( const char *message, StkError::Type type );
208 
210  static void handleError( std::string message, StkError::Type type );
211 
213  static void showWarnings( bool status ) { showWarnings_ = status; }
214 
216  static void printErrors( bool status ) { printErrors_ = status; }
217 
218 private:
219  static StkFloat srate_;
220  static std::string rawwavepath_;
221  static bool showWarnings_;
222  static bool printErrors_;
223  static std::vector<Stk *> alertList_;
224 
225 protected:
226 
227  static std::ostringstream oStream_;
228  bool ignoreSampleRateChange_;
229 
231  Stk( void );
232 
234  virtual ~Stk( void );
235 
237  virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
238 
240  void addSampleRateAlert( Stk *ptr );
241 
243  void removeSampleRateAlert( Stk *ptr );
244 
246  void handleError( StkError::Type type ) const;
247 };
248 
249 
250 /***************************************************/
276 /***************************************************/
277 
279 {
280 public:
281 
283  StkFrames( unsigned int nFrames = 0, unsigned int nChannels = 0 );
284 
286  StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels );
287 
289  virtual ~StkFrames();
290 
291  // A copy constructor.
292  StkFrames( const StkFrames& f );
293 
294  // Assignment operator that returns a reference to self.
295  virtual StkFrames& operator= ( const StkFrames& f );
296 
298 
304  StkFloat& operator[] ( size_t n );
305 
307 
311  StkFloat operator[] ( size_t n ) const;
312 
314 
319  StkFrames operator+(const StkFrames &frames) const;
320 
322 
328 
330 
336 
338  StkFrames operator* ( StkFloat v ) const;
339 
341  friend StkFrames operator*(StkFloat v, const StkFrames& f);
342 
344  StkFrames& operator*= ( StkFloat v );
345 
347 
354  StkFloat& operator() ( size_t frame, unsigned int channel );
355 
357 
362  StkFloat operator() ( size_t frame, unsigned int channel ) const;
363 
365 
371  StkFloat interpolate( StkFloat frame, unsigned int channel = 0 ) const;
372 
374  size_t size() const { return size_; };
375 
377  bool empty() const;
378 
380 
387  virtual void resize( size_t nFrames, unsigned int nChannels = 1 );
388 
390 
397  virtual void resize( size_t nFrames, unsigned int nChannels, StkFloat value );
398 
400 
405  StkFrames& getChannel(unsigned int channel,StkFrames& destinationFrames, unsigned int destinationChannel) const;
406 
408 
413  void setChannel(unsigned int channel,const StkFrames &sourceFrames,unsigned int sourceChannel);
414 
416  unsigned int channels( void ) const { return nChannels_; };
417 
419  unsigned int frames( void ) const { return (unsigned int)nFrames_; };
420 
422 
426  void setDataRate( StkFloat rate ) { dataRate_ = rate; };
427 
429 
433  StkFloat dataRate( void ) const { return dataRate_; };
434 
435 protected:
436 
437  StkFloat *data_;
438  StkFloat dataRate_;
439  size_t nFrames_;
440  unsigned int nChannels_;
441  size_t size_;
442  size_t bufferSize_;
443 
444 };
445 
446 inline bool StkFrames :: empty() const
447 {
448  if ( size_ > 0 ) return false;
449  else return true;
450 }
451 
452 inline StkFloat& StkFrames :: operator[] ( size_t n )
453 {
454 #if defined(_STK_DEBUG_)
455  if ( n >= size_ ) {
456  std::ostringstream error;
457  error << "StkFrames::operator[]: invalid index (" << n << ") value!";
458  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
459  }
460 #endif
461 
462  return data_[n];
463 }
464 
465 inline StkFloat StkFrames :: operator[] ( size_t n ) const
466 {
467 #if defined(_STK_DEBUG_)
468  if ( n >= size_ ) {
469  std::ostringstream error;
470  error << "StkFrames::operator[]: invalid index (" << n << ") value!";
471  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
472  }
473 #endif
474 
475  return data_[n];
476 }
477 
478 inline StkFloat& StkFrames :: operator() ( size_t frame, unsigned int channel )
479 {
480 #if defined(_STK_DEBUG_)
481  if ( frame >= nFrames_ || channel >= nChannels_ ) {
482  std::ostringstream error;
483  error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
484  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
485  }
486 #endif
487 
488  return data_[ frame * nChannels_ + channel ];
489 }
490 
491 inline StkFloat StkFrames :: operator() ( size_t frame, unsigned int channel ) const
492 {
493 #if defined(_STK_DEBUG_)
494  if ( frame >= nFrames_ || channel >= nChannels_ ) {
495  std::ostringstream error;
496  error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
497  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
498  }
499 #endif
500 
501  return data_[ frame * nChannels_ + channel ];
502 }
503 
505 {
506 #if defined(_STK_DEBUG_)
507  if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
508  std::ostringstream error;
509  error << "StkFrames::operator+: frames argument must be of equal dimensions!";
510  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
511  }
512 #endif
513  StkFrames sum((unsigned int)nFrames_,nChannels_);
514  StkFloat *sumPtr = &sum[0];
515  const StkFloat *fptr = f.data_;
516  const StkFloat *dPtr = data_;
517  for (unsigned int i = 0; i < size_; i++) {
518  *sumPtr++ = *fptr++ + *dPtr++;
519  }
520  return sum;
521 }
522 
524 {
525 #if defined(_STK_DEBUG_)
526  if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
527  std::ostringstream error;
528  error << "StkFrames::operator+=: frames argument must be of equal dimensions!";
529  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
530  }
531 #endif
532 
533  StkFloat *fptr = &f[0];
534  StkFloat *dptr = data_;
535  for ( unsigned int i=0; i<size_; i++ )
536  *dptr++ += *fptr++;
537  return *this;
538 }
539 
541 {
542 #if defined(_STK_DEBUG_)
543  if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
544  std::ostringstream error;
545  error << "StkFrames::operator*=: frames argument must be of equal dimensions!";
546  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
547  }
548 #endif
549 
550  StkFloat *fptr = &f[0];
551  StkFloat *dptr = data_;
552  for ( unsigned int i=0; i<size_; i++ )
553  *dptr++ *= *fptr++;
554  return *this;
555 }
556 
557 inline StkFrames StkFrames::operator*(StkFloat v) const
558 {
559  StkFrames res((unsigned int)nFrames_, nChannels_);
560  StkFloat *resPtr = &res[0];
561  const StkFloat *dPtr = data_;
562  for (unsigned int i = 0; i < size_; i++) {
563  *resPtr++ = v * *dPtr++;
564  }
565  return res;
566 }
567 
568 inline StkFrames operator*(StkFloat v, const StkFrames& f)
569 {
570  StkFrames res((unsigned int)f.nFrames_, f.nChannels_);
571  StkFloat *resPtr = &res[0];
572  StkFloat *dPtr = f.data_;
573  for (unsigned int i = 0; i < f.size_; i++) {
574  *resPtr++ = v * *dPtr++;
575  }
576  return res;
577 }
578 
579 inline StkFrames& StkFrames :: operator*= ( StkFloat v )
580 {
581  StkFloat *dptr = data_;
582  for ( unsigned int i=0; i<size_; i++ )
583  *dptr++ *= v;
584  return *this;
585 }
586 
587 
588 // Here are a few other useful typedefs.
589 typedef unsigned short UINT16;
590 typedef unsigned int UINT32;
591 typedef signed short SINT16;
592 typedef signed int SINT32;
593 typedef float FLOAT32;
594 typedef double FLOAT64;
595 
596 // The default sampling rate.
597 const StkFloat SRATE = 44100.0;
598 
599 // The default real-time audio input and output buffer size. If
600 // clicks are occuring in the input and/or output sound stream, a
601 // larger buffer size may help. Larger buffer sizes, however, produce
602 // more latency.
603 const unsigned int RT_BUFFER_SIZE = 512;
604 
605 // The default rawwave path value is set with the preprocessor
606 // definition RAWWAVE_PATH. This can be specified as an argument to
607 // the configure script, in an integrated development environment, or
608 // below. The global STK rawwave path variable can be dynamically set
609 // with the Stk::setRawwavePath() function. This value is
610 // concatenated to the beginning of all references to rawwave files in
611 // the various STK core classes (e.g. Clarinet.cpp). If you wish to
612 // move the rawwaves directory to a different location in your file
613 // system, you will need to set this path definition appropriately.
614 #if !defined(RAWWAVE_PATH)
615  #define RAWWAVE_PATH "../../rawwaves/"
616 #endif
617 
618 const StkFloat PI = 3.14159265358979;
619 const StkFloat TWO_PI = 2 * PI;
620 const StkFloat ONE_OVER_128 = 0.0078125;
621 
622 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_MM__)
623  #define __OS_WINDOWS__
624  #define __STK_REALTIME__
625 #elif defined(__LINUX_OSS__) || defined(__LINUX_ALSA__) || defined(__UNIX_JACK__)
626  #define __OS_LINUX__
627  #define __STK_REALTIME__
628 #elif defined(__IRIX_AL__)
629  #define __OS_IRIX__
630 #elif defined(__MACOSX_CORE__) || defined(__UNIX_JACK__)
631  #define __OS_MACOSX__
632  #define __STK_REALTIME__
633 #endif
634 
635 } // stk namespace
636 
637 #endif

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