{"id":2993,"date":"2018-09-03T18:37:17","date_gmt":"2018-09-03T13:07:17","guid":{"rendered":"https:\/\/www.suramya.com\/blog\/?p=2993"},"modified":"2022-06-16T15:14:47","modified_gmt":"2022-06-16T09:44:47","slug":"software-hack-to-keep-my-speaker-powered-on","status":"publish","type":"post","link":"https:\/\/www.suramya.com\/blog\/2018\/09\/software-hack-to-keep-my-speaker-powered-on\/","title":{"rendered":"Software hack to keep my speaker powered on"},"content":{"rendered":"<p>A little while ago I bought a new klipsch speaker as my previous one was starting to die and I love it except for a minor irritation. The speaker has builtin power saving tech that powers it off if its not used for a certain period of time and that means that I have to physically power it on every time I wanted to listen to music which was annoying. As I would invariably be comfortably seated and start the music before remembering that I needed to power it on. Also, I could not start the music from my phone whenever I felt like as the speaker was powered off and I would have to walk to the room to power it on.<\/p>\n<p>After living with the irritation for a while I finally decided to do something about it and whipped up a small script that checks if any music\/audio is already playing on the system and if not it plays a 1 second mp3 of an ultrasonic beep. This forces the system to keep the speaker on and I love it as now I can start the music first thing in the morning while lazing in bed. \ud83d\ude42<\/p>\n<p>The script requires the <i>mpg123<\/i> to be installed and you can install it on a Debian system by issuing the following command:<\/p>\n<pre class='code'>apt-get install mpg123<\/pre>\n<p>The Script itself is only 4 lines long:<\/p>\n<pre class='code'>#!\/bin\/bash\r\n\r\nif ! grep RUNNING \/proc\/asound\/card*\/pcm*\/sub*\/status &> \/dev\/null ; then\r\n    \/usr\/bin\/mpg123 -q \/home\/suramya\/bin\/KeepSpeakerOn.mp3 &> \/dev\/null\r\nfi<\/pre>\n<p>What it does is to check if any of the PCM soundcards have a status of RUNNING and if not it plays the mp3. I have a cron job scheduled to run the script every one min:<\/p>\n<pre class='code'>XDG_RUNTIME_DIR=\/run\/user\/1000\r\n\r\n* * * * * \/home\/suramya\/bin\/KeepSpeakerOn.sh <\/pre>\n<p>One interesting issue I hit during the initial testing was that the mpg123 application kept segfaulting whenever I initiated it from the Cron but it would work fine if I ran the same command from the command prompt. The error I got in the logs was:<\/p>\n<pre class='code'>High Performance MPEG 1.0\/2.0\/2.5 Audio Player for Layers 1, 2 and 3\r\n        version 1.25.10; written and copyright by Michael Hipp and others\r\n        free software (LGPL) without any warranty but with best wishes\r\nCannot connect to server socket err = No such file or directory\r\nCannot connect to server request channel\r\njack server is not running or cannot be started\r\nJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock\r\nJackShmReadWritePtr::~JackShmReadWritePtr - Init not done for -1, skipping unlock\r\n\/home\/suramya\/bin\/KeepSpeakerOn.sh: line 5: 10993 Segmentation fault      \/usr\/bin\/mpg123 \/home\/suramya\/bin\/KeepSpeakerOn.mp3 -v<\/pre>\n<p>Spent a while trying to debug and finally figured out that the fix for this issue was to add <i>XDG_RUNTIME_DIR=\/run\/user\/&lt;userid><\/i> to the cron where you can get the value of &lt;userid> by running the following command and taking the value of uid:<\/p>\n<pre class='code'>id &lt;username_the_cronjob_is_running_under> <\/pre>\n<p>e.g.<\/p>\n<pre class='code'>suramya@StarKnight:~\/bin$ id suramya\r\nuid=1000(suramya) gid=1000(suramya) groups=1000(suramya),24(cdrom)....<\/pre>\n<p>Putting that line in the cron entry resolved the issue. Not sure why but it works so&#8230; <\/p>\n<p>Well this is all for now. Will write more later.<\/p>\n<p>&#8211; Suramya<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A little while ago I bought a new klipsch speaker as my previous one was starting to die and I love it except for a minor irritation. The speaker has builtin power saving tech that powers it off if its not used for a certain period of time and that means that I have to [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"activitypub_content_warning":"","activitypub_content_visibility":"","activitypub_max_image_attachments":3,"activitypub_interaction_policy_quote":"anyone","activitypub_status":"","footnotes":""},"categories":[21,13,2,14],"tags":[],"class_list":["post-2993","post","type-post","status-publish","format-standard","hentry","category-computer-hardware","category-linuxunix-related","category-techie-stuff","category-tutorials"],"_links":{"self":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/2993","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/comments?post=2993"}],"version-history":[{"count":4,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/2993\/revisions"}],"predecessor-version":[{"id":2997,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/posts\/2993\/revisions\/2997"}],"wp:attachment":[{"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/media?parent=2993"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/categories?post=2993"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.suramya.com\/blog\/wp-json\/wp\/v2\/tags?post=2993"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}