Cookie Notice

As far as I know, and as far as I remember, nothing in this page does anything with Cookies.

2017/01/20

Ding! Ding! The Process is Dead!

Starts with a thing I saw on David Walsh's Blog:
I've been working with beefy virtual machines, docker containers, and build processes lately. Believe it or not, working on projects aimed at making Mozilla developers more productive can mean executing code that can take anywhere from a minute to an hour, which in itself can hit how productive I can be. For the longer tasks, I often get away from my desk, make a cup of coffee, and check in to see how the rest of the Walsh clan is doing.

When I walk away, however, it would be nice to know when the task is done, so I can jet back to my desk and get back to work. My awesome Mozilla colleague Byron "glob" Jones recently showed me his script for task completion notification and I forced him to put it up on GitHub so you all can get it too; it's called ding!
OK, that sounds cool. So I go to Github and I see one line that gives me pause.

Requires ding.mp3 and error.mp3 in same directory as script. OSX only.

I can handle the image thing, but I don't own or run an OSX computer. (I have one somewhere, but it's ancient and has no functioning battery. I don't use it.)

"So," I think, "how could I do this on my Linux box? What's the shortest path toward functionality on this concept?"

Well, recently, I have been playing with Text-to-Speech. Actually, I have been a long-time user of TTS, using festival then espeak to tell me the current time and temperature on the hour and half-hour. I switched to Amazon's Polly in December, deciding that the service sounded much better than the on-my-computer choices. (Hear for yourself.) So, I knew how to handle the audio aspects.

The other part required me to get much more familiar with Perl's system function than I had been previously.

#!/usr/bin/env perl
# perl implementation of ding
# https://github.com/globau/ding/
# globau licensed his MIT, so this is too
# requires mpg321 for audio playback.
# to install on Ubuntu,
# sudo apt install mpg321
use feature qw{ say state } ;
use strict ;
use warnings ;
use utf8 ;
use DateTime ;
use DateTime::Duration ;
my $sound = join '/', $ENV{HOME}, 'sounds' ;
my $ding = join '/', $sound, 'ding.mp3' ;
my $error = join '/', $sound, 'error.mp3' ;
if ( scalar @ARGV ) {
my $start = DateTime->now() ;
if ( system(@ARGV) == 0 ) {
qx{mpg123 -q $ding} ;
}
else {
qx{mpg123 -q $error} ;
}
my $end = DateTime->now() ;
my $duration = $end->subtract_datetime_absolute($start) ;
my $seconds = $duration->delta_seconds();
say qq{process took $seconds seconds};
}
else {
qx{mpg123 -q $ding} ;
}
__DATA__
As I understand ding
if there's something in ARGV
get starting timestamp
try
sound = error
syscall ARGV
sound = ding
catch
if error, print error
get ending timestamp
find duration between both
if duration > 1 seconds
print elaptsed time
else
play ding
MIT License
Copyright (c) 2017 Dave Jacoby
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
view raw ding.pl hosted with ❤ by GitHub

I'm not yet 100% happy with this code, but I'm reasonably okay with it so far. Certainly the concept has been proven. (I use the audio files from globau's ding.) With enough interest, I will switch it from being a GitHub gist to being a repo.