2005-07-24 16:12:37 +02:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 22:21:05 +02:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
|
|
|
|
* OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
* See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
|
2008-05-06 17:11:33 +02:00
|
|
|
/** @file win32_s.cpp Handling of sound for Windows. */
|
|
|
|
|
2005-07-25 09:16:10 +02:00
|
|
|
#include "../stdafx.h"
|
|
|
|
#include "../openttd.h"
|
|
|
|
#include "../driver.h"
|
|
|
|
#include "../mixer.h"
|
2007-12-25 10:48:53 +01:00
|
|
|
#include "../core/alloc_func.hpp"
|
2010-01-15 17:41:15 +01:00
|
|
|
#include "../core/bitmath_func.hpp"
|
2005-07-25 09:16:10 +02:00
|
|
|
#include "win32_s.h"
|
2005-07-23 17:16:57 +02:00
|
|
|
#include <windows.h>
|
2005-07-25 09:16:10 +02:00
|
|
|
#include <mmsystem.h>
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
static FSoundDriver_Win32 iFSoundDriver_Win32;
|
|
|
|
|
2005-07-23 17:16:57 +02:00
|
|
|
static HWAVEOUT _waveout;
|
|
|
|
static WAVEHDR _wave_hdr[2];
|
|
|
|
static int _bufsize;
|
2010-01-23 02:36:49 +01:00
|
|
|
static HANDLE _thread;
|
|
|
|
static DWORD _threadId;
|
2005-07-23 17:16:57 +02:00
|
|
|
|
|
|
|
static void PrepareHeader(WAVEHDR *hdr)
|
|
|
|
{
|
|
|
|
hdr->dwBufferLength = _bufsize * 4;
|
|
|
|
hdr->dwFlags = 0;
|
2007-01-11 18:29:39 +01:00
|
|
|
hdr->lpData = MallocT<char>(_bufsize * 4);
|
2010-01-23 02:36:49 +01:00
|
|
|
if (waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) usererror("waveOutPrepareHeader failed");
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2010-01-23 02:36:49 +01:00
|
|
|
static DWORD WINAPI SoundThread(LPVOID arg)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
2010-01-23 02:36:49 +01:00
|
|
|
MSG msg;
|
2005-07-23 17:16:57 +02:00
|
|
|
|
2010-01-23 02:36:49 +01:00
|
|
|
while (_waveout != NULL) {
|
2010-01-25 01:43:51 +01:00
|
|
|
for (WAVEHDR *hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
|
2010-01-23 02:36:49 +01:00
|
|
|
if ((hdr->dwFlags & WHDR_INQUEUE) != 0) continue;
|
2006-02-05 10:56:04 +01:00
|
|
|
MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
|
2010-01-23 02:36:49 +01:00
|
|
|
if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR) usererror("waveOutWrite failed");
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
2010-01-25 01:43:51 +01:00
|
|
|
|
|
|
|
/* Wait for the device to be closed or ready to play new data. */
|
|
|
|
GetMessage(&msg, NULL, MM_WOM_CLOSE, MM_WOM_DONE);
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
2010-01-25 01:43:51 +01:00
|
|
|
|
2010-01-23 02:36:49 +01:00
|
|
|
return 0;
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|
|
|
|
|
2009-01-10 01:31:47 +01:00
|
|
|
const char *SoundDriver_Win32::Start(const char * const *parm)
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
|
|
|
WAVEFORMATEX wfex;
|
|
|
|
wfex.wFormatTag = WAVE_FORMAT_PCM;
|
|
|
|
wfex.nChannels = 2;
|
|
|
|
wfex.wBitsPerSample = 16;
|
2009-08-10 01:04:08 +02:00
|
|
|
wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 44100);
|
2007-02-12 22:55:10 +01:00
|
|
|
wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
|
|
|
|
wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
|
|
|
|
|
2009-08-10 01:04:08 +02:00
|
|
|
_bufsize = GetDriverParamInt(parm, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 8192 : 4096);
|
2007-02-12 22:55:10 +01:00
|
|
|
|
2010-01-25 01:43:51 +01:00
|
|
|
/* Create the sound thread in suspended state because we are not ready to play anything. */
|
2010-01-23 02:36:49 +01:00
|
|
|
if (NULL == (_thread = CreateThread(NULL, 8192, SoundThread, 0, CREATE_SUSPENDED, &_threadId))) return "Failed to create thread";
|
|
|
|
|
2010-01-25 01:43:51 +01:00
|
|
|
/* Open the sound device, it will send messages to sound thread. */
|
2010-01-23 02:36:49 +01:00
|
|
|
if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)_threadId, 0, CALLBACK_THREAD) != MMSYSERR_NOERROR) return "waveOutOpen failed";
|
2007-02-12 22:55:10 +01:00
|
|
|
|
2009-07-10 20:22:04 +02:00
|
|
|
MxInitialize(wfex.nSamplesPerSec);
|
|
|
|
|
2005-07-23 17:16:57 +02:00
|
|
|
PrepareHeader(&_wave_hdr[0]);
|
|
|
|
PrepareHeader(&_wave_hdr[1]);
|
2010-01-23 02:36:49 +01:00
|
|
|
|
2010-01-25 01:43:51 +01:00
|
|
|
/* We are now ready to play sound, so resume the sound thread. */
|
2010-01-23 02:36:49 +01:00
|
|
|
ResumeThread(_thread);
|
|
|
|
|
2005-07-23 17:16:57 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2007-07-05 14:23:54 +02:00
|
|
|
void SoundDriver_Win32::Stop()
|
2005-07-23 17:16:57 +02:00
|
|
|
{
|
|
|
|
HWAVEOUT waveout = _waveout;
|
|
|
|
|
2010-01-25 01:43:51 +01:00
|
|
|
/* Break the sound thread loop, but the thread can still be waiting for a message. */
|
2005-07-23 17:16:57 +02:00
|
|
|
_waveout = NULL;
|
2010-01-23 02:36:49 +01:00
|
|
|
|
2010-01-25 01:43:51 +01:00
|
|
|
/* Stop the playback (if any) and close the device. This will unlock the thread if it's waiting for a message. */
|
|
|
|
waveOutReset(waveout); // Triggers MM_WOM_DONE message if there were pending playbacks.
|
2005-07-23 17:16:57 +02:00
|
|
|
waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
|
|
|
|
waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
|
2010-01-25 01:43:51 +01:00
|
|
|
waveOutClose(waveout); // Triggers MM_WOM_CLOSE message.
|
2010-01-23 02:36:49 +01:00
|
|
|
|
2010-01-25 01:43:51 +01:00
|
|
|
/* Now we can wait for the sound thread to finish because we know it will. */
|
|
|
|
WaitForMultipleObjects(1, &_thread, true, INFINITE);
|
2010-01-23 02:36:49 +01:00
|
|
|
CloseHandle(_thread);
|
2005-07-23 17:16:57 +02:00
|
|
|
}
|