| 1 | /* $Id: OS2SNDBUFFER.CPP,v 1.8 2000-05-18 20:37:09 mike Exp $ */ | 
|---|
| 2 |  | 
|---|
| 3 | /* | 
|---|
| 4 | * DirectSound SoundBuffer class | 
|---|
| 5 | * | 
|---|
| 6 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 7 | * Copyright 2000 Michal Necasek | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 |  | 
|---|
| 13 |  | 
|---|
| 14 | /*@Header*********************************************************************** | 
|---|
| 15 | *   Header Files                                                               * | 
|---|
| 16 | *******************************************************************************/ | 
|---|
| 17 | #define INCL_DOSMISC | 
|---|
| 18 | #include <os2win.h> | 
|---|
| 19 |  | 
|---|
| 20 | #include <stdlib.h> | 
|---|
| 21 | #include <string.h> | 
|---|
| 22 |  | 
|---|
| 23 | #define INITGUID | 
|---|
| 24 | #include <dsound.h> | 
|---|
| 25 |  | 
|---|
| 26 | #include "OS2DSound.h" | 
|---|
| 27 | #include "OS2SndBuffer.h" | 
|---|
| 28 | #include "OS2PrimBuff.h" | 
|---|
| 29 | #include "OS23DListener.h" | 
|---|
| 30 | #include "OS23DBuffer.h" | 
|---|
| 31 | #include "OS2Notify.h" | 
|---|
| 32 | #include <misc.h> | 
|---|
| 33 |  | 
|---|
| 34 | #include "DSVolume.h" | 
|---|
| 35 |  | 
|---|
| 36 | WAVEFORMATEX wfxDefaultPrimaryBuf = { WAVE_FORMAT_PCM, | 
|---|
| 37 | 2, | 
|---|
| 38 | 22050, | 
|---|
| 39 | 44100, | 
|---|
| 40 | 2, | 
|---|
| 41 | 8, | 
|---|
| 42 | 0}; | 
|---|
| 43 |  | 
|---|
| 44 | OS2IDirectSoundBuffer *OS2IDirectSoundBuffer::dsbroot = NULL; | 
|---|
| 45 | OS2IDirectSoundBuffer *OS2IDirectSoundBuffer::primary = NULL; | 
|---|
| 46 |  | 
|---|
| 47 | //****************************************************************************** | 
|---|
| 48 | //****************************************************************************** | 
|---|
| 49 | OS2IDirectSoundBuffer::OS2IDirectSoundBuffer(OS2IDirectSound *DSound, const DSBUFFERDESC *lpDSBufferDesc) | 
|---|
| 50 | : Referenced(0), lastError(DS_OK) | 
|---|
| 51 | { | 
|---|
| 52 | lpVtbl = &Vtbl; | 
|---|
| 53 | Vtbl.AddRef               = SoundBufAddRef; | 
|---|
| 54 | Vtbl.Release              = SoundBufRelease; | 
|---|
| 55 | Vtbl.QueryInterface       = SoundBufQueryInterface; | 
|---|
| 56 | Vtbl.GetCaps              = SoundBufGetCaps; | 
|---|
| 57 | Vtbl.GetFormat            = SoundBufGetFormat; | 
|---|
| 58 | Vtbl.GetVolume            = SoundBufGetVolume; | 
|---|
| 59 | Vtbl.GetStatus            = SoundBufGetStatus; | 
|---|
| 60 | Vtbl.GetCurrentPosition   = SoundBufGetCurrentPosition; | 
|---|
| 61 | Vtbl.GetPan               = SoundBufGetPan; | 
|---|
| 62 | Vtbl.GetFrequency         = SoundBufGetFrequency; | 
|---|
| 63 | Vtbl.Initialize           = SoundBufInitialize; | 
|---|
| 64 | Vtbl.Restore              = SoundBufRestore; | 
|---|
| 65 | Vtbl.SetFormat            = SoundBufSetFormat; | 
|---|
| 66 | Vtbl.SetVolume            = SoundBufSetVolume; | 
|---|
| 67 | Vtbl.SetCurrentPosition   = SoundBufSetCurrentPosition; | 
|---|
| 68 | Vtbl.SetPan               = SoundBufSetPan; | 
|---|
| 69 | Vtbl.SetFrequency         = SoundBufSetFrequency; | 
|---|
| 70 | Vtbl.Lock                 = SoundBufLock; | 
|---|
| 71 | Vtbl.Unlock               = SoundBufUnlock; | 
|---|
| 72 | Vtbl.Stop                 = SoundBufStop; | 
|---|
| 73 | Vtbl.Play                 = SoundBufPlay; | 
|---|
| 74 |  | 
|---|
| 75 | dprintf(("DSOUND-OS2IDirectSoundBuffer::OS2IDirectSoundBuffer (buf=%X)", this)); | 
|---|
| 76 |  | 
|---|
| 77 | // get the primary buffer from the DSound instance. | 
|---|
| 78 | primary = DSound->primary; | 
|---|
| 79 |  | 
|---|
| 80 | // We can still to the below logic just in case!!  Should not be required thow! | 
|---|
| 81 | // If the primary SoundBuffer doesn't exist by now, we have to create it! | 
|---|
| 82 | if (primary == NULL) { | 
|---|
| 83 | OS2PrimBuff *primbuff; | 
|---|
| 84 | primbuff = new OS2PrimBuff(DSound, lpDSBufferDesc); | 
|---|
| 85 |  | 
|---|
| 86 | if (primary == NULL) { | 
|---|
| 87 | lastError = DSERR_OUTOFMEMORY; | 
|---|
| 88 | return; | 
|---|
| 89 | } | 
|---|
| 90 | } | 
|---|
| 91 | // Add a reference to the primary buffer for _every_ secondary buffer; this makes | 
|---|
| 92 | // sure that as long as a secondary buffer exists the primary buffer won't get | 
|---|
| 93 | // destroyed; moreover if the user didn't create  a primary buffer himself/herself, | 
|---|
| 94 | // it will automatically get created and destroyed with the last secondary buffer | 
|---|
| 95 | primary->Vtbl.AddRef(primary); | 
|---|
| 96 |  | 
|---|
| 97 | // Start playing the primary buffer | 
|---|
| 98 | // TODO: only start playing the primary buffer when necessary! | 
|---|
| 99 | OS2IDirectSoundBuffer::primary->Vtbl.Play(OS2IDirectSoundBuffer::primary, 0, 0, DSBPLAY_LOOPING); | 
|---|
| 100 |  | 
|---|
| 101 | parentDS  = DSound; | 
|---|
| 102 | writepos  = 0; | 
|---|
| 103 | playpos   = 0; | 
|---|
| 104 | status    = 0; | 
|---|
| 105 | fLocked   = FALSE; | 
|---|
| 106 | fPrimary  = FALSE; | 
|---|
| 107 | volume    = 255; | 
|---|
| 108 | DSvolume  = 0; | 
|---|
| 109 | pan       = 0; | 
|---|
| 110 | DSpan     = 0; | 
|---|
| 111 | lpfxFormat= NULL; | 
|---|
| 112 | lpBuffer  = NULL; | 
|---|
| 113 | fPlaying  = FALSE; | 
|---|
| 114 | fLoop     = FALSE; | 
|---|
| 115 | notify    = NULL; | 
|---|
| 116 | memcpy(&bufferdesc, lpDSBufferDesc, sizeof(DSBUFFERDESC)); | 
|---|
| 117 |  | 
|---|
| 118 | // Note: this cannot be a primary buffer - those take a different route | 
|---|
| 119 |  | 
|---|
| 120 | // frequency has to be set according to the bufer format | 
|---|
| 121 | frequency = bufferdesc.lpwfxFormat->nSamplesPerSec; | 
|---|
| 122 |  | 
|---|
| 123 | if (bufferdesc.lpwfxFormat == NULL || bufferdesc.dwBufferBytes == 0) { | 
|---|
| 124 | dprintf(("bufferdesc not valid!")); | 
|---|
| 125 | lastError = DSERR_INVALIDPARAM; | 
|---|
| 126 | return; | 
|---|
| 127 | } | 
|---|
| 128 |  | 
|---|
| 129 | // Some apps pass trash in cbSize, can't rely on that! | 
|---|
| 130 | lpfxFormat = (WAVEFORMATEX *)malloc(/*bufferdesc.lpwfxFormat->cbSize +*/ sizeof(WAVEFORMATEX)); | 
|---|
| 131 | memcpy(lpfxFormat, | 
|---|
| 132 | bufferdesc.lpwfxFormat, | 
|---|
| 133 | /*bufferdesc.lpwfxFormat->cbSize + */sizeof(WAVEFORMATEX)); | 
|---|
| 134 |  | 
|---|
| 135 | dprintf((" Buffer format: %dHz, %dbit, %d channels", lpfxFormat->nSamplesPerSec, lpfxFormat->wBitsPerSample, lpfxFormat->nChannels)); | 
|---|
| 136 |  | 
|---|
| 137 | lpBuffer = (LPSTR)VirtualAlloc(0, bufferdesc.dwBufferBytes, MEM_COMMIT, PAGE_READWRITE); | 
|---|
| 138 | if (lpBuffer == NULL) { | 
|---|
| 139 | dprintf(("VirtualAlloc failed")); | 
|---|
| 140 | lpBuffer  = NULL; | 
|---|
| 141 | lastError = DSERR_OUTOFMEMORY; | 
|---|
| 142 | return; | 
|---|
| 143 | } | 
|---|
| 144 |  | 
|---|
| 145 | next = dsbroot; | 
|---|
| 146 | dsbroot = this; | 
|---|
| 147 | } | 
|---|
| 148 |  | 
|---|
| 149 | //****************************************************************************** | 
|---|
| 150 | //****************************************************************************** | 
|---|
| 151 | OS2IDirectSoundBuffer::~OS2IDirectSoundBuffer() | 
|---|
| 152 | { | 
|---|
| 153 | /* We must not run this destructor on the primary buffer! */ | 
|---|
| 154 | if (fPrimary) | 
|---|
| 155 | return; | 
|---|
| 156 |  | 
|---|
| 157 | dprintf(("DSOUND-OS2IDirectSoundBuffer::~OS2IDirectSoundBuffer (buf=%X)", this)); | 
|---|
| 158 |  | 
|---|
| 159 | // free allocted memory | 
|---|
| 160 | if (lpBuffer) | 
|---|
| 161 | VirtualFree(lpBuffer, 0, MEM_RELEASE); | 
|---|
| 162 |  | 
|---|
| 163 | if (lpfxFormat) | 
|---|
| 164 | free(lpfxFormat); | 
|---|
| 165 |  | 
|---|
| 166 | // remove ourselves from the linked list | 
|---|
| 167 | OS2IDirectSoundBuffer *cur  = dsbroot; | 
|---|
| 168 | OS2IDirectSoundBuffer *prev = NULL; | 
|---|
| 169 |  | 
|---|
| 170 | // release the primary buffer too | 
|---|
| 171 | primary->Vtbl.Release(primary); | 
|---|
| 172 |  | 
|---|
| 173 | if (this == dsbroot)  // is this the first SoundBuffer? | 
|---|
| 174 | dsbroot = next; | 
|---|
| 175 | else {                // walk the chain | 
|---|
| 176 | while (cur->next != this) | 
|---|
| 177 | cur  = cur->next; | 
|---|
| 178 |  | 
|---|
| 179 | cur->next = next; | 
|---|
| 180 | } | 
|---|
| 181 |  | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | //****************************************************************************** | 
|---|
| 185 | //****************************************************************************** | 
|---|
| 186 | void OS2IDirectSoundBuffer::DestroyAllBuffers() | 
|---|
| 187 | { | 
|---|
| 188 | dprintf(("DSOUND-OS2IDirectSoundBuffer::DestroyAllBuffers")); | 
|---|
| 189 |  | 
|---|
| 190 | // if any SoundBuffers still exist when the OS2IDirectSound object is | 
|---|
| 191 | // being closed, just kill them all | 
|---|
| 192 | OS2IDirectSoundBuffer *cur  = dsbroot; | 
|---|
| 193 | OS2IDirectSoundBuffer *tmp; | 
|---|
| 194 |  | 
|---|
| 195 | if (primary != NULL) | 
|---|
| 196 | primary->Vtbl.Stop(primary); | 
|---|
| 197 |  | 
|---|
| 198 | while (cur != NULL) { | 
|---|
| 199 | tmp = cur->next; | 
|---|
| 200 | delete cur; | 
|---|
| 201 | cur = tmp; | 
|---|
| 202 | } | 
|---|
| 203 | dprintf(("DSOUND-OS2IDirectSoundBuffer::DestroyAllBuffers - %X", primary)); | 
|---|
| 204 | if (primary != NULL) | 
|---|
| 205 | primary->Vtbl.Release(primary); | 
|---|
| 206 | } | 
|---|
| 207 |  | 
|---|
| 208 | //****************************************************************************** | 
|---|
| 209 | //****************************************************************************** | 
|---|
| 210 | HRESULT WIN32API SoundBufQueryInterface(THIS, REFIID riid, LPVOID * ppvObj) | 
|---|
| 211 | { | 
|---|
| 212 | dprintf(("DSOUND-OS2IDirectSoundBuffer::QueryInterface")); | 
|---|
| 213 | if (This == NULL) { | 
|---|
| 214 | return DSERR_INVALIDPARAM; | 
|---|
| 215 | } | 
|---|
| 216 | *ppvObj = NULL; | 
|---|
| 217 |  | 
|---|
| 218 | if (IsEqualGUID(riid, IID_IDirectSoundBuffer)) { | 
|---|
| 219 | *ppvObj = This; | 
|---|
| 220 |  | 
|---|
| 221 | SoundBufAddRef(This); | 
|---|
| 222 | return DS_OK; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | if (IsEqualGUID(riid, IID_IDirectSoundNotify)) { | 
|---|
| 226 | OS2IDirectSoundBuffer  *me     = (OS2IDirectSoundBuffer *)This; | 
|---|
| 227 | OS2IDirectSoundNotify  *notify; | 
|---|
| 228 |  | 
|---|
| 229 | notify     = new OS2IDirectSoundNotify(me); | 
|---|
| 230 | *ppvObj    = notify; | 
|---|
| 231 | notify->Vtbl.AddRef(notify); | 
|---|
| 232 | return DS_OK; | 
|---|
| 233 | } | 
|---|
| 234 |  | 
|---|
| 235 | if (IsEqualGUID(riid, IID_IDirectSound3DBuffer)) { | 
|---|
| 236 | OS2IDirectSoundBuffer   *me     = (OS2IDirectSoundBuffer *)This; | 
|---|
| 237 | OS2IDirectSound3DBuffer *buffer3D; | 
|---|
| 238 |  | 
|---|
| 239 | buffer3D   = new OS2IDirectSound3DBuffer(me); | 
|---|
| 240 | *ppvObj    = buffer3D; | 
|---|
| 241 | buffer3D->Vtbl.AddRef((IDirectSound3DBuffer *)buffer3D); | 
|---|
| 242 | return DS_OK; | 
|---|
| 243 | } | 
|---|
| 244 |  | 
|---|
| 245 | return E_NOINTERFACE; | 
|---|
| 246 | } | 
|---|
| 247 | //****************************************************************************** | 
|---|
| 248 | //****************************************************************************** | 
|---|
| 249 | ULONG WIN32API SoundBufAddRef(THIS) | 
|---|
| 250 | { | 
|---|
| 251 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 252 |  | 
|---|
| 253 | dprintf(("DSOUND-OS2IDirectSoundBuffer::AddRef (buf=%X) %d", me, me->Referenced+1)); | 
|---|
| 254 | if (me == NULL) { | 
|---|
| 255 | return 0; | 
|---|
| 256 | } | 
|---|
| 257 | return ++me->Referenced; | 
|---|
| 258 | } | 
|---|
| 259 | //****************************************************************************** | 
|---|
| 260 | //****************************************************************************** | 
|---|
| 261 | ULONG WIN32API SoundBufRelease(THIS) | 
|---|
| 262 | { | 
|---|
| 263 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 264 |  | 
|---|
| 265 | dprintf(("DSOUND-OS2IDirectSoundBuffer::Release (buf=%X) %d", me, me->Referenced-1)); | 
|---|
| 266 | if (me == NULL) { | 
|---|
| 267 | return 0; | 
|---|
| 268 | } | 
|---|
| 269 | if (me->Referenced) { | 
|---|
| 270 | me->Referenced--; | 
|---|
| 271 | if (me->Referenced == 0) { | 
|---|
| 272 | delete me; | 
|---|
| 273 | return 0; | 
|---|
| 274 | } | 
|---|
| 275 | else | 
|---|
| 276 | return me->Referenced; | 
|---|
| 277 | } | 
|---|
| 278 | else | 
|---|
| 279 | return 0; | 
|---|
| 280 | } | 
|---|
| 281 | //****************************************************************************** | 
|---|
| 282 | //****************************************************************************** | 
|---|
| 283 | HRESULT __stdcall SoundBufGetCaps(THIS_ LPDSBCAPS lpDSCaps) | 
|---|
| 284 | { | 
|---|
| 285 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 286 |  | 
|---|
| 287 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufGetCaps (buf=%X)", me)); | 
|---|
| 288 | if (me == NULL || lpDSCaps == NULL) { | 
|---|
| 289 | return DSERR_INVALIDPARAM; | 
|---|
| 290 | } | 
|---|
| 291 | /* FIXME: fill with more realistic values */ | 
|---|
| 292 | lpDSCaps->dwSize               = sizeof(DSBCAPS); | 
|---|
| 293 | lpDSCaps->dwFlags              = me->bufferdesc.dwFlags | DSBCAPS_LOCSOFTWARE; | 
|---|
| 294 | lpDSCaps->dwBufferBytes        = me->bufferdesc.dwBufferBytes; | 
|---|
| 295 | lpDSCaps->dwUnlockTransferRate = 0;     /* in KB/sec - 0 == no transfer needed */ | 
|---|
| 296 | lpDSCaps->dwPlayCpuOverhead    = 0;     /* % CPU time - let's lie */ | 
|---|
| 297 |  | 
|---|
| 298 | return DS_OK; | 
|---|
| 299 | } | 
|---|
| 300 | //****************************************************************************** | 
|---|
| 301 | //****************************************************************************** | 
|---|
| 302 | HRESULT __stdcall SoundBufGetCurrentPosition(THIS_ LPDWORD lpdwCurrentPlayCursor, | 
|---|
| 303 | LPDWORD lpdwCurrentWriteCursor) | 
|---|
| 304 | { | 
|---|
| 305 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 306 |  | 
|---|
| 307 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufGetCurrentPosition (buf=%X)", me)); | 
|---|
| 308 | if ( me == NULL || lpdwCurrentPlayCursor == NULL || lpdwCurrentWriteCursor == NULL) { | 
|---|
| 309 | dprintf(("  Invalid parameters %d %d %d",me,lpdwCurrentPlayCursor,lpdwCurrentWriteCursor)); | 
|---|
| 310 | return DSERR_INVALIDPARAM; | 
|---|
| 311 | } | 
|---|
| 312 |  | 
|---|
| 313 | dprintf(("  PlayPos %d, WritePos %d", me->playpos, me->writepos)); | 
|---|
| 314 | *lpdwCurrentPlayCursor  = me->playpos; | 
|---|
| 315 | *lpdwCurrentWriteCursor = me->writepos; | 
|---|
| 316 | return DS_OK; | 
|---|
| 317 | } | 
|---|
| 318 | //****************************************************************************** | 
|---|
| 319 | //****************************************************************************** | 
|---|
| 320 | HRESULT __stdcall SoundBufGetFormat(THIS_ LPWAVEFORMATEX lpwfxFormat, | 
|---|
| 321 | DWORD ddwSizeAllocated, LPDWORD lpdwSizeWritten) | 
|---|
| 322 | { | 
|---|
| 323 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 324 | int copysize; | 
|---|
| 325 |  | 
|---|
| 326 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufGetFormat (buf=%X)", me)); | 
|---|
| 327 | if (me == NULL || lpwfxFormat == NULL || ddwSizeAllocated == 0) { | 
|---|
| 328 | return DSERR_INVALIDPARAM; | 
|---|
| 329 | } | 
|---|
| 330 | copysize = min(ddwSizeAllocated, (me->lpfxFormat->cbSize + sizeof(WAVEFORMATEX))); | 
|---|
| 331 | memcpy(lpwfxFormat, me->lpfxFormat, copysize); | 
|---|
| 332 |  | 
|---|
| 333 | if (lpdwSizeWritten) { | 
|---|
| 334 | *lpdwSizeWritten = copysize; | 
|---|
| 335 | } | 
|---|
| 336 | return DS_OK; | 
|---|
| 337 | } | 
|---|
| 338 | //****************************************************************************** | 
|---|
| 339 | //****************************************************************************** | 
|---|
| 340 | HRESULT __stdcall SoundBufGetVolume(THIS_ LPLONG lplVolume) | 
|---|
| 341 | { | 
|---|
| 342 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 343 |  | 
|---|
| 344 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufGetVolume (buf=%X)", me)); | 
|---|
| 345 | if (me == NULL || lplVolume == NULL) { | 
|---|
| 346 | return DSERR_INVALIDPARAM; | 
|---|
| 347 | } | 
|---|
| 348 | *lplVolume = me->DSvolume; | 
|---|
| 349 | return DS_OK; | 
|---|
| 350 | } | 
|---|
| 351 | //****************************************************************************** | 
|---|
| 352 | //****************************************************************************** | 
|---|
| 353 | HRESULT __stdcall SoundBufGetPan(THIS_ LPLONG lplPan) | 
|---|
| 354 | { | 
|---|
| 355 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 356 |  | 
|---|
| 357 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufGetPan (buf=%X)", me)); | 
|---|
| 358 | if (me == NULL || lplPan == NULL) { | 
|---|
| 359 | return DSERR_INVALIDPARAM; | 
|---|
| 360 | } | 
|---|
| 361 | *lplPan = me->DSpan; | 
|---|
| 362 | return DS_OK; | 
|---|
| 363 | } | 
|---|
| 364 | //****************************************************************************** | 
|---|
| 365 | //****************************************************************************** | 
|---|
| 366 | HRESULT __stdcall SoundBufGetFrequency(THIS_ LPDWORD lpdwFrequency) | 
|---|
| 367 | { | 
|---|
| 368 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 369 |  | 
|---|
| 370 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufGetFrequency (buf=%X)", me)); | 
|---|
| 371 | if (me == NULL || lpdwFrequency == NULL) { | 
|---|
| 372 | return DSERR_INVALIDPARAM; | 
|---|
| 373 | } | 
|---|
| 374 | *lpdwFrequency = me->frequency; | 
|---|
| 375 | return DS_OK; | 
|---|
| 376 | } | 
|---|
| 377 | //****************************************************************************** | 
|---|
| 378 | //****************************************************************************** | 
|---|
| 379 | HRESULT __stdcall SoundBufGetStatus(THIS_ LPDWORD lpdwStatus) | 
|---|
| 380 | { | 
|---|
| 381 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 382 |  | 
|---|
| 383 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufGetStatus (buf=%X)", me)); | 
|---|
| 384 | if (me == NULL || lpdwStatus == NULL) { | 
|---|
| 385 | return DSERR_INVALIDPARAM; | 
|---|
| 386 | } | 
|---|
| 387 |  | 
|---|
| 388 | if (me->fPlaying) | 
|---|
| 389 | if (me->fLoop) | 
|---|
| 390 | me->status |= DSBSTATUS_PLAYING | DSBSTATUS_LOOPING; | 
|---|
| 391 | else | 
|---|
| 392 | me->status |= DSBSTATUS_PLAYING; | 
|---|
| 393 | else | 
|---|
| 394 | me->status  &= ~(DSBSTATUS_PLAYING | DSBSTATUS_LOOPING); | 
|---|
| 395 |  | 
|---|
| 396 | *lpdwStatus = me->status; | 
|---|
| 397 | return DS_OK; | 
|---|
| 398 | } | 
|---|
| 399 | //****************************************************************************** | 
|---|
| 400 | //****************************************************************************** | 
|---|
| 401 | HRESULT __stdcall SoundBufInitialize(THIS_ LPDIRECTSOUND, LPDSBUFFERDESC ) | 
|---|
| 402 | { | 
|---|
| 403 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 404 |  | 
|---|
| 405 | dprintf(("DSOUND-SoundBufInitialize (buf=%X)", me)); | 
|---|
| 406 | if (me == NULL) { | 
|---|
| 407 | return DSERR_INVALIDPARAM; | 
|---|
| 408 | } | 
|---|
| 409 | return DSERR_ALREADYINITIALIZED;  //todo: for future extensions (dx5/6 ??) | 
|---|
| 410 | } | 
|---|
| 411 | //****************************************************************************** | 
|---|
| 412 | //****************************************************************************** | 
|---|
| 413 | HRESULT __stdcall SoundBufLock(THIS_ DWORD dwWriteCursor, DWORD dwWriteBytes, | 
|---|
| 414 | LPVOID lplpvAudioPtr1, LPDWORD lpdwAudioBytes1, | 
|---|
| 415 | LPVOID lplpvAudioPtr2, LPDWORD lpdwAudioBytes2, | 
|---|
| 416 | DWORD dwFlags) | 
|---|
| 417 | { | 
|---|
| 418 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 419 |  | 
|---|
| 420 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufLock (buf=%X, %d bytes of %d)", me, dwWriteBytes, me->bufferdesc.dwBufferBytes)); | 
|---|
| 421 | if (me == NULL || !lplpvAudioPtr1 || !lpdwAudioBytes1) | 
|---|
| 422 | return DSERR_INVALIDPARAM; | 
|---|
| 423 |  | 
|---|
| 424 | //not sure if this is an error, but it's certainly a smart thing to do (cond. == true) | 
|---|
| 425 | if(dwWriteBytes > me->bufferdesc.dwBufferBytes) { | 
|---|
| 426 | dprintf(("SoundBufLock: dwWriteBytes > me->bufferdesc.dwBufferBytes")); | 
|---|
| 427 | return DSERR_INVALIDPARAM; | 
|---|
| 428 | } | 
|---|
| 429 | if (dwFlags & DSBLOCK_FROMWRITECURSOR) { | 
|---|
| 430 | dwWriteCursor = me->writepos; | 
|---|
| 431 | } | 
|---|
| 432 | if (dwWriteCursor + dwWriteBytes > me->bufferdesc.dwBufferBytes) { | 
|---|
| 433 | *(DWORD *)lplpvAudioPtr1  = (DWORD)(me->lpBuffer + dwWriteCursor); | 
|---|
| 434 | *lpdwAudioBytes1          = me->bufferdesc.dwBufferBytes - dwWriteCursor; | 
|---|
| 435 | if (lplpvAudioPtr2 && lpdwAudioBytes2) { | 
|---|
| 436 | *(DWORD *)lplpvAudioPtr2   = (DWORD)me->lpBuffer; | 
|---|
| 437 | *lpdwAudioBytes2           = dwWriteBytes - *lpdwAudioBytes1; | 
|---|
| 438 | } | 
|---|
| 439 | } | 
|---|
| 440 | else { | 
|---|
| 441 | *(DWORD *)lplpvAudioPtr1  = (DWORD)(me->lpBuffer + dwWriteCursor); | 
|---|
| 442 | *lpdwAudioBytes1          = dwWriteBytes; | 
|---|
| 443 | if (lplpvAudioPtr2 && lpdwAudioBytes2) { | 
|---|
| 444 | *(DWORD *)lplpvAudioPtr2   = 0; | 
|---|
| 445 | *lpdwAudioBytes2           = 0; | 
|---|
| 446 | } | 
|---|
| 447 | } | 
|---|
| 448 |  | 
|---|
| 449 | me->fLocked = TRUE; | 
|---|
| 450 |  | 
|---|
| 451 | return DS_OK; | 
|---|
| 452 | } | 
|---|
| 453 |  | 
|---|
| 454 | //****************************************************************************** | 
|---|
| 455 | //****************************************************************************** | 
|---|
| 456 | HRESULT __stdcall SoundBufPlay(THIS_ DWORD dwRes1, DWORD dwRes2, DWORD dwFlags) | 
|---|
| 457 | { | 
|---|
| 458 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 459 |  | 
|---|
| 460 | ULONG ulBytesToPlay; | 
|---|
| 461 |  | 
|---|
| 462 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufPlay (buf=%X)", me)); | 
|---|
| 463 | if (me == NULL) { | 
|---|
| 464 | dprintf(("  Invalid parms - me is NULL")); | 
|---|
| 465 | return DSERR_INVALIDPARAM; | 
|---|
| 466 | } | 
|---|
| 467 |  | 
|---|
| 468 | if (!me->fPlaying) { | 
|---|
| 469 | me->frac     = 0; | 
|---|
| 470 | me->fPlaying = TRUE; | 
|---|
| 471 | me->status   = DSBSTATUS_PLAYING; | 
|---|
| 472 | me->fLoop = dwFlags == DSBPLAY_LOOPING; | 
|---|
| 473 | if (me->fLoop) | 
|---|
| 474 | me->status |= DSBSTATUS_LOOPING; | 
|---|
| 475 |  | 
|---|
| 476 | dprintf(("  Buffer %X: start at pos %d, loop %s",me, me->playpos, me->fLoop?"YES":"NO")); | 
|---|
| 477 | } | 
|---|
| 478 | else { | 
|---|
| 479 | me->fLoop = dwFlags == DSBPLAY_LOOPING; | 
|---|
| 480 | if (me->fLoop) | 
|---|
| 481 | me->status |= DSBSTATUS_LOOPING; | 
|---|
| 482 |  | 
|---|
| 483 | dprintf(("  Buffer %X: already playing, loop %s",me, me->fLoop?"YES":"NO")); | 
|---|
| 484 | } | 
|---|
| 485 |  | 
|---|
| 486 | return DS_OK; | 
|---|
| 487 | } | 
|---|
| 488 |  | 
|---|
| 489 | //****************************************************************************** | 
|---|
| 490 | //****************************************************************************** | 
|---|
| 491 | HRESULT __stdcall SoundBufSetCurrentPosition(THIS_ DWORD dwNewPosition) | 
|---|
| 492 | { | 
|---|
| 493 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 494 |  | 
|---|
| 495 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufSetCurrentPosition (buf=%X) to %d", me, dwNewPosition)); | 
|---|
| 496 | if (me == NULL) { | 
|---|
| 497 | return DSERR_INVALIDPARAM; | 
|---|
| 498 | } | 
|---|
| 499 | me->playpos   = dwNewPosition; | 
|---|
| 500 |  | 
|---|
| 501 | return DS_OK; | 
|---|
| 502 | } | 
|---|
| 503 |  | 
|---|
| 504 | //****************************************************************************** | 
|---|
| 505 | //****************************************************************************** | 
|---|
| 506 | HRESULT __stdcall SoundBufSetFormat(THIS_ LPWAVEFORMATEX lpWaveFormatEx) | 
|---|
| 507 | { | 
|---|
| 508 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 509 |  | 
|---|
| 510 | // Note: this sets the format of the _primary_ buffer; | 
|---|
| 511 | // since this class only handles secondary buffers, we just return error | 
|---|
| 512 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufSetFormat (buf=%X)", me)); | 
|---|
| 513 |  | 
|---|
| 514 | return DSERR_UNSUPPORTED; | 
|---|
| 515 | } | 
|---|
| 516 |  | 
|---|
| 517 | //****************************************************************************** | 
|---|
| 518 | //****************************************************************************** | 
|---|
| 519 | HRESULT __stdcall SoundBufSetVolume(THIS_ LONG lVolume) | 
|---|
| 520 | { | 
|---|
| 521 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 522 |  | 
|---|
| 523 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufSetVolume (%d, buf=%X)", lVolume, me)); | 
|---|
| 524 | if (me == NULL || lVolume < -10000) { | 
|---|
| 525 | return DSERR_INVALIDPARAM; | 
|---|
| 526 | } | 
|---|
| 527 | // some apps pass positive values in lVolume, that's wrong right?!? | 
|---|
| 528 | me->DSvolume = (lVolume > 0) ? 0 : lVolume; | 
|---|
| 529 |  | 
|---|
| 530 | /* = (10 ^ (1/10)) = 1dB - but the formula below gives results _very_ similar */ | 
|---|
| 531 | /* to 'real' DirectSound, indistinguishable for all practical purposes        */ | 
|---|
| 532 | //me->volume = 255.0 * pow(4, me->DSvolume / 1000.0); | 
|---|
| 533 |  | 
|---|
| 534 | /* Note: for some strange reason the above code sometimes gives erroneous     */ | 
|---|
| 535 | /* results, hence we now use a simple conversion table (in steps of 4/100 dB) */ | 
|---|
| 536 |  | 
|---|
| 537 | if (me->DSvolume < -4000) | 
|---|
| 538 | me->volume = 0; | 
|---|
| 539 | else | 
|---|
| 540 | me->volume = VolTable[-me->DSvolume / 4]; | 
|---|
| 541 |  | 
|---|
| 542 | dprintf(("  New volume: %d", me->volume)); | 
|---|
| 543 |  | 
|---|
| 544 | return DS_OK; | 
|---|
| 545 | } | 
|---|
| 546 | //****************************************************************************** | 
|---|
| 547 | //****************************************************************************** | 
|---|
| 548 | HRESULT __stdcall SoundBufSetPan(THIS_ LONG lPan) | 
|---|
| 549 | { | 
|---|
| 550 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 551 |  | 
|---|
| 552 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufSetPan (%d, buf=%X)", lPan, me)); | 
|---|
| 553 | if (me == NULL || lPan < -10000 || lPan > 10000) { | 
|---|
| 554 | return DSERR_INVALIDPARAM; | 
|---|
| 555 | } | 
|---|
| 556 | me->DSpan = lPan; | 
|---|
| 557 | if (lPan == 0) { | 
|---|
| 558 | me->pan = 0; | 
|---|
| 559 | return DS_OK; | 
|---|
| 560 | } | 
|---|
| 561 |  | 
|---|
| 562 | /* Note: we use very similar code as for volume above   */ | 
|---|
| 563 | if (lPan < 0) | 
|---|
| 564 | if (lPan < -4000) | 
|---|
| 565 | me->pan = -255; | 
|---|
| 566 | else | 
|---|
| 567 | me->pan = VolTable[-lPan / 4] - 255; | 
|---|
| 568 | else | 
|---|
| 569 | if (lPan > 4000) | 
|---|
| 570 | me->pan = 255; | 
|---|
| 571 | else | 
|---|
| 572 | me->pan = 255 - VolTable[lPan / 4]; | 
|---|
| 573 |  | 
|---|
| 574 | dprintf(("  New pan: %d", me->pan)); | 
|---|
| 575 |  | 
|---|
| 576 | return DS_OK; | 
|---|
| 577 | } | 
|---|
| 578 |  | 
|---|
| 579 | //****************************************************************************** | 
|---|
| 580 | //****************************************************************************** | 
|---|
| 581 | HRESULT __stdcall SoundBufSetFrequency(THIS_ DWORD dwFrequency) | 
|---|
| 582 | { | 
|---|
| 583 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 584 |  | 
|---|
| 585 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufSetFrequency (buf=%X)", me)); | 
|---|
| 586 | if (me == NULL) { | 
|---|
| 587 | return DSERR_INVALIDPARAM; | 
|---|
| 588 | } | 
|---|
| 589 |  | 
|---|
| 590 | // zero means default (buffer format) frequency | 
|---|
| 591 | if (dwFrequency) | 
|---|
| 592 | me->frequency = dwFrequency; | 
|---|
| 593 | else | 
|---|
| 594 | me->frequency = me->lpfxFormat->nSamplesPerSec; | 
|---|
| 595 |  | 
|---|
| 596 | return DS_OK; | 
|---|
| 597 | } | 
|---|
| 598 |  | 
|---|
| 599 | //****************************************************************************** | 
|---|
| 600 | //****************************************************************************** | 
|---|
| 601 | HRESULT __stdcall SoundBufStop(THIS  ) | 
|---|
| 602 | { | 
|---|
| 603 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 604 |  | 
|---|
| 605 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufStop (buf=%X)", me)); | 
|---|
| 606 | if (me == NULL) { | 
|---|
| 607 | return DSERR_INVALIDPARAM; | 
|---|
| 608 | } | 
|---|
| 609 |  | 
|---|
| 610 | me->fPlaying = FALSE; | 
|---|
| 611 | me->status  &= ~(DSBSTATUS_PLAYING | DSBSTATUS_LOOPING); | 
|---|
| 612 |  | 
|---|
| 613 | if (me->notify != NULL) | 
|---|
| 614 | me->notify->CheckStop(); | 
|---|
| 615 |  | 
|---|
| 616 | return DS_OK; | 
|---|
| 617 | } | 
|---|
| 618 |  | 
|---|
| 619 | //****************************************************************************** | 
|---|
| 620 | //****************************************************************************** | 
|---|
| 621 | HRESULT __stdcall SoundBufUnlock(THIS_ | 
|---|
| 622 | LPVOID lpvAudioPtr1, DWORD dwAudioBytes1, | 
|---|
| 623 | LPVOID lpvAudioPtr2, DWORD dwAudioBytes2) | 
|---|
| 624 | { | 
|---|
| 625 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 626 |  | 
|---|
| 627 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufUnlock (buf=%X)", me)); | 
|---|
| 628 | if (me == NULL) { | 
|---|
| 629 | return DSERR_INVALIDPARAM; | 
|---|
| 630 | } | 
|---|
| 631 |  | 
|---|
| 632 | me->fLocked = TRUE; | 
|---|
| 633 |  | 
|---|
| 634 | return DS_OK; | 
|---|
| 635 | } | 
|---|
| 636 |  | 
|---|
| 637 | //****************************************************************************** | 
|---|
| 638 | //****************************************************************************** | 
|---|
| 639 | HRESULT __stdcall SoundBufRestore(THIS  ) | 
|---|
| 640 | { | 
|---|
| 641 | OS2IDirectSoundBuffer *me = (OS2IDirectSoundBuffer *)This; | 
|---|
| 642 |  | 
|---|
| 643 | dprintf(("DSOUND-OS2IDirectSoundBuffer::SoundBufRestore (buf=%X)", me)); | 
|---|
| 644 | if (me == NULL) { | 
|---|
| 645 | return DSERR_INVALIDPARAM; | 
|---|
| 646 | } | 
|---|
| 647 | return DS_OK; | 
|---|
| 648 | } | 
|---|
| 649 | //****************************************************************************** | 
|---|
| 650 | //****************************************************************************** | 
|---|