| 1 | /* $Id: DAudioNotify.cpp,v 1.1 2001-04-27 17:39:48 sandervl Exp $ */ | 
|---|
| 2 | /* | 
|---|
| 3 | * DirectSound DirectSoundNotify class (DirectAudio) | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright 2000 Michal Necasek | 
|---|
| 6 | * | 
|---|
| 7 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 8 | * | 
|---|
| 9 | */ | 
|---|
| 10 |  | 
|---|
| 11 |  | 
|---|
| 12 | /*@Header*********************************************************************** | 
|---|
| 13 | *   Header Files                                                               * | 
|---|
| 14 | *******************************************************************************/ | 
|---|
| 15 | #define INCL_DOSMISC | 
|---|
| 16 | #include <os2win.h> | 
|---|
| 17 |  | 
|---|
| 18 | #include <stdlib.h> | 
|---|
| 19 | #include <string.h> | 
|---|
| 20 |  | 
|---|
| 21 | #define INITGUID | 
|---|
| 22 | #include <dsound.h> | 
|---|
| 23 |  | 
|---|
| 24 | #include "os2dsound.h" | 
|---|
| 25 | #include "daudiobuffer.h" | 
|---|
| 26 | #include "daudionotify.h" | 
|---|
| 27 | #include <misc.h> | 
|---|
| 28 |  | 
|---|
| 29 | //****************************************************************************** | 
|---|
| 30 | //****************************************************************************** | 
|---|
| 31 | IDirectAudioNotify::IDirectAudioNotify(IDirectAudioBuffer *parentBuffer) | 
|---|
| 32 | { | 
|---|
| 33 | lpVtbl = &Vtbl; | 
|---|
| 34 | Vtbl.AddRef                   = DAudioNotifyAddRef; | 
|---|
| 35 | Vtbl.Release                  = DAudioNotifyRelease; | 
|---|
| 36 | Vtbl.QueryInterface           = DAudioNotifyQueryInterface; | 
|---|
| 37 | Vtbl.SetNotificationPositions = DAudioNotifySetNotificationPositions; | 
|---|
| 38 |  | 
|---|
| 39 | dprintf(("DSOUND-IDirectAudioNotify::IDirectAudioNotify (this=%X)", this)); | 
|---|
| 40 |  | 
|---|
| 41 | lpSoundBuffer = parentBuffer; | 
|---|
| 42 | lpPositions   = NULL; | 
|---|
| 43 | cPositions    = 0; | 
|---|
| 44 | Referenced    = 0; | 
|---|
| 45 |  | 
|---|
| 46 | // add a reference to the parent SoundBuffer to make sure it won't suddenly disappear | 
|---|
| 47 | lpSoundBuffer->Vtbl.AddRef(lpSoundBuffer); | 
|---|
| 48 | // set pointer to ourselves in parent SoundBuffer | 
|---|
| 49 | lpSoundBuffer->SetNotify(this); | 
|---|
| 50 | } | 
|---|
| 51 |  | 
|---|
| 52 | //****************************************************************************** | 
|---|
| 53 | //****************************************************************************** | 
|---|
| 54 | IDirectAudioNotify::~IDirectAudioNotify() | 
|---|
| 55 | { | 
|---|
| 56 | dprintf(("DSOUND-IDirectAudioNotify::~IDirectAudioNotify (this=%X)", this)); | 
|---|
| 57 | lpSoundBuffer->SetNotify(NULL); | 
|---|
| 58 | lpSoundBuffer->Vtbl.Release(lpSoundBuffer); | 
|---|
| 59 | if (lpPositions != NULL) | 
|---|
| 60 | free(lpPositions); | 
|---|
| 61 | } | 
|---|
| 62 |  | 
|---|
| 63 | //****************************************************************************** | 
|---|
| 64 | //****************************************************************************** | 
|---|
| 65 | HRESULT __stdcall DAudioNotifyQueryInterface(THIS, REFIID riid, LPVOID * ppvObj) | 
|---|
| 66 | { | 
|---|
| 67 | dprintf(("DSOUND-IDirectAudioNotify::QueryInterface")); | 
|---|
| 68 | if (This == NULL) { | 
|---|
| 69 | return DSERR_INVALIDPARAM; | 
|---|
| 70 | } | 
|---|
| 71 | *ppvObj = NULL; | 
|---|
| 72 |  | 
|---|
| 73 | if (!IsEqualGUID(riid, IID_IDirectSoundNotify)) | 
|---|
| 74 | return E_NOINTERFACE; | 
|---|
| 75 |  | 
|---|
| 76 | *ppvObj = This; | 
|---|
| 77 |  | 
|---|
| 78 | DAudioNotifyAddRef(This); | 
|---|
| 79 | return DS_OK; | 
|---|
| 80 | } | 
|---|
| 81 |  | 
|---|
| 82 | //****************************************************************************** | 
|---|
| 83 | //****************************************************************************** | 
|---|
| 84 | ULONG __stdcall DAudioNotifyAddRef(THIS) | 
|---|
| 85 | { | 
|---|
| 86 | IDirectAudioNotify *me = (IDirectAudioNotify *)This; | 
|---|
| 87 |  | 
|---|
| 88 | dprintf(("DSOUND-IDirectAudioNotify::AddRef (this=%X) %d", me, me->Referenced+1)); | 
|---|
| 89 | if (me == NULL) { | 
|---|
| 90 | return 0; | 
|---|
| 91 | } | 
|---|
| 92 | return ++me->Referenced; | 
|---|
| 93 | } | 
|---|
| 94 |  | 
|---|
| 95 | //****************************************************************************** | 
|---|
| 96 | //****************************************************************************** | 
|---|
| 97 | ULONG __stdcall DAudioNotifyRelease(THIS) | 
|---|
| 98 | { | 
|---|
| 99 | IDirectAudioNotify *me = (IDirectAudioNotify *)This; | 
|---|
| 100 |  | 
|---|
| 101 | dprintf(("DSOUND-IDirectAudioNotify::Release (this=%X) %d", me, me->Referenced-1)); | 
|---|
| 102 | if (me == NULL) { | 
|---|
| 103 | return 0; | 
|---|
| 104 | } | 
|---|
| 105 | if (me->Referenced) { | 
|---|
| 106 | me->Referenced--; | 
|---|
| 107 | if (me->Referenced == 0) { | 
|---|
| 108 | delete me; | 
|---|
| 109 | return DS_OK; | 
|---|
| 110 | } | 
|---|
| 111 | else | 
|---|
| 112 | return me->Referenced; | 
|---|
| 113 | } | 
|---|
| 114 | else | 
|---|
| 115 | return DS_OK; | 
|---|
| 116 | } | 
|---|
| 117 |  | 
|---|
| 118 | //****************************************************************************** | 
|---|
| 119 | //****************************************************************************** | 
|---|
| 120 | HRESULT __stdcall DAudioNotifySetNotificationPositions(THIS, DWORD cPositionNotifies, | 
|---|
| 121 | LPCDSBPOSITIONNOTIFY lpcPositionNotifies) | 
|---|
| 122 | { | 
|---|
| 123 | IDirectAudioNotify *me = (IDirectAudioNotify *)This; | 
|---|
| 124 |  | 
|---|
| 125 | dprintf(("DSOUND-IDirectAudioNotify::DAudioNotifySetPositions (%d, this=%X)", cPositionNotifies, me)); | 
|---|
| 126 | if (me == NULL || lpcPositionNotifies == NULL) { | 
|---|
| 127 | return DSERR_INVALIDPARAM; | 
|---|
| 128 | } | 
|---|
| 129 |  | 
|---|
| 130 | // we have to copy the notifications array to our own memory here; if any notifications | 
|---|
| 131 | // were registered previously, discard them | 
|---|
| 132 | me->cPositions  = cPositionNotifies; | 
|---|
| 133 | if (me->lpPositions != NULL) | 
|---|
| 134 | free(me->lpPositions); | 
|---|
| 135 | me->lpPositions = (LPDSBPOSITIONNOTIFY)malloc(cPositionNotifies * sizeof(DSBPOSITIONNOTIFY)); | 
|---|
| 136 | for (int i = 0; i < cPositionNotifies; i++) { | 
|---|
| 137 | me->lpPositions[i].dwOffset     = lpcPositionNotifies[i].dwOffset; | 
|---|
| 138 | me->lpPositions[i].hEventNotify = lpcPositionNotifies[i].hEventNotify; | 
|---|
| 139 | } | 
|---|
| 140 |  | 
|---|
| 141 | return DS_OK; | 
|---|
| 142 | } | 
|---|
| 143 |  | 
|---|
| 144 | //****************************************************************************** | 
|---|
| 145 | //****************************************************************************** | 
|---|
| 146 | void  IDirectAudioNotify::CheckPos(DWORD dwOldpos, DWORD dwNewpos) | 
|---|
| 147 | { | 
|---|
| 148 | //dprintf(("DSOUND-IDirectAudioNotify::CheckPos (%d, %d) (this=%X)", dwOldpos, dwNewpos, this)); | 
|---|
| 149 | // loop thru all registered notifications and check if any offset was reached | 
|---|
| 150 | DWORD   i; | 
|---|
| 151 |  | 
|---|
| 152 | for (i = 0; i < cPositions; i++) { | 
|---|
| 153 | // we do NOT handle the stop notification here | 
|---|
| 154 | if (lpPositions[i].dwOffset == DSBPN_OFFSETSTOP) | 
|---|
| 155 | continue; | 
|---|
| 156 |  | 
|---|
| 157 | if (dwOldpos < dwNewpos) { // did we just wrap? | 
|---|
| 158 | if ((lpPositions[i].dwOffset >= dwOldpos) && (lpPositions[i].dwOffset < dwNewpos)) | 
|---|
| 159 | SetEvent(lpPositions[i].hEventNotify); | 
|---|
| 160 | } else {                   // yes we wrapped | 
|---|
| 161 | if ((lpPositions[i].dwOffset >= dwOldpos) || (lpPositions[i].dwOffset < dwNewpos)) | 
|---|
| 162 | SetEvent(lpPositions[i].hEventNotify); | 
|---|
| 163 | } | 
|---|
| 164 | } | 
|---|
| 165 | } | 
|---|
| 166 |  | 
|---|
| 167 | //****************************************************************************** | 
|---|
| 168 | //****************************************************************************** | 
|---|
| 169 | void  IDirectAudioNotify::CheckStop() | 
|---|
| 170 | { | 
|---|
| 171 | //dprintf(("DSOUND-IDirectAudioNotify::CheckStop (this=%X)", this)); | 
|---|
| 172 | // just loop thru all notifications and check if DSBPN_OFFSETSTOP is there | 
|---|
| 173 | DWORD   i; | 
|---|
| 174 |  | 
|---|
| 175 | for (i = 0; i < cPositions; i++) { | 
|---|
| 176 | if (lpPositions[i].dwOffset == DSBPN_OFFSETSTOP) | 
|---|
| 177 | SetEvent(lpPositions[i].hEventNotify); | 
|---|
| 178 | } | 
|---|
| 179 | } | 
|---|