ReplayGain.cpp 1.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. #include "main.h"
  2. #include "ReplayGain.h"
  3. #include <api/service/waservicefactory.h>
  4. static obj_replaygain *replayGain = 0;
  5. HANDLE rgThread = 0;
  6. static HANDLE killSwitch = 0;
  7. extern HWND m_extract_wnd;
  8. template <class api_T>
  9. void ServiceBuild(api_T *&api_t, GUID factoryGUID_t)
  10. {
  11. if (plugin.service)
  12. {
  13. waServiceFactory *factory = plugin.service->service_getServiceByGuid(factoryGUID_t);
  14. if (factory)
  15. api_t = reinterpret_cast<api_T *>( factory->getInterface() );
  16. }
  17. }
  18. template <class api_T>
  19. void ServiceRelease(api_T *api_t, GUID factoryGUID_t)
  20. {
  21. if (plugin.service && api_t)
  22. {
  23. waServiceFactory *factory = plugin.service->service_getServiceByGuid(factoryGUID_t);
  24. if (factory)
  25. factory->releaseInterface(api_t);
  26. }
  27. api_t = NULL;
  28. }
  29. DWORD WINAPI RGProc(void *data)
  30. {
  31. while (WaitForSingleObjectEx(killSwitch, INFINITE, TRUE) != WAIT_OBJECT_0);
  32. return 0;
  33. }
  34. void CreateGain()
  35. {
  36. killSwitch = CreateEvent(0, FALSE, FALSE, 0);
  37. DWORD dummy;
  38. rgThread = CreateThread(0, 0, RGProc, 0, 0, &dummy);
  39. }
  40. void CALLBACK StartGain(ULONG_PTR data)
  41. {
  42. int mode = (int)data;
  43. ServiceBuild(replayGain, RGGUID);
  44. if (replayGain)
  45. replayGain->Open(mode);
  46. }
  47. void CALLBACK WriteGain(ULONG_PTR data)
  48. {
  49. if (replayGain)
  50. replayGain->Write();
  51. HANDLE notifyHandle =(HANDLE)data;
  52. if (notifyHandle)
  53. SetEvent(notifyHandle);
  54. PostMessage(m_extract_wnd, WM_APP+4, 0, 0);
  55. }
  56. void CALLBACK CalculateGain(ULONG_PTR data)
  57. {
  58. wchar_t *lastfn = (wchar_t *)data;
  59. if (replayGain)
  60. {
  61. PostMessage(m_extract_wnd, WM_APP+2, 0, 0);
  62. replayGain->ProcessTrack(lastfn);
  63. }
  64. free(lastfn);
  65. PostMessage(m_extract_wnd, WM_APP+3, 0, 0);
  66. }
  67. void CALLBACK CloseGain(ULONG_PTR data)
  68. {
  69. if (replayGain)
  70. {
  71. replayGain->Close();
  72. ServiceRelease(replayGain, RGGUID);
  73. replayGain = 0;
  74. }
  75. }
  76. void CALLBACK QuitThread(ULONG_PTR data)
  77. {
  78. if (rgThread)
  79. {
  80. SetEvent(killSwitch);
  81. }
  82. }