Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

234 lines
7.6 KiB

  1. //+-------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. //
  5. // Copyright (C) Microsoft Corporation, 1998 - 1999
  6. //
  7. // File: dsoundPrimaryBufferobj.cpp
  8. //
  9. //--------------------------------------------------------------------------
  10. // dSoundPrimaryBufferObj.cpp : Implementation of CDirectApp and DLL registration.
  11. // DHF_DS entire file
  12. #include "stdafx.h"
  13. #include "Direct.h"
  14. #include "dSound.h"
  15. #include "dms.h"
  16. #include "dSoundPrimaryBufferObj.h"
  17. #include "dSoundObj.h"
  18. #include "dSound3DListener.h"
  19. #define SAFE_DELETE(p) { free(p); p = NULL; }
  20. extern HRESULT AudioBSTRtoGUID(LPGUID,BSTR);
  21. extern void *g_dxj_DirectSoundPrimaryBuffer;
  22. CONSTRUCTOR(_dxj_DirectSoundPrimaryBuffer, {});
  23. DESTRUCTOR(_dxj_DirectSoundPrimaryBuffer, {});
  24. GETSET_OBJECT(_dxj_DirectSoundPrimaryBuffer);
  25. PASS_THROUGH1_R(_dxj_DirectSoundPrimaryBuffer, getVolume, GetVolume, long*);
  26. PASS_THROUGH1_R(_dxj_DirectSoundPrimaryBuffer, getPan, GetPan, long*);
  27. PASS_THROUGH_CAST_1_R(_dxj_DirectSoundPrimaryBuffer, getStatus, GetStatus, long*,(DWORD*));
  28. PASS_THROUGH_CAST_1_R(_dxj_DirectSoundPrimaryBuffer, setFormat, SetFormat, WAVEFORMATEX_CDESC*, (LPWAVEFORMATEX));
  29. PASS_THROUGH1_R(_dxj_DirectSoundPrimaryBuffer, setPan, SetPan, LONG);
  30. PASS_THROUGH_R(_dxj_DirectSoundPrimaryBuffer, stop, Stop);
  31. PASS_THROUGH_R(_dxj_DirectSoundPrimaryBuffer, restore, Restore);
  32. STDMETHODIMP C_dxj_DirectSoundPrimaryBufferObject::setVolume(LONG vol)
  33. {
  34. return m__dxj_DirectSoundPrimaryBuffer->SetVolume(vol);
  35. }
  36. STDMETHODIMP C_dxj_DirectSoundPrimaryBufferObject::getDirectSound3dListener(I_dxj_DirectSound3dListener **retval)
  37. {
  38. IDirectSound3DListener *lp3dl;
  39. HRESULT hr = S_OK;
  40. if((hr=m__dxj_DirectSoundPrimaryBuffer->QueryInterface(IID_IDirectSound3DListener, (void**) &lp3dl)) != S_OK)
  41. return hr;
  42. INTERNAL_CREATE(_dxj_DirectSound3dListener, lp3dl, retval);
  43. return hr;
  44. }
  45. STDMETHODIMP C_dxj_DirectSoundPrimaryBufferObject::getCaps(DSBCAPS_CDESC* caps)
  46. {
  47. if(!caps)
  48. return E_POINTER;
  49. caps->lSize = sizeof(DSBCAPS);
  50. return m__dxj_DirectSoundPrimaryBuffer->GetCaps((LPDSBCAPS)caps);
  51. }
  52. /////////////////////////////////////////////////////////////////////////////
  53. STDMETHODIMP C_dxj_DirectSoundPrimaryBufferObject::getCurrentPosition(DSCURSORS_CDESC *desc)
  54. {
  55. if(!desc) return E_POINTER;
  56. return (m__dxj_DirectSoundPrimaryBuffer->GetCurrentPosition((DWORD*)&desc->lPlay, (DWORD*)&desc->lWrite) );
  57. }
  58. /////////////////////////////////////////////////////////////////////////////
  59. //Java has no direct access to system memory, so it allocates it's own PrimaryBuffer
  60. //which is passed into WritePrimaryBuffer(). Because the environment is now double
  61. //PrimaryBuffered there is no need to Lock Java memory. WritePrimaryBuffer() calls
  62. //both lock and Unlock internally to write the result after the fact.
  63. /////////////////////////////////////////////////////////////////////////////
  64. STDMETHODIMP C_dxj_DirectSoundPrimaryBufferObject::writeBuffer(long start, long totsz,
  65. void *buf, long flags)
  66. {
  67. #pragma message ("SoundPrimaryBuffer writePrimaryBuffer ")
  68. byte *PrimaryBuffer=(byte*)buf; //(byte*)((SAFEARRAY*)*ppsa)->pvData;
  69. if(!PrimaryBuffer)
  70. return E_POINTER;
  71. LPVOID p1, p2;
  72. DWORD size1=0, size2=0;
  73. HRESULT val = E_FAIL;
  74. __try {
  75. if ((val = m__dxj_DirectSoundPrimaryBuffer->Lock((DWORD)start, (DWORD)totsz,
  76. &p1, &size1, &p2, &size2,
  77. (DWORD)flags)) != DS_OK)
  78. return val;
  79. // Copy to buffer end, then do a wrapped portion if it exists, then unlock
  80. DPF1(1,"----- DXVB: DSoundPrimaryBuffer (WriteBuffer) about to copy to buffer (size1 = %d )\n",size1);
  81. if (p1)
  82. {
  83. DPF1(1,"----- DXVB: DSoundPrimaryBuffer (WriteBuffer) about to copy to buffer (size1 = %d )\n",size1);
  84. memcpy (p1, PrimaryBuffer, size1);
  85. }
  86. if (p2) //There was wrapping
  87. {
  88. DPF1(1,"----- DXVB: DSoundPrimaryBuffer (WriteBuffer) about to copy to buffer (size2 = %d )\n",size2);
  89. memcpy(p2, &PrimaryBuffer[size1], size2);
  90. }
  91. //docdoc: because Lock and Unlock are tied together within WriteBuffer,
  92. // DSBufferDesc no longer needs to save Lock's system pointers.
  93. DPF(1,"----- DXVB: DSoundPrimaryBuffer (WriteBuffer) Unlocking buffer.\n");
  94. val=m__dxj_DirectSoundPrimaryBuffer->Unlock(p1, size1, p2, size2);
  95. }
  96. __except(0,0){
  97. return E_FAIL;
  98. }
  99. return val;
  100. }
  101. /////////////////////////////////////////////////////////////////////////////
  102. //Java has no direct access to system memory, so it allocates it's own PrimaryBuffer
  103. //which is passed into WritePrimaryBuffer(). Because the environment is now double
  104. //PrimaryBuffered there is no need to Lock Java memory. WritePrimaryBuffer() calls
  105. //both lock and Unlock internally to write the result after the fact.
  106. /////////////////////////////////////////////////////////////////////////////
  107. STDMETHODIMP C_dxj_DirectSoundPrimaryBufferObject::readBuffer(long start, long totsz,
  108. void *buf, long flags)
  109. {
  110. byte *PrimaryBuffer=(byte*)buf;
  111. if(!PrimaryBuffer)
  112. return E_POINTER;
  113. LPVOID p1, p2;
  114. DWORD size1=0, size2=0;
  115. HRESULT val = E_FAIL;
  116. __try {
  117. if ((val = m__dxj_DirectSoundPrimaryBuffer->Lock((DWORD)start, (DWORD)totsz, &p1, &size1, &p2, &size2,
  118. (DWORD)flags)) != DS_OK)
  119. return val;
  120. // Copy to buffer end, then do a wrapped portion if it exists, then unlock
  121. if (p1)
  122. {
  123. DPF1(1,"----- DXVB: DSoundPrimaryBuffer (ReadBuffer) about to copy to buffer (size1 = %d )\n",size1);
  124. memcpy (PrimaryBuffer,p1, size1);
  125. }
  126. if (p2) //There was wrapping
  127. {
  128. DPF1(1,"----- DXVB: DSoundPrimaryBuffer (ReadBuffer) about to copy to buffer (size2 = %d )\n",size2);
  129. memcpy(&PrimaryBuffer[size1],p2, size2);
  130. }
  131. //docdoc: because Lock and Unlock are tied together within WriteBuffer,
  132. // DSBufferDesc no longer needs to save Lock's system pointers.
  133. DPF(1,"----- DXVB: DSoundPrimaryBuffer (ReadBuffer) Unlocking buffer.\n");
  134. val= m__dxj_DirectSoundPrimaryBuffer->Unlock(p1, size1, p2, size2);
  135. }
  136. __except(1,1){
  137. return E_FAIL;
  138. }
  139. return val;
  140. }
  141. /////////////////////////////////////////////////////////////////////////////
  142. STDMETHODIMP C_dxj_DirectSoundPrimaryBufferObject::getFormat(WAVEFORMATEX_CDESC *format)
  143. {
  144. DWORD *wsize=0; // docdoc: throw away returned written size
  145. HRESULT hr=DS_OK;
  146. hr=m__dxj_DirectSoundPrimaryBuffer->GetFormat((LPWAVEFORMATEX)format, (DWORD)sizeof(WAVEFORMATEX_CDESC), wsize);
  147. return hr;
  148. }
  149. /////////////////////////////////////////////////////////////////////////////
  150. STDMETHODIMP C_dxj_DirectSoundPrimaryBufferObject::initialize(I_dxj_DirectSound *ds,
  151. DSBUFFERDESC_CDESC *buf, unsigned char *wave)
  152. {
  153. if(! (ds && buf && wave) )
  154. return E_POINTER;
  155. LPDSBUFFERDESC lpds = NULL;
  156. lpds = (LPDSBUFFERDESC)malloc(sizeof(DSBUFFERDESC));
  157. if (!lpds)
  158. return E_OUTOFMEMORY;
  159. ZeroMemory(lpds, sizeof(DSBUFFERDESC));
  160. lpds->dwSize = sizeof(DSBUFFERDESC);
  161. lpds->dwFlags = buf->lFlags;
  162. lpds->dwBufferBytes = buf->lBufferBytes;
  163. lpds->dwReserved = buf->lReserved;
  164. #ifdef _WIN64
  165. lpds->lpwfxFormat = (WAVEFORMATEX*)wave;
  166. #else
  167. lpds->lpwfxFormat = (WAVEFORMATEX*)PtrToLong(wave);
  168. #endif
  169. AudioBSTRtoGUID(&lpds->guid3DAlgorithm, buf->guid3DAlgorithm);
  170. DO_GETOBJECT_NOTNULL(LPDIRECTSOUND, lpdsound, ds)
  171. m__dxj_DirectSoundPrimaryBuffer->Initialize(lpdsound, (LPDSBUFFERDESC)lpds);
  172. return S_OK;
  173. }
  174. /////////////////////////////////////////////////////////////////////////////
  175. STDMETHODIMP C_dxj_DirectSoundPrimaryBufferObject::play(long flags)
  176. {
  177. HRESULT hr=DS_OK;
  178. if((hr=m__dxj_DirectSoundPrimaryBuffer->Play(0, 0, (DWORD)flags)) != DS_OK)
  179. return hr;
  180. return hr;
  181. }