aboutsummaryrefslogtreecommitdiffstats
path: root/src/views/SkOSSound.cpp
blob: 13cd037756b1b4dd7587a3d0f418c242f1c2a830 (plain)
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
#include "SkOSSound.h"

#ifdef SK_BUILD_FOR_WIN

//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////

#include <Mmreg.h>
#if defined _WIN32 && _MSC_VER >= 1300	// disable nameless struct/union
#pragma warning ( push )
#pragma warning ( disable : 4201 )
#endif
#include <Mmsystem.h>
#if defined _WIN32 && _MSC_VER >= 1300	
#pragma warning ( pop )
#endif
#include <stdio.h>

class CWaveFile {
public:
	BOOL Open(const char path[]);
	void Close();

	long Read(char* pData, long nLength);

	long GetLength() const {return m_nLength;}
	WAVEFORMATEX* GetWaveFormat()	{return (&m_Format);}

protected:
	FILE* m_pFile;
	long m_nLength;
	WAVEFORMATEX m_Format;

private:
	enum {
		WF_OFFSET_FORMATTAG	=		20,
		WF_OFFSET_CHANNELS =			22,
		WF_OFFSET_SAMPLESPERSEC =		24,
		WF_OFFSET_AVGBYTESPERSEC =	28,
		WF_OFFSET_BLOCKALIGN =		32,
		WF_OFFSET_BITSPERSAMPLE =		34,
		WF_OFFSET_DATASIZE =			40,
		WF_OFFSET_DATA =				44,
		WF_HEADER_SIZE = WF_OFFSET_DATA
	};
};

BOOL CWaveFile::Open(const char path[])
{
	BYTE aHeader[WF_HEADER_SIZE];

/*	hResInfo = FindResource (hInst, lpName, "WAVE"); 

	if (hResInfo == NULL) 
	return FALSE; 

	// Load the wave resource. 
	hRes = LoadResource (hInst, hResInfo); 

	if (hRes == NULL) 
		return FALSE; 

	// Lock the wave resource and play it. 
	lpRes = LockResource (0);
*/


	// open file
//	m_pFile = _tfopen(szFileName, TEXT("rb"));
	m_pFile = fopen(path, "rb");
	if (!m_pFile) {
		return FALSE;
	}

	// set file length
	fseek(m_pFile, 0, SEEK_END);
	m_nLength = ftell(m_pFile) - WF_HEADER_SIZE;

	// set the format attribute members
	fseek(m_pFile, 0, SEEK_SET);
	fread(aHeader, 1, WF_HEADER_SIZE, m_pFile);
	m_Format.wFormatTag = *((WORD*) (aHeader + WF_OFFSET_FORMATTAG));
	m_Format.nChannels = *((WORD*) (aHeader + WF_OFFSET_CHANNELS));
	m_Format.nSamplesPerSec = *((DWORD*) (aHeader + WF_OFFSET_SAMPLESPERSEC));
	m_Format.nAvgBytesPerSec = *((DWORD*) (aHeader + WF_OFFSET_AVGBYTESPERSEC));
	m_Format.nBlockAlign = *((WORD*) (aHeader + WF_OFFSET_BLOCKALIGN));
	m_Format.wBitsPerSample = *((WORD*) (aHeader + WF_OFFSET_BITSPERSAMPLE));

	return TRUE;
}

void CWaveFile::Close()
{
	fclose(m_pFile);
}

long CWaveFile::Read(char* pData, long nLength)
{
	return fread(pData, 1, nLength, m_pFile);
}

////////////////////////////////////////////////////////////////////////////////////////

struct SkOSSoundWave {
	HWAVEOUT hwo;
	WAVEHDR whdr;
	DWORD dwOldVolume;
	CWaveFile waveFile;
	HANDLE hDoneEvent;
};

static SkOSSoundWave gWave;
static bool			 gWavePaused;
static U8			 gVolume;
static bool			 gInited = false;

static void init_wave()
{
	if (gInited == false)
	{
		gWave.hwo = nil;
		gWavePaused = false;
		gVolume = 0x80;
		gInited = true;
	}
}

MMRESULT StartWave(const char path[], SkOSSoundWave* wave, U32 vol);
MMRESULT EndWave(SkOSSoundWave* wave);

#define MAX_ERRMSG 256

//#include "SkOSFile.h"	// for utf16

void SkOSSound::Play(const char path[]) 
{
	init_wave();

	if (gWave.hwo != nil)
		SkOSSound::Stop();

	U32 v32 = (gVolume << 8) | gVolume;	// fill it out to 16bits
	v32 |= v32 << 16;					// set the left and right channels

	StartWave(path, &gWave, v32);
	gWavePaused = false;
}

bool SkOSSound::TogglePause() 
{
	init_wave();

	if (gWavePaused) 
		SkOSSound::Resume();
	else 
		SkOSSound::Pause(); 
	return !gWavePaused;
}


void SkOSSound::Pause() 
{
	init_wave();

	if (gWave.hwo == nil || (gWave.whdr.dwFlags & WHDR_DONE))
		return;
	waveOutPause(gWave.hwo);
	gWavePaused = true;
}

void SkOSSound::Resume() 
{
	init_wave();

	if (gWave.hwo == nil || (gWave.whdr.dwFlags & WHDR_DONE))
		return;
	waveOutRestart(gWave.hwo);
	gWavePaused = false;
}

void SkOSSound::Stop() 
{
	init_wave();

//	if (gWave.hwo == nil || (gWave.whdr.dwFlags & WHDR_DONE))
	if (gWave.hwo == nil)
		return;
	waveOutReset(gWave.hwo);
	EndWave(&gWave);
	gWavePaused = false;
	gWave.hwo = nil;
}

U8 SkOSSound::GetVolume()
{
	init_wave();
	return gVolume;
}

void SkOSSound::SetVolume(U8CPU vol)
{
	if ((int)vol < 0)
		vol = 0;
	else if (vol > 255)
		vol = 255;

	init_wave();
	gVolume = SkToU8(vol);

	if (gWave.hwo)
	{
		unsigned long v32 = (vol << 8) | vol;	// fill it out to 16bits
		v32 |= v32 << 16;						// set the left and right channels
		waveOutSetVolume(gWave.hwo, v32);
	}
}

#if 0
unsigned long SoundManager::GetPosition()
{
	if (fWave.hwo == nil)
		return 0;
	MMTIME time;
	time.wType = TIME_MS;
	if (waveOutGetPosition(fWave.hwo, &time, sizeof(time)) == MMSYSERR_NOERROR &&
		time.wType == TIME_MS)
	{
		return time.u.ms;
	}
	return 0;
}
#endif

MMRESULT StartWave(const char path[], SkOSSoundWave* wave, U32 vol)
{
	HWAVEOUT hwo = nil;
//	WAVEHDR whdr;
	MMRESULT mmres = 0;
//	CWaveFile waveFile;
//	HANDLE hDoneEvent = wave.hDoneEvent = 
//		CreateEvent(NULL, FALSE, FALSE, TEXT("DONE_EVENT"));
	UINT devId;
//	DWORD dwOldVolume;

	// Open wave file
	if (!wave->waveFile.Open(path)) {
//		TCHAR szErrMsg[MAX_ERRMSG];
//		_stprintf(szErrMsg,	TEXT("Unable to open file: %s\n"), szWavFile);
//		MessageBox(NULL, szErrMsg, TEXT("File I/O Error"), MB_OK);
		return MMSYSERR_NOERROR;
	}

	// Open audio device
	for (devId = 0; devId < waveOutGetNumDevs(); devId++)
	{
		mmres = waveOutOpen(&hwo, devId, wave->waveFile.GetWaveFormat(), 0, 0, CALLBACK_NULL);
		if (mmres == MMSYSERR_NOERROR)
		{
			wave->hwo = hwo;
			break;
		}
	}
	if (mmres != MMSYSERR_NOERROR)
	{
		SkDEBUGCODE(SkDebugf("waveOutOpen(%s) -> %d\n", path, mmres);) 
		return mmres;
	}

	// Set volume
	mmres = waveOutGetVolume(hwo, &wave->dwOldVolume);
	if (mmres != MMSYSERR_NOERROR) {
		return mmres;
	}

	waveOutSetVolume(hwo, vol);
	if (mmres != MMSYSERR_NOERROR) {
		return mmres;
	}

	// Initialize wave header
	ZeroMemory(&wave->whdr, sizeof(WAVEHDR));
	wave->whdr.lpData = new char[wave->waveFile.GetLength()];
	wave->whdr.dwBufferLength = wave->waveFile.GetLength();
	wave->whdr.dwUser = 0;
	wave->whdr.dwFlags = 0;
	wave->whdr.dwLoops = 0;
	wave->whdr.dwBytesRecorded = 0;
	wave->whdr.lpNext = 0;
	wave->whdr.reserved = 0;

	// Play buffer
	wave->waveFile.Read(wave->whdr.lpData, wave->whdr.dwBufferLength);

	mmres = waveOutPrepareHeader(hwo, &wave->whdr, sizeof(WAVEHDR));	
	if (mmres != MMSYSERR_NOERROR) {
		return mmres;
	}

	mmres = waveOutWrite(hwo, &wave->whdr, sizeof(WAVEHDR));	
//	if (mmres != MMSYSERR_NOERROR) {
		return mmres;
//	}
}

#if 0
void IdleWave(Wave& wave)
{
	// Wait for audio to finish playing
	while (!(wave.whdr.dwFlags & WHDR_DONE)) {
		WaitForSingleObject(wave.hDoneEvent, INFINITE);
	}
}
#endif

MMRESULT EndWave(SkOSSoundWave* wave)
{
	HWAVEOUT hwo = wave->hwo;
	MMRESULT mmres;
	// Clean up
	mmres = waveOutUnprepareHeader(hwo, &wave->whdr, sizeof(WAVEHDR));	
	if (mmres != MMSYSERR_NOERROR) {
		return mmres;
	}

	waveOutSetVolume(hwo, wave->dwOldVolume);
	if (mmres != MMSYSERR_NOERROR) {
		return mmres;
	}

	mmres = waveOutClose(hwo);
	if (mmres != MMSYSERR_NOERROR) {
		return mmres;
	}

	delete [] wave->whdr.lpData;
	wave->waveFile.Close();

	return MMSYSERR_NOERROR;
}

#endif	/* SK_BUILD_FOR_WIN */