-
Notifications
You must be signed in to change notification settings - Fork 13
/
bping
executable file
·82 lines (70 loc) · 2.44 KB
/
bping
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#!/usr/bin/python
import argparse
import math
import os
import re
import subprocess
import sys
from textwrap import dedent, wrap
DEFAULT_VOLUME = 0.05
DEFAULT_DURATION = 0.1
def parse_args():
parser = argparse.ArgumentParser(
description = "\n".join(wrap(dedent('''\
Wrapper around ping to make it do a lot of beeping ;-)
Pitch of beeps represents latency: concert A (440Hz) for
10ms, going one octave up or down for every order of
magnitude.
'''))),
epilog=epilog(),
formatter_class=argparse.RawDescriptionHelpFormatter,
)
parser.add_argument('-v', '--volume', metavar='N', type=float,
help='volume of beep [%(default)s]', default = DEFAULT_VOLUME)
parser.add_argument('-d', '--duration', metavar='SECS', type=float,
help='duration of beep [%(default)s]', default = DEFAULT_DURATION)
if len(sys.argv) < 2:
parser.print_help()
sys.exit(1)
return parser.parse_known_args()
def epilog():
text = 'Guide to conversion of latency to frequency:\n\n'
text += ' %10s %11s\n' % ('Latency', 'Frequency')
text += ' ' + '-' * 19 + '\n'
for latency in (0.01, 0.1, 1, 10, 100, 1000, 10000):
unit = 'ms'
hlatency = latency
if latency >= 1000:
hlatency = latency / 1000
unit = 's'
text += ' %10s %9dHz\n' % (str(hlatency) + unit, frequency(latency))
return text
def frequency(latency):
return 440 * 2 ** (math.log10(latency) - 2)
def bip(frequency, duration, volume):
subprocess.call(
[
'bip',
'--volume', str(volume),
'--frequency', str(frequency),
'--duration', str(duration)
]
)
def main():
options, ping_args = parse_args()
regexp = re.compile('time=(\d+(?:\.\d+)?)\s*(\w+)')
ping = subprocess.Popen(['ping'] + ping_args, stdin=None, stdout=subprocess.PIPE)
while True:
if ping.poll():
break
line = ping.stdout.readline()
print line,
m = regexp.search(line)
if m:
latency, unit = m.groups()
if unit != 'ms':
bip(440, duration=options.duration, volume = options.volume * 2)
raise RuntimeError, "Unrecognised unit: %s" % unit
freq = frequency(float(latency))
bip(freq, options.duration, options.volume)
main()