#include //int debug = ShowWindow( GetConsoleWindow(), SW_HIDE ); // hide console on startup #include #include #include #include using namespace std; BOOL bMuted; long uMsg = 0x440; long double2long(const double d) { long l = static_cast((static_cast(d * 200.0 + 1.0))) / 2; return l; } class CVolumeNotification : public IAudioEndpointVolumeCallback { LONG m_RefCount; ~CVolumeNotification(void) {}; public: CVolumeNotification(void) : m_RefCount(1) { } STDMETHODIMP_(ULONG)AddRef() { return InterlockedIncrement(&m_RefCount); } STDMETHODIMP_(ULONG)Release() { LONG ref = InterlockedDecrement(&m_RefCount); if (ref == 0) delete this; return ref; } STDMETHODIMP QueryInterface(REFIID IID, void **ReturnValue) { if (IID == IID_IUnknown || IID== __uuidof(IAudioEndpointVolumeCallback)) { *ReturnValue = static_cast(this); AddRef(); return S_OK; } *ReturnValue = NULL; return E_NOINTERFACE; } STDMETHODIMP OnNotify(PAUDIO_VOLUME_NOTIFICATION_DATA NotificationData) { HWND running = FindWindow(_T("w>audiomon"),NULL); if (NotificationData->bMuted != bMuted) //(Un)muted { bMuted = NotificationData->bMuted; if (bMuted) cout << "muted" << endl; else cout << "unmuted" << endl; if (running) { if (bMuted) SendMessage(running, uMsg, 0, 1); else SendMessage(running, uMsg, 0, 0); } } else // Volume changed { int iVol = (int)(double2long(NotificationData->fMasterVolume)); cout << iVol << endl; if (running) { SendMessage(running, uMsg, 1, iVol); } } return S_OK; } }; int _tmain(int argc, _TCHAR* argv[]) { HANDLE running = FindWindow(NULL, _T("snarl-audiomon")); if (running) { SetConsoleTitle(_T("snarl-audiomon-exit")); while (FindWindow(NULL, _T("snarl-audiomon"))) { Sleep(55); } ExitProcess(1); } else { SetConsoleTitle(_T("snarl-audiomon")); } CoInitialize(NULL); HRESULT hr; IMMDeviceEnumerator *deviceEnumerator = NULL;; // // Instantiate an endpoint volume object. // hr = CoCreateInstance(__uuidof(MMDeviceEnumerator), NULL, CLSCTX_INPROC_SERVER, __uuidof(IMMDeviceEnumerator), (LPVOID *)&deviceEnumerator); IMMDevice *defaultDevice = NULL; hr = deviceEnumerator->GetDefaultAudioEndpoint(eRender, eConsole, &defaultDevice); deviceEnumerator->Release(); deviceEnumerator = NULL; IAudioEndpointVolume *endpointVolume = NULL; hr = defaultDevice->Activate(__uuidof(IAudioEndpointVolume), CLSCTX_INPROC_SERVER, NULL, (LPVOID *)&endpointVolume); CVolumeNotification *volumeNotification = new CVolumeNotification(); hr = endpointVolume->RegisterControlChangeNotify(volumeNotification); hr = endpointVolume->GetMute(&bMuted); UINT pnStep, pnStepCount; hr = endpointVolume->GetVolumeStepInfo(&pnStep, &pnStepCount); int iVol = (100/(pnStepCount-1)*pnStep); HWND startup = FindWindow(_T("w>audiomon"),NULL); cout << iVol << endl; SendMessage(startup, uMsg, 1, iVol); if (bMuted) { cout << "muted" << endl; SendMessage(startup, uMsg, 0, 1); } else { cout << "unmuted" << endl; SendMessage(startup, uMsg, 0, 0); } defaultDevice->Release(); defaultDevice = NULL; wchar_t inputChar = '\0'; // while (!(FindWindow(NULL, _T("snarl-audiomon-exit")))) { Sleep(1); } // //while (inputChar != '\r') //{ //DWORD read; //ReadConsole(GetStdHandle(STD_INPUT_HANDLE), &inputChar, 1, &read, NULL); //} // // Remove our notification. // endpointVolume->UnregisterControlChangeNotify(volumeNotification); endpointVolume->Release(); volumeNotification->Release(); CoUninitialize(); return 0; }