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