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