From 79c905749a82e50317add5f3411cf52967cb21c8 Mon Sep 17 00:00:00 2001 From: Manuel Sgobino Date: Wed, 16 Nov 2022 17:54:40 +0100 Subject: [PATCH] adds audible ping feature This relies on the 'play' executable from the 'sox' package. If it is not found in your 'path' variable (when using the '-a' option), please provide the full path with the '-playbin' option. The sound output is based on this tweet by @climagic: https://twitter.com/climagic/status/1592725846783328256 --- README.md | 1 + prettyping | 44 +++++++++++++++++++++++++++++++++++++++----- 2 files changed, 40 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 27d5493..0cf5fd6 100644 --- a/README.md +++ b/README.md @@ -27,6 +27,7 @@ Requirements * `ping` (from `iputils`, or any other version that prints essentially the same output, like Mac OS X ping or [oping][]) * Optional dependency on `stty` or `tput` to auto-detect the terminal size. +* Optional dependency on `play` from the `sox` package to play audible pings. Installation ------------ diff --git a/prettyping b/prettyping index 8a22efd..794c257 100755 --- a/prettyping +++ b/prettyping @@ -30,8 +30,6 @@ # # TODO: Print the current time in the beginning of each line. # -# TODO: Implement audible ping. -# # TODO: Autodetect the width of printf numbers, so they will always line up correctly. # # TODO: Test the behavior of this script upon receiving out-of-order packets, like these: @@ -61,10 +59,13 @@ prettyping parameters: --rttmax Maximum RTT represented in the unicode graph. (default: auto) --awkbin Override the awk interpreter. (default: awk) --pingbin Override the ping tool. (default: ping) + --playbin Provide full path to the 'play' executable and enable audible pings -6 Shortcut for: --pingbin ping6 + -a Play audible ping, frequency based on packet latency + /!\\ requires the "play" executable from the sox package! + Will check for 'play' in 'path' and use it if found. ping parameters handled by prettyping: - -a Audible ping is not implemented yet. -f Flood mode is not allowed in prettyping. -q Quiet output is not allowed in prettyping. -R Record route mode is not allowed in prettyping. @@ -81,6 +82,7 @@ parse_arguments() { USE_MULTICOLOR=1 USE_UNICODE=1 USE_LEGEND=1 + USE_SOUND=0 if [ -t 1 ]; then IS_TERMINAL=1 @@ -101,6 +103,8 @@ parse_arguments() { AWK_BIN="awk" AWK_PARAMS=( ) + PLAY_BIN="" + while [[ $# != 0 ]] ; do case "$1" in -h | -help | --help ) @@ -133,8 +137,18 @@ parse_arguments() { # New parameters: -a ) - # TODO: Implement audible ping for responses or for missing packets - ;; + PLAY_IN_PATH=`which play` + if [ "$?" == "0" ] + then + USE_SOUND=1 + PLAY_BIN="${PLAY_IN_PATH}" + else + echo "Audible pings could not be enabled, 'play' executable not found." + echo "Check if the 'sox' package is installed and the user is able to use it" + echo "If 'play' is not found in the 'path' variable, use the '-playbin' option" + echo "to specify the full path to the executable (and enable audio as well)." + fi + ;; -color | --color ) USE_COLOR=1 ;; -nocolor | --nocolor ) USE_COLOR=0 ;; @@ -149,6 +163,16 @@ parse_arguments() { -awkbin | --awkbin ) AWK_BIN="$2" ; shift ;; -pingbin | --pingbin ) PING_BIN="$2" ; shift ;; + -playbin ) + PLAY_BIN="$2" + if [ -f "${PLAY_BIN}" ] + then + USE_SOUND=1 + else + echo "Could not find the file you specified for '-playbin', sound is disabled." + fi + shift + ;; -6 ) PING_BIN="ping6" ;; #TODO: Check if these parameters are numbers. @@ -464,6 +488,16 @@ function print_received_response(rtt, block_index) { } printf( BLOCK[block_index] ) CURR_COL++ + play_sound( rtt ) +} + +function play_sound(rtt, frq, CMD) { + if ( ! '"${USE_SOUND}"' ) { + return + } + frq = 3000 - 14 * log( rtt^20 ) + CMD = "'${PLAY_BIN}' -V0 -q -n synth 1 pl " frq " vol 1 reverb -w &" + system( CMD ) } function print_missing_response(rtt) {