1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
|
#include <Windows.h>
#include <cstdio>
struct RIFFCHUNK {
UCHAR lpszName[4];
DWORD dwSize;
};
#define RECAST(x) reinterpret_cast<x>
#define STCAST(x) static_cast<x>
int main() {
HANDLE hWaveFile;
RIFFCHUNK rcChunk;
WAVEFORMATEX wfInfo;
DWORD dwBytesOut;
LPTSTR lpszError[256];
UCHAR* lpAudioData;
hWaveFile = CreateFile(L"F:\sound.wav", GENERIC_ALL, NULL, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_SEQUENTIAL_SCAN, NULL);
SetFilePointer(hWaveFile, 12, 0, FILE_BEGIN);
ReadFile(hWaveFile, RECAST(LPVOID)(&rcChunk), STCAST(DWORD)(sizeof(RIFFCHUNK)), &dwBytesOut, NULL);
ReadFile(hWaveFile, RECAST(LPVOID)(&wfInfo), STCAST(DWORD)(sizeof(WAVEFORMATEX)), &dwBytesOut, NULL);
SetFilePointer(hWaveFile, wfInfo.cbSize, 0, FILE_CURRENT);
do{
ReadFile(hWaveFile, RECAST(LPVOID)(&rcChunk), STCAST(DWORD)(sizeof(rcChunk)), &dwBytesOut, NULL);
}while(lstrcmpiA(RECAST(LPSTR)(rcChunk.lpszName), "data") == 0);
lpAudioData = RECAST(UCHAR*)(HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, rcChunk.dwSize - 8));
ReadFile(hWaveFile, lpAudioData, rcChunk.dwSize, &dwBytesOut, NULL);
HWAVEOUT wo;
WAVEHDR whdr;
MMRESULT mmr;
whdr.dwBufferLength = rcChunk.dwSize - 8;
whdr.lpData = RECAST(LPSTR)(lpAudioData);
whdr.dwFlags = 0;
mmr = waveOutOpen(&wo, WAVE_MAPPER, &wfInfo, NULL, NULL, CALLBACK_NULL);
if(mmr != MMSYSERR_NOERROR) {
std::printf("MMERROR: %d", mmr);
ExitProcess(1);
}
mmr = waveOutPrepareHeader(wo, &whdr, sizeof(WAVEHDR));
if(mmr != MMSYSERR_NOERROR) {
std::printf("MMERROR: %d", mmr);
ExitProcess(2);
}
mmr = waveOutWrite(wo, &whdr, sizeof(WAVEHDR));
if(mmr != MMSYSERR_NOERROR) {
std::printf("MMERROR: %d", mmr);
ExitProcess(3);
}
Sleep(3000);
mmr = waveOutUnprepareHeader(wo, &whdr, sizeof(WAVEHDR));
if(mmr != MMSYSERR_NOERROR) {
std::printf("MMERROR: %d", mmr);
ExitProcess(4);
}
waveOutClose(wo);
return 0;
}
|