PolyXAudio2AudioInterface.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*
  2. Copyright (C) 2015 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #pragma once
  20. #include <xaudio2.h>
  21. #include "polycode/core/PolyGlobals.h"
  22. #include "polycode/core/PolyThreaded.h"
  23. #include "polycode/core/PolySoundManager.h"
  24. #include <queue>
  25. #define MAX_XAUDIO_BUFFER_COUNT 4
  26. #ifndef SAFE_RELEASE
  27. #define SAFE_RELEASE(p) { if(p) { (p)->Release(); (p)=nullptr; } }
  28. #endif
  29. namespace Polycode {
  30. class XAudioInterfaceBuffer {
  31. public:
  32. int16_t bufferData[POLY_FRAMES_PER_BUFFER * POLY_NUM_CHANNELS];
  33. };
  34. class XAudio2FillThread : public Threaded {
  35. public:
  36. void runThread();
  37. AudioMixer *mixer;
  38. std::queue<XAudioInterfaceBuffer*> bufferQueue;
  39. std::mutex bufferMutex;
  40. };
  41. class XAudio2Stream : public Threaded, IXAudio2VoiceCallback {
  42. public:
  43. XAudio2Stream();
  44. STDMETHOD_(void, OnVoiceProcessingPassStart)(UINT32) override
  45. {
  46. }
  47. STDMETHOD_(void, OnVoiceProcessingPassEnd)() override
  48. {
  49. }
  50. STDMETHOD_(void, OnStreamEnd)() override
  51. {
  52. }
  53. STDMETHOD_(void, OnBufferStart)(void*) override
  54. {
  55. }
  56. STDMETHOD_(void, OnBufferEnd)(void*) override
  57. {
  58. SetEvent(hBufferEndEvent);
  59. }
  60. STDMETHOD_(void, OnLoopEnd)(void*) override
  61. {
  62. }
  63. STDMETHOD_(void, OnVoiceError)(void*, HRESULT) override
  64. {
  65. }
  66. void setMixer(AudioMixer *mixer);
  67. HRESULT initXAudio2();
  68. void runThread();
  69. private:
  70. XAudioInterfaceBuffer *lastBuffer;
  71. XAudioInterfaceBuffer *lastBuffer2;
  72. AudioMixer *mixer;
  73. HANDLE hBufferEndEvent;
  74. XAudio2FillThread *fillThread;
  75. IXAudio2* pXAudio2;
  76. IXAudio2MasteringVoice* pMasterVoice;
  77. IXAudio2SourceVoice* pSourceVoice;
  78. };
  79. class XAudio2AudioInterface : public Polycode::AudioInterface {
  80. public:
  81. XAudio2AudioInterface(Core *core);
  82. ~XAudio2AudioInterface();
  83. void setMixer(AudioMixer *mixer);
  84. private:
  85. Core *core;
  86. XAudio2Stream *xAudioStream;
  87. };
  88. }