The Synthesis ToolKit in C++ (STK)

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 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 void clear_alertList(){std::vector<Stk *>().swap(alertList_);};
177
179 static std::string rawwavePath(void) { return rawwavepath_; }
180
182 static void setRawwavePath( std::string path );
183
185 static void swap16( unsigned char *ptr );
186
188 static void swap32( unsigned char *ptr );
189
191 static void swap64( unsigned char *ptr );
192
194 static void sleep( unsigned long milliseconds );
195
197 static bool inRange( StkFloat value, StkFloat min, StkFloat max ) {
198 if ( value < min ) return false;
199 else if ( value > max ) return false;
200 else return true;
201 }
202
204 static void handleError( const char *message, StkError::Type type );
205
207 static void handleError( std::string message, StkError::Type type );
208
210 static void showWarnings( bool status ) { showWarnings_ = status; }
211
213 static void printErrors( bool status ) { printErrors_ = status; }
214
215 private:
216 static StkFloat srate_;
217 static std::string rawwavepath_;
218 static bool showWarnings_;
219 static bool printErrors_;
220 static std::vector<Stk *> alertList_;
221
222 protected:
223
224 static std::ostringstream oStream_;
225 bool ignoreSampleRateChange_;
226
228 Stk( void );
229
231 virtual ~Stk( void );
232
234 virtual void sampleRateChanged( StkFloat newRate, StkFloat oldRate );
235
237 void addSampleRateAlert( Stk *ptr );
238
240 void removeSampleRateAlert( Stk *ptr );
241
243 void handleError( StkError::Type type ) const;
244 };
245
246
247 /***************************************************/
273 /***************************************************/
274
275 class StkFrames
276 {
277 public:
278
280 StkFrames( unsigned int nFrames = 0, unsigned int nChannels = 0 );
281
283 StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels );
284
286 ~StkFrames();
287
288 // A copy constructor.
289 StkFrames( const StkFrames& f );
290
291 // Assignment operator that returns a reference to self.
292 StkFrames& operator= ( const StkFrames& f );
293
295
301 StkFloat& operator[] ( size_t n );
302
304
308 StkFloat operator[] ( size_t n ) const;
309
311
316 StkFrames operator+(const StkFrames &frames) const;
317
319
324 void operator+= ( StkFrames& f );
325
327
332 void operator*= ( StkFrames& f );
333
335
342 StkFloat& operator() ( size_t frame, unsigned int channel );
343
345
350 StkFloat operator() ( size_t frame, unsigned int channel ) const;
351
353
359 StkFloat interpolate( StkFloat frame, unsigned int channel = 0 ) const;
360
362 size_t size() const { return size_; };
363
365 bool empty() const;
366
368
375 void resize( size_t nFrames, unsigned int nChannels = 1 );
376
378
385 void resize( size_t nFrames, unsigned int nChannels, StkFloat value );
386
388
393 StkFrames& getChannel(unsigned int channel,StkFrames& destinationFrames, unsigned int destinationChannel) const;
394
396
401 void setChannel(unsigned int channel,const StkFrames &sourceFrames,unsigned int sourceChannel);
402
404 unsigned int channels( void ) const { return nChannels_; };
405
407 unsigned int frames( void ) const { return (unsigned int)nFrames_; };
408
410
414 void setDataRate( StkFloat rate ) { dataRate_ = rate; };
415
417
421 StkFloat dataRate( void ) const { return dataRate_; };
422
423 private:
424
425 StkFloat *data_;
426 StkFloat dataRate_;
427 size_t nFrames_;
428 unsigned int nChannels_;
429 size_t size_;
430 size_t bufferSize_;
431
432 };
433
434 inline bool StkFrames :: empty() const
435 {
436 if ( size_ > 0 ) return false;
437 else return true;
438 }
439
440 inline StkFloat& StkFrames :: operator[] ( size_t n )
441 {
442 #if defined(_STK_DEBUG_)
443 if ( n >= size_ ) {
444 std::ostringstream error;
445 error << "StkFrames::operator[]: invalid index (" << n << ") value!";
446 Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
447 }
448 #endif
449
450 return data_[n];
451 }
452
453 inline StkFloat StkFrames :: operator[] ( size_t n ) const
454 {
455 #if defined(_STK_DEBUG_)
456 if ( n >= size_ ) {
457 std::ostringstream error;
458 error << "StkFrames::operator[]: invalid index (" << n << ") value!";
459 Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
460 }
461 #endif
462
463 return data_[n];
464 }
465
466 inline StkFloat& StkFrames :: operator() ( size_t frame, unsigned int channel )
467 {
468 #if defined(_STK_DEBUG_)
469 if ( frame >= nFrames_ || channel >= nChannels_ ) {
470 std::ostringstream error;
471 error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
472 Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
473 }
474 #endif
475
476 return data_[ frame * nChannels_ + channel ];
477 }
478
479 inline StkFloat StkFrames :: operator() ( size_t frame, unsigned int channel ) const
480 {
481 #if defined(_STK_DEBUG_)
482 if ( frame >= nFrames_ || channel >= nChannels_ ) {
483 std::ostringstream error;
484 error << "StkFrames::operator(): invalid frame (" << frame << ") or channel (" << channel << ") value!";
485 Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
486 }
487 #endif
488
489 return data_[ frame * nChannels_ + channel ];
490 }
491
492 inline StkFrames StkFrames::operator+(const StkFrames &f) const
493 {
494 #if defined(_STK_DEBUG_)
495 if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
496 std::ostringstream error;
497 error << "StkFrames::operator+: frames argument must be of equal dimensions!";
498 Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
499 }
500 #endif
501 StkFrames sum((unsigned int)nFrames_,nChannels_);
502 StkFloat *sumPtr = &sum[0];
503 const StkFloat *fptr = f.data_;
504 const StkFloat *dPtr = data_;
505 for (unsigned int i = 0; i < size_; i++) {
506 *sumPtr++ = *fptr++ + *dPtr++;
507 }
508 return sum;
509 }
510
511 inline void StkFrames :: operator+= ( StkFrames& f )
512 {
513 #if defined(_STK_DEBUG_)
514 if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
515 std::ostringstream error;
516 error << "StkFrames::operator+=: frames argument must be of equal dimensions!";
517 Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
518 }
519 #endif
520
521 StkFloat *fptr = &f[0];
522 StkFloat *dptr = data_;
523 for ( unsigned int i=0; i<size_; i++ )
524 *dptr++ += *fptr++;
525 }
526
527 inline void StkFrames :: operator*= ( StkFrames& f )
528 {
529 #if defined(_STK_DEBUG_)
530 if ( f.frames() != nFrames_ || f.channels() != nChannels_ ) {
531 std::ostringstream error;
532 error << "StkFrames::operator*=: frames argument must be of equal dimensions!";
533 Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
534 }
535 #endif
536
537 StkFloat *fptr = &f[0];
538 StkFloat *dptr = data_;
539 for ( unsigned int i=0; i<size_; i++ )
540 *dptr++ *= *fptr++;
541 }
542
543 // Here are a few other useful typedefs.
544 typedef unsigned short UINT16;
545 typedef unsigned int UINT32;
546 typedef signed short SINT16;
547 typedef signed int SINT32;
548 typedef float FLOAT32;
549 typedef double FLOAT64;
550
551 // The default sampling rate.
552 const StkFloat SRATE = 44100.0;
553
554 // The default real-time audio input and output buffer size. If
555 // clicks are occuring in the input and/or output sound stream, a
556 // larger buffer size may help. Larger buffer sizes, however, produce
557 // more latency.
558 const unsigned int RT_BUFFER_SIZE = 512;
559
560 // The default rawwave path value is set with the preprocessor
561 // definition RAWWAVE_PATH. This can be specified as an argument to
562 // the configure script, in an integrated development environment, or
563 // below. The global STK rawwave path variable can be dynamically set
564 // with the Stk::setRawwavePath() function. This value is
565 // concatenated to the beginning of all references to rawwave files in
566 // the various STK core classes (e.g. Clarinet.cpp). If you wish to
567 // move the rawwaves directory to a different location in your file
568 // system, you will need to set this path definition appropriately.
569 #if !defined(RAWWAVE_PATH)
570 #define RAWWAVE_PATH "../../rawwaves/"
571 #endif
572
573 const StkFloat PI = 3.14159265358979;
574 const StkFloat TWO_PI = 2 * PI;
575 const StkFloat ONE_OVER_128 = 0.0078125;
576
577 #if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_MM__)
578 #define __OS_WINDOWS__
579 #define __STK_REALTIME__
580 #elif defined(__LINUX_OSS__) || defined(__LINUX_ALSA__) || defined(__UNIX_JACK__)
581 #define __OS_LINUX__
582 #define __STK_REALTIME__
583 #elif defined(__IRIX_AL__)
584 #define __OS_IRIX__
585 #elif defined(__MACOSX_CORE__) || defined(__UNIX_JACK__)
586 #define __OS_MACOSX__
587 #define __STK_REALTIME__
588 #endif
589
590 } // stk namespace
591
592 #endif
stk::Stk::~Stk
virtual ~Stk(void)
Class destructor.
stk::Stk::swap32
static void swap32(unsigned char *ptr)
Static method that byte-swaps a 32-bit data type.
stk::StkFrames::~StkFrames
~StkFrames()
The destructor.
stk::StkFrames::frames
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition: Stk.h:407
stk::Stk::sampleRateChanged
virtual void sampleRateChanged(StkFloat newRate, StkFloat oldRate)
This function should be implemented in subclasses that depend on the sample rate.
stk::StkFrames::size
size_t size() const
Returns the total number of audio samples represented by the object.
Definition: Stk.h:362
stk::Stk::clear_alertList
static void clear_alertList()
Static method that frees memory from alertList_.
Definition: Stk.h:176
stk::StkError::printMessage
virtual void printMessage(void)
Prints thrown error message to stderr.
Definition: Stk.h:119
stk::Stk::rawwavePath
static std::string rawwavePath(void)
Static method that returns the current rawwave path.
Definition: Stk.h:179
stk::StkError::getMessageCString
virtual const char * getMessageCString(void)
Returns the thrown error message as a C string.
Definition: Stk.h:128
stk::Stk::swap16
static void swap16(unsigned char *ptr)
Static method that byte-swaps a 16-bit data type.
stk::StkFrames::operator[]
StkFloat & operator[](size_t n)
Subscript operator that returns a reference to element n of self.
Definition: Stk.h:440
stk::Stk::sleep
static void sleep(unsigned long milliseconds)
Static cross-platform method to sleep for a number of milliseconds.
stk::Stk::setRawwavePath
static void setRawwavePath(std::string path)
Static method that sets the STK rawwave path.
stk::Stk::sampleRate
static StkFloat sampleRate(void)
Static method that returns the current STK sample rate.
Definition: Stk.h:145
stk::StkFrames::resize
void resize(size_t nFrames, unsigned int nChannels=1)
Resize self to represent the specified number of channels and frames.
stk::StkFrames::operator*=
void operator*=(StkFrames &f)
Assignment by product operator into self.
Definition: Stk.h:527
stk::StkError::StkError
StkError(const std::string &message, Type type=StkError::UNSPECIFIED)
The constructor.
Definition: Stk.h:112
stk::StkFrames
An STK class to handle vectorized audio data.
Definition: Stk.h:275
stk::Stk::setSampleRate
static void setSampleRate(StkFloat rate)
Static method that sets the STK sample rate.
stk::StkFrames::operator+
StkFrames operator+(const StkFrames &frames) const
Sum operator.
Definition: Stk.h:492
stk::StkFrames::StkFrames
StkFrames(unsigned int nFrames=0, unsigned int nChannels=0)
The default constructor initializes the frame data structure to size zero.
stk::Stk::showWarnings
static void showWarnings(bool status)
Toggle display of WARNING and STATUS messages.
Definition: Stk.h:210
stk::StkFrames::setDataRate
void setDataRate(StkFloat rate)
Set the sample rate associated with the StkFrames data.
Definition: Stk.h:414
stk::Stk::STK_SINT16
static const StkFormat STK_SINT16
Definition: Stk.h:138
stk::StkFrames::channels
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:404
stk::Stk::handleError
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
stk::Stk::swap64
static void swap64(unsigned char *ptr)
Static method that byte-swaps a 64-bit data type.
stk::StkFrames::operator()
StkFloat & operator()(size_t frame, unsigned int channel)
Channel / frame subscript operator that returns a reference.
Definition: Stk.h:466
stk::Stk::removeSampleRateAlert
void removeSampleRateAlert(Stk *ptr)
Remove class pointer from list for sample rate change notification.
stk::Stk::STK_FLOAT32
static const StkFormat STK_FLOAT32
Definition: Stk.h:141
stk::Stk::addSampleRateAlert
void addSampleRateAlert(Stk *ptr)
Add class pointer to list for sample rate change notification.
stk::StkFrames::interpolate
StkFloat interpolate(StkFloat frame, unsigned int channel=0) const
Return an interpolated value at the fractional frame index and channel.
stk::StkFrames::dataRate
StkFloat dataRate(void) const
Return the sample rate associated with the StkFrames data.
Definition: Stk.h:421
stk::StkFrames::operator+=
void operator+=(StkFrames &f)
Assignment by sum operator into self.
Definition: Stk.h:511
stk::StkError::getType
virtual const Type & getType(void)
Returns the thrown error message type.
Definition: Stk.h:122
stk::Stk::ignoreSampleRateChange
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:173
stk::StkError::getMessage
virtual const std::string & getMessage(void)
Returns the thrown error message string.
Definition: Stk.h:125
stk::Stk::printErrors
static void printErrors(bool status)
Toggle display of error messages before throwing exceptions.
Definition: Stk.h:213
stk
The STK namespace.
Definition: ADSR.h:6
stk::Stk::inRange
static bool inRange(StkFloat value, StkFloat min, StkFloat max)
Static method to check whether a value is within a specified range.
Definition: Stk.h:197
stk::StkError::~StkError
virtual ~StkError(void)
The destructor.
Definition: Stk.h:116
stk::Stk::Stk
Stk(void)
Default constructor.
stk::Stk::STK_FLOAT64
static const StkFormat STK_FLOAT64
Definition: Stk.h:142
stk::StkFrames::setChannel
void setChannel(unsigned int channel, const StkFrames &sourceFrames, unsigned int sourceChannel)
Sets a single channel.
stk::Stk::STK_SINT24
static const StkFormat STK_SINT24
Definition: Stk.h:139
stk::StkFrames::empty
bool empty() const
Returns true if the object size is zero and false otherwise.
Definition: Stk.h:434
stk::StkFrames::getChannel
StkFrames & getChannel(unsigned int channel, StkFrames &destinationFrames, unsigned int destinationChannel) const
Retrieves a single channel.
stk::Stk::STK_SINT8
static const StkFormat STK_SINT8
Definition: Stk.h:137
stk::Stk::STK_SINT32
static const StkFormat STK_SINT32
Definition: Stk.h:140
stk::Stk
STK base class.
Definition: Stk.h:132
stk::StkError
STK error handling class.
Definition: Stk.h:85

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