| 1 |
/***************************************************/ |
| 2 |
/*! \class Stk |
| 3 |
\brief STK base class |
| 4 |
|
| 5 |
Nearly all STK classes inherit from this class. |
| 6 |
The global sample rate and rawwave path variables |
| 7 |
can be queried and modified via Stk. In addition, |
| 8 |
this class provides error handling and |
| 9 |
byte-swapping functions. |
| 10 |
|
| 11 |
by Perry R. Cook and Gary P. Scavone, 1995 - 2005. |
| 12 |
*/ |
| 13 |
/***************************************************/ |
| 14 |
|
| 15 |
#ifndef STK_STK_H |
| 16 |
#define STK_STK_H |
| 17 |
#ifdef WIN32 |
| 18 |
/* THESE NO LONGER SEEM NECESSARY, AT LEAST NOT ON VS15 |
| 19 |
// this is a fix for Visual Studio 14; not sure if it will cause problems elsewhere |
| 20 |
typedef signed __int8 int8_t; |
| 21 |
typedef signed __int16 int16_t; |
| 22 |
typedef signed __int32 int32_t; |
| 23 |
typedef signed __int64 int64_t; |
| 24 |
typedef unsigned __int8 uint8_t; |
| 25 |
typedef unsigned __int16 uint16_t; |
| 26 |
typedef unsigned __int32 uint32_t; |
| 27 |
typedef unsigned __int64 uint64_t; |
| 28 |
typedef int8_t int_least8_t; |
| 29 |
typedef int16_t int_least16_t; |
| 30 |
typedef int32_t int_least32_t; |
| 31 |
typedef int64_t int_least64_t; |
| 32 |
typedef uint8_t uint_least8_t; |
| 33 |
typedef uint16_t uint_least16_t; |
| 34 |
typedef uint32_t uint_least32_t; |
| 35 |
typedef uint64_t uint_least64_t; |
| 36 |
typedef int8_t int_fast8_t; |
| 37 |
typedef int16_t int_fast16_t; |
| 38 |
typedef int32_t int_fast32_t; |
| 39 |
typedef int64_t int_fast64_t; |
| 40 |
typedef uint8_t uint_fast8_t; |
| 41 |
typedef uint16_t uint_fast16_t; |
| 42 |
typedef uint32_t uint_fast32_t; |
| 43 |
typedef uint64_t uint_fast64_t; |
| 44 |
typedef int64_t intmax_t; |
| 45 |
typedef uint64_t uintmax_t; |
| 46 |
*/ |
| 47 |
#endif |
| 48 |
|
| 49 |
#include <cstddef> |
| 50 |
#include <cstdint> |
| 51 |
#include <string> |
| 52 |
#include <iostream> |
| 53 |
#include <sstream> |
| 54 |
|
| 55 |
using namespace std; |
| 56 |
|
| 57 |
namespace Nyq |
| 58 |
{ |
| 59 |
|
| 60 |
// Most data in STK is passed and calculated with the |
| 61 |
// following user-definable floating-point type. You |
| 62 |
// can change this to "float" if you prefer or perhaps |
| 63 |
// a "long double" in the future. |
| 64 |
typedef double StkFloat; |
| 65 |
|
| 66 |
// The "MY_FLOAT" type was deprecated in STK |
| 67 |
// versions higher than 4.1.3 and replaced with the variable |
| 68 |
// "StkFloat". |
| 69 |
#if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) |
| 70 |
typedef StkFloat MY_FLOAT; |
| 71 |
#pragma deprecated(MY_FLOAT) |
| 72 |
#elif defined(__GXX__) |
| 73 |
typedef StkFloat MY_FLOAT __attribute__ ((deprecated)); |
| 74 |
#else |
| 75 |
typedef StkFloat MY_FLOAT; // temporary |
| 76 |
#endif |
| 77 |
|
| 78 |
|
| 79 |
//! STK error handling class. |
| 80 |
/*! |
| 81 |
This is a fairly abstract exception handling class. There could |
| 82 |
be sub-classes to take care of more specific error conditions ... or |
| 83 |
not. |
| 84 |
*/ |
| 85 |
class StkError |
| 86 |
{ |
| 87 |
public: |
| 88 |
enum Type { |
| 89 |
STATUS, |
| 90 |
WARNING, |
| 91 |
DEBUG_WARNING, |
| 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: |
| 111 |
//! The constructor. |
| 112 |
StkError(const std::string& message, Type type = StkError::UNSPECIFIED) |
| 113 |
: message_(message), type_(type) {} |
| 114 |
|
| 115 |
//! The destructor. |
| 116 |
virtual ~StkError(void) {}; |
| 117 |
|
| 118 |
//! Prints thrown error message to stderr. |
| 119 |
virtual void printMessage(void) { std::cerr << '\n' << message_ << "\n\n"; } |
| 120 |
|
| 121 |
//! Returns the thrown error message type. |
| 122 |
virtual const Type& getType(void) { return type_; } |
| 123 |
|
| 124 |
//! Returns the thrown error message string. |
| 125 |
virtual const std::string& getMessage(void) { return message_; } |
| 126 |
|
| 127 |
//! Returns the thrown error message as a C string. |
| 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; /*!< -128 to +127 */ |
| 138 |
static const StkFormat STK_SINT16; /*!< -32768 to +32767 */ |
| 139 |
static const StkFormat STK_SINT24; /*!< Upper 3 bytes of 32-bit signed integer. */ |
| 140 |
static const StkFormat STK_SINT32; /*!< -2147483648 to +2147483647. */ |
| 141 |
static const StkFormat STK_FLOAT32; /*!< Normalized between plus/minus 1.0. */ |
| 142 |
static const StkFormat STK_FLOAT64; /*!< Normalized between plus/minus 1.0. */ |
| 143 |
|
| 144 |
//! Static method which returns the current STK sample rate. |
| 145 |
static StkFloat sampleRate(void) { return srate_; } |
| 146 |
|
| 147 |
//! Static method which sets the STK sample rate. |
| 148 |
/*! |
| 149 |
The sample rate set using this method is queried by all STK |
| 150 |
classes which depend on its value. It is initialized to the |
| 151 |
default SRATE set in Stk.h. Many STK classes use the sample rate |
| 152 |
during instantiation. Therefore, if you wish to use a rate which |
| 153 |
is different from the default rate, it is imperative that it be |
| 154 |
set \e BEFORE STK objects are instantiated. |
| 155 |
*/ |
| 156 |
static void setSampleRate(StkFloat rate) { if (rate > 0.0) srate_ = rate; } |
| 157 |
|
| 158 |
//! Static method which returns the current rawwave path. |
| 159 |
static std::string rawwavePath(void) { return rawwavepath_; } |
| 160 |
|
| 161 |
//! Static method which sets the STK rawwave path. |
| 162 |
static void setRawwavePath(std::string path); |
| 163 |
|
| 164 |
//! Static method which byte-swaps a 16-bit data type. |
| 165 |
static void byteSwap16(unsigned char *ptr); |
| 166 |
|
| 167 |
//! Static method which byte-swaps a 32-bit data type. |
| 168 |
static void byteSwap32(unsigned char *ptr); |
| 169 |
|
| 170 |
//! Static method which byte-swaps a 64-bit data type. |
| 171 |
static void byteSwap64(unsigned char *ptr); |
| 172 |
|
| 173 |
//! Static cross-platform method to sleep for a number of milliseconds. |
| 174 |
static void sleep(unsigned long milliseconds); |
| 175 |
|
| 176 |
//! Static function for error reporting and handling using c-strings. |
| 177 |
static void handleError( const char *message, StkError::Type type ); |
| 178 |
|
| 179 |
//! Static function for error reporting and handling using c++ strings. |
| 180 |
static void handleError( std::string message, StkError::Type type ); |
| 181 |
|
| 182 |
//! Toggle display of WARNING and STATUS messages. |
| 183 |
static void showWarnings( bool status ) { showWarnings_ = status; } |
| 184 |
|
| 185 |
//! Toggle display of error messages before throwing exceptions. |
| 186 |
static void printErrors( bool status ) { printErrors_ = status; } |
| 187 |
|
| 188 |
private: |
| 189 |
static StkFloat srate_; |
| 190 |
static std::string rawwavepath_; |
| 191 |
static bool showWarnings_; |
| 192 |
static bool printErrors_; |
| 193 |
|
| 194 |
protected: |
| 195 |
|
| 196 |
std::ostringstream errorString_; |
| 197 |
|
| 198 |
//! Default constructor. |
| 199 |
Stk(void); |
| 200 |
|
| 201 |
//! Class destructor. |
| 202 |
virtual ~Stk(void); |
| 203 |
|
| 204 |
//! Internal function for error reporting which assumes message in \c errorString_ variable. |
| 205 |
void handleError( StkError::Type type ); |
| 206 |
}; |
| 207 |
|
| 208 |
|
| 209 |
/***************************************************/ |
| 210 |
/*! \class StkFrames |
| 211 |
\brief An STK class to handle vectorized audio data. |
| 212 |
|
| 213 |
This class can hold single- or multi-channel audio data in either |
| 214 |
interleaved or non-interleaved formats. The data type is always |
| 215 |
StkFloat. In an effort to maintain efficiency, no out-of-bounds |
| 216 |
checks are performed in this class. |
| 217 |
|
| 218 |
Possible future improvements in this class could include functions |
| 219 |
to inter- or de-interleave the data and to convert to and return |
| 220 |
other data types. |
| 221 |
|
| 222 |
by Perry R. Cook and Gary P. Scavone, 1995 - 2005. |
| 223 |
*/ |
| 224 |
/***************************************************/ |
| 225 |
|
| 226 |
class StkFrames |
| 227 |
{ |
| 228 |
public: |
| 229 |
|
| 230 |
//! The default constructor initializes the frame data structure to size zero. |
| 231 |
StkFrames( unsigned int nFrames = 0, unsigned int nChannels = 0, bool interleaved = true ); |
| 232 |
|
| 233 |
//! Overloaded constructor that initializes the frame data to the specified size with \c value. |
| 234 |
StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels, bool interleaved = true ); |
| 235 |
|
| 236 |
//! The destructor. |
| 237 |
~StkFrames(); |
| 238 |
|
| 239 |
//! Subscript operator which returns a reference to element \c n of self. |
| 240 |
/*! |
| 241 |
The result can be used as an lvalue . This reference is valid |
| 242 |
until the resize function is called or the array is destroyed. The |
| 243 |
index \c n must be between 0 and size less one. No range checking |
| 244 |
is performed unless _STK_DEBUG_ is defined. |
| 245 |
*/ |
| 246 |
StkFloat& operator[] ( size_t n ); |
| 247 |
|
| 248 |
//! Subscript operator that returns the value at element \c n of self. |
| 249 |
/*! |
| 250 |
The index \c n must be between 0 and size less one. No range |
| 251 |
checking is performed unless _STK_DEBUG_ is defined. |
| 252 |
*/ |
| 253 |
StkFloat operator[] ( size_t n ) const; |
| 254 |
|
| 255 |
//! Channel / frame subscript operator that returns a reference. |
| 256 |
/*! |
| 257 |
The result can be used as an lvalue. This reference is valid |
| 258 |
until the resize function is called or the array is destroyed. The |
| 259 |
\c frame index must be between 0 and frames() - 1. The \c channel |
| 260 |
index must be between 0 and channels() - 1. No range checking is |
| 261 |
performed unless _STK_DEBUG_ is defined. |
| 262 |
*/ |
| 263 |
StkFloat& operator() ( size_t frame, unsigned int channel ); |
| 264 |
|
| 265 |
//! Channel / frame subscript operator that returns a value. |
| 266 |
/*! |
| 267 |
The \c frame index must be between 0 and frames() - 1. The \c |
| 268 |
channel index must be between 0 and channels() - 1. No range checking |
| 269 |
is performed unless _STK_DEBUG_ is defined. |
| 270 |
*/ |
| 271 |
StkFloat operator() ( size_t frame, unsigned int channel ) const; |
| 272 |
|
| 273 |
//! Return an interpolated value at the fractional frame index and channel. |
| 274 |
/*! |
| 275 |
This function performs linear interpolation. The \c frame |
| 276 |
index must be between 0.0 and frames() - 1. The \c channel index |
| 277 |
must be between 0 and channels() - 1. No range checking is |
| 278 |
performed unless _STK_DEBUG_ is defined. |
| 279 |
*/ |
| 280 |
StkFloat interpolate( StkFloat frame, unsigned int channel = 0 ) const; |
| 281 |
|
| 282 |
//! Returns the total number of audio samples represented by the object. |
| 283 |
size_t size() const { return size_; }; |
| 284 |
|
| 285 |
//! Returns \e true if the object size is zero and \e false otherwise. |
| 286 |
bool empty() const; |
| 287 |
|
| 288 |
//! Resize self to represent the specified number of channels and frames. |
| 289 |
/*! |
| 290 |
Changes the size of self based on the number of frames and |
| 291 |
channels. No element assignment is performed. No memory |
| 292 |
deallocation occurs if the new size is smaller than the previous |
| 293 |
size. Further, no new memory is allocated when the new size is |
| 294 |
smaller or equal to a previously allocated size. |
| 295 |
*/ |
| 296 |
void resize( size_t nFrames, unsigned int nChannels = 1 ); |
| 297 |
|
| 298 |
//! Resize self to represent the specified number of channels and frames and perform element initialization. |
| 299 |
/*! |
| 300 |
Changes the size of self based on the number of frames and |
| 301 |
channels, and assigns \c value to every element. No memory |
| 302 |
deallocation occurs if the new size is smaller than the previous |
| 303 |
size. Further, no new memory is allocated when the new size is |
| 304 |
smaller or equal to a previously allocated size. |
| 305 |
*/ |
| 306 |
void resize( size_t nFrames, unsigned int nChannels, StkFloat value ); |
| 307 |
|
| 308 |
//! Return the number of channels represented by the data. |
| 309 |
unsigned int channels( void ) const { return nChannels_; }; |
| 310 |
|
| 311 |
//! Return the number of sample frames represented by the data. |
| 312 |
size_t frames( void ) const { return nFrames_; }; |
| 313 |
|
| 314 |
//! Set the sample rate associated with the StkFrames data. |
| 315 |
/*! |
| 316 |
By default, this value is set equal to the current STK sample |
| 317 |
rate at the time of instantiation. |
| 318 |
*/ |
| 319 |
void setDataRate( StkFloat rate ) { dataRate_ = rate; }; |
| 320 |
|
| 321 |
//! Return the sample rate associated with the StkFrames data. |
| 322 |
/*! |
| 323 |
By default, this value is set equal to the current STK sample |
| 324 |
rate at the time of instantiation. |
| 325 |
*/ |
| 326 |
StkFloat dataRate( void ) const { return dataRate_; }; |
| 327 |
|
| 328 |
//! Returns \c true if the data is in interleaved format, \c false if the data is non-interleaved. |
| 329 |
bool interleaved( void ) const { return interleaved_; }; |
| 330 |
|
| 331 |
//! Set the flag to indicate whether the internal data is in interleaved (\c true) or non-interleaved (\c false) format. |
| 332 |
/*! |
| 333 |
Note that this function does not modify the internal data order |
| 334 |
with respect to the argument value. It simply changes the |
| 335 |
indicator flag value. |
| 336 |
*/ |
| 337 |
void setInterleaved( bool isInterleaved ) { interleaved_ = isInterleaved; }; |
| 338 |
|
| 339 |
private: |
| 340 |
|
| 341 |
StkFloat *data_; |
| 342 |
StkFloat dataRate_; |
| 343 |
size_t nFrames_; |
| 344 |
unsigned int nChannels_; |
| 345 |
size_t size_; |
| 346 |
size_t bufferSize_; |
| 347 |
bool interleaved_; |
| 348 |
|
| 349 |
}; |
| 350 |
|
| 351 |
|
| 352 |
// Here are a few other useful typedefs. |
| 353 |
typedef unsigned short UINT16; |
| 354 |
typedef unsigned int UINT32; |
| 355 |
typedef signed short SINT16; |
| 356 |
typedef signed int SINT32; |
| 357 |
typedef float FLOAT32; |
| 358 |
typedef double FLOAT64; |
| 359 |
|
| 360 |
// The default sampling rate. |
| 361 |
const StkFloat SRATE = 44100.0; |
| 362 |
|
| 363 |
// The default real-time audio input and output buffer size. If |
| 364 |
// clicks are occuring in the input and/or output sound stream, a |
| 365 |
// larger buffer size may help. Larger buffer sizes, however, produce |
| 366 |
// more latency. |
| 367 |
const unsigned int RT_BUFFER_SIZE = 512; |
| 368 |
|
| 369 |
// The default rawwave path value is set with the preprocessor |
| 370 |
// definition RAWWAVE_PATH. This can be specified as an argument to |
| 371 |
// the configure script, in an integrated development environment, or |
| 372 |
// below. The global STK rawwave path variable can be dynamically set |
| 373 |
// with the Stk::setRawwavePath() function. This value is |
| 374 |
// concatenated to the beginning of all references to rawwave files in |
| 375 |
// the various STK core classes (ex. Clarinet.cpp). If you wish to |
| 376 |
// move the rawwaves directory to a different location in your file |
| 377 |
// system, you will need to set this path definition appropriately. |
| 378 |
#if !defined(RAWWAVE_PATH) |
| 379 |
#define RAWWAVE_PATH "../../rawwaves/" |
| 380 |
#endif |
| 381 |
|
| 382 |
const StkFloat PI = 3.14159265358979; |
| 383 |
const StkFloat TWO_PI = 2 * PI; |
| 384 |
const StkFloat ONE_OVER_128 = 0.0078125; |
| 385 |
|
| 386 |
#if defined(__WINDOWS_DS__) || defined(__WINDOWS_ASIO__) || defined(__WINDOWS_MM__) |
| 387 |
#define __OS_WINDOWS__ |
| 388 |
#define __STK_REALTIME__ |
| 389 |
#elif defined(__LINUX_OSS__) || defined(__LINUX_ALSA__) || defined(__LINUX_JACK__) |
| 390 |
#define __OS_LINUX__ |
| 391 |
#define __STK_REALTIME__ |
| 392 |
#elif defined(__IRIX_AL__) |
| 393 |
#define __OS_IRIX__ |
| 394 |
#define __STK_REALTIME__ |
| 395 |
#elif defined(__MACOSX_CORE__) |
| 396 |
#define __OS_MACOSX__ |
| 397 |
#define __STK_REALTIME__ |
| 398 |
#endif |
| 399 |
|
| 400 |
//#define _STK_DEBUG_ |
| 401 |
|
| 402 |
} // namespace Nyq |
| 403 |
|
| 404 |
#endif |