Nautical bell: Difference between revisions

Content added Content deleted
(→‎{{header|Perl 6}}: snazz up a bit with Unicode)
(→‎{{header|Perl 6}}: Add Python.)
Line 203: Line 203:
23:00 First watch, Six Bells Gone: ♫ ♫ ♫
23:00 First watch, Six Bells Gone: ♫ ♫ ♫
23:30 First watch, Seven Bells Gone: ♫ ♫ ♫ ♪</pre>
23:30 First watch, Seven Bells Gone: ♫ ♫ ♫ ♪</pre>

=={{header|Python}}==
As well as typing output to stdout, this program plays a sound for each bell as the ␇ characters are printed (The spaces between the ␇ characters are mirrored as varying delays between each ring).
<lang python>import time, calendar, sched, winsound

duration = 750 # Bell duration in ms
freq = 1280 # Bell frequency in hertz
bellchar = "\u2407"
watches = 'Middle,Morning,Forenoon,Afternoon,First/Last dog,First'.split(',')

def gap(n=1):
time.sleep(n * duration / 1000)
off = gap
def on(n=1):
winsound.Beep(freq, n * duration)
def bong():
on(); off(0.5)

def bongs(m):
for i in range(m):
print(bellchar, end=' ')
bong()
if i % 2:
print(' ', end='')
off(0.5)
print('')
scheds = sched.scheduler(time.time, time.sleep)

def ships_bell(now=None):
def adjust_to_half_hour(atime):
atime[4] = (atime[4] // 30) * 30
atime[5] = 0
return atime

debug = now is not None
rightnow = time.gmtime()
if not debug:
now = adjust_to_half_hour( list(rightnow) )
then = now[::]
then[4] += 30
hr, mn = now[3:5]
watch, b = divmod(int(2 * hr + mn // 30 - 1), 8)
b += 1
bells = '%i bell%s' % (b, 's' if b > 1 else ' ')
if debug:
print("%02i:%02i, %-20s %s" % (now[3], now[4], watches[watch] + ' watch', bells), end=' ')
else:
print("%02i:%02i, %-20s %s" % (rightnow[3], rightnow[4], watches[watch] + ' watch', bells), end=' ')
bongs(b)
if not debug:
scheds.enterabs(calendar.timegm(then), 0, ships_bell)
#print(time.struct_time(then))
scheds.run()

def dbg_tester():
for h in range(24):
for m in (0, 30):
if (h,m) == (24,30): break
ships_bell( [2013, 3, 2, h, m, 15, 5, 61, 0] )
if __name__ == '__main__':
ships_bell()</lang>
{{out}}
<pre>00:00, First watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇
00:30, Middle watch 1 bell ␇
01:00, Middle watch 2 bells ␇ ␇
01:30, Middle watch 3 bells ␇ ␇ ␇
02:00, Middle watch 4 bells ␇ ␇ ␇ ␇
02:30, Middle watch 5 bells ␇ ␇ ␇ ␇ ␇
03:00, Middle watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇
03:30, Middle watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇
04:00, Middle watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇
04:30, Morning watch 1 bell ␇
05:00, Morning watch 2 bells ␇ ␇
05:30, Morning watch 3 bells ␇ ␇ ␇
06:00, Morning watch 4 bells ␇ ␇ ␇ ␇
06:30, Morning watch 5 bells ␇ ␇ ␇ ␇ ␇
07:00, Morning watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇
07:30, Morning watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇
08:00, Morning watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇
08:30, Forenoon watch 1 bell ␇
09:00, Forenoon watch 2 bells ␇ ␇
09:30, Forenoon watch 3 bells ␇ ␇ ␇
10:00, Forenoon watch 4 bells ␇ ␇ ␇ ␇
10:30, Forenoon watch 5 bells ␇ ␇ ␇ ␇ ␇
11:00, Forenoon watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇
11:30, Forenoon watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇
12:00, Forenoon watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇
12:30, Afternoon watch 1 bell ␇
13:00, Afternoon watch 2 bells ␇ ␇
13:30, Afternoon watch 3 bells ␇ ␇ ␇
14:00, Afternoon watch 4 bells ␇ ␇ ␇ ␇
14:30, Afternoon watch 5 bells ␇ ␇ ␇ ␇ ␇
15:00, Afternoon watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇
15:30, Afternoon watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇
16:00, Afternoon watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇
16:30, First/Last dog watch 1 bell ␇
17:00, First/Last dog watch 2 bells ␇ ␇
17:30, First/Last dog watch 3 bells ␇ ␇ ␇
18:00, First/Last dog watch 4 bells ␇ ␇ ␇ ␇
18:30, First/Last dog watch 5 bells ␇ ␇ ␇ ␇ ␇
19:00, First/Last dog watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇
19:30, First/Last dog watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇
20:00, First/Last dog watch 8 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ ␇
20:30, First watch 1 bell ␇
21:00, First watch 2 bells ␇ ␇
21:30, First watch 3 bells ␇ ␇ ␇
22:00, First watch 4 bells ␇ ␇ ␇ ␇
22:30, First watch 5 bells ␇ ␇ ␇ ␇ ␇
23:00, First watch 6 bells ␇ ␇ ␇ ␇ ␇ ␇
23:30, First watch 7 bells ␇ ␇ ␇ ␇ ␇ ␇ ␇ </pre>


=={{header|REXX}}==
=={{header|REXX}}==