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.

142 lines
4.8KB

  1. #ifndef STK_FILEREAD_H
  2. #define STK_FILEREAD_H
  3. #include "Stk.h"
  4. namespace stk {
  5. /***************************************************/
  6. /*! \class FileRead
  7. \brief STK audio file input class.
  8. This class provides input support for various
  9. audio file formats. Multi-channel (>2)
  10. soundfiles are supported. The file data is
  11. returned via an external StkFrames object
  12. passed to the read() function. This class
  13. does not store its own copy of the file data,
  14. rather the data is read directly from disk.
  15. FileRead currently supports uncompressed WAV,
  16. AIFF/AIFC, SND (AU), MAT-file (Matlab), and
  17. STK RAW file formats. Signed integer (8-,
  18. 16-, 24-, and 32-bit) and floating-point (32- and
  19. 64-bit) data types are supported. Compressed
  20. data types are not supported.
  21. STK RAW files have no header and are assumed to
  22. contain a monophonic stream of 16-bit signed
  23. integers in big-endian byte order at a sample
  24. rate of 22050 Hz. MAT-file data should be saved
  25. in an array with each data channel filling a
  26. matrix row. The sample rate for MAT-files should
  27. be specified in a variable named "fs". If no
  28. such variable is found, the sample rate is
  29. assumed to be 44100 Hz.
  30. by Perry R. Cook and Gary P. Scavone, 1995--2017.
  31. */
  32. /***************************************************/
  33. class FileRead : public Stk
  34. {
  35. public:
  36. //! Default constructor.
  37. FileRead( void );
  38. //! Overloaded constructor that opens a file during instantiation.
  39. /*!
  40. An StkError will be thrown if the file is not found or its
  41. format is unknown or unsupported. The optional arguments allow a
  42. headerless file type to be supported. If \c typeRaw is false (the
  43. default), the subsequent parameters are ignored.
  44. */
  45. FileRead( std::string fileName, bool typeRaw = false, unsigned int nChannels = 1,
  46. StkFormat format = STK_SINT16, StkFloat rate = 22050.0 );
  47. //! Class destructor.
  48. ~FileRead( void );
  49. //! Open the specified file and determine its formatting.
  50. /*!
  51. An StkError will be thrown if the file is not found or its
  52. format is unknown or unsupported. The optional arguments allow a
  53. headerless file type to be supported. If \c typeRaw is false (the
  54. default), the subsequent parameters are ignored.
  55. */
  56. void open( std::string fileName, bool typeRaw = false, unsigned int nChannels = 1,
  57. StkFormat format = STK_SINT16, StkFloat rate = 22050.0 );
  58. //! If a file is open, close it.
  59. void close( void );
  60. //! Returns \e true if a file is currently open.
  61. bool isOpen( void );
  62. //! Return the file size in sample frames.
  63. unsigned long fileSize( void ) const { return fileSize_; };
  64. //! Return the number of audio channels in the file.
  65. unsigned int channels( void ) const { return channels_; };
  66. //! Return the data format of the file.
  67. StkFormat format( void ) const { return dataType_; };
  68. //! Return the file sample rate in Hz.
  69. /*!
  70. WAV, SND, and AIF formatted files specify a sample rate in
  71. their headers. By definition, STK RAW files have a sample rate of
  72. 22050 Hz. MAT-files are assumed to have a rate of 44100 Hz.
  73. */
  74. StkFloat fileRate( void ) const { return fileRate_; };
  75. //! Read sample frames from the file into an StkFrames object.
  76. /*!
  77. The number of sample frames to read will be determined from the
  78. number of frames of the StkFrames argument. If this size is
  79. larger than the available data in the file (given the file size
  80. and starting frame index), the extra frames will be unaffected
  81. (the StkFrames object will not be resized). Optional parameters
  82. are provided to specify the starting sample frame within the file
  83. (default = 0) and whether to normalize the data with respect to
  84. fixed-point limits (default = true). An StkError will be thrown
  85. if a file error occurs or if the number of channels in the
  86. StkFrames argument is not equal to that in the file.
  87. */
  88. void read( StkFrames& buffer, unsigned long startFrame = 0, bool doNormalize = true );
  89. protected:
  90. // Get STK RAW file information.
  91. bool getRawInfo( const char *fileName, unsigned int nChannels,
  92. StkFormat format, StkFloat rate );
  93. // Get WAV file header information.
  94. bool getWavInfo( const char *fileName );
  95. // Get SND (AU) file header information.
  96. bool getSndInfo( const char *fileName );
  97. // Get AIFF file header information.
  98. bool getAifInfo( const char *fileName );
  99. // Get MAT-file header information.
  100. bool getMatInfo( const char *fileName );
  101. // Helper function for MAT-file parsing.
  102. bool findNextMatArray( SINT32 *chunkSize, SINT32 *rows, SINT32 *columns, SINT32 *nametype );
  103. FILE *fd_;
  104. bool byteswap_;
  105. bool wavFile_;
  106. unsigned long fileSize_;
  107. unsigned long dataOffset_;
  108. unsigned int channels_;
  109. StkFormat dataType_;
  110. StkFloat fileRate_;
  111. };
  112. } // stk namespace
  113. #endif