-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmodplay.cpp
143 lines (129 loc) · 3.48 KB
/
modplay.cpp
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
#include "modplay.h"
//const char ModPlay::EVENT_DONE_NAME[] = "MODPLAY_EVENT_DONE";
ModPlay::ModPlay(const void* data, size_t size) : hWaveOut(NULL), hThread(NULL), hEventDone(NULL), currentBuffer(0), stopped(false)
{
initPlayer();
if(loadData(data, size))
{
if(startListener() && initDevice())
{
for(int i = 0; i < BUFFERS; i++)
{
memset(&headers[i], 0, sizeof(WAVEHDR));
headers[i].lpData = reinterpret_cast<LPSTR>(buffers[i]);
headers[i].dwBufferLength = BUFSIZE;
fillBuffer(i);
}
}
}
}
ModPlay::~ModPlay()
{
stopped = true; // tell the thread to exit
waveOutPause(hWaveOut); // make sure no buffers are written and played in case of a race condition
waveOutReset(hWaveOut); // calls WOM_DONE for all buffers (thread checks if stopped == true and exits)
WaitForSingleObject(hThread, INFINITE); // Let the thread finish
waveOutClose(hWaveOut);
CloseHandle(hEventDone);
CloseHandle(hThread);
ModPlug_Unload(modFile);
}
bool ModPlay::loadData(const void* data, size_t size)
{
modFile = ModPlug_Load(data, size);
return (modFile != 0);
}
void ModPlay::play()
{
waveOutRestart(hWaveOut);
}
void ModPlay::pause()
{
waveOutPause(hWaveOut);
}
void ModPlay::initPlayer() // Set up libmodplug
{
ModPlug_Settings settings;
ModPlug_GetSettings(&settings);
settings.mFrequency = FREQUENCY;
settings.mBits = BITS;
settings.mChannels = CHANNELS;
settings.mLoopCount = -1; // endless
ModPlug_SetSettings(&settings);
}
bool ModPlay::initDevice() // Set up WINMM
{
WAVEFORMATEX wfx = {0};
wfx.wFormatTag = WAVE_FORMAT_PCM;
wfx.nSamplesPerSec = FREQUENCY;
wfx.wBitsPerSample = BITS;
wfx.nChannels = CHANNELS;
wfx.nBlockAlign = BLOCKSIZE;
wfx.nAvgBytesPerSec = BYTESPERSEC;
if(MMSYSERR_NOERROR == waveOutOpen(&hWaveOut, WAVE_MAPPER, &wfx, reinterpret_cast<DWORD_PTR>(&waveOutCallback), reinterpret_cast<DWORD_PTR>(this), CALLBACK_FUNCTION))
{
waveOutPause(hWaveOut);
return true;
}
hWaveOut = NULL;
return false;
}
bool ModPlay::startListener()
{
hEventDone = CreateEvent(NULL, false, false, NULL/*EVENT_DONE_NAME*/);
if(hEventDone/* && GetLastError() != ERROR_ALREADY_EXISTS*/)
{
DWORD threadID;
hThread = CreateThread(NULL, 0, &ThreadProc, static_cast<LPVOID>(this), 0, &threadID);
if(hThread)
{
return true;
}
CloseHandle(hEventDone);
}
hEventDone = NULL;
return false;
}
void ModPlay::fillBuffer(size_t buffer)
{
WAVEHDR* hdr = &headers[buffer];
waveOutUnprepareHeader(hWaveOut, hdr, sizeof(WAVEHDR));
int total = 0;
while(total != BUFSIZE)
{
int read = ModPlug_Read(modFile, hdr->lpData, BUFSIZE - total);
if(read == 0)
{
ModPlug_Seek(modFile, 0);
}
total += read;
}
waveOutPrepareHeader(hWaveOut, hdr, sizeof(WAVEHDR));
waveOutWrite(hWaveOut, hdr, sizeof(WAVEHDR));
}
void ModPlay::waveOutCallback(HWAVEOUT hWaveOut, UINT uMsg, DWORD_PTR dwInstance, DWORD_PTR dwParam1, DWORD_PTR dwParam2)
{
switch(uMsg)
{
case WOM_DONE:
ModPlay* instance = reinterpret_cast<ModPlay*>(dwInstance);
SetEvent(instance->hEventDone);
/*
HANDLE hEvent;
hEvent = OpenEvent(EVENT_MODIFY_STATE, false, EVENT_DONE_NAME);
SetEvent(hEvent);
CloseHandle(hEvent);
*/
break;
}
}
DWORD ModPlay::ThreadProc(LPVOID lpParameter) // Refills played buffers
{
ModPlay* instance = reinterpret_cast<ModPlay*>(lpParameter);
while(WAIT_OBJECT_0 == WaitForSingleObject(instance->hEventDone, INFINITE) && !instance->stopped)
{
instance->fillBuffer(instance->currentBuffer);
instance->currentBuffer = (instance->currentBuffer + 1) % BUFFERS;
}
return 0;
}