src/sound/win32_s.cpp
author richk
Tue, 17 Jun 2008 10:32:49 +0000
branchNewGRF_ports
changeset 10991 d8811e327d12
parent 10724 68a692eacf22
permissions -rw-r--r--
(svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
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
10724
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10211
diff changeset
     3
/** @file win32_s.cpp Handling of sound for Windows. */
68a692eacf22 (svn r13274) [NewGRF_ports] -Sync: with trunk r12806:13144.
richk
parents: 10211
diff changeset
     4
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
     5
#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
     6
#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
     7
#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
     8
#include "../mixer.h"
6872
1c4a4a609f85 (svn r11950) [NewGRF_ports] -Sync with trunk r11566:11949.
rubidium
parents: 6720
diff changeset
     9
#include "../core/alloc_func.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
    10
#include "win32_s.h"
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    11
#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
    12
#include <mmsystem.h>
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    13
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6573
diff changeset
    14
static FSoundDriver_Win32 iFSoundDriver_Win32;
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6573
diff changeset
    15
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    16
static HWAVEOUT _waveout;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    17
static WAVEHDR _wave_hdr[2];
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    18
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
    19
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    20
static void PrepareHeader(WAVEHDR *hdr)
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    21
{
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    22
	hdr->dwBufferLength = _bufsize * 4;
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    23
	hdr->dwFlags = 0;
5860
7fdc9b423ba1 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5838
diff changeset
    24
	hdr->lpData = MallocT<char>(_bufsize * 4);
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    25
	if (hdr->lpData == NULL ||
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    26
			waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10724
diff changeset
    27
		usererror("waveOutPrepareHeader failed");
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
6573
7624f942237f (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 6314
diff changeset
    30
static void FillHeaders()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    31
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    32
	WAVEHDR *hdr;
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    33
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    34
	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
    35
		if (!(hdr->dwFlags & WHDR_INQUEUE)) {
2977
0240dd4704a7 (svn r3552) Remove the global variable _mixer
tron
parents: 2449
diff changeset
    36
			MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    37
			if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
10991
d8811e327d12 (svn r13545) [NewGRF_ports] -Sync: with trunk r13281:13411.
richk
parents: 10724
diff changeset
    38
				usererror("waveOutWrite failed");
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    39
		}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    40
	}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    41
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    42
2449
29c0920aee6d (svn r2975) Use the correct types, not types which accidently have the same size on 32bit machines
tron
parents: 2189
diff changeset
    43
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
    44
	DWORD dwParam1, DWORD dwParam2)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    45
{
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    46
	switch (uMsg) {
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    47
		case WOM_DONE:
6314
f738bcf05ad6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5860
diff changeset
    48
			if (_waveout != NULL) FillHeaders();
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    49
			break;
6314
f738bcf05ad6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5860
diff changeset
    50
		default: break;
0
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
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    53
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6573
diff changeset
    54
const char *SoundDriver_Win32::Start(const char* const* parm)
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    55
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    56
	WAVEFORMATEX wfex;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    57
	wfex.wFormatTag = WAVE_FORMAT_PCM;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    58
	wfex.nChannels = 2;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    59
	wfex.wBitsPerSample = 16;
6314
f738bcf05ad6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5860
diff changeset
    60
	wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 11025);
f738bcf05ad6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5860
diff changeset
    61
	wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
f738bcf05ad6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5860
diff changeset
    62
	wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
f738bcf05ad6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5860
diff changeset
    63
10211
c1391c8ed5c6 (svn r12743) [NewGRF_ports] -Sync: with trunk r12705:12741.
richk
parents: 10184
diff changeset
    64
	_bufsize = GetDriverParamInt(parm, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 2048 : 1024);
6314
f738bcf05ad6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5860
diff changeset
    65
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";
6314
f738bcf05ad6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5860
diff changeset
    68
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    69
	PrepareHeader(&_wave_hdr[0]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    70
	PrepareHeader(&_wave_hdr[1]);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    71
	FillHeaders();
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    72
	return NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    73
}
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    74
6720
35756db7e577 (svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents: 6573
diff changeset
    75
void SoundDriver_Win32::Stop()
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    76
{
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    77
	HWAVEOUT waveout = _waveout;
1468
8073826fe82d (svn r1972) Several cleanups and fix some latent bugs
tron
parents: 1466
diff changeset
    78
0
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    79
	_waveout = NULL;
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    80
	waveOutReset(waveout);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    81
	waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    82
	waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    83
	waveOutClose(waveout);
29654efe3188 (svn r1) Import of revision 975 of old (crashed) SVN
truelight
parents:
diff changeset
    84
}