| 1 |
#define _WIN32_DCOM |
| 2 |
#include <xaudio2.h> |
| 3 |
#include <windows.h> |
| 4 |
#include <iostream> |
| 5 |
#include <fstream> |
| 6 |
using std::cout; |
| 7 |
using std::endl; |
| 8 |
using std::ifstream; |
| 9 |
|
| 10 |
int main() |
| 11 |
{ |
| 12 |
IXAudio2MasteringVoice* pMasteringVoice = NULL; |
| 13 |
IXAudio2* pXAudio2 = NULL; |
| 14 |
IXAudio2SourceVoice* pSourceVoice = NULL; |
| 15 |
char* buffer = NULL; |
| 16 |
CoInitializeEx(0, COINIT_MULTITHREADED); |
| 17 |
while(1) //Non-neccesary loop to reduce code reproduction. |
| 18 |
{ |
| 19 |
if(FAILED(XAudio2Create(&pXAudio2, 0))) |
| 20 |
{ |
| 21 |
cout << "Failed on creation" << endl; |
| 22 |
break; |
| 23 |
} |
| 24 |
|
| 25 |
if(FAILED(pXAudio2->CreateMasteringVoice(&pMasteringVoice))) |
| 26 |
{ |
| 27 |
cout << "Failed creating mastering voice" << endl; |
| 28 |
break; |
| 29 |
} |
| 30 |
|
| 31 |
ifstream xwmaFile("Techno", std::ios::binary); |
| 32 |
if(!xwmaFile.is_open()) |
| 33 |
{ |
| 34 |
cout << "Failure to open file" << endl; |
| 35 |
break; |
| 36 |
} |
| 37 |
|
| 38 |
char riff[4]; |
| 39 |
xwmaFile.read(riff, sizeof(riff)); |
| 40 |
|
| 41 |
long fileSize = 0; |
| 42 |
xwmaFile.read((char*)&fileSize, sizeof(fileSize)); |
| 43 |
|
| 44 |
char xwmafmt[8]; |
| 45 |
xwmaFile.read(xwmafmt, sizeof(xwmafmt)); |
| 46 |
|
| 47 |
unsigned long xwmafmtSize; |
| 48 |
xwmaFile.read((char*)&xwmafmtSize, sizeof(xwmafmtSize)); |
| 49 |
|
| 50 |
XMA2WAVEFORMATEX format; |
| 51 |
xwmaFile.read((char*)&format.wfx, sizeof(format.wfx)); |
| 52 |
if(xwmafmtSize > sizeof(format.wfx)) |
| 53 |
{ |
| 54 |
xwmaFile.read((char*)&format.NumStreams, sizeof(format.NumStreams)); |
| 55 |
xwmaFile.read((char*)&format.ChannelMask, sizeof(format.ChannelMask)); |
| 56 |
xwmaFile.read((char*)&format.SamplesEncoded, sizeof(format.SamplesEncoded)); |
| 57 |
xwmaFile.read((char*)&format.BytesPerBlock, sizeof(format.BytesPerBlock)); |
| 58 |
xwmaFile.read((char*)&format.EncoderVersion, sizeof(format.EncoderVersion)); |
| 59 |
xwmaFile.read((char*)&format.BlockCount, sizeof(format.BlockCount)); |
| 60 |
} |
| 61 |
|
| 62 |
xwmaFile |
| 63 |
//Find the dpds chunk. |
| 64 |
char dpds = 'a'; |
| 65 |
DPDSFAIL: |
| 66 |
do |
| 67 |
xwmaFile.read(&dpds, 1); |
| 68 |
while(dpds != 'd'); |
| 69 |
xwmaFile.read(&dpds, 1); |
| 70 |
if(dpds != 'p') |
| 71 |
goto DPDSFAIL; |
| 72 |
xwmaFile.read(&dpds, 1); |
| 73 |
if(dpds != 'd') |
| 74 |
goto DPDSFAIL; |
| 75 |
xwmaFile.read(&dpds, 1); |
| 76 |
if(dpds != 's') |
| 77 |
goto DPDSFAIL; |
| 78 |
|
| 79 |
XAUDIO2_BUFFER_WMA wmaBuffer = {0}; |
| 80 |
long dpdsSize = 0; |
| 81 |
xwmaFile.read((char*)&dpdsSize, sizeof(dpdsSize)); |
| 82 |
wmaBuffer.PacketCount = dpdsSize / 4; |
| 83 |
UINT32* DecodedPacket = new UINT32[wmaBuffer.PacketCount]; |
| 84 |
xwmaFile.read((char*)DecodedPacket, dpdsSize); |
| 85 |
wmaBuffer.pDecodedPacketCumulativeBytes = DecodedPacket; |
| 86 |
|
| 87 |
//Find the data chunk. |
| 88 |
char data = 'a'; |
| 89 |
DATAFAIL: |
| 90 |
do |
| 91 |
xwmaFile.read(&data, 1); |
| 92 |
while(data != 'd'); |
| 93 |
xwmaFile.read(&data, 1); |
| 94 |
if(data != 'a') |
| 95 |
goto DATAFAIL; |
| 96 |
xwmaFile.read(&data, 1); |
| 97 |
if(data != 't') |
| 98 |
goto DATAFAIL; |
| 99 |
xwmaFile.read(&data, 1); |
| 100 |
if(data != 'a') |
| 101 |
goto DATAFAIL; |
| 102 |
|
| 103 |
unsigned long dataSize = 0; |
| 104 |
xwmaFile.read((char*)&dataSize, sizeof(dataSize)); |
| 105 |
HRESULT hr; |
| 106 |
if(FAILED(hr = pXAudio2->CreateSourceVoice(&pSourceVoice, (WAVEFORMATEX*)&format))) |
| 107 |
{ |
| 108 |
cout << "Failed to create a source voice: " << (hr & 0xFFFF) << endl; |
| 109 |
break; |
| 110 |
} |
| 111 |
buffer = new char[dataSize]; |
| 112 |
xwmaFile.read(buffer, dataSize); |
| 113 |
xwmaFile.close(); |
| 114 |
|
| 115 |
XAUDIO2_BUFFER xBuffer = {0}; |
| 116 |
xBuffer.pAudioData = ((unsigned char*)buffer); |
| 117 |
xBuffer.Flags = XAUDIO2_END_OF_STREAM; |
| 118 |
xBuffer.AudioBytes = dataSize; |
| 119 |
|
| 120 |
if(FAILED(pSourceVoice->SubmitSourceBuffer(&xBuffer, &wmaBuffer))) |
| 121 |
{ |
| 122 |
cout << "Failed to submit the source buffer." << endl; |
| 123 |
break; |
| 124 |
} |
| 125 |
if(SUCCEEDED(pSourceVoice->Start(0))) |
| 126 |
{ |
| 127 |
bool isRunning = true; |
| 128 |
while(isRunning) |
| 129 |
{ |
| 130 |
XAUDIO2_VOICE_STATE state; |
| 131 |
pSourceVoice->GetState(&state); |
| 132 |
isRunning = (state.Buffer |