Arduino STK  4.6.2
Stk.h
1 #ifndef STK_STK_H
2 #define STK_STK_H
3 
4 #include "ArdConfig.h"
5 #include <string>
6 #include <cstring>
7 #include <vector>
8 #include <iostream>
9 #include <sstream>
10 #ifdef __ARDUINO__
11 #include <cstdlib>
12 #endif
13 
14 
21 namespace stk {
22 
23 /***************************************************/
73 /***************************************************/
74 
75 //#define _STK_DEBUG_
76 
77 // Most data in STK is passed and calculated with the
78 // following user-definable floating-point type. You
79 // can change this to "float" if you prefer or perhaps
80 // a "long double" in the future.
81 typedef float StkFloat;
82 
84 
89 class StkError
90 {
91 public:
92  enum Type {
93  STATUS,
94  WARNING,
95  DEBUG_PRINT,
96  MEMORY_ALLOCATION,
97  MEMORY_ACCESS,
98  FUNCTION_ARGUMENT,
99  FILE_NOT_FOUND,
100  FILE_UNKNOWN_FORMAT,
101  FILE_ERROR,
102  PROCESS_THREAD,
103  PROCESS_SOCKET,
104  PROCESS_SOCKET_IPADDR,
105  AUDIO_SYSTEM,
106  MIDI_SYSTEM,
107  UNSPECIFIED
108  };
109 
110 protected:
111  std::string message_;
112  Type type_;
113 
114 public:
116  StkError(const std::string& message, Type type = StkError::UNSPECIFIED)
117  : message_(message), type_(type) {}
118 
120  virtual ~StkError(void) {};
121 
123  virtual void printMessage(void) {
124  #ifdef __ARDUINO__
125  std::ostream* out = &std::cout;
126  #else
127  std::ostream* out = &std::cerr;
128  #endif
129  *out << '\n' << message_ << "\n\n";
130  }
131 
133  virtual const Type& getType(void) { return type_; }
134 
136  virtual const std::string& getMessage(void) { return message_; }
137 
139  virtual const char *getMessageCString(void) { return message_.c_str(); }
140 };
141 
142 
143 class Stk
144 {
145 public:
146 
147  typedef unsigned long StkFormat;
148  static const StkFormat STK_SINT8;
149  static const StkFormat STK_SINT16;
150  static const StkFormat STK_SINT24;
151  static const StkFormat STK_SINT32;
152  static const StkFormat STK_FLOAT32;
153  static const StkFormat STK_FLOAT64;
156  static StkFloat sampleRate( void ) { return srate_; }
157 
159 
176  static void setSampleRate( StkFloat rate );
177 
179 
184  void ignoreSampleRateChange( bool ignore = true ) { ignoreSampleRateChange_ = ignore; };
185 
187  static void clear_alertList(){std::vector<Stk *>().swap(alertList_);};
188 
190  static std::string rawwavePath(void) { return rawwavepath_; }
191 
193  static void setRawwavePath( std::string path );
194 
196  static void swap16( unsigned char *ptr );
197 
199  static void swap32( unsigned char *ptr );
200 
202  static void swap64( unsigned char *ptr );
203 
205  static void sleep( unsigned long milliseconds );
206 
208  static bool inRange( StkFloat value, StkFloat min, StkFloat max ) {
209  if ( value < min ) return false;
210  else if ( value > max ) return false;
211  else return true;
212  }
213 
215  static void handleError( const char *message, StkError::Type type );
216 
218  static void handleError( std::string message, StkError::Type type );
219 
221  static void showWarnings( bool status ) { showWarnings_ = status; }
222 
224  static void printErrors( bool status ) { printErrors_ = status; }
225 
226 private:
227  static StkFloat srate_;
228  static std::string rawwavepath_;
229  static bool showWarnings_;
230  static bool printErrors_;
231  static std::vector<Stk *> alertList_;
232 
233 protected:
234 
235  static std::ostringstream oStream_;
236  bool ignoreSampleRateChange_;
237 
239  Stk( void );
240 
242  virtual ~Stk( void );
243 
245  virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
246 
248  void addSampleRateAlert( Stk *ptr );
249 
252 
254  void handleError( StkError::Type type ) const;
255 };
256 
257 
258 /***************************************************/
284 /***************************************************/
285 
287 {
288 public:
289 
291  StkFrames( unsigned int nFrames = 0, unsigned int nChannels = 0 );
292 
294  StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels );
295 
298 
299  // A copy constructor.
300  StkFrames( const StkFrames& f );
301 
302  // Assignment operator that returns a reference to self.
303  StkFrames& operator= ( const StkFrames& f );
304 
306 
312  StkFloat& operator[] ( size_t n );
313 
315 
319  StkFloat operator[] ( size_t n ) const;
320 
322 
327  StkFrames operator+(const StkFrames &frames) const;
328 
330 
335  void operator+= ( StkFrames& f );
336 
338 
343  void operator*= ( StkFrames& f );
344 
346 
353  StkFloat& operator() ( size_t frame, unsigned int channel );
354 
356 
361  StkFloat operator() ( size_t frame, unsigned int channel ) const;
362 
364 
370  StkFloat interpolate( StkFloat frame, unsigned int channel = 0 ) const;
371 
373  size_t size() const { return size_; };
374 
376  bool empty() const;
377 
379 
386  void resize( size_t nFrames, unsigned int nChannels = 1 );
387 
389 
396  void resize( size_t nFrames, unsigned int nChannels, StkFloat value );
397 
399 
404  StkFrames& getChannel(unsigned int channel,StkFrames& destinationFrames, unsigned int destinationChannel) const;
405 
407 
412  void setChannel(unsigned int channel,const StkFrames &sourceFrames,unsigned int sourceChannel);
413 
415  unsigned int channels( void ) const { return nChannels_; };
416 
418  unsigned int frames( void ) const { return (unsigned int)nFrames_; };
419 
421 
425  void setDataRate( StkFloat rate ) { dataRate_ = rate; };
426 
428 
432  StkFloat dataRate( void ) const { return dataRate_; };
433 
434 private:
435 
436  StkFloat *data_;
437  StkFloat dataRate_;
438  size_t nFrames_;
439  unsigned int nChannels_;
440  size_t size_;
441  size_t bufferSize_;
442 
443 };
444 
445 inline bool StkFrames :: empty() const
446 {
447  if ( size_ > 0 ) return false;
448  else return true;
449 }
450 
451 inline StkFloat& StkFrames :: operator[] ( size_t n )
452 {
453 #if defined(_STK_DEBUG_)
454  if ( n >= size_ ) {
455  std::ostringstream error;
456  error << "StkFrames::operator[]: invalid index (" << n << ") value!";
457  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
458  }
459 #endif
460 
461  return data_[n];
462 }
463 
464 inline StkFloat StkFrames :: operator[] ( size_t n ) const
465 {
466 #if defined(_STK_DEBUG_)
467  if ( n >= size_ ) {
468  std::ostringstream error;
469  error << "StkFrames::operator[]: invalid index (" << n << ") value!";
470  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
471  }
472 #endif
473 
474  return data_[n];
475 }
476 
477 inline StkFloat& StkFrames :: operator() ( size_t frame, unsigned int channel )
478 {
479 #if defined(_STK_DEBUG_)
480  if ( frame >= nFrames_ || channel >= nChannels_ ) {
481  std::ostringstream error;
482  error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
483  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
484  }
485 #endif
486 
487  return data_[ frame * nChannels_ + channel ];
488 }
489 
490 inline StkFloat StkFrames :: operator() ( size_t frame, unsigned int channel ) const
491 {
492 #if defined(_STK_DEBUG_)
493  if ( frame >= nFrames_ || channel >= nChannels_ ) {
494  std::ostringstream error;
495  error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
496  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
497  }
498 #endif
499 
500  return data_[ frame * nChannels_ + channel ];
501 }
502 
504 {
505 #if defined(_STK_DEBUG_)
506  if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
507  std::ostringstream error;
508  error << "StkFrames::operator+: frames argument must be of equal dimensions!";
509  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
510  }
511 #endif
512  StkFrames sum((unsigned int)nFrames_,nChannels_);
513  StkFloat *sumPtr = &sum[0];
514  const StkFloat *fptr = f.data_;
515  const StkFloat *dPtr = data_;
516  for (unsigned int i = 0; i < size_; i++) {
517  *sumPtr++ = *fptr++ + *dPtr++;
518  }
519  return sum;
520 }
521 
523 {
524 #if defined(_STK_DEBUG_)
525  if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
526  std::ostringstream error;
527  error << "StkFrames::operator+=: frames argument must be of equal dimensions!";
528  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
529  }
530 #endif
531 
532  StkFloat *fptr = &f[0];
533  StkFloat *dptr = data_;
534  for ( unsigned int i=0; i<size_; i++ )
535  *dptr++ += *fptr++;
536 }
537 
539 {
540 #if defined(_STK_DEBUG_)
541  if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
542  std::ostringstream error;
543  error << "StkFrames::operator*=: frames argument must be of equal dimensions!";
544  Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
545  }
546 #endif
547 
548  StkFloat *fptr = &f[0];
549  StkFloat *dptr = data_;
550  for ( unsigned int i=0; i<size_; i++ )
551  *dptr++ *= *fptr++;
552 }
553 
554 // Here are a few other useful typedefs.
555 typedef unsigned short UINT16;
556 typedef unsigned int UINT32;
557 typedef signed short SINT16;
558 typedef signed int SINT32;
559 typedef float FLOAT32;
560 typedef double FLOAT64;
561 
562 // The default sampling rate.
563 const StkFloat SRATE = 44100.0;
564 
565 // The default real-time audio input and output buffer size. If
566 // clicks are occuring in the input and/or output sound stream, a
567 // larger buffer size may help. Larger buffer sizes, however, produce
568 // more latency.
569 const unsigned int RT_BUFFER_SIZE = 512;
570 
571 // The default rawwave path value is set with the preprocessor
572 // definition RAWWAVE_PATH. This can be specified as an argument to
573 // the configure script, in an integrated development environment, or
574 // below. The global STK rawwave path variable can be dynamically set
575 // with the Stk::setRawwavePath() function. This value is
576 // concatenated to the beginning of all references to rawwave files in
577 // the various STK core classes (e.g. Clarinet.cpp). If you wish to
578 // move the rawwaves directory to a different location in your file
579 // system, you will need to set this path definition appropriately.
580 #if !defined(RAWWAVE_PATH)
581  #define RAWWAVE_PATH "../../rawwaves/"
582 #endif
583 
584 const StkFloat STK_PI = 3.14159265358979;
585 const StkFloat STK_TWO_PI = 2 * STK_PI;
586 
587 const StkFloat ONE_OVER_128 = 0.0078125;
588 
589 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_MM__)
590  #define __OS_WINDOWS__
591  #define __STK_REALTIME__
592 #elif defined(__LINUX_OSS__) || defined(__LINUX_ALSA__) || defined(__UNIX_JACK__)
593  #define __OS_LINUX__
594  #define __STK_REALTIME__
595 #elif defined(__IRIX_AL__)
596  #define __OS_IRIX__
597 #elif defined(__MACOSX_CORE__) || defined(__UNIX_JACK__)
598  #define __OS_MACOSX__
599  #define __STK_REALTIME__
600 #endif
601 
602 } // stk namespace
603 
604 #endif
STK error handling class.
Definition: Stk.h:90
virtual void printMessage(void)
Prints thrown error message to stderr.
Definition: Stk.h:123
virtual const Type & getType(void)
Returns the thrown error message type.
Definition: Stk.h:133
virtual ~StkError(void)
The destructor.
Definition: Stk.h:120
StkError(const std::string &message, Type type=StkError::UNSPECIFIED)
The constructor.
Definition: Stk.h:116
virtual const std::string & getMessage(void)
Returns the thrown error message string.
Definition: Stk.h:136
virtual const char * getMessageCString(void)
Returns the thrown error message as a C string.
Definition: Stk.h:139
An STK class to handle vectorized audio data.
Definition: Stk.h:287
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:415
void operator*=(StkFrames &f)
Assignment by product operator into self.
Definition: Stk.h:538
void operator+=(StkFrames &f)
Assignment by sum operator into self.
Definition: Stk.h:522
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition: Stk.h:418
size_t size() const
Returns the total number of audio samples represented by the object.
Definition: Stk.h:373
void resize(size_t nFrames, unsigned int nChannels=1)
Resize self to represent the specified number of channels and frames.
~StkFrames()
The destructor.
StkFloat interpolate(StkFloat frame, unsigned int channel=0) const
Return an interpolated value at the fractional frame index and channel.
StkFrames operator+(const StkFrames &frames) const
Sum operator.
Definition: Stk.h:503
void resize(size_t nFrames, unsigned int nChannels, StkFloat value)
Resize self to represent the specified number of channels and frames and perform element initializati...
StkFloat dataRate(void) const
Return the sample rate associated with the StkFrames data.
Definition: Stk.h:432
bool empty() const
Returns true if the object size is zero and false otherwise.
Definition: Stk.h:445
StkFloat & operator()(size_t frame, unsigned int channel)
Channel / frame subscript operator that returns a reference.
Definition: Stk.h:477
StkFloat & operator[](size_t n)
Subscript operator that returns a reference to element n of self.
Definition: Stk.h:451
StkFrames & getChannel(unsigned int channel, StkFrames &destinationFrames, unsigned int destinationChannel) const
Retrieves a single channel.
StkFrames(unsigned int nFrames=0, unsigned int nChannels=0)
The default constructor initializes the frame data structure to size zero.
StkFrames(const StkFloat &value, unsigned int nFrames, unsigned int nChannels)
Overloaded constructor that initializes the frame data to the specified size with value.
void setChannel(unsigned int channel, const StkFrames &sourceFrames, unsigned int sourceChannel)
Sets a single channel.
void setDataRate(StkFloat rate)
Set the sample rate associated with the StkFrames data.
Definition: Stk.h:425
STK base class.
Definition: Stk.h:144
static const StkFormat STK_FLOAT32
Definition: Stk.h:152
static void showWarnings(bool status)
Toggle display of WARNING and STATUS messages.
Definition: Stk.h:221
static void clear_alertList()
Static method that frees memory from alertList_.
Definition: Stk.h:187
void removeSampleRateAlert(Stk *ptr)
Remove class pointer from list for sample rate change notification.
static void setSampleRate(StkFloat rate)
Static method that sets the STK sample rate.
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
static void setRawwavePath(std::string path)
Static method that sets the STK rawwave path.
static const StkFormat STK_FLOAT64
Definition: Stk.h:153
static const StkFormat STK_SINT16
Definition: Stk.h:149
void addSampleRateAlert(Stk *ptr)
Add class pointer to list for sample rate change notification.
static std::string rawwavePath(void)
Static method that returns the current rawwave path.
Definition: Stk.h:190
static void swap64(unsigned char *ptr)
Static method that byte-swaps a 64-bit data type.
static StkFloat sampleRate(void)
Static method that returns the current STK sample rate.
Definition: Stk.h:156
static const StkFormat STK_SINT32
Definition: Stk.h:151
virtual void sampleRateChanged(StkFloat newRate, StkFloat oldRate)
This function should be implemented in subclasses that depend on the sample rate.
static bool inRange(StkFloat value, StkFloat min, StkFloat max)
Static method to check whether a value is within a specified range.
Definition: Stk.h:208
static void swap32(unsigned char *ptr)
Static method that byte-swaps a 32-bit data type.
Stk(void)
Default constructor.
static void swap16(unsigned char *ptr)
Static method that byte-swaps a 16-bit data type.
void ignoreSampleRateChange(bool ignore=true)
A function to enable/disable the automatic updating of class data when the STK sample rate changes.
Definition: Stk.h:184
static void handleError(std::string message, StkError::Type type)
Static function for error reporting and handling using c++ strings.
virtual ~Stk(void)
Class destructor.
static void sleep(unsigned long milliseconds)
Static cross-platform method to sleep for a number of milliseconds.
static const StkFormat STK_SINT8
Definition: Stk.h:148
void handleError(StkError::Type type) const
Internal function for error reporting that assumes message in oStream_ variable.
static const StkFormat STK_SINT24
Definition: Stk.h:150
static void printErrors(bool status)
Toggle display of error messages before throwing exceptions.
Definition: Stk.h:224
The STK namespace.
Definition: ADSR.h:8