Metronome: Difference between revisions

Content added Content deleted
(→‎{{header|Go}}: Add simple Go example)
Line 222: Line 222:
MAIN: metronome-main</lang>
MAIN: metronome-main</lang>


=={{header|Go}}==
As with the [[#Perl_6|Perl example]], just simple text output.
It would be reasonably simple (but covered better in other tasks)
to change bpm and bpb into command line arguments,
make it a function/object,
and/or substitute sound production instead of text output.

<code>time.Ticker</code>'s documentation says that it
"adjusts the intervals or drops ticks to make up for slow receivers".
So, as long as the output or sound production finishes before the next tick,
the timing will be reliable and will not drift which is the gist of this task.
<lang go>package main

import (
"fmt"
"time"
)

func main() {
var bpm = 72.0 // Beats Per Minute
var bpb = 4 // Beats Per Bar

d := time.Duration(float64(time.Minute) / bpm)
fmt.Println("Delay:", d)
t := time.NewTicker(d)
i := 1
for _ = range t.C {
i--
if i == 0 {
i = bpb
fmt.Printf("\nTICK ")
} else {
fmt.Printf("tick ")
}
}
}</lang>
{{out}}
<pre>
Delay: 833.333333ms

TICK tick tick tick
TICK tick tick tick
TICK tick ^C
</pre>
=={{header|Haskell}}==
=={{header|Haskell}}==