Advertisement
Guest User

Untitled

a guest
Dec 11th, 2017
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. internal void
  2. Win32InitWASAPI(void)
  3. {
  4. win32_audio *Result = 0;
  5. Result = (win32_audio *) Win32AllocateMemory(sizeof(win32_audio));
  6. ZeroMemory(Result, sizeof(win32_audio));
  7.  
  8. IMMDeviceEnumerator *DeviceEnumerator = 0;
  9. HRESULT Result1 = CoCreateInstance(__uuidof(MMDeviceEnumerator), 0,
  10. CLSCTX_ALL, __uuidof(IMMDeviceEnumerator),
  11. (void **)&DeviceEnumerator);
  12. if(Result1 == S_OK)
  13. {
  14. IMMDevice *Device = 0;
  15. HRESULT Result2 = DeviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &Device);
  16. if(Result2 == S_OK)
  17. {
  18. HRESULT Result3 = Device->Activate(__uuidof(IAudioClient), CLSCTX_ALL,
  19. 0, (void **)&Result->WriteAudioClient);
  20. if(Result3 == S_OK)
  21. {
  22. // NOTE(zak): This is just a default normal PCM format
  23. // nothing special really. We might wanna change the SamplesPerSec,
  24. // and BitsPerSample when we get real sound effects and music
  25. // into the game
  26. WAVEFORMATEX WaveFormat = {0};
  27. WaveFormat.wFormatTag = WAVE_FORMAT_PCM;
  28. WaveFormat.nChannels = 2;
  29. WaveFormat.nSamplesPerSec = 44100;
  30. WaveFormat.wBitsPerSample = 16;
  31. WaveFormat.nBlockAlign = (WaveFormat.nChannels * WaveFormat.wBitsPerSample) / 8;
  32. WaveFormat.nAvgBytesPerSec = WaveFormat.nSamplesPerSec * WaveFormat.nBlockAlign;
  33.  
  34. Result->SamplesPerSecond = WaveFormat.nSamplesPerSec;
  35. Result->ChannelCount = WaveFormat.nChannels;
  36.  
  37. REFERENCE_TIME TimeRequested = 10000000;
  38. HRESULT Result4 = Result->WriteAudioClient->Initialize(AUDCLNT_SHAREMODE_SHARED,
  39. AUDCLNT_STREAMFLAGS_EVENTCALLBACK|AUDCLNT_STREAMFLAGS_RATEADJUST | AUDCLNT_STREAMFLAGS_AUTOCONVERTPCM,
  40. (WaveFormat.nSamplesPerSec * (sizeof(s16) * 2)),
  41. 0,
  42. &WaveFormat,
  43. 0);
  44.  
  45. if(Result4 == S_OK)
  46. {
  47. HRESULT Result5 = Result->WriteAudioClient->GetService(__uuidof(IAudioRenderClient),
  48. (void **)&Result->AudioRenderClient);
  49. if(Result5 == S_OK)
  50. {
  51. SAFE_RELEASE(DeviceEnumerator);
  52.  
  53. CreateThread(0, 0, Win32AudioThreadProc, Result, 0, 0);
  54. }
  55. else
  56. {
  57. WriteCOMErrorToLog("Failed to GetService for AudioRenderClient with error", Result5);
  58.  
  59. }
  60. }
  61. else
  62. {
  63. WriteCOMErrorToLog("Failed to Initialize WriteAudioClient with error", Result4);
  64. }
  65. }
  66. else
  67. {
  68. WriteCOMErrorToLog("Failed to Activate AudioDevice and get WriteAudioClient with error", Result3);
  69. }
  70. }
  71. else
  72. {
  73. WriteCOMErrorToLog("GetDefaultAudioEndpoint failed with error", Result2);
  74. }
  75. }
  76. else
  77. {
  78. WriteCOMErrorToLog("CoCreateInstance failed to create MMDeviceEnumerator with error", Result1);
  79. }
  80. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement