NumeRe v1.1.4
NumeRe: Framework für Numerische Rechnungen
Audio::WavFile Class Reference

This class implements the wave file type using PCM encoding (the simplest encoding). Due to its simplicity it is a seekable file type. More...

#include <wavfile.hpp>

Inheritance diagram for Audio::WavFile:
Collaboration diagram for Audio::WavFile:

Public Member Functions

 WavFile (const std::string &sFileName)
 Constructor. Tries to open the wave file, if it exists. More...
 
virtual ~WavFile ()
 Destructor. Will close the file stream and update the header. More...
 
virtual bool isValid () const
 Returns, whether the currently opened file is actually open and readable. More...
 
virtual void newFile () override
 Prepares the wave file header for a new file, opens the stream and writes the header to the file. Set number of channels and sample frequency before calling this member function. More...
 
virtual void setChannels (size_t channels) override
 Set the channels, which shall be used in the new file. More...
 
virtual void setSampleRate (size_t freq) override
 Set the sample rate, which shall be used in the new file. More...
 
virtual void write (const Sample &sample) override
 Write a sample to the audio stream. More...
 
virtual size_t getChannels () const override
 Get the number of channels of the current file. More...
 
virtual size_t getSampleRate () const override
 Get the sample rate of the current file. More...
 
virtual size_t getLength () const override
 Get the length in samples of the current file. More...
 
virtual Sample read () const override
 Read a sample from the audio stream. More...
 
virtual size_t getPosition () const override
 Get the current position in samples of the current file. More...
 
virtual void setPosition (size_t pos) override
 Set the current position in Samples in the current file. More...
 
virtual std::vector< SamplereadSome (size_t len) const override
 Read a block of samples from the audio stream. More...
 
virtual void writeSome (const std::vector< Sample > vSamples) override
 Write a block of samples to the audio stream. More...
 
- Public Member Functions inherited from Audio::SeekableFile
 SeekableFile ()
 
virtual bool isSeekable () const override
 Overrides the base classes member function to signal the possibility to safely up-cast to this class. More...
 
virtual size_t getPosition () const =0
 
virtual void setPosition (size_t pos)=0
 
virtual std::vector< SamplereadSome (size_t len) const =0
 
virtual void writeSome (const std::vector< Sample > vFrames)=0
 
- Public Member Functions inherited from Audio::File
 File ()
 
virtual ~File ()
 
virtual bool isValid () const =0
 
virtual void newFile ()=0
 
virtual void setChannels (size_t channels)=0
 
virtual void setSampleRate (size_t freq)=0
 
virtual void write (const Sample &frame)=0
 
virtual size_t getChannels () const =0
 
virtual size_t getSampleRate () const =0
 
virtual size_t getLength () const =0
 
virtual Sample read () const =0
 
virtual bool isSeekable () const
 Audio files, which inherit from this class do not have any seeking functionality. More...
 

Private Member Functions

bool readHeader ()
 Private member function to read the wave file header to memory. It will ensure that the current file is actually a RIFF WAVE file. More...
 
void closeFile ()
 Updates the wave file header, which is currently open, and closes the file afterwards. More...
 

Private Attributes

std::string sName
 
std::fstream m_WavFileStream
 
WavFileHeader m_Header
 
uint32_t m_DataBlockLength
 
const long long int m_StreamOffset = 44
 
bool isNewFile
 

Detailed Description

This class implements the wave file type using PCM encoding (the simplest encoding). Due to its simplicity it is a seekable file type.

Definition at line 52 of file wavfile.hpp.

Constructor & Destructor Documentation

◆ WavFile()

Audio::WavFile::WavFile ( const std::string &  sFileName)

Constructor. Tries to open the wave file, if it exists.

Parameters
sFileNameconst std::string&

Definition at line 61 of file wavfile.cpp.

References m_WavFileStream, readHeader(), and sName.

Here is the call graph for this function:

◆ ~WavFile()

Audio::WavFile::~WavFile ( )
virtual

Destructor. Will close the file stream and update the header.

Definition at line 75 of file wavfile.cpp.

References closeFile().

Here is the call graph for this function:

Member Function Documentation

◆ closeFile()

void Audio::WavFile::closeFile ( )
private

Updates the wave file header, which is currently open, and closes the file afterwards.

Returns
void

Definition at line 141 of file wavfile.cpp.

References m_WavFileStream.

Referenced by newFile(), and ~WavFile().

◆ getChannels()

virtual size_t Audio::WavFile::getChannels ( ) const
inlineoverridevirtual

Get the number of channels of the current file.

Returns
virtual size_t

Implements Audio::File.

Definition at line 118 of file wavfile.hpp.

References Audio::WavFileHeader::channels, and m_Header.

◆ getLength()

virtual size_t Audio::WavFile::getLength ( ) const
inlineoverridevirtual

Get the length in samples of the current file.

Returns
virtual size_t

Implements Audio::File.

Definition at line 142 of file wavfile.hpp.

References Audio::WavFileHeader::bitsPerSample, Audio::WavFileHeader::channels, m_DataBlockLength, and m_Header.

◆ getPosition()

virtual size_t Audio::WavFile::getPosition ( ) const
inlineoverridevirtual

Get the current position in samples of the current file.

Returns
virtual size_t

Implements Audio::SeekableFile.

Definition at line 156 of file wavfile.hpp.

References Audio::WavFileHeader::bitsPerSample, Audio::WavFileHeader::channels, m_Header, m_StreamOffset, and m_WavFileStream.

◆ getSampleRate()

virtual size_t Audio::WavFile::getSampleRate ( ) const
inlineoverridevirtual

Get the sample rate of the current file.

Returns
virtual size_t

Implements Audio::File.

Definition at line 130 of file wavfile.hpp.

References m_Header, and Audio::WavFileHeader::sampleRate.

◆ isValid()

virtual bool Audio::WavFile::isValid ( ) const
inlinevirtual

Returns, whether the currently opened file is actually open and readable.

Returns
virtual bool

Implements Audio::File.

Definition at line 76 of file wavfile.hpp.

References m_WavFileStream.

◆ newFile()

void Audio::WavFile::newFile ( )
overridevirtual

Prepares the wave file header for a new file, opens the stream and writes the header to the file. Set number of channels and sample frequency before calling this member function.

Returns
void

Implements Audio::File.

Definition at line 167 of file wavfile.cpp.

References Audio::WavFileHeader::bitsPerSample, Audio::WavFileHeader::blockAlign, Audio::WavFileHeader::bytesPerSecond, Audio::WavFileHeader::channels, closeFile(), Audio::WavFileHeader::formatTag, m_Header, m_WavFileStream, PCM, Audio::WavFileHeader::sampleRate, sName, and date::detail::trunc().

Here is the call graph for this function:

◆ read()

Sample Audio::WavFile::read ( ) const
overridevirtual

Read a sample from the audio stream.

Returns
Sample

Implements Audio::File.

Definition at line 215 of file wavfile.cpp.

References Audio::WavFileHeader::bitsPerSample, Audio::WavFileHeader::blockAlign, Audio::WavFileHeader::channels, Audio::convertFromUnsigned(), Audio::getMaxVal(), m_Header, and m_WavFileStream.

Referenced by readSome().

Here is the call graph for this function:

◆ readHeader()

bool Audio::WavFile::readHeader ( )
private

Private member function to read the wave file header to memory. It will ensure that the current file is actually a RIFF WAVE file.

Returns
bool

Definition at line 90 of file wavfile.cpp.

References Audio::WavFileHeader::formatTag, m_DataBlockLength, m_Header, m_WavFileStream, and PCM.

Referenced by WavFile().

◆ readSome()

std::vector< Sample > Audio::WavFile::readSome ( size_t  len) const
overridevirtual

Read a block of samples from the audio stream.

Parameters
lensize_t
Returns
std::vector<Sample>

Implements Audio::SeekableFile.

Definition at line 243 of file wavfile.cpp.

References m_WavFileStream, and read().

Here is the call graph for this function:

◆ setChannels()

virtual void Audio::WavFile::setChannels ( size_t  channels)
inlineoverridevirtual

Set the channels, which shall be used in the new file.

Parameters
channelssize_t
Returns
virtual void

Implements Audio::File.

Definition at line 91 of file wavfile.hpp.

References Audio::WavFileHeader::channels, and m_Header.

◆ setPosition()

virtual void Audio::WavFile::setPosition ( size_t  pos)
inlineoverridevirtual

Set the current position in Samples in the current file.

Parameters
possize_t
Returns
virtual void

Implements Audio::SeekableFile.

Definition at line 172 of file wavfile.hpp.

References Audio::WavFileHeader::bitsPerSample, Audio::WavFileHeader::channels, m_DataBlockLength, m_Header, m_StreamOffset, and m_WavFileStream.

◆ setSampleRate()

virtual void Audio::WavFile::setSampleRate ( size_t  freq)
inlineoverridevirtual

Set the sample rate, which shall be used in the new file.

Parameters
freqsize_t
Returns
virtual void

Implements Audio::File.

Definition at line 104 of file wavfile.hpp.

References m_Header, and Audio::WavFileHeader::sampleRate.

◆ write()

void Audio::WavFile::write ( const Sample sample)
overridevirtual

Write a sample to the audio stream.

Parameters
sampleconst Sample&
Returns
void

Implements Audio::File.

Definition at line 193 of file wavfile.cpp.

References Audio::WavFileHeader::channels, Audio::getMaxVal(), mu::isnan(), Audio::Sample::leftOrMono, m_Header, m_WavFileStream, and Audio::Sample::right.

Referenced by writeSome().

Here is the call graph for this function:

◆ writeSome()

void Audio::WavFile::writeSome ( const std::vector< Sample vSamples)
overridevirtual

Write a block of samples to the audio stream.

Parameters
vSamplesconst std::vector<Sample>
Returns
void

Implements Audio::SeekableFile.

Definition at line 267 of file wavfile.cpp.

References write().

Here is the call graph for this function:

Member Data Documentation

◆ isNewFile

bool Audio::WavFile::isNewFile
private

Definition at line 60 of file wavfile.hpp.

◆ m_DataBlockLength

uint32_t Audio::WavFile::m_DataBlockLength
private

Definition at line 58 of file wavfile.hpp.

Referenced by getLength(), readHeader(), and setPosition().

◆ m_Header

WavFileHeader Audio::WavFile::m_Header
private

◆ m_StreamOffset

const long long int Audio::WavFile::m_StreamOffset = 44
private

Definition at line 59 of file wavfile.hpp.

Referenced by getPosition(), and setPosition().

◆ m_WavFileStream

std::fstream Audio::WavFile::m_WavFileStream
mutableprivate

◆ sName

std::string Audio::WavFile::sName
private

Definition at line 55 of file wavfile.hpp.

Referenced by newFile(), and WavFile().


The documentation for this class was generated from the following files: