dsound.cpp 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. #define WIN32_LEAN_AND_MEAN
  2. #include <windows.h>
  3. #include <objbase.h>
  4. #include <mmsystem.h>
  5. #include <stdio.h>
  6. #include "dsound.h"
  7. extern "C" {
  8. int bmx_directsound_IDirectSound_release(IDirectSound * dsound);
  9. int bmx_directsound_IDirectSound_create(IDirectSound ** dsound);
  10. int bmx_directsound_IDirectSound_setcooperativeLevel(IDirectSound * dsound, HWND hwnd, int flags);
  11. int bmx_directsound_IDirectSound_duplicatesoundbuffer(IDirectSound * dsound, IDirectSoundBuffer * buffer, IDirectSoundBuffer ** buf);
  12. int bmx_directsound_IDirectSound_createsoundbuffer(IDirectSound * dsound, IDirectSoundBuffer ** buf, int length, int hertz, int format, int chans, int bps, int size, int flags, int mode);
  13. int bmx_directsound_IDirectSoundBuffer_release(IDirectSoundBuffer * buffer);
  14. int bmx_directsound_IDirectSoundBuffer_stop(IDirectSoundBuffer * buffer);
  15. int bmx_directsound_IDirectSoundBuffer_play(IDirectSoundBuffer * buffer, int res, int priority, int flags);
  16. int bmx_directsound_IDirectSoundBuffer_setvolume(IDirectSoundBuffer * buffer, int volume);
  17. int bmx_directsound_IDirectSoundBuffer_setpan(IDirectSoundBuffer * buffer, int pan);
  18. int bmx_directsound_IDirectSoundBuffer_setfrequency(IDirectSoundBuffer * buffer, int freq);
  19. int bmx_directsound_IDirectSoundBuffer_setcurrentposition(IDirectSoundBuffer * buffer, int pos);
  20. int bmx_directsound_IDirectSoundBuffer_lock(IDirectSoundBuffer * buffer, int offset, int size, void ** ptr1, int * bytes1, void ** ptr2, int * bytes2, int flags);
  21. int bmx_directsound_IDirectSoundBuffer_unlock(IDirectSoundBuffer * buffer, void * ptr1, int bytes1, void * ptr2, int bytes2);
  22. int bmx_directsound_IDirectSoundBuffer_getstatus(IDirectSoundBuffer * buffer, int * status);
  23. }
  24. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  25. int bmx_directsound_IDirectSound_release(IDirectSound * dsound) {
  26. return dsound->Release();
  27. }
  28. int bmx_directsound_IDirectSound_create(IDirectSound ** dsound) {
  29. HINSTANCE dsoundlib;
  30. HRESULT WINAPI (*DirectSoundCreate)(LPGUID,LPDIRECTSOUND*,LPUNKNOWN);
  31. dsoundlib=LoadLibraryA("dsound");
  32. DirectSoundCreate=(HRESULT WINAPI (*)(LPGUID,LPDIRECTSOUND*,LPUNKNOWN))GetProcAddress(dsoundlib,"DirectSoundCreate");
  33. int res=DirectSoundCreate(0,dsound,0);
  34. return res;
  35. }
  36. int bmx_directsound_IDirectSound_setcooperativeLevel(IDirectSound * dsound, HWND hwnd, int flags) {
  37. return dsound->SetCooperativeLevel(hwnd, flags);
  38. }
  39. int bmx_directsound_IDirectSound_duplicatesoundbuffer(IDirectSound * dsound, IDirectSoundBuffer * buffer, IDirectSoundBuffer ** buf) {
  40. return dsound->DuplicateSoundBuffer(buffer, buf);
  41. }
  42. int bmx_directsound_IDirectSound_createsoundbuffer(IDirectSound * dsound, IDirectSoundBuffer ** buf, int length, int hertz, int format, int chans, int bps, int size, int flags, int mode) {
  43. WAVEFORMATEX fmt;
  44. memset(&fmt, 0, sizeof(WAVEFORMATEX));
  45. fmt.wFormatTag = 1;
  46. fmt.nChannels = chans;
  47. fmt.nSamplesPerSec = hertz;
  48. fmt.wBitsPerSample = bps*8;
  49. fmt.nBlockAlign = fmt.wBitsPerSample/8*fmt.nChannels;
  50. fmt.nAvgBytesPerSec = fmt.nSamplesPerSec*fmt.nBlockAlign;
  51. DSBUFFERDESC desc;
  52. memset(&desc, 0, sizeof(DSBUFFERDESC));
  53. desc.dwSize = sizeof(DSBUFFERDESC);
  54. desc.dwFlags = DSBCAPS_GLOBALFOCUS|DSBCAPS_STATIC|DSBCAPS_CTRLPAN|DSBCAPS_CTRLVOLUME|DSBCAPS_CTRLFREQUENCY;
  55. if (mode==1 || ((flags & 2)!=2)) {
  56. desc.dwFlags |= DSBCAPS_LOCSOFTWARE;
  57. }
  58. desc.dwBufferBytes = size;
  59. desc.lpwfxFormat = &fmt;
  60. return dsound->CreateSoundBuffer( &desc,buf,0 );
  61. }
  62. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++
  63. int bmx_directsound_IDirectSoundBuffer_release(IDirectSoundBuffer * buffer) {
  64. return buffer->Release();
  65. }
  66. int bmx_directsound_IDirectSoundBuffer_stop(IDirectSoundBuffer * buffer) {
  67. return buffer->Stop();
  68. }
  69. int bmx_directsound_IDirectSoundBuffer_play(IDirectSoundBuffer * buffer, int res, int priority, int flags) {
  70. return buffer->Play(res, priority, flags);
  71. }
  72. int bmx_directsound_IDirectSoundBuffer_setvolume(IDirectSoundBuffer * buffer, int volume) {
  73. return buffer->SetVolume(volume);
  74. }
  75. int bmx_directsound_IDirectSoundBuffer_setpan(IDirectSoundBuffer * buffer, int pan) {
  76. return buffer->SetPan(pan);
  77. }
  78. int bmx_directsound_IDirectSoundBuffer_setfrequency(IDirectSoundBuffer * buffer, int freq) {
  79. return buffer->SetFrequency(freq);
  80. }
  81. int bmx_directsound_IDirectSoundBuffer_setcurrentposition(IDirectSoundBuffer * buffer, int pos) {
  82. return buffer->SetCurrentPosition(pos);
  83. }
  84. int bmx_directsound_IDirectSoundBuffer_lock(IDirectSoundBuffer * buffer, int offset, int size, void ** ptr1, int * bytes1, void ** ptr2, int * bytes2, int flags) {
  85. DWORD b1, b2;
  86. int res = buffer->Lock(offset, size, ptr1, &b1, ptr2, &b2, flags);
  87. *bytes1 = b1;
  88. *bytes2 = b2;
  89. return res;
  90. }
  91. int bmx_directsound_IDirectSoundBuffer_unlock(IDirectSoundBuffer * buffer, void * ptr1, int bytes1, void * ptr2, int bytes2) {
  92. return buffer->Unlock(ptr1, bytes1, ptr2, bytes2);
  93. }
  94. int bmx_directsound_IDirectSoundBuffer_getstatus(IDirectSoundBuffer * buffer, int * status) {
  95. DWORD s;
  96. int res = buffer->GetStatus(&s);
  97. *status = s;
  98. return res;
  99. }
  100. // ++++++++++++++++++++++++++++++++++++++++++++++++++++++++