|
1 | | -# Install as a service |
| 1 | +# AudioMeterEvent |
| 2 | +*Brought to you by [Etienne Dechamps][] - [GitHub][]* |
| 3 | + |
| 4 | +**If you are looking for executables, see the [GitHub releases page][].** |
| 5 | + |
| 6 | +This is a small utility that can monitor the sound level on a Windows audio |
| 7 | +device and react when it crosses a threshold by sending HTTP requests. |
| 8 | + |
| 9 | +Currently it is mainly designed to interact with the [OpenHAB][] [REST API][], |
| 10 | +allowing you to switch an [Item][] on and off based on audio device levels. My |
| 11 | +personal use case is to command "smart" wall plugs to automatically turn on my |
| 12 | +active speakers when sound is playing on my PC. That said, the utility should be |
| 13 | +flexible enough to adapt to other use cases. |
| 14 | + |
| 15 | +## Features |
| 16 | + |
| 17 | + - Sends HTTP requests when detecting sound on a Windows audio device |
| 18 | + - Keeps sending requests at regular intervals for a period of time |
| 19 | + - Sends a special request when… |
| 20 | + - …no sound has been detected for some time |
| 21 | + - …the computer is going into standby |
| 22 | + - …AudioMeterEvent is shutting down |
| 23 | + - Sound level threshold and minimum sound duration can be customized |
| 24 | + - HTTP URI, POST payload and Content-Type can be customized |
| 25 | + - Defaults work well with OpenHAB |
| 26 | + |
| 27 | +## Requirements |
| 28 | + |
| 29 | + - Windows Vista or later |
| 30 | + - .NET Framework 4.8 |
| 31 | + |
| 32 | +## How to use |
| 33 | + |
| 34 | +Run `AudioMeterEvent.exe --help` to see a list of options. The most important |
| 35 | +one is `--audio-device-id` to specify which device AudioMeterEvent should |
| 36 | +monitor. Use `AudioDevicesList.exe` to figure out what your device ID is. |
| 37 | + |
| 38 | +If you don't specify any URI to use for HTTP requests, all AudioMeterEvent does |
| 39 | +is output messages to the console. Use the `--event-uri` option to specify an |
| 40 | +address to send requests to. |
| 41 | + |
| 42 | +### How to interface with OpenHAB |
| 43 | + |
| 44 | +AudioMeterEvent is designed to send an ON request at regular intervals after |
| 45 | +sound is playing, to "keep alive" an OpenHAB item that would otherwise be turned |
| 46 | +off by an [Expire][] trigger. This approach is the most robust because it |
| 47 | +ensures the item will eventually be turned off in case of crashes or |
| 48 | +connectivity issues, or any other case where AudioMeterEvent did not get the |
| 49 | +chance to send an explicit OFF request. |
| 50 | + |
| 51 | +Example items configuration: |
| 52 | + |
| 53 | +``` |
| 54 | +Switch PC_Sounding {expire="1m,state=OFF"} |
| 55 | +``` |
| 56 | + |
| 57 | +Example AudioMeterEvent command line: |
| 58 | + |
| 59 | +``` |
| 60 | +AudioMeterEvent.exe --audio-device-id={…}.{…} --event-uri=http://openhab.example/rest/items/PC_Sounding |
| 61 | +``` |
| 62 | + |
| 63 | +If you have a secure OpenHAB setup using an HTTP username and password, you can |
| 64 | +use the `--http-username` and `--http-password-file` options to provide them. |
| 65 | + |
| 66 | +## How to install as a service |
| 67 | + |
| 68 | +If you plan on using AudioMeterEvent continuously, it is possible (and |
| 69 | +recommended) to run it as a standard Windows Service. |
| 70 | + |
| 71 | +The service can be installed using `InstallUtil.exe` which can be found in a |
| 72 | +subdirectory of `C:\Windows\Microsoft.NET\Framework`. For example: |
2 | 73 |
|
3 | 74 | ``` |
4 | | -C:\Windows\Microsoft.NET\Framework\v4.0.*\InstallUtil.exe --audio-device-id=foo "C:\Path\To\AudioMeterEvent.exe" |
5 | | -``` |
| 75 | +C:\Windows\Microsoft.NET\Framework\v4.0.30319\InstallUtil.exe --audio-device-id={…,…} … "C:\Path\To\AudioMeterEvent.exe" |
| 76 | +``` |
| 77 | + |
| 78 | +**Note:** `InstallUtil.exe` is opinionated about command line parsing - |
| 79 | +`--option=value` will work, but `--option value` won't. |
| 80 | + |
| 81 | +**Note:** for security reasons, the service runs with limited privileges. Make |
| 82 | +sure that your file permissions are set up so that the service can access the |
| 83 | +executable (e.g. move the files to Program Files first). The same applies to |
| 84 | +`--http-password-file`. The service runs under a dedicated |
| 85 | +`NT SERVICE\AudioMeterEvent` user account. |
| 86 | + |
| 87 | +You can then find and start the service under the name "AudioMeterEvent" in the |
| 88 | +Windows services list. |
| 89 | + |
| 90 | +Log messages from the service will appear in the Windows Event Viewer |
| 91 | +(Application). |
| 92 | + |
| 93 | +To uninstall the service, use `InstallUtil.exe` again with the `/u` switch. |
| 94 | + |
| 95 | +## Limitations and caveats |
| 96 | + |
| 97 | +This is an early release. Expect bugs. [Feedback is welcome][feedback]. |
| 98 | + |
| 99 | +I have only tested monitoring output devices. Monitoring input devices (e.g. |
| 100 | +microphones) should work, but I have not tried. |
| 101 | + |
| 102 | +AudioMeterEvent works by continuously polling the sound level on the audio |
| 103 | +device. This necessarily means it uses a tiny bit of CPU in the background. In |
| 104 | +practice, with the default settings the CPU usage seems to be less than 0.5% of |
| 105 | +one logical CPU. |
| 106 | + |
| 107 | +## Developer information |
| 108 | + |
| 109 | +AudioMeterEvent is written in C#. It should build out-of-the-box in Visual |
| 110 | +Studio 2019. |
| 111 | + |
| 112 | +**Note:** in addition to the C# components, some Visual Studio C++ components |
| 113 | +might be required in order to build the WASAPI shim layers. |
| 114 | + |
| 115 | +[Etienne Dechamps]: mailto:etienne@edechamps.fr |
| 116 | +[GitHub]: https://github.com/dechamps/AudioMeterEvent |
| 117 | +[GitHub releases page]: https://github.com/dechamps/AudioMeterEvent/releases |
| 118 | +[OpenHAB]: https://www.openhab.org/ |
| 119 | +[REST API]: https://www.openhab.org/docs/configuration/restdocs.html |
| 120 | +[Item]: https://www.openhab.org/docs/configuration/items.html |
| 121 | +[Expire]: https://www.openhab.org/addons/bindings/expire1/ |
| 122 | +[feedback]: https://github.com/dechamps/AudioMeterEvent/issues |
0 commit comments