Main.cpp
#include <iostream>
#include "SoundFile_WAV.h"
using namespace std;
int main(int argc, char* argv[])
{
/* read .wav file */
/************************************************************************/
CSoundFile_WAV wav;
if (!CSoundFile_WAV::Load("test1.wav", wav))
{
cout << "ERROR: cant read .wav file" << wav.FileName() << endl;
cin.get();
return 1;
}
cout << "\t---> " << wav.FileName() << endl;
cout << "AudioFormat " << wav.AudioFormat() << endl;
cout << "NumberOfChannels " << wav.NumberOfChannels() << endl;
cout << "SampleRate " << wav.SampleRate() << endl;
cout << "ByteRate " << wav.ByteRate() << endl;
cout << "BlockAlign " << wav.BlockAlign() << endl;
cout << "BitsPerSample " << wav.BitsPerSample() << endl;
cout << "AudioSize in KiloBytes " << wav.AudioSize() / 1000 << endl;
cout << endl;
cin.get();
return 0;
}
SoundFile_WAV.h
#pragma once
#include <string>
#include <vector>
#include <cstdint>
struct CSoundFile_WAVHeader
{
/* RIFF */
std::uint32_t ChunkID{ 0 }; /* 4 bytes, big endian */
std::uint32_t ChunkSize{ 0 }; /* 4 bytes, little endian */
std::uint32_t Format{ 0 }; /* 4 bytes, big endian */
/* fmt, describes sample format */
std::uint32_t SubChunk1ID{ 0 }; /* 4 bytes, big endian */
std::uint32_t SubChunk1Size{ 0 }; /* 4 bytes, little endian */
std::uint16_t AudioFormat{ 0 }; /* 2 bytes, little endian */
std::uint16_t NumChannels{ 0 }; /* 2 bytes, little endian */
std::uint32_t SampleRate{ 0 }; /* 4 bytes, little endian */
std::uint32_t ByteRate{ 0 }; /* 4 bytes, little endian */
std::uint16_t BlockAlign{ 0 }; /* 2 bytes, little endian */
std::uint16_t BitsPerSample{ 0 }; /* 2 bytes, little endian */
/* data */
std::uint32_t SubChunk2ID{ 0 }; /* 4 bytes, big endian */
std::uint32_t SubChunk2Size{ 0 }; /* 4 bytes, little endian */
/* data chunk, little endian */
};
class CSoundFile_WAV
{
public:
static bool Load(const std::string& filename, CSoundFile_WAV& wav);
CSoundFile_WAV();
virtual ~CSoundFile_WAV();
std::string FileName() const;
unsigned int AudioFormat() const;
unsigned int NumberOfChannels() const;
unsigned int SampleRate() const;
unsigned int ByteRate() const;
unsigned int BlockAlign() const;
unsigned int BitsPerSample() const;
unsigned int AudioSize() const;
const void* AudioData() const;
protected:
std::string m_filename;
CSoundFile_WAVHeader m_header;
std::vector<std::uint8_t> m_audiodata;
};
SoundFile_WAV.cpp
#include "SoundFile_WAV.h"
#include <iostream>
#include <fstream>
#include <string.h>
bool CSoundFile_WAV::Load(const std::string & filename, CSoundFile_WAV & wav)
{
/* reset data, assign filename */
wav = CSoundFile_WAV();
wav.m_filename = filename;
/* open resource file */
std::ifstream f(filename, std::ios::binary);
if (!f.good())
{
std::cout << "ERROR: " << __FILE__ << " " << __LINE__ << std::endl;
std::cout << "\tcant open file " << filename << std::endl << std::endl;
return false;
}
/* read file header */
f.read((char*)&wav.m_header, sizeof(CSoundFile_WAVHeader));
/* check for errors */
if (strncmp((char*)&wav.m_header.ChunkID, "RIFF", 4) != 0)
{
std::cout << "ERROR: " << __FILE__ << " " << __LINE__ << std::endl;
std::cout << "\tinvalid .wav file " << filename << " ... missing \"RIFF\"" << std::endl << std::endl;
return false;
}
if (wav.m_header.SubChunk2Size == 0)
{
std::cout << "ERROR: " << __FILE__ << " " << __LINE__ << std::endl;
std::cout << "\tinvalid .wav file " << filename << " ... audio data size = 0" << std::endl << std::endl;
return false;
}
/* read audio data */
wav.m_audiodata.resize(wav.m_header.SubChunk2Size);
f.read((char*)wav.m_audiodata.data(), wav.m_header.SubChunk2Size);
/* successfully loaded .wav file */
return true;
}
CSoundFile_WAV::CSoundFile_WAV()
{}
CSoundFile_WAV::~CSoundFile_WAV()
{}
std::string CSoundFile_WAV::FileName() const
{
return m_filename;
}
unsigned int CSoundFile_WAV::AudioFormat() const
{
return m_header.AudioFormat;
}
unsigned int CSoundFile_WAV::NumberOfChannels() const
{
return m_header.NumChannels;
}
unsigned int CSoundFile_WAV::SampleRate() const
{
return m_header.SampleRate;
}
unsigned int CSoundFile_WAV::ByteRate() const
{
return m_header.ByteRate;
}
unsigned int CSoundFile_WAV::BlockAlign() const
{
return m_header.BlockAlign;
}
unsigned int CSoundFile_WAV::BitsPerSample() const
{
return m_header.BitsPerSample;
}
unsigned int CSoundFile_WAV::AudioSize() const
{
return m_audiodata.size();
}
const void * CSoundFile_WAV::AudioData() const
{
return m_audiodata.data();
}