sound.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. #include "pch.h"
  2. #include "dsound.h"
  3. //////////////////////////////////////////////////////////////////////////////
  4. //
  5. // Stuff duplicated from other files
  6. //
  7. //////////////////////////////////////////////////////////////////////////////
  8. template<class Type>
  9. class TZeroFillWithSize : public Type {
  10. protected:
  11. TZeroFillWithSize()
  12. {
  13. Reinitialize();
  14. }
  15. public:
  16. void Reinitialize()
  17. {
  18. memset(this, 0, sizeof(Type));
  19. this->dwSize = sizeof(Type);
  20. }
  21. };
  22. //////////////////////////////////////////////////////////////////////////////
  23. //
  24. //
  25. //
  26. //////////////////////////////////////////////////////////////////////////////
  27. typedef IDirectSound IDirectSoundX;
  28. typedef IDirectSoundBuffer IDirectSoundBufferX;
  29. void DDCall(HRESULT hr)
  30. {
  31. ZSucceeded(hr);
  32. }
  33. class DSBD : public TZeroFillWithSize<DSBUFFERDESC> {
  34. };
  35. class DSCaps : public TZeroFillWithSize<DSCAPS> {
  36. };
  37. class WaveFormatEX : public TZeroFill<WAVEFORMATEX> {
  38. public:
  39. WaveFormatEX()
  40. {
  41. cbSize = sizeof(WaveFormatEX);
  42. }
  43. };
  44. //////////////////////////////////////////////////////////////////////////////
  45. //
  46. //
  47. //
  48. //////////////////////////////////////////////////////////////////////////////
  49. class SoundBuffer {
  50. };
  51. //////////////////////////////////////////////////////////////////////////////
  52. //
  53. //
  54. //
  55. //////////////////////////////////////////////////////////////////////////////
  56. class SoundEngineImpl : public SoundEngine {
  57. private:
  58. //////////////////////////////////////////////////////////////////////////////
  59. //
  60. //
  61. //
  62. //////////////////////////////////////////////////////////////////////////////
  63. TRef<IDirectSoundX> m_pds;
  64. DSCaps m_dscaps;
  65. TRef<IDirectSoundBufferX> m_pdsb;
  66. public:
  67. SoundEngineImpl(HWND hwnd)
  68. {
  69. //
  70. // Create the DSound Device
  71. //
  72. DDCall(::DirectSoundCreate(NULL, &m_pds, NULL));
  73. DDCall(m_pds->SetCooperativeLevel(hwnd, DSSCL_PRIORITY));
  74. DDCall(m_pds->GetCaps(&m_dscaps));
  75. //
  76. // Create a buffer
  77. //
  78. WaveFormatEX wf;
  79. wf.wFormatTag = WAVE_FORMAT_PCM;
  80. wf.nChannels = 1;
  81. wf.nSamplesPerSec = 22050;
  82. wf.wBitsPerSample = 16;
  83. wf.nBlockAlign = wf.wBitsPerSample / 8 * wf.nChannels;
  84. wf.nAvgBytesPerSec = wf.nSamplesPerSec * wf.nBlockAlign;
  85. DSBD dsbd;
  86. dsbd.dwFlags = DSBCAPS_GETCURRENTPOSITION2;
  87. dsbd.dwBufferBytes = 20 * wf.nSamplesPerSec * 2;
  88. dsbd.dwReserved = 0;
  89. dsbd.lpwfxFormat = &wf;
  90. DDCall(m_pds->CreateSoundBuffer(&dsbd, &m_pdsb, NULL));
  91. //
  92. // Fill the buffer
  93. //
  94. short* p1;
  95. DWORD dw1;
  96. short* p2;
  97. DWORD dw2;
  98. DDCall(m_pdsb->Lock(
  99. 0,
  100. dsbd.dwBufferBytes,
  101. (void**)&p1,
  102. &dw1,
  103. (void**)&p2,
  104. &dw2,
  105. DSBLOCK_ENTIREBUFFER
  106. ));
  107. for(DWORD index = 0; index < dw1 / 2; index++) {
  108. float time = float(index) / float(wf.nSamplesPerSec);
  109. float value = Function(time);
  110. int i = int(32767.0f * value);
  111. p1[index] = short(i);
  112. }
  113. DDCall(m_pdsb->Unlock(p1, dw1, p2, dw2));
  114. //
  115. // Play the Buffer
  116. //
  117. DDCall(m_pdsb->Play(0, 0, DSBPLAY_LOOPING));
  118. }
  119. float Function(float time)
  120. {
  121. float f = 880.0f;
  122. float v = 0.8f + 0.2f * sin(2.0f * pi * 5.0f * time);
  123. return v * sin(2.0f * pi * f * time);
  124. }
  125. //////////////////////////////////////////////////////////////////////////////
  126. //
  127. //
  128. //
  129. //////////////////////////////////////////////////////////////////////////////
  130. void Update()
  131. {
  132. }
  133. };
  134. TRef<SoundEngine> CreateSoundEngine(HWND hwnd)
  135. {
  136. return new SoundEngineImpl(hwnd);
  137. }