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