directsound.cpp 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. #include <dsound.h>
  2. struct AudioDirectSound : AudioDriver {
  3. AudioDirectSound& self = *this;
  4. AudioDirectSound(Audio& super) : AudioDriver(super) {}
  5. ~AudioDirectSound() { terminate(); }
  6. auto create() -> bool override {
  7. super.setChannels(2);
  8. super.setFrequency(48000);
  9. super.setLatency(40);
  10. return initialize();
  11. }
  12. auto driver() -> string override { return "DirectSound 7.0"; }
  13. auto ready() -> bool override { return _ready; }
  14. auto hasBlocking() -> bool override { return true; }
  15. auto hasFrequencies() -> vector<uint> override {
  16. return {44100, 48000, 96000};
  17. }
  18. auto hasLatencies() -> vector<uint> override {
  19. return {40, 60, 80, 100};
  20. }
  21. auto setBlocking(bool blocking) -> bool override { return true; }
  22. auto setFrequency(uint frequency) -> bool override { return initialize(); }
  23. auto setLatency(uint latency) -> bool override { return initialize(); }
  24. auto clear() -> void override {
  25. if(!ready()) return;
  26. _ringRead = 0;
  27. _ringWrite = _rings - 1;
  28. _ringDistance = _rings - 1;
  29. if(_buffer) memory::fill<uint32_t>(_buffer, _period * _rings);
  30. _offset = 0;
  31. if(!_secondary) return;
  32. _secondary->Stop();
  33. _secondary->SetCurrentPosition(0);
  34. void* output;
  35. DWORD size;
  36. _secondary->Lock(0, _period * _rings * 4, &output, &size, 0, 0, 0);
  37. memory::fill<uint8_t>(output, size);
  38. _secondary->Unlock(output, size, 0, 0);
  39. _secondary->Play(0, 0, DSBPLAY_LOOPING);
  40. }
  41. auto output(const double samples[]) -> void override {
  42. if(!ready()) return;
  43. _buffer[_offset] = (uint16_t)sclamp<16>(samples[0] * 32767.0) << 0;
  44. _buffer[_offset] |= (uint16_t)sclamp<16>(samples[1] * 32767.0) << 16;
  45. if(++_offset < _period) return;
  46. _offset = 0;
  47. if(self.blocking) {
  48. //wait until playback buffer has an empty ring to write new audio data to
  49. while(_ringDistance >= _rings - 1) {
  50. DWORD position;
  51. _secondary->GetCurrentPosition(&position, 0);
  52. uint ringActive = position / (_period * 4);
  53. if(ringActive == _ringRead) continue;
  54. //subtract number of played rings from ring distance counter
  55. _ringDistance -= (_rings + ringActive - _ringRead) % _rings;
  56. _ringRead = ringActive;
  57. if(_ringDistance < 2) {
  58. //buffer underflow; set max distance to recover quickly
  59. _ringDistance = _rings - 1;
  60. _ringWrite = (_rings + _ringRead - 1) % _rings;
  61. break;
  62. }
  63. }
  64. }
  65. _ringWrite = (_ringWrite + 1) % _rings;
  66. _ringDistance = (_ringDistance + 1) % _rings;
  67. void* output;
  68. DWORD size;
  69. if(_secondary->Lock(_ringWrite * _period * 4, _period * 4, &output, &size, 0, 0, 0) == DS_OK) {
  70. memory::copy<uint32_t>(output, _buffer, _period);
  71. _secondary->Unlock(output, size, 0, 0);
  72. }
  73. }
  74. private:
  75. auto initialize() -> bool {
  76. terminate();
  77. _rings = 8;
  78. _period = self.frequency * self.latency / _rings / 1000.0 + 0.5;
  79. _buffer = new uint32_t[_period * _rings];
  80. _offset = 0;
  81. if(DirectSoundCreate(0, &_interface, 0) != DS_OK) return terminate(), false;
  82. _interface->SetCooperativeLevel(GetDesktopWindow(), DSSCL_PRIORITY);
  83. DSBUFFERDESC primaryDescription = {};
  84. primaryDescription.dwSize = sizeof(DSBUFFERDESC);
  85. primaryDescription.dwFlags = DSBCAPS_PRIMARYBUFFER;
  86. primaryDescription.dwBufferBytes = 0;
  87. primaryDescription.lpwfxFormat = 0;
  88. _interface->CreateSoundBuffer(&primaryDescription, &_primary, 0);
  89. WAVEFORMATEX waveFormat = {};
  90. waveFormat.wFormatTag = WAVE_FORMAT_PCM;
  91. waveFormat.nChannels = self.channels;
  92. waveFormat.nSamplesPerSec = self.frequency;
  93. waveFormat.wBitsPerSample = 16;
  94. waveFormat.nBlockAlign = waveFormat.nChannels * waveFormat.wBitsPerSample / 8;
  95. waveFormat.nAvgBytesPerSec = waveFormat.nSamplesPerSec * waveFormat.nBlockAlign;
  96. _primary->SetFormat(&waveFormat);
  97. DSBUFFERDESC secondaryDescription = {};
  98. secondaryDescription.dwSize = sizeof(DSBUFFERDESC);
  99. secondaryDescription.dwFlags = DSBCAPS_GETCURRENTPOSITION2 | DSBCAPS_CTRLFREQUENCY | DSBCAPS_GLOBALFOCUS | DSBCAPS_LOCSOFTWARE;
  100. secondaryDescription.dwBufferBytes = _period * _rings * 4;
  101. secondaryDescription.guid3DAlgorithm = GUID_NULL;
  102. secondaryDescription.lpwfxFormat = &waveFormat;
  103. _interface->CreateSoundBuffer(&secondaryDescription, &_secondary, 0);
  104. _secondary->SetFrequency(self.frequency);
  105. _secondary->SetCurrentPosition(0);
  106. _ready = true;
  107. clear();
  108. return true;
  109. }
  110. auto terminate() -> void {
  111. _ready = false;
  112. if(_buffer) { delete[] _buffer; _buffer = nullptr; }
  113. if(_secondary) { _secondary->Stop(); _secondary->Release(); _secondary = nullptr; }
  114. if(_primary) { _primary->Stop(); _primary->Release(); _primary = nullptr; }
  115. if(_interface) { _interface->Release(); _interface = nullptr; }
  116. }
  117. bool _ready = false;
  118. LPDIRECTSOUND _interface = nullptr;
  119. LPDIRECTSOUNDBUFFER _primary = nullptr;
  120. LPDIRECTSOUNDBUFFER _secondary = nullptr;
  121. uint32_t* _buffer = nullptr;
  122. uint _offset = 0;
  123. uint _period = 0;
  124. uint _rings = 0;
  125. uint _ringRead = 0;
  126. uint _ringWrite = 0;
  127. int _ringDistance = 0;
  128. };