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 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 (ex. 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

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