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