src/sound/win32_s.cpp
author terom@frrb.lan
Fri, 19 Dec 2008 01:38:09 +0200
changeset 10439 50f056aa3024
parent 9470 08424e2e79e4
permissions -rw-r--r--
industries, unmoveables... everything but the landscape
2186
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2174
diff changeset
     1
/* $Id$ */
db48cf29b983 (svn r2701) Insert Id tags into all source files
tron
parents: 2174
diff changeset
     2
9111
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8936
diff changeset
     3
/** @file win32_s.cpp Handling of sound for Windows. */
48ce04029fe4 (svn r12971) -Documentation: add @file in files that missed them and add something more than whitespace as description of files that don't have a description.
rubidium
parents: 8936
diff changeset
     4
2189
5cdc11ffeaa4 (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"
5cdc11ffeaa4 (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"
5cdc11ffeaa4 (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"
5cdc11ffeaa4 (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"
8130
d2eb7d04f6e1 (svn r11691) -Codechange: move+rename helpers.hpp and only include it when it is really needed.
rubidium
parents: 7170
diff changeset
     9
#include "../core/alloc_func.hpp"
2189
5cdc11ffeaa4 (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"
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    11
#include <windows.h>
2189
5cdc11ffeaa4 (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>
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    13
7170
923946ec324f (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6247
diff changeset
    14
static FSoundDriver_Win32 iFSoundDriver_Win32;
923946ec324f (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6247
diff changeset
    15
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    16
static HWAVEOUT _waveout;
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    17
static WAVEHDR _wave_hdr[2];
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    18
static int _bufsize;
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    19
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    20
static void PrepareHeader(WAVEHDR *hdr)
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    21
{
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    22
	hdr->dwBufferLength = _bufsize * 4;
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    23
	hdr->dwFlags = 0;
5609
dc6a58930ba4 (svn r8066) - Codechange: MallocT(), CallocT(), ReallocT() now return the pointer to allocated memory instead of modifying the pointer given as parameter
KUDr
parents: 5587
diff changeset
    24
	hdr->lpData = MallocT<char>(_bufsize * 4);
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    25
	if (hdr->lpData == NULL ||
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    26
			waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9111
diff changeset
    27
		usererror("waveOutPrepareHeader failed");
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    28
}
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    29
6247
7d81e3a5d803 (svn r9050) -Codechange: Foo(void) -> Foo()
rubidium
parents: 5988
diff changeset
    30
static void FillHeaders()
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    31
{
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    32
	WAVEHDR *hdr;
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    33
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    34
	for (hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    35
		if (!(hdr->dwFlags & WHDR_INQUEUE)) {
2977
97c1c76b499a (svn r3552) Remove the global variable _mixer
tron
parents: 2449
diff changeset
    36
			MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    37
			if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
9470
08424e2e79e4 (svn r13390) -Codechange: introduce usererror() for fatal but not openttd related errors. Now all error() will 'crash' openttd after showing the message in win32 releases (MSVC), creating a crash.log and crash.dmp (like the '!' hack used before). On the other hand, usererror() will just close the game. So use error() only when it can be helpful to debugging, else use usererror().
glx
parents: 9111
diff changeset
    38
				usererror("waveOutWrite failed");
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    39
		}
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    40
	}
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    41
}
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    42
2449
bcbfed6d3f58 (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,
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    44
	DWORD dwParam1, DWORD dwParam2)
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    45
{
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    46
	switch (uMsg) {
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    47
		case WOM_DONE:
5988
1aabf94612c6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5609
diff changeset
    48
			if (_waveout != NULL) FillHeaders();
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    49
			break;
5988
1aabf94612c6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5609
diff changeset
    50
		default: break;
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    51
	}
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    52
}
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    53
7170
923946ec324f (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6247
diff changeset
    54
const char *SoundDriver_Win32::Start(const char* const* parm)
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    55
{
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    56
	WAVEFORMATEX wfex;
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    57
	wfex.wFormatTag = WAVE_FORMAT_PCM;
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    58
	wfex.nChannels = 2;
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    59
	wfex.wBitsPerSample = 16;
5988
1aabf94612c6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5609
diff changeset
    60
	wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 11025);
1aabf94612c6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5609
diff changeset
    61
	wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
1aabf94612c6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5609
diff changeset
    62
	wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
1aabf94612c6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5609
diff changeset
    63
8936
0a1da26eb8cc (svn r12708) -Fix (FS#1914): increase default sound buffer size only for vista
glx
parents: 8784
diff changeset
    64
	_bufsize = GetDriverParamInt(parm, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 2048 : 1024);
5988
1aabf94612c6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5609
diff changeset
    65
2449
bcbfed6d3f58 (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)
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    67
		return "waveOutOpen failed";
5988
1aabf94612c6 (svn r8691) -Cleanup: Some proper #endif comments for sound/music/video files, and a little elimination of magic numbers in Win32SoundStart
Darkvater
parents: 5609
diff changeset
    68
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    69
	PrepareHeader(&_wave_hdr[0]);
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    70
	PrepareHeader(&_wave_hdr[1]);
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    71
	FillHeaders();
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    72
	return NULL;
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    73
}
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    74
7170
923946ec324f (svn r10444) -Codechange: switch to c++ classes and inheritance for sound/music/video drivers, using self-registration based on the blitter-model.
peter1138
parents: 6247
diff changeset
    75
void SoundDriver_Win32::Stop()
2174
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    76
{
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    77
	HWAVEOUT waveout = _waveout;
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    78
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    79
	_waveout = NULL;
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    80
	waveOutReset(waveout);
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    81
	waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    82
	waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    83
	waveOutClose(waveout);
24d868f1c576 (svn r2688) MSVC and Watcom can't handle identical file names in different directories, oh my...
tron
parents:
diff changeset
    84
}