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.

141 lines
3.4KB

  1. /***************************************************/
  2. /*! \class FileWvOut
  3. \brief STK audio file output class.
  4. This class inherits from WvOut. It provides a "tick-level"
  5. interface to the FileWrite class.
  6. FileWvOut writes samples to an audio file and supports
  7. multi-channel data. It is important to distinguish the tick()
  8. method that outputs a single sample to all channels in a sample
  9. frame from the overloaded one that takes a reference to an
  10. StkFrames object for multi-channel and/or multi-frame data.
  11. See the FileWrite class for a description of the supported audio
  12. file formats.
  13. Currently, FileWvOut is non-interpolating and the output rate is
  14. always Stk::sampleRate().
  15. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  16. */
  17. /***************************************************/
  18. #include "FileWvOut.h"
  19. namespace stk {
  20. FileWvOut :: FileWvOut( unsigned int bufferFrames )
  21. :bufferFrames_( bufferFrames )
  22. {
  23. }
  24. FileWvOut::FileWvOut( std::string fileName, unsigned int nChannels, FileWrite::FILE_TYPE type, Stk::StkFormat format, unsigned int bufferFrames )
  25. :bufferFrames_( bufferFrames )
  26. {
  27. this->openFile( fileName, nChannels, type, format );
  28. }
  29. FileWvOut :: ~FileWvOut()
  30. {
  31. this->closeFile();
  32. }
  33. void FileWvOut :: closeFile( void )
  34. {
  35. if ( file_.isOpen() ) {
  36. // Output any remaining samples in the buffer before closing.
  37. if ( bufferIndex_ > 0 ) {
  38. data_.resize( bufferIndex_, data_.channels() );
  39. file_.write( data_ );
  40. }
  41. file_.close();
  42. frameCounter_ = 0;
  43. }
  44. }
  45. void FileWvOut :: openFile( std::string fileName,
  46. unsigned int nChannels,
  47. FileWrite::FILE_TYPE type,
  48. Stk::StkFormat format )
  49. {
  50. closeFile();
  51. if ( nChannels < 1 ) {
  52. oStream_ << "FileWvOut::openFile: the channels argument must be greater than zero!";
  53. handleError( StkError::FUNCTION_ARGUMENT );
  54. }
  55. // An StkError can be thrown by the FileWrite class here.
  56. file_.open( fileName, nChannels, type, format );
  57. // Allocate new memory if necessary.
  58. data_.resize( bufferFrames_, nChannels );
  59. bufferIndex_ = 0;
  60. iData_ = 0;
  61. }
  62. void FileWvOut :: incrementFrame( void )
  63. {
  64. frameCounter_++;
  65. bufferIndex_++;
  66. if ( bufferIndex_ == bufferFrames_ ) {
  67. file_.write( data_ );
  68. bufferIndex_ = 0;
  69. iData_ = 0;
  70. }
  71. }
  72. void FileWvOut :: tick( const StkFloat sample )
  73. {
  74. #if defined(_STK_DEBUG_)
  75. if ( !file_.isOpen() ) {
  76. oStream_ << "FileWvOut::tick(): no file open!";
  77. handleError( StkError::WARNING );
  78. return;
  79. }
  80. #endif
  81. unsigned int nChannels = data_.channels();
  82. StkFloat input = sample;
  83. clipTest( input );
  84. for ( unsigned int j=0; j<nChannels; j++ )
  85. data_[iData_++] = input;
  86. this->incrementFrame();
  87. }
  88. void FileWvOut :: tick( const StkFrames& frames )
  89. {
  90. #if defined(_STK_DEBUG_)
  91. if ( !file_.isOpen() ) {
  92. oStream_ << "FileWvOut::tick(): no file open!";
  93. handleError( StkError::WARNING );
  94. return;
  95. }
  96. if ( data_.channels() != frames.channels() ) {
  97. oStream_ << "FileWvOut::tick(): incompatible channel value in StkFrames argument!";
  98. handleError( StkError::FUNCTION_ARGUMENT );
  99. }
  100. #endif
  101. unsigned int iFrames = 0;
  102. unsigned int j, nChannels = data_.channels();
  103. for ( unsigned int i=0; i<frames.frames(); i++ ) {
  104. for ( j=0; j<nChannels; j++ ) {
  105. data_[iData_] = frames[iFrames++];
  106. clipTest( data_[iData_++] );
  107. }
  108. this->incrementFrame();
  109. }
  110. }
  111. } // stk namespace