main.cpp 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. #include "../Winamp/OUT.H"
  2. #include "api.h"
  3. #include "resource.h"
  4. #include <Mmdeviceapi.h>
  5. #include <Audioclient.h>
  6. #include <Audiosessiontypes.h>
  7. #include "../winamp/wa_ipc.h"
  8. #include <api/service/waServiceFactory.h>
  9. #include <strsafe.h>
  10. #define WASAPI_PLUGIN_VERSION L"0.3"
  11. constexpr auto VolumeLevelMultiplier = 255;
  12. static wchar_t plugin_name[256];
  13. // wasabi based services for localisation support
  14. api_service *WASABI_API_SVC = 0;
  15. api_language *WASABI_API_LNG = 0;
  16. HINSTANCE WASABI_API_LNG_HINST = 0, WASABI_API_ORIG_HINST = 0;
  17. static const UINT32 REFTIMES_PER_SEC = 10000000;
  18. static const UINT32 REFTIMES_PER_MILLISEC = 10000;
  19. // TODO(benski) is there some library that has this
  20. static const CLSID CLSID_MMDeviceEnumerator = __uuidof(MMDeviceEnumerator);
  21. static const IID IID_IMMDeviceEnumerator = __uuidof(IMMDeviceEnumerator);
  22. static const IID IID_IAudioClient = __uuidof(IAudioClient);
  23. static const IID IID_IAudioRenderClient = __uuidof(IAudioRenderClient);
  24. static const IID IID_IAudioClock = __uuidof(IAudioClock);
  25. static bool InitializedCOM;
  26. extern Out_Module plugin;
  27. static IAudioClient *client=0;
  28. static IAudioRenderClient *render_client=0;
  29. static IAudioClock *clock=0;
  30. static ISimpleAudioVolume *audio_volume = 0;
  31. static IChannelAudioVolume *channel_volume = 0;
  32. static UINT32 bufferFrameCount;
  33. static WORD bytes_per_frame;
  34. static UINT64 frequency=0;
  35. static UINT32 sample_rate;
  36. static double start_time_ms = 0;
  37. static bool paused=false;
  38. static float start_volume = 1.0;
  39. static float start_pan = 0;
  40. WAVEFORMATEXTENSIBLE WaveFormatForParameters(int samplerate, int numchannels, int bitspersamp);
  41. static void SetVolume(int volume);
  42. static void SetPan(int pan);
  43. static CRITICAL_SECTION ThreadSync;
  44. static void Config(HWND hwndParent)
  45. {
  46. }
  47. static int DoAboutMessageBox(HWND parent, wchar_t* title, wchar_t* message)
  48. {
  49. MSGBOXPARAMSW msgbx = {sizeof(MSGBOXPARAMSW),0};
  50. msgbx.lpszText = message;
  51. msgbx.lpszCaption = title;
  52. msgbx.lpszIcon = MAKEINTRESOURCEW(102);
  53. msgbx.hInstance = GetModuleHandle(0);
  54. msgbx.dwStyle = MB_USERICON;
  55. msgbx.hwndOwner = parent;
  56. return MessageBoxIndirectW(&msgbx);
  57. }
  58. static void About(HWND hwndParent)
  59. {
  60. wchar_t message[1024], text[1024] =L"";
  61. WASABI_API_LNGSTRINGW_BUF(IDS_NULLSOFT_WASAPI_OLD,text,1024);
  62. StringCchPrintfW(message, 1024, WASABI_API_LNGSTRINGW(IDS_ABOUT_TEXT),
  63. plugin.description, TEXT(__DATE__));
  64. DoAboutMessageBox(hwndParent,text,message);
  65. }
  66. static void Init()
  67. {
  68. /*
  69. HRESULT hr;
  70. hr=CoInitializeEx(0, COINIT_MULTITHREADED);
  71. if (SUCCEEDED(hr)) {
  72. InitializedCOM = true;
  73. } else {
  74. InitializedCOM = false;
  75. }
  76. */
  77. // loader so that we can get the localisation service api for use
  78. WASABI_API_SVC = (api_service*)SendMessage(plugin.hMainWindow, WM_WA_IPC, 0, IPC_GET_API_SERVICE);
  79. if (WASABI_API_SVC == (api_service*)1) WASABI_API_SVC = NULL;
  80. waServiceFactory *sf = WASABI_API_SVC->service_getServiceByGuid(languageApiGUID);
  81. if (sf) WASABI_API_LNG = reinterpret_cast<api_language*>(sf->getInterface());
  82. // need to have this initialised before we try to do anything with localisation features
  83. WASABI_API_START_LANG(plugin.hDllInstance,OutWasapiLangGUID);
  84. StringCbPrintfW(plugin_name,sizeof(plugin_name),WASABI_API_LNGSTRINGW(IDS_NULLSOFT_WASAPI), WASAPI_PLUGIN_VERSION);
  85. plugin.description = (char *)plugin_name;
  86. }
  87. static void Quit()
  88. {
  89. /*
  90. if (InitializedCOM) {
  91. CoUninitialize();
  92. }*/
  93. }
  94. static int Open(int samplerate, int numchannels, int bitspersamp, int bufferlenms, int prebufferms)
  95. {
  96. CoInitialize(0);
  97. IMMDeviceEnumerator *enumerator=0;
  98. IMMDevice *device=0;
  99. sample_rate = samplerate;
  100. HRESULT hr = CoCreateInstance(CLSID_MMDeviceEnumerator, NULL, CLSCTX_ALL, IID_IMMDeviceEnumerator, (void**)&enumerator);
  101. hr = enumerator->GetDefaultAudioEndpoint(eRender, eMultimedia, &device);
  102. hr = device->Activate(IID_IAudioClient, CLSCTX_ALL, NULL, (void **)&client);
  103. if (FAILED(hr)) {
  104. wchar_t temp[1234];
  105. wsprintf(temp, L"device->Activate: %x", hr);
  106. ::MessageBox(NULL, temp, L"error", MB_OK);
  107. return -1;
  108. }
  109. WAVEFORMATEXTENSIBLE wave_format = WaveFormatForParameters(samplerate, numchannels, bitspersamp);
  110. bytes_per_frame = wave_format.Format.nBlockAlign;
  111. hr = client->Initialize(
  112. AUDCLNT_SHAREMODE_SHARED,
  113. 0x80000000,
  114. 1 * REFTIMES_PER_SEC,
  115. 0,
  116. (WAVEFORMATEX *)&wave_format,
  117. NULL);
  118. if (FAILED(hr)) {
  119. wchar_t temp[1234];
  120. wsprintf(temp, L"client->Initialize: %x", hr);
  121. ::MessageBox(NULL, temp, L"error", MB_OK);
  122. return -1;
  123. }
  124. // Get the actual size of the allocated buffer.
  125. hr = client->GetBufferSize(&bufferFrameCount);
  126. if (FAILED(hr)) {
  127. wchar_t temp[1234];
  128. wsprintf(temp, L"client->GetBufferSize: %x", hr);
  129. ::MessageBox(NULL, temp, L"error", MB_OK);
  130. return -1;
  131. }
  132. hr = client->GetService(
  133. IID_IAudioRenderClient,
  134. (void**)&render_client);
  135. if (FAILED(hr)) {
  136. wchar_t temp[1234];
  137. wsprintf(temp, L"client->GetService(IID_IAudioRenderClient): %x", hr);
  138. ::MessageBox(NULL, temp, L"error", MB_OK);
  139. return -1;
  140. }
  141. hr = client->GetService(
  142. IID_IAudioClock,
  143. (void**)&clock);
  144. if (FAILED(hr)) {
  145. wchar_t temp[1234];
  146. wsprintf(temp, L"client->GetService(IID_IAudioClock): %x", hr);
  147. ::MessageBox(NULL, temp, L"error", MB_OK);
  148. return -1;
  149. }
  150. hr = clock->GetFrequency(&frequency);
  151. hr = client->GetService(__uuidof(ISimpleAudioVolume), reinterpret_cast<void **>( & audio_volume));
  152. hr = client->GetService(__uuidof(IChannelAudioVolume), (void **)&channel_volume);
  153. start_time_ms = 0;
  154. paused=false;
  155. client->Start();
  156. // Start volume is in range 0.0 to 1.0, should be converted
  157. SetVolume((int)(start_volume * VolumeLevelMultiplier));
  158. SetPan((int)start_pan);
  159. return 1000;
  160. }
  161. static void Close()
  162. {
  163. if (client) {
  164. client->Stop();
  165. client->Release();
  166. client=0;
  167. }
  168. if (render_client) {
  169. render_client->Release();
  170. render_client=0;
  171. }
  172. if (clock) {
  173. clock->Release();
  174. clock=0;
  175. }
  176. if (audio_volume) {
  177. audio_volume->Release();
  178. audio_volume=0;
  179. }
  180. if (channel_volume) {
  181. channel_volume->Release();
  182. channel_volume=0;
  183. }
  184. }
  185. static int CanWrite()
  186. {
  187. if (client) {
  188. UINT32 numFramesPadding;
  189. HRESULT hr = client->GetCurrentPadding(&numFramesPadding);
  190. return (bufferFrameCount - numFramesPadding) * bytes_per_frame;
  191. }
  192. else {
  193. return 0;
  194. }
  195. }
  196. static int Write(char* buf, int len)
  197. {
  198. if (!render_client)
  199. {
  200. return -1;
  201. }
  202. else
  203. {
  204. int LenghtToWrite = CanWrite();
  205. if (LenghtToWrite > 0 && LenghtToWrite >= len)
  206. {
  207. BYTE* data;
  208. render_client->GetBuffer(len / bytes_per_frame, &data);
  209. memcpy(data, buf, len);
  210. render_client->ReleaseBuffer(len / bytes_per_frame, 0);
  211. }
  212. }
  213. return 0;
  214. }
  215. static int IsPlaying()
  216. {
  217. return CanWrite() == 0;
  218. }
  219. static int Pause(int pause)
  220. {
  221. int old_paused = paused?1:0;
  222. if (client) {
  223. if (pause) {
  224. client->Stop();
  225. paused=true;
  226. } else {
  227. client->Start();
  228. paused=false;
  229. }
  230. }
  231. return old_paused;
  232. }
  233. static void SetVolume(int volume)
  234. {
  235. float fVolume = (float)volume / (float)VolumeLevelMultiplier;
  236. if (volume >= 0) {
  237. start_volume = fVolume;
  238. if (audio_volume) {
  239. audio_volume->SetMasterVolume(fVolume, 0);
  240. }
  241. }
  242. }
  243. static void SetPan(int pan)
  244. {
  245. float fPan = (float)pan/128.0f;
  246. if (channel_volume) {
  247. start_pan = fPan;
  248. if (fPan < 0) {
  249. channel_volume->SetChannelVolume(0, 1.0f, NULL);
  250. channel_volume->SetChannelVolume(1, 1.0f-fPan, NULL);
  251. } else if (fPan > 0) {
  252. channel_volume->SetChannelVolume(1, 1.0f, NULL);
  253. channel_volume->SetChannelVolume(0, 1.0f-fPan, NULL);
  254. }
  255. }
  256. }
  257. static void Flush(int t)
  258. {
  259. if (client) {
  260. client->Stop();
  261. client->Reset();
  262. start_time_ms = t;
  263. client->Start();
  264. }
  265. }
  266. static double GetOutputTimeAsDouble()
  267. {
  268. if (clock) {
  269. UINT64 position;
  270. HRESULT hr = clock->GetPosition(&position, NULL);
  271. double output_time = (double)position * 1000.0 / (double)frequency;
  272. return output_time + start_time_ms;
  273. } else {
  274. return 0;
  275. }
  276. }
  277. static int GetOutputTime()
  278. {
  279. return (int)GetOutputTimeAsDouble();
  280. }
  281. static int GetWrittenTime()
  282. {
  283. double time_in_buffer = (1000.0 * (double)CanWrite()) / ((double)bytes_per_frame * (double)sample_rate);
  284. return (int)(GetOutputTimeAsDouble() + time_in_buffer);
  285. }
  286. Out_Module plugin = {
  287. OUT_VER_U,
  288. 0,
  289. 70,
  290. NULL,
  291. NULL,
  292. Config,
  293. About,
  294. Init,
  295. Quit,
  296. Open,
  297. Close,
  298. Write,
  299. CanWrite,
  300. IsPlaying,
  301. Pause,
  302. SetVolume,
  303. SetPan,
  304. Flush,
  305. GetOutputTime,
  306. GetWrittenTime,
  307. };
  308. extern "C" {
  309. __declspec(dllexport) int __cdecl winampUninstallPlugin(HINSTANCE hDllInst, HWND hwndDlg, int param)
  310. {
  311. return OUT_PLUGIN_UNINSTALL_REBOOT;
  312. }
  313. __declspec(dllexport) Out_Module * __cdecl winampGetOutModule(){ return &plugin; }
  314. __declspec(dllexport) void __cdecl winampGetOutModeChange(int mode)
  315. {
  316. }
  317. }