# win32_s.cpp

Go to the documentation of this file.

    00001 /* $Id: win32_s.cpp 14949 2009-01-10 00:31:47Z rubidium $ */
    00002
    00005 #include "../stdafx.h"
    00006 #include "../openttd.h"
    00007 #include "../driver.h"
    00008 #include "../mixer.h"
    00009 #include "../core/alloc_func.hpp"
    00010 #include "win32_s.h"
    00011 #include <windows.h>
    00012 #include <mmsystem.h>
    00013
    00014 static FSoundDriver_Win32 iFSoundDriver_Win32;
    00015
    00016 static HWAVEOUT _waveout;
    00017 static WAVEHDR _wave_hdr[2];
    00018 static int _bufsize;
    00019
    00020 static void PrepareHeader(WAVEHDR *hdr)
    00021 {
    00022   hdr->dwBufferLength = _bufsize * 4;
    00023   hdr->dwFlags = 0;
    00024   hdr->lpData = MallocT<char>(_bufsize * 4);
    00025   if (hdr->lpData == NULL ||
    00026       waveOutPrepareHeader(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
    00027     usererror("waveOutPrepareHeader failed");
    00028 }
    00029
    00030 static void FillHeaders()
    00031 {
    00032   WAVEHDR *hdr;
    00033
    00034   for (hdr = _wave_hdr; hdr != endof(_wave_hdr); hdr++) {
    00035     if (!(hdr->dwFlags & WHDR_INQUEUE)) {
    00036       MxMixSamples(hdr->lpData, hdr->dwBufferLength / 4);
    00037       if (waveOutWrite(_waveout, hdr, sizeof(WAVEHDR)) != MMSYSERR_NOERROR)
    00038         usererror("waveOutWrite failed");
    00039     }
    00040   }
    00041 }
    00042
    00043 static void CALLBACK waveOutProc(HWAVEOUT hwo, UINT uMsg, DWORD_PTR dwInstance,
    00044   DWORD dwParam1, DWORD dwParam2)
    00045 {
    00046   switch (uMsg) {
    00047     case WOM_DONE:
    00048       if (_waveout != NULL) FillHeaders();
    00049       break;
    00050     default: break;
    00051   }
    00052 }
    00053
    00054 const char *SoundDriver_Win32::Start(const char * const *parm)
    00055 {
    00056   WAVEFORMATEX wfex;
    00057   wfex.wFormatTag = WAVE_FORMAT_PCM;
    00058   wfex.nChannels = 2;
    00059   wfex.wBitsPerSample = 16;
    00060   wfex.nSamplesPerSec = GetDriverParamInt(parm, "hz", 11025);
    00061   wfex.nBlockAlign = (wfex.nChannels * wfex.wBitsPerSample) / 8;
    00062   wfex.nAvgBytesPerSec = wfex.nSamplesPerSec * wfex.nBlockAlign;
    00063
    00064   _bufsize = GetDriverParamInt(parm, "bufsize", (GB(GetVersion(), 0, 8) > 5) ? 2048 : 1024);
    00065
    00066   if (waveOutOpen(&_waveout, WAVE_MAPPER, &wfex, (DWORD_PTR)&waveOutProc, 0, CALLBACK_FUNCTION) != MMSYSERR_NOERROR)
    00067     return "waveOutOpen failed";
    00068
    00069   PrepareHeader(&_wave_hdr[0]);
    00070   PrepareHeader(&_wave_hdr[1]);
    00071   FillHeaders();
    00072   return NULL;
    00073 }
    00074
    00075 void SoundDriver_Win32::Stop()
    00076 {
    00077   HWAVEOUT waveout = _waveout;
    00078
    00079   _waveout = NULL;
    00080   waveOutReset(waveout);
    00081   waveOutUnprepareHeader(waveout, &_wave_hdr[0], sizeof(WAVEHDR));
    00082   waveOutUnprepareHeader(waveout, &_wave_hdr[1], sizeof(WAVEHDR));
    00083   waveOutClose(waveout);
    00084 }