Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds audible ping feature (optional dependency) #34

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
------------
Expand Down
44 changes: 39 additions & 5 deletions prettyping
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -61,10 +59,13 @@ prettyping parameters:
--rttmax <n> Maximum RTT represented in the unicode graph. (default: auto)
--awkbin <exec> Override the awk interpreter. (default: awk)
--pingbin <exec> Override the ping tool. (default: ping)
--playbin <exec> 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.
Expand All @@ -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
Expand All @@ -101,6 +103,8 @@ parse_arguments() {
AWK_BIN="awk"
AWK_PARAMS=( )

PLAY_BIN=""

while [[ $# != 0 ]] ; do
case "$1" in
-h | -help | --help )
Expand Down Expand Up @@ -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 ;;
Expand All @@ -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.
Expand Down Expand Up @@ -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) {
Expand Down