Following solution is for wav files only
In my 2nd semester of university, I made a console(ASCII) game in C++ as semester project. While making that game, It was required to play multiple audio files. After googling, I wrote following code:
Audio.h
#pragma once
#include <xaudio2.h>
#include <iostream>
#include <string>
using namespace std;
#ifdef _XBOX
#define fourccRIFF 'RIFF'
#define fourccDATA 'data'
#define fourccFMT 'fmt '
#define fourccWAVE 'WAVE'
#define fourccXWMA 'XWMA'
#define fourccDPDS 'dpds'
#endif
#ifndef _XBOX
#define fourccRIFF 'FFIR'
#define fourccDATA 'atad'
#define fourccFMT ' tmf'
#define fourccWAVE 'EVAW'
#define fourccXWMA 'AMWX'
#define fourccDPDS 'sdpd'
#endif
class Audio
{
private:
HRESULT hr;
IXAudio2* pXAudio2;
IXAudio2MasteringVoice* pMasterVoice;
HRESULT FindChunk(HANDLE hFile, DWORD fourcc, DWORD& dwChunkSize, DWORD& dwChunkDataPosition);
HRESULT ReadChunkData(HANDLE hFile, void* buffer, DWORD buffersize, DWORD bufferoffset);
public:
Audio();
int Play(string path, float volume = 1, bool ShouldLoop = false); // plays the audio file with specified volume and can be looped
string BasePath; // Directory where all audio files (relevant to project) are stored i.e if all audio files are stored in "D:\game" than set BasePath to "D:\game", this will be automatically added in path of every audio file
};
Audio.cpp
#include "Audio.h"
Audio::Audio()
{
BasePath = "";
hr = CoInitializeEx(nullptr, COINIT_MULTITHREADED);
if (FAILED(hr))
cout << hr;
pXAudio2 = nullptr;
if (FAILED(hr = XAudio2Create(&pXAudio2, 0, XAUDIO2_DEFAULT_PROCESSOR)))
cout << hr;
pMasterVoice = nullptr;
if (FAILED(hr = pXAudio2->CreateMasteringVoice(&pMasterVoice)))
cout << hr;
}
int Audio::Play(string path, float volume, bool ShouldLoop) {
path = BasePath + "\\" + path;
WAVEFORMATEXTENSIBLE wfx = { 0 };
XAUDIO2_BUFFER buffer = { 0 };
#ifdef _XBOX
char* strFileName = path;
#else
TCHAR* strFileName = new TCHAR[path.size() + 1];
strFileName[path.size()] = 0;
std::copy(path.begin(), path.end(), strFileName);
#endif
// Open the file
HANDLE hFile = CreateFile(
strFileName,
GENERIC_READ,
FILE_SHARE_READ,
NULL,
OPEN_EXISTING,
0,
NULL);
if (INVALID_HANDLE_VALUE == hFile)
return HRESULT_FROM_WIN32(GetLastError());
if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, 0, NULL, FILE_BEGIN))
return HRESULT_FROM_WIN32(GetLastError());
DWORD dwChunkSize;
DWORD dwChunkPosition;
//check the file type, should be fourccWAVE or 'XWMA'
FindChunk(hFile, fourccRIFF, dwChunkSize, dwChunkPosition);
DWORD filetype;
ReadChunkData(hFile, &filetype, sizeof(DWORD), dwChunkPosition);
if (filetype != fourccWAVE)
return S_FALSE;
FindChunk(hFile, fourccFMT, dwChunkSize, dwChunkPosition);
ReadChunkData(hFile, &wfx, dwChunkSize, dwChunkPosition);
FindChunk(hFile, fourccDATA, dwChunkSize, dwChunkPosition);
BYTE* pDataBuffer = new BYTE[dwChunkSize];
ReadChunkData(hFile, pDataBuffer, dwChunkSize, dwChunkPosition);
buffer.AudioBytes = dwChunkSize; // size of the audio buffer in bytes
buffer.pAudioData = pDataBuffer; // buffer containing audio data
buffer.Flags = XAUDIO2_END_OF_STREAM; // tell the source voice not to expect any data after this buffer
if (ShouldLoop) buffer.LoopCount = XAUDIO2_LOOP_INFINITE;
IXAudio2SourceVoice* pSourceVoice;
if (FAILED(hr = pXAudio2->CreateSourceVoice(&pSourceVoice, (WAVEFORMATEX*)&wfx))) cout << hr;
//if (FAILED(hr = pSourceVoice->SetSourceSampleRate(20000))) cout << hr;
if (FAILED(hr = pSourceVoice->SubmitSourceBuffer(&buffer))) cout << hr;
//if (FAILED(hr = pSourceVoice->SetFrequencyRatio(1.2))) cout << hr;
pSourceVoice->SetVolume(volume);
if (FAILED(hr = pSourceVoice->Start(0)))
cout << hr;
return 0;
}
HRESULT Audio::FindChunk(HANDLE hFile, DWORD fourcc, DWORD& dwChunkSize, DWORD& dwChunkDataPosition)
{
HRESULT hr = S_OK;
if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, 0, NULL, FILE_BEGIN))
return HRESULT_FROM_WIN32(GetLastError());
DWORD dwChunkType;
DWORD dwChunkDataSize;
DWORD dwRIFFDataSize = 0;
DWORD dwFileType;
DWORD bytesRead = 0;
DWORD dwOffset = 0;
while (hr == S_OK)
{
DWORD dwRead;
if (0 == ReadFile(hFile, &dwChunkType, sizeof(DWORD), &dwRead, NULL))
hr = HRESULT_FROM_WIN32(GetLastError());
if (0 == ReadFile(hFile, &dwChunkDataSize, sizeof(DWORD), &dwRead, NULL))
hr = HRESULT_FROM_WIN32(GetLastError());
switch (dwChunkType)
{
case fourccRIFF:
dwRIFFDataSize = dwChunkDataSize;
dwChunkDataSize = 4;
if (0 == ReadFile(hFile, &dwFileType, sizeof(DWORD), &dwRead, NULL))
hr = HRESULT_FROM_WIN32(GetLastError());
break;
default:
if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, dwChunkDataSize, NULL, FILE_CURRENT))
return HRESULT_FROM_WIN32(GetLastError());
}
dwOffset += sizeof(DWORD) * 2;
if (dwChunkType == fourcc)
{
dwChunkSize = dwChunkDataSize;
dwChunkDataPosition = dwOffset;
return S_OK;
}
dwOffset += dwChunkDataSize;
if (bytesRead >= dwRIFFDataSize) return S_FALSE;
}
return S_OK;
}
HRESULT Audio::ReadChunkData(HANDLE hFile, void* buffer, DWORD buffersize, DWORD bufferoffset)
{
HRESULT hr = S_OK;
if (INVALID_SET_FILE_POINTER == SetFilePointer(hFile, bufferoffset, NULL, FILE_BEGIN))
return HRESULT_FROM_WIN32(GetLastError());
DWORD dwRead;
if (0 == ReadFile(hFile, buffer, buffersize, &dwRead, NULL))
hr = HRESULT_FROM_WIN32(GetLastError());
return hr;
}
In main file use it like this
#include "Audio.h"
string getExePath(string x) // removes fileName from path i.e "D:\path\to\exe\sample.exe" changes it into "D:\path\to\exe"
{
std::string f = x;
return f.substr(0, f.find_last_of("\\/"));
}
int main(int argc, char* argv[]) {
string exeLocation = argv[0]; // returns path of currently running program i.e "D:\path\to\exe\sample.exe"
Audio audioManager;
audioManager.BasePath = getExePath(exeLocation);
audioManager.Play("bg.wav");
while(1){}
return 0;
}
Note
I don't know the drawbacks of using this code because I wrote it along time ago and most of code is taken from documentation of XAudio2 or some other sources