author | richk |
Tue, 17 Jun 2008 10:32:49 +0000 | |
branch | NewGRF_ports |
changeset 10991 | d8811e327d12 |
parent 10724 | 68a692eacf22 |
permissions | -rw-r--r-- |
2186 | 1 |
/* $Id$ */ |
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 | 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 | 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 | 16 |
static HWAVEOUT _waveout; |
17 |
static WAVEHDR _wave_hdr[2]; |
|
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 | 20 |
static void PrepareHeader(WAVEHDR *hdr) |
21 |
{ |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
22 |
hdr->dwBufferLength = _bufsize * 4; |
0 | 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 | 28 |
} |
29 |
||
6573 | 30 |
static void FillHeaders() |
0 | 31 |
{ |
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 | 35 |
if (!(hdr->dwFlags & WHDR_INQUEUE)) { |
2977 | 36 |
MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4); |
0 | 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 | 39 |
} |
40 |
} |
|
41 |
} |
|
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 | 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 | 51 |
} |
52 |
} |
|
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 | 55 |
{ |
56 |
WAVEFORMATEX wfex; |
|
57 |
wfex.wFormatTag = WAVE_FORMAT_PCM; |
|
58 |
wfex.nChannels = 2; |
|
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 | 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 | 69 |
PrepareHeader(&_wave_hdr[0]); |
70 |
PrepareHeader(&_wave_hdr[1]); |
|
71 |
FillHeaders(); |
|
72 |
return NULL; |
|
73 |
} |
|
74 |
||
6720
35756db7e577
(svn r10560) [NewGRF_ports] -Sync: with trunk r10027-10559
richk
parents:
6573
diff
changeset
|
75 |
void SoundDriver_Win32::Stop() |
0 | 76 |
{ |
77 |
HWAVEOUT waveout = _waveout; |
|
1468
8073826fe82d
(svn r1972) Several cleanups and fix some latent bugs
tron
parents:
1466
diff
changeset
|
78 |
|
0 | 79 |
_waveout = NULL; |
80 |
waveOutReset(waveout); |
|
81 |
waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR)); |
|
82 |
waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR)); |
|
83 |
waveOutClose(waveout); |
|
84 |
} |