You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

402 lines
12KB

  1. /***************************************************/
  2. /*! \class Stk
  3. \brief STK base class
  4. Nearly all STK classes inherit from this class.
  5. The global sample rate can be queried and
  6. modified via Stk. In addition, this class
  7. provides error handling and byte-swapping
  8. functions.
  9. The Synthesis ToolKit in C++ (STK) is a set of open source audio
  10. signal processing and algorithmic synthesis classes written in the
  11. C++ programming language. STK was designed to facilitate rapid
  12. development of music synthesis and audio processing software, with
  13. an emphasis on cross-platform functionality, realtime control,
  14. ease of use, and educational example code. STK currently runs
  15. with realtime support (audio and MIDI) on Linux, Macintosh OS X,
  16. and Windows computer platforms. Generic, non-realtime support has
  17. been tested under NeXTStep, Sun, and other platforms and should
  18. work with any standard C++ compiler.
  19. STK WWW site: http://ccrma.stanford.edu/software/stk/
  20. The Synthesis ToolKit in C++ (STK)
  21. Copyright (c) 1995--2017 Perry R. Cook and Gary P. Scavone
  22. Permission is hereby granted, free of charge, to any person
  23. obtaining a copy of this software and associated documentation files
  24. (the "Software"), to deal in the Software without restriction,
  25. including without limitation the rights to use, copy, modify, merge,
  26. publish, distribute, sublicense, and/or sell copies of the Software,
  27. and to permit persons to whom the Software is furnished to do so,
  28. subject to the following conditions:
  29. The above copyright notice and this permission notice shall be
  30. included in all copies or substantial portions of the Software.
  31. Any person wishing to distribute modifications to the Software is
  32. asked to send the modifications to the original developer so that
  33. they can be incorporated into the canonical version. This is,
  34. however, not a binding provision of this license.
  35. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  36. EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  37. MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  38. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR
  39. ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF
  40. CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
  41. WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  42. */
  43. /***************************************************/
  44. #include "Stk.h"
  45. #include <stdlib.h>
  46. namespace stk {
  47. StkFloat Stk :: srate_ = (StkFloat) SRATE;
  48. std::string Stk :: rawwavepath_ = RAWWAVE_PATH;
  49. const Stk::StkFormat Stk :: STK_SINT8 = 0x1;
  50. const Stk::StkFormat Stk :: STK_SINT16 = 0x2;
  51. const Stk::StkFormat Stk :: STK_SINT24 = 0x4;
  52. const Stk::StkFormat Stk :: STK_SINT32 = 0x8;
  53. const Stk::StkFormat Stk :: STK_FLOAT32 = 0x10;
  54. const Stk::StkFormat Stk :: STK_FLOAT64 = 0x20;
  55. bool Stk :: showWarnings_ = true;
  56. bool Stk :: printErrors_ = true;
  57. std::vector<Stk *> Stk :: alertList_;
  58. std::ostringstream Stk :: oStream_;
  59. Stk :: Stk( void )
  60. : ignoreSampleRateChange_(false)
  61. {
  62. }
  63. Stk :: ~Stk( void )
  64. {
  65. }
  66. void Stk :: setSampleRate( StkFloat rate )
  67. {
  68. if ( rate > 0.0 && rate != srate_ ) {
  69. StkFloat oldRate = srate_;
  70. srate_ = rate;
  71. for ( unsigned int i=0; i<alertList_.size(); i++ )
  72. alertList_[i]->sampleRateChanged( srate_, oldRate );
  73. }
  74. }
  75. void Stk :: sampleRateChanged( StkFloat /*newRate*/, StkFloat /*oldRate*/ )
  76. {
  77. // This function should be reimplemented in classes that need to
  78. // make internal variable adjustments in response to a global sample
  79. // rate change.
  80. }
  81. void Stk :: addSampleRateAlert( Stk *ptr )
  82. {
  83. for ( unsigned int i=0; i<alertList_.size(); i++ )
  84. if ( alertList_[i] == ptr ) return;
  85. alertList_.push_back( ptr );
  86. }
  87. void Stk :: removeSampleRateAlert( Stk *ptr )
  88. {
  89. for ( unsigned int i=0; i<alertList_.size(); i++ ) {
  90. if ( alertList_[i] == ptr ) {
  91. alertList_.erase( alertList_.begin() + i );
  92. return;
  93. }
  94. }
  95. }
  96. void Stk :: setRawwavePath( std::string path )
  97. {
  98. if ( !path.empty() )
  99. rawwavepath_ = path;
  100. // Make sure the path includes a "/"
  101. if ( rawwavepath_[rawwavepath_.length()-1] != '/' )
  102. rawwavepath_ += "/";
  103. }
  104. void Stk :: swap16(unsigned char *ptr)
  105. {
  106. unsigned char val;
  107. // Swap 1st and 2nd bytes
  108. val = *(ptr);
  109. *(ptr) = *(ptr+1);
  110. *(ptr+1) = val;
  111. }
  112. void Stk :: swap32(unsigned char *ptr)
  113. {
  114. unsigned char val;
  115. // Swap 1st and 4th bytes
  116. val = *(ptr);
  117. *(ptr) = *(ptr+3);
  118. *(ptr+3) = val;
  119. //Swap 2nd and 3rd bytes
  120. ptr += 1;
  121. val = *(ptr);
  122. *(ptr) = *(ptr+1);
  123. *(ptr+1) = val;
  124. }
  125. void Stk :: swap64(unsigned char *ptr)
  126. {
  127. unsigned char val;
  128. // Swap 1st and 8th bytes
  129. val = *(ptr);
  130. *(ptr) = *(ptr+7);
  131. *(ptr+7) = val;
  132. // Swap 2nd and 7th bytes
  133. ptr += 1;
  134. val = *(ptr);
  135. *(ptr) = *(ptr+5);
  136. *(ptr+5) = val;
  137. // Swap 3rd and 6th bytes
  138. ptr += 1;
  139. val = *(ptr);
  140. *(ptr) = *(ptr+3);
  141. *(ptr+3) = val;
  142. // Swap 4th and 5th bytes
  143. ptr += 1;
  144. val = *(ptr);
  145. *(ptr) = *(ptr+1);
  146. *(ptr+1) = val;
  147. }
  148. #if (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
  149. #include <unistd.h>
  150. #elif defined(__OS_WINDOWS__)
  151. #include <windows.h>
  152. #endif
  153. void Stk :: sleep(unsigned long milliseconds)
  154. {
  155. #if defined(__OS_WINDOWS__)
  156. Sleep((DWORD) milliseconds);
  157. #elif (defined(__OS_IRIX__) || defined(__OS_LINUX__) || defined(__OS_MACOSX__))
  158. usleep( (unsigned long) (milliseconds * 1000.0) );
  159. #endif
  160. }
  161. void Stk :: handleError( StkError::Type type ) const
  162. {
  163. handleError( oStream_.str(), type );
  164. oStream_.str( std::string() ); // reset the ostringstream buffer
  165. }
  166. void Stk :: handleError( const char *message, StkError::Type type )
  167. {
  168. std::string msg( message );
  169. handleError( msg, type );
  170. }
  171. void Stk :: handleError( std::string message, StkError::Type type )
  172. {
  173. if ( type == StkError::WARNING || type == StkError::STATUS ) {
  174. if ( !showWarnings_ ) return;
  175. std::cerr << '\n' << message << '\n' << std::endl;
  176. }
  177. else if (type == StkError::DEBUG_PRINT) {
  178. #if defined(_STK_DEBUG_)
  179. std::cerr << '\n' << message << '\n' << std::endl;
  180. #endif
  181. }
  182. else {
  183. if ( printErrors_ ) {
  184. // Print error message before throwing.
  185. std::cerr << '\n' << message << '\n' << std::endl;
  186. }
  187. throw StkError(message, type);
  188. }
  189. }
  190. //
  191. // StkFrames definitions
  192. //
  193. StkFrames :: StkFrames( unsigned int nFrames, unsigned int nChannels )
  194. : data_( 0 ), nFrames_( nFrames ), nChannels_( nChannels )
  195. {
  196. size_ = nFrames_ * nChannels_;
  197. bufferSize_ = size_;
  198. if ( size_ > 0 ) {
  199. data_ = (StkFloat *) calloc( size_, sizeof( StkFloat ) );
  200. #if defined(_STK_DEBUG_)
  201. if ( data_ == NULL ) {
  202. std::string error = "StkFrames: memory allocation error in constructor!";
  203. Stk::handleError( error, StkError::MEMORY_ALLOCATION );
  204. }
  205. #endif
  206. }
  207. dataRate_ = Stk::sampleRate();
  208. }
  209. StkFrames :: StkFrames( const StkFloat& value, unsigned int nFrames, unsigned int nChannels )
  210. : data_( 0 ), nFrames_( nFrames ), nChannels_( nChannels )
  211. {
  212. size_ = nFrames_ * nChannels_;
  213. bufferSize_ = size_;
  214. if ( size_ > 0 ) {
  215. data_ = (StkFloat *) malloc( size_ * sizeof( StkFloat ) );
  216. #if defined(_STK_DEBUG_)
  217. if ( data_ == NULL ) {
  218. std::string error = "StkFrames: memory allocation error in constructor!";
  219. Stk::handleError( error, StkError::MEMORY_ALLOCATION );
  220. }
  221. #endif
  222. for ( long i=0; i<(long)size_; i++ ) data_[i] = value;
  223. }
  224. dataRate_ = Stk::sampleRate();
  225. }
  226. StkFrames :: ~StkFrames()
  227. {
  228. if ( data_ ) free( data_ );
  229. }
  230. StkFrames :: StkFrames( const StkFrames& f )
  231. : data_(0), size_(0), bufferSize_(0)
  232. {
  233. resize( f.frames(), f.channels() );
  234. dataRate_ = Stk::sampleRate();
  235. for ( unsigned int i=0; i<size_; i++ ) data_[i] = f[i];
  236. }
  237. StkFrames& StkFrames :: operator= ( const StkFrames& f )
  238. {
  239. if ( data_ ) free( data_ );
  240. data_ = 0;
  241. size_ = 0;
  242. bufferSize_ = 0;
  243. resize( f.frames(), f.channels() );
  244. dataRate_ = Stk::sampleRate();
  245. for ( unsigned int i=0; i<size_; i++ ) data_[i] = f[i];
  246. return *this;
  247. }
  248. void StkFrames :: resize( size_t nFrames, unsigned int nChannels )
  249. {
  250. nFrames_ = nFrames;
  251. nChannels_ = nChannels;
  252. size_ = nFrames_ * nChannels_;
  253. if ( size_ > bufferSize_ ) {
  254. if ( data_ ) free( data_ );
  255. data_ = (StkFloat *) malloc( size_ * sizeof( StkFloat ) );
  256. #if defined(_STK_DEBUG_)
  257. if ( data_ == NULL ) {
  258. std::string error = "StkFrames::resize: memory allocation error!";
  259. Stk::handleError( error, StkError::MEMORY_ALLOCATION );
  260. }
  261. #endif
  262. bufferSize_ = size_;
  263. }
  264. }
  265. void StkFrames :: resize( size_t nFrames, unsigned int nChannels, StkFloat value )
  266. {
  267. this->resize( nFrames, nChannels );
  268. for ( size_t i=0; i<size_; i++ ) data_[i] = value;
  269. }
  270. StkFrames& StkFrames::getChannel(unsigned int sourceChannel,StkFrames& destinationFrames, unsigned int destinationChannel) const
  271. {
  272. #if defined(_STK_DEBUG_)
  273. if (sourceChannel > channels() - 1) {
  274. std::ostringstream error;
  275. error << "StkFrames::getChannel invalid sourceChannel (" << sourceChannel << ")";
  276. Stk::handleError( error.str(), StkError::FUNCTION_ARGUMENT);
  277. }
  278. if (destinationChannel > destinationFrames.channels() - 1) {
  279. std::ostringstream error;
  280. error << "StkFrames::getChannel invalid destinationChannel (" << destinationChannel << ")";
  281. Stk::handleError( error.str(), StkError::FUNCTION_ARGUMENT );
  282. }
  283. if (destinationFrames.frames() < frames()) {
  284. std::ostringstream error;
  285. error << "StkFrames::getChannel destination.frames() < frames()";
  286. Stk::handleError( error.str(), StkError::MEMORY_ACCESS);
  287. }
  288. #endif
  289. int sourceHop = nChannels_;
  290. int destinationHop = destinationFrames.nChannels_;
  291. for (unsigned int i = sourceChannel, j= destinationChannel;
  292. i < nFrames_ * nChannels_;
  293. i+=sourceHop,j+=destinationHop)
  294. {
  295. destinationFrames[j] = data_[i];
  296. }
  297. return destinationFrames;
  298. }
  299. void StkFrames::setChannel(unsigned int destinationChannel, const stk::StkFrames &sourceFrames,unsigned int sourceChannel)
  300. {
  301. #if defined(_STK_DEBUG_)
  302. if (sourceChannel > sourceFrames.channels() - 1) {
  303. std::ostringstream error;
  304. error << "StkFrames::setChannel invalid sourceChannel (" << sourceChannel << ")";
  305. Stk::handleError( error.str(), StkError::FUNCTION_ARGUMENT);
  306. }
  307. if (destinationChannel > channels() - 1) {
  308. std::ostringstream error;
  309. error << "StkFrames::setChannel invalid channel (" << destinationChannel << ")";
  310. Stk::handleError( error.str(), StkError::FUNCTION_ARGUMENT );
  311. }
  312. if (sourceFrames.frames() != frames()) {
  313. std::ostringstream error;
  314. error << "StkFrames::setChannel sourceFrames.frames() != frames()";
  315. Stk::handleError( error.str(), StkError::MEMORY_ACCESS);
  316. }
  317. #endif
  318. unsigned int sourceHop = sourceFrames.nChannels_;
  319. unsigned int destinationHop = nChannels_;
  320. for (unsigned int i = destinationChannel,j = sourceChannel;
  321. i < nFrames_ * nChannels_;
  322. i+=destinationHop,j+=sourceHop)
  323. {
  324. data_[i] = sourceFrames[j];
  325. }
  326. }
  327. StkFloat StkFrames :: interpolate( StkFloat frame, unsigned int channel ) const
  328. {
  329. #if defined(_STK_DEBUG_)
  330. if ( frame < 0.0 || frame > (StkFloat) ( nFrames_ - 1 ) || channel >= nChannels_ ) {
  331. std::ostringstream error;
  332. error << "StkFrames::interpolate: invalid frame (" << frame << ") or channel (" << channel << ") value!";
  333. Stk::handleError( error.str(), StkError::MEMORY_ACCESS );
  334. }
  335. #endif
  336. size_t iIndex = ( size_t ) frame; // integer part of index
  337. StkFloat output, alpha = frame - (StkFloat) iIndex; // fractional part of index
  338. iIndex = iIndex * nChannels_ + channel;
  339. output = data_[ iIndex ];
  340. if ( alpha > 0.0 )
  341. output += ( alpha * ( data_[ iIndex + nChannels_ ] - output ) );
  342. return output;
  343. }
  344. } // stk namespace