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 <stdexcept>
10//#include <cstdlib>
11
18namespace 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.
78typedef double StkFloat;
79
81
86 class StkError : public std::exception
87{
88public:
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
107protected:
108 std::string message_;
109 Type type_;
110
111public:
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{
137public:
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;
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
218private:
219 static StkFloat srate_;
220 static std::string rawwavepath_;
221 static bool showWarnings_;
222 static bool printErrors_;
223 static std::vector<Stk *> alertList_;
224
225protected:
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
278 class StkFrames
279{
280public:
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
327 StkFrames& operator+= ( StkFrames& f );
328
330
335 StkFrames& operator*= ( StkFrames& f );
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
435protected:
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
504 inline StkFrames StkFrames::operator+(const StkFrames &f) const
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
523 inline StkFrames& StkFrames :: operator+= ( StkFrames& f )
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
540 inline StkFrames& StkFrames :: operator*= ( StkFrames& f )
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.
589typedef unsigned short UINT16;
590typedef unsigned int UINT32;
591typedef signed short SINT16;
592typedef signed int SINT32;
593typedef float FLOAT32;
594typedef double FLOAT64;
595
596// The default sampling rate.
597const 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.
603const 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
618const StkFloat PI = 3.14159265358979;
619const StkFloat TWO_PI = 2 * PI;
620const 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
stk::StkError
STK error handling class.
Definition: Stk.h:87
stk::StkError::printMessage
virtual void printMessage(void)
Prints thrown error message to stderr.
Definition: Stk.h:120
stk::StkError::getMessage
virtual const std::string & getMessage(void)
Returns the thrown error message string.
Definition: Stk.h:126
stk::StkError::getMessageCString
virtual const char * getMessageCString(void)
Returns the thrown error message as a C string.
Definition: Stk.h:129
stk::StkError::getType
virtual const Type & getType(void)
Returns the thrown error message type.
Definition: Stk.h:123
stk::StkError::~StkError
virtual ~StkError(void)
The destructor.
Definition: Stk.h:117
stk::StkError::StkError
StkError(const std::string &message, Type type=StkError::UNSPECIFIED)
The constructor.
Definition: Stk.h:113
stk::StkFrames
An STK class to handle vectorized audio data.
Definition: Stk.h:279
stk::StkFrames::channels
unsigned int channels(void) const
Return the number of channels represented by the data.
Definition: Stk.h:416
stk::StkFrames::operator+=
StkFrames & operator+=(StkFrames &f)
Assignment by sum operator into self.
Definition: Stk.h:523
stk::StkFrames::frames
unsigned int frames(void) const
Return the number of sample frames represented by the data.
Definition: Stk.h:419
stk::StkFrames::size
size_t size() const
Returns the total number of audio samples represented by the object.
Definition: Stk.h:374
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::operator+
StkFrames operator+(const StkFrames &frames) const
Sum operator.
Definition: Stk.h:504
stk::StkFrames::resize
virtual void resize(size_t nFrames, unsigned int nChannels=1)
Resize self to represent the specified number of channels and frames.
stk::StkFrames::dataRate
StkFloat dataRate(void) const
Return the sample rate associated with the StkFrames data.
Definition: Stk.h:433
stk::StkFrames::getChannel
StkFrames & getChannel(unsigned int channel, StkFrames &destinationFrames, unsigned int destinationChannel) const
Retrieves a single channel.
stk::StkFrames::empty
bool empty() const
Returns true if the object size is zero and false otherwise.
Definition: Stk.h:446
stk::StkFrames::operator()
StkFloat & operator()(size_t frame, unsigned int channel)
Channel / frame subscript operator that returns a reference.
Definition: Stk.h:478
stk::StkFrames::operator[]
StkFloat & operator[](size_t n)
Subscript operator that returns a reference to element n of self.
Definition: Stk.h:452
stk::StkFrames::~StkFrames
virtual ~StkFrames()
The destructor.
stk::StkFrames::StkFrames
StkFrames(unsigned int nFrames=0, unsigned int nChannels=0)
The default constructor initializes the frame data structure to size zero.
stk::StkFrames::operator*=
StkFrames & operator*=(StkFrames &f)
Assignment by product operator into self.
Definition: Stk.h:540
stk::StkFrames::resize
virtual 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...
stk::StkFrames::StkFrames
StkFrames(const StkFloat &value, unsigned int nFrames, unsigned int nChannels)
Overloaded constructor that initializes the frame data to the specified size with value.
stk::StkFrames::setChannel
void setChannel(unsigned int channel, const StkFrames &sourceFrames, unsigned int sourceChannel)
Sets a single channel.
stk::StkFrames::setDataRate
void setDataRate(StkFloat rate)
Set the sample rate associated with the StkFrames data.
Definition: Stk.h:426
stk::StkFrames::operator*
friend StkFrames operator*(StkFloat v, const StkFrames &f)
Scaling operator (StkFloat * StkFrame)
Definition: Stk.h:568
stk::Stk
STK base class.
Definition: Stk.h:136
stk::Stk::STK_FLOAT32
static const StkFormat STK_FLOAT32
Definition: Stk.h:144
stk::Stk::showWarnings
static void showWarnings(bool status)
Toggle display of WARNING and STATUS messages.
Definition: Stk.h:213
stk::Stk::clear_alertList
static void clear_alertList()
Static method that frees memory from alertList_.
Definition: Stk.h:179
stk::Stk::removeSampleRateAlert
void removeSampleRateAlert(Stk *ptr)
Remove class pointer from list for sample rate change notification.
stk::Stk::setSampleRate
static void setSampleRate(StkFloat rate)
Static method that sets the STK sample rate.
stk::Stk::handleError
static void handleError(const char *message, StkError::Type type)
Static function for error reporting and handling using c-strings.
stk::Stk::setRawwavePath
static void setRawwavePath(std::string path)
Static method that sets the STK rawwave path.
stk::Stk::STK_FLOAT64
static const StkFormat STK_FLOAT64
Definition: Stk.h:145
stk::Stk::STK_SINT16
static const StkFormat STK_SINT16
Definition: Stk.h:141
stk::Stk::addSampleRateAlert
void addSampleRateAlert(Stk *ptr)
Add class pointer to list for sample rate change notification.
stk::Stk::rawwavePath
static std::string rawwavePath(void)
Static method that returns the current rawwave path.
Definition: Stk.h:182
stk::Stk::swap64
static void swap64(unsigned char *ptr)
Static method that byte-swaps a 64-bit data type.
stk::Stk::sampleRate
static StkFloat sampleRate(void)
Static method that returns the current STK sample rate.
Definition: Stk.h:148
stk::Stk::STK_SINT32
static const StkFormat STK_SINT32
Definition: Stk.h:143
stk::Stk::sampleRateChanged
virtual void sampleRateChanged(StkFloat newRate, StkFloat oldRate)
This function should be implemented in subclasses that depend on the sample rate.
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:200
stk::Stk::swap32
static void swap32(unsigned char *ptr)
Static method that byte-swaps a 32-bit data type.
stk::Stk::Stk
Stk(void)
Default constructor.
stk::Stk::swap16
static void swap16(unsigned char *ptr)
Static method that byte-swaps a 16-bit data type.
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:176
stk::Stk::handleError
static void handleError(std::string message, StkError::Type type)
Static function for error reporting and handling using c++ strings.
stk::Stk::~Stk
virtual ~Stk(void)
Class destructor.
stk::Stk::sleep
static void sleep(unsigned long milliseconds)
Static cross-platform method to sleep for a number of milliseconds.
stk::Stk::STK_SINT8
static const StkFormat STK_SINT8
Definition: Stk.h:140
stk::Stk::handleError
void handleError(StkError::Type type) const
Internal function for error reporting that assumes message in oStream_ variable.
stk::Stk::STK_SINT24
static const StkFormat STK_SINT24
Definition: Stk.h:142
stk::Stk::printErrors
static void printErrors(bool status)
Toggle display of error messages before throwing exceptions.
Definition: Stk.h:216
stk
The STK namespace.
Definition: ADSR.h:6

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