| 1 | /* $Id: OS2DSOUND.CPP,v 1.8 2001-03-10 06:21:06 mike Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * DirectSound main class | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998 Sander van Leeuwen | 
|---|
| 7 | * Copyright 2000 Kevin Langman | 
|---|
| 8 | * Copyright 2001 Michal Necasek | 
|---|
| 9 | * | 
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 |  | 
|---|
| 14 | /*@Header*********************************************************************** | 
|---|
| 15 | *   Header Files                                                               * | 
|---|
| 16 | *******************************************************************************/ | 
|---|
| 17 | #include <os2win.h> | 
|---|
| 18 | #include <stdlib.h> | 
|---|
| 19 | #include <string.h> | 
|---|
| 20 |  | 
|---|
| 21 | #include <stdio.h> /****** DEBUGGING ********/ | 
|---|
| 22 |  | 
|---|
| 23 | #define CINTERFACE | 
|---|
| 24 | #include <dsound.h> | 
|---|
| 25 |  | 
|---|
| 26 | #include "OS2DSound.h" | 
|---|
| 27 | #include "OS2SndBuffer.h" | 
|---|
| 28 | #include "OS2PrimBuff.h" | 
|---|
| 29 | #include "OS23DListener.h" | 
|---|
| 30 | #include "OS2Notify.h" | 
|---|
| 31 | #include <misc.h> | 
|---|
| 32 |  | 
|---|
| 33 | // TODO: handle cooperative levels properly!! | 
|---|
| 34 |  | 
|---|
| 35 | // this flag is set if the OS2IDirectObject exists | 
|---|
| 36 | BOOL OS2IDirectSound::fDSExists = FALSE; | 
|---|
| 37 |  | 
|---|
| 38 | //****************************************************************************** | 
|---|
| 39 | //****************************************************************************** | 
|---|
| 40 | OS2IDirectSound::OS2IDirectSound(const GUID *lpGUID) : Referenced(0), | 
|---|
| 41 | lastError(DS_OK), hwndClient(0) | 
|---|
| 42 | { | 
|---|
| 43 | lpVtbl = &Vtbl; | 
|---|
| 44 | Vtbl.fnAddRef               = SoundAddRef; | 
|---|
| 45 | Vtbl.fnRelease              = SoundRelease; | 
|---|
| 46 | Vtbl.fnQueryInterface       = SoundQueryInterface; | 
|---|
| 47 | Vtbl.fnCompact              = SoundCompact; | 
|---|
| 48 | Vtbl.fnCreateSoundBuffer    = SoundCreateSoundBuffer; | 
|---|
| 49 | Vtbl.fnGetCaps              = SoundGetCaps; | 
|---|
| 50 | Vtbl.fnDuplicateSoundBuffer = SoundDuplicateSoundBuffer; | 
|---|
| 51 | Vtbl.fnSetCooperativeLevel  = SoundSetCooperativeLevel; | 
|---|
| 52 | Vtbl.fnCompact              = SoundCompact; | 
|---|
| 53 | Vtbl.fnGetSpeakerConfig     = SoundGetSpeakerConfig; | 
|---|
| 54 | Vtbl.fnSetSpeakerConfig     = SoundSetSpeakerConfig; | 
|---|
| 55 | Vtbl.fnInitialize           = SoundInitialize; | 
|---|
| 56 |  | 
|---|
| 57 | dprintf(("DSOUND-OS2IDirectSound::OS2IDirectSound")); | 
|---|
| 58 |  | 
|---|
| 59 | speakerConfig = DSSPEAKER_STEREO; | 
|---|
| 60 | CoopLevel     = DSSCL_EXCLUSIVE;  //default | 
|---|
| 61 |  | 
|---|
| 62 | OS2IDirectSound::fDSExists = TRUE; | 
|---|
| 63 |  | 
|---|
| 64 | // Create the primary buffer | 
|---|
| 65 | primary = new OS2PrimBuff(this, /*lpDSBufferDesc*/NULL); | 
|---|
| 66 | if (primary == NULL){ | 
|---|
| 67 | lastError = DSERR_OUTOFMEMORY; | 
|---|
| 68 | return ; | 
|---|
| 69 | } | 
|---|
| 70 | if ( primary->GetLastError() != DS_OK ){ | 
|---|
| 71 | ULONG lastErr = primary->GetLastError(); | 
|---|
| 72 | dprintf(("LastErr = %d", lastErr)); | 
|---|
| 73 | delete primary; | 
|---|
| 74 | lastError = lastErr; | 
|---|
| 75 | return; | 
|---|
| 76 | } | 
|---|
| 77 | primary->Vtbl.fnAddRef(primary); | 
|---|
| 78 |  | 
|---|
| 79 | dprintf(("Sound init OK")); | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | //****************************************************************************** | 
|---|
| 83 | //****************************************************************************** | 
|---|
| 84 | OS2IDirectSound::~OS2IDirectSound() | 
|---|
| 85 | { | 
|---|
| 86 | // TODO: Close all SoundBuffers here | 
|---|
| 87 | OS2IDirectSoundBuffer::DestroyAllBuffers(); | 
|---|
| 88 | OS2IDirectSound::fDSExists = FALSE; | 
|---|
| 89 | } | 
|---|
| 90 |  | 
|---|
| 91 | //****************************************************************************** | 
|---|
| 92 | //****************************************************************************** | 
|---|
| 93 | HRESULT WIN32API SoundQueryInterface(THIS, REFIID riid, LPVOID * ppvObj) | 
|---|
| 94 | { | 
|---|
| 95 | dprintf(("DSOUND-OS2IDirectSound::QueryInterface")); | 
|---|
| 96 | if (This == NULL) { | 
|---|
| 97 | return DSERR_INVALIDPARAM; | 
|---|
| 98 | } | 
|---|
| 99 | *ppvObj = NULL; | 
|---|
| 100 |  | 
|---|
| 101 | if (!IsEqualGUID(riid, &CLSID_DirectSound) && | 
|---|
| 102 | !IsEqualGUID(riid, &IID_IDirectSound)) | 
|---|
| 103 | return E_NOINTERFACE; | 
|---|
| 104 |  | 
|---|
| 105 | *ppvObj = This; | 
|---|
| 106 |  | 
|---|
| 107 | SoundAddRef(This); | 
|---|
| 108 | return DS_OK; | 
|---|
| 109 | } | 
|---|
| 110 |  | 
|---|
| 111 | //****************************************************************************** | 
|---|
| 112 | //****************************************************************************** | 
|---|
| 113 | ULONG WIN32API SoundAddRef(THIS) | 
|---|
| 114 | { | 
|---|
| 115 | OS2IDirectSound *me = (OS2IDirectSound *)This; | 
|---|
| 116 |  | 
|---|
| 117 | dprintf(("DSOUND-OS2IDirectSound::AddRef %d", me->Referenced+1)); | 
|---|
| 118 | return ++me->Referenced; | 
|---|
| 119 | } | 
|---|
| 120 |  | 
|---|
| 121 | //****************************************************************************** | 
|---|
| 122 | //****************************************************************************** | 
|---|
| 123 | ULONG WIN32API SoundRelease(THIS) | 
|---|
| 124 | { | 
|---|
| 125 | OS2IDirectSound *me = (OS2IDirectSound *)This; | 
|---|
| 126 |  | 
|---|
| 127 | dprintf(("DSOUND-OS2IDirectSound::Release %d", me->Referenced-1)); | 
|---|
| 128 | if (me->Referenced) { | 
|---|
| 129 | me->Referenced--; | 
|---|
| 130 | if (me->Referenced == 0) { | 
|---|
| 131 | delete me; | 
|---|
| 132 | return 0; | 
|---|
| 133 | } | 
|---|
| 134 | else | 
|---|
| 135 | return me->Referenced; | 
|---|
| 136 | } | 
|---|
| 137 | else | 
|---|
| 138 | return 0; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | //****************************************************************************** | 
|---|
| 142 | //****************************************************************************** | 
|---|
| 143 | HRESULT WIN32API SoundCompact(THIS) | 
|---|
| 144 | { | 
|---|
| 145 | dprintf(("DSOUND-OS2IDirectSound::Compact - NOP")); | 
|---|
| 146 | return DS_OK; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | //****************************************************************************** | 
|---|
| 150 | //****************************************************************************** | 
|---|
| 151 | HRESULT WIN32API SoundCreateSoundBuffer(THIS_ | 
|---|
| 152 | LPDSBUFFERDESC lpDSBufferDesc, //new, const | 
|---|
| 153 | LPDIRECTSOUNDBUFFER *lplpDirectSoundBuffer, | 
|---|
| 154 | LPUNKNOWN pUnkOuter) | 
|---|
| 155 | { | 
|---|
| 156 | OS2IDirectSound *me = (OS2IDirectSound *)This; | 
|---|
| 157 | OS2IDirectSoundBuffer *sndbuf; | 
|---|
| 158 |  | 
|---|
| 159 | dprintf(("DSOUND-OS2IDirectSound::CreateSoundBuffer")); | 
|---|
| 160 | if (me == NULL || lpDSBufferDesc == NULL || lplpDirectSoundBuffer == NULL) { | 
|---|
| 161 | return DSERR_INVALIDPARAM; | 
|---|
| 162 | } | 
|---|
| 163 |  | 
|---|
| 164 | if ((lpDSBufferDesc->dwFlags & DSBCAPS_PRIMARYBUFFER) && (lpDSBufferDesc->lpwfxFormat != NULL)) { | 
|---|
| 165 | // Primary buffers must be created without format info! | 
|---|
| 166 | return DSERR_INVALIDPARAM; | 
|---|
| 167 | } | 
|---|
| 168 |  | 
|---|
| 169 | if (lpDSBufferDesc->dwFlags & DSBCAPS_PRIMARYBUFFER) { | 
|---|
| 170 | *lplpDirectSoundBuffer = (LPDIRECTSOUNDBUFFER)me->primary; | 
|---|
| 171 | return DS_OK; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | sndbuf = new OS2IDirectSoundBuffer(me, lpDSBufferDesc); | 
|---|
| 175 | if (sndbuf == NULL) { | 
|---|
| 176 | return DSERR_OUTOFMEMORY; | 
|---|
| 177 | } | 
|---|
| 178 | if (sndbuf->GetLastError() != DS_OK) { | 
|---|
| 179 | ULONG lastErr = sndbuf->GetLastError(); | 
|---|
| 180 | delete sndbuf; | 
|---|
| 181 | return lastErr; | 
|---|
| 182 | } | 
|---|
| 183 | sndbuf->Vtbl.fnAddRef(sndbuf); | 
|---|
| 184 |  | 
|---|
| 185 | *lplpDirectSoundBuffer = (LPDIRECTSOUNDBUFFER)sndbuf; | 
|---|
| 186 |  | 
|---|
| 187 | return DS_OK; | 
|---|
| 188 | } | 
|---|
| 189 |  | 
|---|
| 190 | //****************************************************************************** | 
|---|
| 191 | //****************************************************************************** | 
|---|
| 192 | HRESULT WIN32API SoundGetCaps(THIS_ LPDSCAPS lpCaps) | 
|---|
| 193 | { | 
|---|
| 194 | dprintf(("DSOUND-OS2IDirectSound::GetCaps")); | 
|---|
| 195 |  | 
|---|
| 196 | if (lpCaps == NULL) | 
|---|
| 197 | return DSERR_INVALIDPARAM; | 
|---|
| 198 |  | 
|---|
| 199 | lpCaps->dwSize                           = sizeof(DSCAPS); | 
|---|
| 200 | lpCaps->dwFlags                          = DSCAPS_SECONDARY8BIT | DSCAPS_SECONDARY16BIT  | | 
|---|
| 201 | DSCAPS_SECONDARYMONO | DSCAPS_SECONDARYSTEREO | | 
|---|
| 202 | DSCAPS_PRIMARY8BIT   | DSCAPS_PRIMARY16BIT    | | 
|---|
| 203 | /* DSCAPS_PRIMARYMONO   |*/ DSCAPS_PRIMARYSTEREO; | 
|---|
| 204 | lpCaps->dwMinSecondarySampleRate         = 100; | 
|---|
| 205 | lpCaps->dwMaxSecondarySampleRate         = 100000; | 
|---|
| 206 | lpCaps->dwPrimaryBuffers                 = 1; | 
|---|
| 207 | lpCaps->dwMaxHwMixingAllBuffers          = 0;  // no HW support, no HW buffers... | 
|---|
| 208 | lpCaps->dwMaxHwMixingStaticBuffers       = 0; | 
|---|
| 209 | lpCaps->dwMaxHwMixingStreamingBuffers    = 0; | 
|---|
| 210 | lpCaps->dwFreeHwMixingAllBuffers         = 0; | 
|---|
| 211 | lpCaps->dwFreeHwMixingStaticBuffers      = 0; | 
|---|
| 212 | lpCaps->dwFreeHwMixingStreamingBuffers   = 0; | 
|---|
| 213 | lpCaps->dwMaxHw3DAllBuffers              = 0; | 
|---|
| 214 | lpCaps->dwMaxHw3DStaticBuffers           = 0; | 
|---|
| 215 | lpCaps->dwMaxHw3DStreamingBuffers        = 0; | 
|---|
| 216 | lpCaps->dwFreeHw3DAllBuffers             = 0; | 
|---|
| 217 | lpCaps->dwFreeHw3DStaticBuffers          = 0; | 
|---|
| 218 | lpCaps->dwFreeHw3DStreamingBuffers       = 0; | 
|---|
| 219 | lpCaps->dwTotalHwMemBytes                = 0; | 
|---|
| 220 | lpCaps->dwFreeHwMemBytes                 = 0; | 
|---|
| 221 | lpCaps->dwMaxContigFreeHwMemBytes        = 0; | 
|---|
| 222 | lpCaps->dwUnlockTransferRateHwBuffers    = 0;  // no transfer needed | 
|---|
| 223 | lpCaps->dwPlayCpuOverheadSwBuffers       = 0;  // we lie here... | 
|---|
| 224 | lpCaps->dwReserved1                      = 0; | 
|---|
| 225 | lpCaps->dwReserved2                      = 0; | 
|---|
| 226 |  | 
|---|
| 227 | return DS_OK; | 
|---|
| 228 | } | 
|---|
| 229 |  | 
|---|
| 230 | //****************************************************************************** | 
|---|
| 231 | //****************************************************************************** | 
|---|
| 232 | HRESULT WIN32API SoundDuplicateSoundBuffer(THIS_ LPDIRECTSOUNDBUFFER lpBuf, LPLPDIRECTSOUNDBUFFER lplpDirectSoundBuffer) | 
|---|
| 233 | { | 
|---|
| 234 | OS2IDirectSound *me = (OS2IDirectSound *)This; | 
|---|
| 235 | OS2IDirectSoundBuffer *srcBuf = (OS2IDirectSoundBuffer*)lpBuf; | 
|---|
| 236 | OS2IDirectSoundBuffer *tgtBuf; | 
|---|
| 237 |  | 
|---|
| 238 | dprintf(("DSOUND-OS2IDirectSound::DuplicateSoundBuffer (%X to %X)", lpBuf, lplpDirectSoundBuffer)); | 
|---|
| 239 | if (me == NULL || lpBuf == NULL || lplpDirectSoundBuffer == NULL) { | 
|---|
| 240 | return DSERR_INVALIDPARAM; | 
|---|
| 241 | } | 
|---|
| 242 |  | 
|---|
| 243 | tgtBuf = new OS2IDirectSoundBuffer(me, srcBuf); | 
|---|
| 244 | if (tgtBuf == NULL) { | 
|---|
| 245 | return DSERR_OUTOFMEMORY; | 
|---|
| 246 | } | 
|---|
| 247 |  | 
|---|
| 248 | if (tgtBuf->GetLastError() != DS_OK) { | 
|---|
| 249 | ULONG lastErr = tgtBuf->GetLastError(); | 
|---|
| 250 | delete tgtBuf; | 
|---|
| 251 | return lastErr; | 
|---|
| 252 | } | 
|---|
| 253 | tgtBuf->Vtbl.fnAddRef(tgtBuf); | 
|---|
| 254 |  | 
|---|
| 255 | *lplpDirectSoundBuffer = (LPDIRECTSOUNDBUFFER)tgtBuf; | 
|---|
| 256 |  | 
|---|
| 257 | return DS_OK; | 
|---|
| 258 | } | 
|---|
| 259 |  | 
|---|
| 260 | //****************************************************************************** | 
|---|
| 261 | //****************************************************************************** | 
|---|
| 262 | HRESULT WIN32API SoundSetCooperativeLevel(THIS_ HWND hwndClient, DWORD level) | 
|---|
| 263 | { | 
|---|
| 264 | OS2IDirectSound *me = (OS2IDirectSound *)This; | 
|---|
| 265 |  | 
|---|
| 266 | dprintf(("DSOUND-OS2IDirectSound::SetCooperativeLevel (to %d)", level)); | 
|---|
| 267 | if (me == NULL) { | 
|---|
| 268 | return DSERR_INVALIDPARAM; | 
|---|
| 269 | } | 
|---|
| 270 | me->hwndClient = (HWND)hwndClient; | 
|---|
| 271 | me->CoopLevel  = level; | 
|---|
| 272 | return DS_OK; | 
|---|
| 273 | } | 
|---|
| 274 |  | 
|---|
| 275 | //****************************************************************************** | 
|---|
| 276 | //****************************************************************************** | 
|---|
| 277 | HRESULT WIN32API SoundGetSpeakerConfig(THIS_ LPDWORD lpSpeakerCfg) | 
|---|
| 278 | { | 
|---|
| 279 | OS2IDirectSound *me = (OS2IDirectSound *)This; | 
|---|
| 280 |  | 
|---|
| 281 | dprintf(("DSOUND-OS2IDirectSound::GetSpeakerConfig")); | 
|---|
| 282 | if (me == NULL) { | 
|---|
| 283 | return(DSERR_INVALIDPARAM); | 
|---|
| 284 | } | 
|---|
| 285 | *lpSpeakerCfg = me->speakerConfig; | 
|---|
| 286 |  | 
|---|
| 287 | return DS_OK; | 
|---|
| 288 | } | 
|---|
| 289 |  | 
|---|
| 290 | //****************************************************************************** | 
|---|
| 291 | //****************************************************************************** | 
|---|
| 292 | HRESULT WIN32API SoundSetSpeakerConfig(THIS_ DWORD speakerCfg) | 
|---|
| 293 | { | 
|---|
| 294 | OS2IDirectSound *me = (OS2IDirectSound *)This; | 
|---|
| 295 |  | 
|---|
| 296 | dprintf(("DSOUND-OS2IDirectSound::SetSpeakerConfig %X", speakerCfg)); | 
|---|
| 297 | if (me == NULL) { | 
|---|
| 298 | return DSERR_INVALIDPARAM; | 
|---|
| 299 | } | 
|---|
| 300 | me->speakerConfig = speakerCfg; | 
|---|
| 301 | return DS_OK; | 
|---|
| 302 | } | 
|---|
| 303 |  | 
|---|
| 304 | //****************************************************************************** | 
|---|
| 305 | //****************************************************************************** | 
|---|
| 306 | HRESULT WIN32API SoundInitialize(THIS_ LPGUID) | 
|---|
| 307 | { | 
|---|
| 308 | dprintf(("DSOUND-OS2IDirectSound::Initialize NOP")); | 
|---|
| 309 | return DS_OK; | 
|---|
| 310 | } | 
|---|
| 311 | //****************************************************************************** | 
|---|
| 312 | //****************************************************************************** | 
|---|