Metronome: Difference between revisions
Content added Content deleted
SqrtNegInf (talk | contribs) (Added Perl example) |
|||
Line 1: | Line 1: | ||
{{task}}<!--{{task|Temporal media}}--> |
{{task}}<!--{{task|Temporal media}}--> |
||
[[ |
[[ile:Metronome.jpg|420px||right]] |
||
The task is to implement a [https://en.wikipedia.org/wiki/Metronomemetronome metronome]. |
The task is to implement a [https://en.wikipedia.org/wiki/Metronomemetronome metronome]. |
||
Line 697: | Line 697: | ||
wait</lang> |
wait</lang> |
||
=={{header|Perl}}== |
|||
The module <code>Time::HiRes</code> provides sub-second <tt>sleep</tt>. Text output only. |
|||
<lang perl>use Time::HiRes qw(sleep); |
|||
$beats_per_minute = shift || 72; |
|||
$beats_per_bar = shift || 4; |
|||
$duration = 60 / ($beats_per_minute * $beats_per_bar); |
|||
while () { |
|||
if (0 == $i++ % $beats_per_bar) { |
|||
$result .= "\nTICK"; |
|||
} else { |
|||
$result .= " tick"; |
|||
} |
|||
sleep $duration; |
|||
}</lang> |
|||
Sample run |
|||
{{out}} |
|||
<pre>$ metronome 60 6 |
|||
TICK tick tick tick tick tick |
|||
TICK tick tick tick tick tick |
|||
TICK tick tick tick tick tick |
|||
TICK tick tick tick tick tick |
|||
^C |
|||
</pre> |
|||
=={{header|Perl 6}}== |
=={{header|Perl 6}}== |