src/sound/win32_s.cpp
author celestar
Mon, 15 Jan 2007 20:14:06 +0000
branchcustombridgeheads
changeset 5650 aefc131bf5ce
parent 5649 55c8267c933f
child 5860 7fdc9b423ba1
permissions -rw-r--r--
(svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
2186
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2174
diff changeset
     1
/* $Id$ */
461a2aff3486 (svn r2701) Insert Id tags into all source files
tron
parents: 2174
diff changeset
     2
2189
d240b9097139 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     3
#include "../stdafx.h"
d240b9097139 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     4
#include "../openttd.h"
d240b9097139 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     5
#include "../driver.h"
d240b9097139 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     6
#include "../functions.h"
d240b9097139 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     7
#include "../mixer.h"
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
     8
#include "../helpers.hpp"
2189
d240b9097139 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
     9
#include "win32_s.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    10
#include <windows.h>
2189
d240b9097139 (svn r2704) Remove . from include path again, too much trouble. Also add some #ifdefs and #includes for the Windows build
tron
parents: 2186
diff changeset
    11
#include <mmsystem.h>
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    12
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
static HWAVEOUT _waveout;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    14
static WAVEHDR _wave_hdr[2];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    15
static int _bufsize;
2171
008122046f7f (svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents: 2163
diff changeset
    16
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
static void PrepareHeader(WAVEHDR *hdr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
{
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    19
	hdr->dwBufferLength = _bufsize * 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
	hdr->dwFlags = 0;
5650
aefc131bf5ce (svn r8149) [cbh] - Sync with -r8038:8038 from trunk (the cpp merge)
celestar
parents: 5649
diff changeset
    21
	MallocT(&hdr->lpData, _bufsize * 4);
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    22
	if (hdr->lpData == NULL ||
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    23
			waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    24
		error("waveOutPrepareHeader failed");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    25
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    26
1102
316643e34104 (svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents: 1040
diff changeset
    27
static void FillHeaders(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    28
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    29
	WAVEHDR *hdr;
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    30
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    31
	for (hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
		if (!(hdr->dwFlags & WHDR_INQUEUE)) {
2977
0240dd4704a7 (svn r3552) Remove the global variable _mixer
tron
parents: 2449
diff changeset
    33
			MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    34
			if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    35
				error("waveOutWrite failed");
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    36
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    38
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
2449
29c0920aee6d (svn r2975) Use the correct types, not types which accidently have the same size on 32bit machines
tron
parents: 2189
diff changeset
    40
static void CALLBACK waveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    41
	DWORD dwParam1, DWORD dwParam2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
{
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    43
	switch (uMsg) {
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    44
		case WOM_DONE:
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    45
			if (_waveout) FillHeaders();
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    46
			break;
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    47
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    48
		default:
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    49
			break;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    50
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    51
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    52
2171
008122046f7f (svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents: 2163
diff changeset
    53
static const char *Win32SoundStart(const char* const* parm)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    54
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
	WAVEFORMATEX wfex;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
	int hz;
193
0a7025304867 (svn r194) -Codechange: stripping trailing-spaces. Please keep this that way!
truelight
parents: 164
diff changeset
    57
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
	_bufsize = GetDriverParamInt(parm, "bufsize", 1024);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
	hz = GetDriverParamInt(parm, "hz", 11025);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    60
	wfex.wFormatTag = WAVE_FORMAT_PCM;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    61
	wfex.nChannels = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    62
	wfex.nSamplesPerSec = hz;
2171
008122046f7f (svn r2685) -Codechange: Split the music/sound/video drivers into separate files and move them into subfolders.
tron
parents: 2163
diff changeset
    63
	wfex.nAvgBytesPerSec = hz * 2 * 2;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    64
	wfex.nBlockAlign = 4;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    65
	wfex.wBitsPerSample = 16;
2449
29c0920aee6d (svn r2975) Use the correct types, not types which accidently have the same size on 32bit machines
tron
parents: 2189
diff changeset
    66
	if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)&waveOutProc, 0, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    67
		return "waveOutOpen failed";
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    68
	PrepareHeader(&_wave_hdr[0]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
	PrepareHeader(&_wave_hdr[1]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
	FillHeaders();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
1102
316643e34104 (svn r1603) -Fix: unused variable in FormatTinyDate
darkvater
parents: 1040
diff changeset
    74
static void Win32SoundStop(void)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    75
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
	HWAVEOUT waveout = _waveout;
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    77
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    78
	_waveout = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
	waveOutReset(waveout);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
	waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
	waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
	waveOutClose(waveout);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    85
const HalSoundDriver _win32_sound_driver = {
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    86
	Win32SoundStart,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    87
	Win32SoundStop,
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    88
};