2009-02-15 05:15:39 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
///
|
|
|
|
/// Classes for easy reading & writing of WAV sound files.
|
|
|
|
///
|
|
|
|
/// For big-endian CPU, define BIG_ENDIAN during compile-time to correctly
|
|
|
|
/// parse the WAV files with such processors.
|
2024-08-06 02:23:12 +00:00
|
|
|
///
|
|
|
|
/// Admittingly, more complete WAV reader routines may exist in public domain, but
|
2009-02-15 05:15:39 +00:00
|
|
|
/// the reason for 'yet another' one is that those generic WAV reader libraries are
|
2024-08-06 02:23:12 +00:00
|
|
|
/// exhaustingly large and cumbersome! Wanted to have something simpler here, i.e.
|
2009-02-15 05:15:39 +00:00
|
|
|
/// something that's not already larger than rest of the SoundTouch/SoundStretch program...
|
|
|
|
///
|
|
|
|
/// Author : Copyright (c) Olli Parviainen
|
|
|
|
/// Author e-mail : oparviai 'at' iki.fi
|
|
|
|
/// SoundTouch WWW: http://www.surina.net/soundtouch
|
|
|
|
///
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
|
|
|
// License :
|
|
|
|
//
|
|
|
|
// SoundTouch audio processing library
|
|
|
|
// Copyright (c) Olli Parviainen
|
|
|
|
//
|
|
|
|
// This library is free software; you can redistribute it and/or
|
|
|
|
// modify it under the terms of the GNU Lesser General Public
|
|
|
|
// License as published by the Free Software Foundation; either
|
|
|
|
// version 2.1 of the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This library is distributed in the hope that it will be useful,
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
|
|
// Lesser General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Lesser General Public
|
|
|
|
// License along with this library; if not, write to the Free Software
|
|
|
|
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
|
|
|
|
//
|
|
|
|
////////////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
#ifndef WAVFILE_H
|
|
|
|
#define WAVFILE_H
|
|
|
|
|
2024-08-06 02:23:12 +00:00
|
|
|
#include <cstdio>
|
|
|
|
#include <string>
|
|
|
|
#include "SS_CharTypes.h"
|
|
|
|
|
|
|
|
namespace soundstretch
|
|
|
|
{
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
#ifndef uint
|
|
|
|
typedef unsigned int uint;
|
2024-08-06 02:23:12 +00:00
|
|
|
#endif
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
/// WAV audio file 'riff' section header
|
2024-08-06 02:23:12 +00:00
|
|
|
typedef struct
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
char riff_char[4];
|
2019-02-02 16:50:38 +00:00
|
|
|
uint package_len;
|
2009-02-15 05:15:39 +00:00
|
|
|
char wave[4];
|
|
|
|
} WavRiff;
|
|
|
|
|
|
|
|
/// WAV audio file 'format' section header
|
2024-08-06 02:23:12 +00:00
|
|
|
typedef struct
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
char fmt[4];
|
2019-02-02 16:50:38 +00:00
|
|
|
unsigned int format_len;
|
|
|
|
unsigned short fixed;
|
|
|
|
unsigned short channel_number;
|
|
|
|
unsigned int sample_rate;
|
|
|
|
unsigned int byte_rate;
|
|
|
|
unsigned short byte_per_sample;
|
|
|
|
unsigned short bits_per_sample;
|
2009-02-15 05:15:39 +00:00
|
|
|
} WavFormat;
|
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
/// WAV audio file 'fact' section header
|
2024-08-06 02:23:12 +00:00
|
|
|
typedef struct
|
2015-08-22 14:16:27 +00:00
|
|
|
{
|
2019-02-02 16:50:38 +00:00
|
|
|
char fact_field[4];
|
|
|
|
uint fact_len;
|
|
|
|
uint fact_sample_len;
|
2015-08-22 14:16:27 +00:00
|
|
|
} WavFact;
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
/// WAV audio file 'data' section header
|
2024-08-06 02:23:12 +00:00
|
|
|
typedef struct
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
char data_field[4];
|
|
|
|
uint data_len;
|
|
|
|
} WavData;
|
|
|
|
|
|
|
|
|
|
|
|
/// WAV audio file header
|
2024-08-06 02:23:12 +00:00
|
|
|
typedef struct
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
WavRiff riff;
|
|
|
|
WavFormat format;
|
2015-08-22 14:16:27 +00:00
|
|
|
WavFact fact;
|
2009-02-15 05:15:39 +00:00
|
|
|
WavData data;
|
|
|
|
} WavHeader;
|
|
|
|
|
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
/// Base class for processing WAV audio files.
|
|
|
|
class WavFileBase
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
/// Conversion working buffer;
|
|
|
|
char *convBuff;
|
|
|
|
int convBuffSize;
|
|
|
|
|
|
|
|
protected:
|
|
|
|
WavFileBase();
|
|
|
|
virtual ~WavFileBase();
|
|
|
|
|
|
|
|
/// Get pointer to conversion buffer of at min. given size
|
|
|
|
void *getConvBuffer(int sizeByte);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
/// Class for reading WAV audio files.
|
2015-08-22 14:16:27 +00:00
|
|
|
class WavInFile : protected WavFileBase
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
/// File pointer.
|
|
|
|
FILE *fptr;
|
|
|
|
|
|
|
|
/// Counter of how many bytes of sample data have been read from the file.
|
2015-08-22 14:16:27 +00:00
|
|
|
long dataRead;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
/// WAV header information
|
|
|
|
WavHeader header;
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
/// Init the WAV file stream
|
|
|
|
void init();
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
/// Read WAV file headers.
|
|
|
|
/// \return zero if all ok, nonzero if file format is invalid.
|
|
|
|
int readWavHeaders();
|
|
|
|
|
|
|
|
/// Checks WAV file header tags.
|
|
|
|
/// \return zero if all ok, nonzero if file format is invalid.
|
2010-03-18 20:04:51 +00:00
|
|
|
int checkCharTags() const;
|
2009-02-15 05:15:39 +00:00
|
|
|
|
|
|
|
/// Reads a single WAV file header block.
|
|
|
|
/// \return zero if all ok, nonzero if file format is invalid.
|
|
|
|
int readHeaderBlock();
|
|
|
|
|
|
|
|
/// Reads WAV file 'riff' block
|
|
|
|
int readRIFFBlock();
|
|
|
|
|
|
|
|
public:
|
|
|
|
/// Constructor: Opens the given WAV file. If the file can't be opened,
|
|
|
|
/// throws 'runtime_error' exception.
|
2024-08-06 02:23:12 +00:00
|
|
|
WavInFile(const STRING& filename);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
WavInFile(FILE *file);
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
/// Destructor: Closes the file.
|
|
|
|
~WavInFile();
|
|
|
|
|
|
|
|
/// Rewind to beginning of the file
|
|
|
|
void rewind();
|
|
|
|
|
|
|
|
/// Get sample rate.
|
|
|
|
uint getSampleRate() const;
|
|
|
|
|
|
|
|
/// Get number of bits per sample, i.e. 8 or 16.
|
|
|
|
uint getNumBits() const;
|
|
|
|
|
2024-08-06 02:23:12 +00:00
|
|
|
/// Get sample data size in bytes. Ahem, this should return same information as
|
2009-02-15 05:15:39 +00:00
|
|
|
/// 'getBytesPerSample'...
|
|
|
|
uint getDataSizeInBytes() const;
|
|
|
|
|
|
|
|
/// Get total number of samples in file.
|
|
|
|
uint getNumSamples() const;
|
|
|
|
|
|
|
|
/// Get number of bytes per audio sample (e.g. 16bit stereo = 4 bytes/sample)
|
|
|
|
uint getBytesPerSample() const;
|
2024-08-06 02:23:12 +00:00
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
/// Get number of audio channels in the file (1=mono, 2=stereo)
|
|
|
|
uint getNumChannels() const;
|
|
|
|
|
|
|
|
/// Get the audio file length in milliseconds
|
|
|
|
uint getLengthMS() const;
|
|
|
|
|
2015-08-22 14:16:27 +00:00
|
|
|
/// Returns how many milliseconds of audio have so far been read from the file
|
|
|
|
///
|
|
|
|
/// \return elapsed duration in milliseconds
|
|
|
|
uint getElapsedMS() const;
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
/// Reads audio samples from the WAV file. This routine works only for 8 bit samples.
|
2024-08-06 02:23:12 +00:00
|
|
|
/// Reads given number of elements from the file or if end-of-file reached, as many
|
2009-02-15 05:15:39 +00:00
|
|
|
/// elements as are left in the file.
|
|
|
|
///
|
|
|
|
/// \return Number of 8-bit integers read from the file.
|
2015-08-22 14:16:27 +00:00
|
|
|
int read(unsigned char *buffer, int maxElems);
|
2009-02-15 05:15:39 +00:00
|
|
|
|
2024-08-06 02:23:12 +00:00
|
|
|
/// Reads audio samples from the WAV file to 16 bit integer format. Reads given number
|
|
|
|
/// of elements from the file or if end-of-file reached, as many elements as are
|
2009-02-15 05:15:39 +00:00
|
|
|
/// left in the file.
|
|
|
|
///
|
|
|
|
/// \return Number of 16-bit integers read from the file.
|
|
|
|
int read(short *buffer, ///< Pointer to buffer where to read data.
|
|
|
|
int maxElems ///< Size of 'buffer' array (number of array elements).
|
|
|
|
);
|
|
|
|
|
2024-08-06 02:23:12 +00:00
|
|
|
/// Reads audio samples from the WAV file to floating point format, converting
|
2009-02-15 05:15:39 +00:00
|
|
|
/// sample values to range [-1,1[. Reads given number of elements from the file
|
|
|
|
/// or if end-of-file reached, as many elements as are left in the file.
|
2015-08-22 14:16:27 +00:00
|
|
|
/// Notice that reading in float format supports 8/16/24/32bit sample formats.
|
2009-02-15 05:15:39 +00:00
|
|
|
///
|
|
|
|
/// \return Number of elements read from the file.
|
|
|
|
int read(float *buffer, ///< Pointer to buffer where to read data.
|
|
|
|
int maxElems ///< Size of 'buffer' array (number of array elements).
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Check end-of-file.
|
|
|
|
///
|
|
|
|
/// \return Nonzero if end-of-file reached.
|
|
|
|
int eof() const;
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/// Class for writing WAV audio files.
|
2015-08-22 14:16:27 +00:00
|
|
|
class WavOutFile : protected WavFileBase
|
2009-02-15 05:15:39 +00:00
|
|
|
{
|
|
|
|
private:
|
|
|
|
/// Pointer to the WAV file
|
|
|
|
FILE *fptr;
|
|
|
|
|
|
|
|
/// WAV file header data.
|
|
|
|
WavHeader header;
|
|
|
|
|
|
|
|
/// Counter of how many bytes have been written to the file so far.
|
|
|
|
int bytesWritten;
|
|
|
|
|
|
|
|
/// Fills in WAV file header information.
|
|
|
|
void fillInHeader(const uint sampleRate, const uint bits, const uint channels);
|
|
|
|
|
|
|
|
/// Finishes the WAV file header by supplementing information of amount of
|
|
|
|
/// data written to file etc
|
|
|
|
void finishHeader();
|
|
|
|
|
|
|
|
/// Writes the WAV file header.
|
|
|
|
void writeHeader();
|
|
|
|
|
|
|
|
public:
|
2024-08-06 02:23:12 +00:00
|
|
|
/// Constructor: Creates a new WAV file. Throws a 'runtime_error' exception
|
2009-02-15 05:15:39 +00:00
|
|
|
/// if file creation fails.
|
2024-08-06 02:23:12 +00:00
|
|
|
WavOutFile(const STRING& fileName, ///< Filename
|
2009-02-15 05:15:39 +00:00
|
|
|
int sampleRate, ///< Sample rate (e.g. 44100 etc)
|
|
|
|
int bits, ///< Bits per sample (8 or 16 bits)
|
|
|
|
int channels ///< Number of channels (1=mono, 2=stereo)
|
|
|
|
);
|
|
|
|
|
2010-03-18 20:04:51 +00:00
|
|
|
WavOutFile(FILE *file, int sampleRate, int bits, int channels);
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
/// Destructor: Finalizes & closes the WAV file.
|
|
|
|
~WavOutFile();
|
|
|
|
|
2024-08-06 02:23:12 +00:00
|
|
|
/// Write data to WAV file. This function works only with 8bit samples.
|
2009-02-15 05:15:39 +00:00
|
|
|
/// Throws a 'runtime_error' exception if writing to file fails.
|
2015-08-22 14:16:27 +00:00
|
|
|
void write(const unsigned char *buffer, ///< Pointer to sample data buffer.
|
|
|
|
int numElems ///< How many array items are to be written to file.
|
2009-02-15 05:15:39 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
/// Write data to WAV file. Throws a 'runtime_error' exception if writing to
|
|
|
|
/// file fails.
|
|
|
|
void write(const short *buffer, ///< Pointer to sample data buffer.
|
|
|
|
int numElems ///< How many array items are to be written to file.
|
|
|
|
);
|
|
|
|
|
|
|
|
/// Write data to WAV file in floating point format, saturating sample values to range
|
|
|
|
/// [-1..+1[. Throws a 'runtime_error' exception if writing to file fails.
|
|
|
|
void write(const float *buffer, ///< Pointer to sample data buffer.
|
|
|
|
int numElems ///< How many array items are to be written to file.
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2024-08-06 02:23:12 +00:00
|
|
|
}
|
|
|
|
|
2009-02-15 05:15:39 +00:00
|
|
|
#endif
|