Metronome: Difference between revisions
Content added Content deleted
Thundergnat (talk | contribs) (Rename Perl 6 -> Raku, alphabetize, minor clean-up) |
|||
Line 40: | Line 40: | ||
end Main;</lang> |
end Main;</lang> |
||
=={{header|AppleScript}}== |
|||
set bpm to the text returned of (display dialog "How many beats per minute?" default answer 60) |
|||
set pauseBetweenBeeps to (60 / bpm) |
|||
repeat |
|||
beep |
|||
delay pauseBetweenBeeps |
|||
end repeat |
|||
=={{header|AutoHotkey}}== |
=={{header|AutoHotkey}}== |
||
Line 74: | Line 87: | ||
MsgBox % "Metronome beeped " beats " beats, over " (A_TickCount-Start)/1000 " seconds. " |
MsgBox % "Metronome beeped " beats " beats, over " (A_TickCount-Start)/1000 " seconds. " |
||
ExitApp</lang> |
ExitApp</lang> |
||
=={{header|AppleScript}}== |
|||
set bpm to the text returned of (display dialog "How many beats per minute?" default answer 60) |
|||
set pauseBetweenBeeps to (60 / bpm) |
|||
repeat |
|||
beep |
|||
delay pauseBetweenBeeps |
|||
end repeat |
|||
=={{header|AWK}}== |
=={{header|AWK}}== |
||
Line 120: | Line 121: | ||
... |
... |
||
</pre> |
</pre> |
||
=={{header|BBC BASIC}}== |
=={{header|BBC BASIC}}== |
||
{{works with|BBC BASIC for Windows}} |
{{works with|BBC BASIC for Windows}} |
||
Line 313: | Line 315: | ||
</lang> |
</lang> |
||
=={{header|F Sharp|F#}}== |
|||
<lang fsharp>open System |
|||
open System.Threading |
|||
// You can use .wav files for your clicks. |
|||
// If used, make sure they are in the same file |
|||
// as this program's executable file. |
|||
let high_pitch = |
|||
new System.Media.SoundPlayer("Ping Hi.wav") |
|||
let low_pitch = |
|||
new System.Media.SoundPlayer("Ping Low.wav") |
|||
let factor x y = x / y |
|||
// Notice that exact bpm would not work by using |
|||
// Thread.Sleep() as there are additional function calls |
|||
// that would consume a miniscule amount of time. |
|||
// This number may need to be adjusted based on the cpu. |
|||
let cpu_error = -750.0 |
|||
let print = function |
|||
| 1 -> high_pitch.Play(); printf "\nTICK " |
|||
| _ -> low_pitch.Play(); printf "tick " |
|||
let wait (time:int) = |
|||
Thread.Sleep(time) |
|||
// Composition of functions |
|||
let tick = float>>factor (60000.0+cpu_error)>>int>>wait |
|||
let rec play beats_per_measure current_beat beats_per_minute = |
|||
match current_beat, beats_per_measure with |
|||
| a, b -> |
|||
current_beat |> print |
|||
beats_per_minute |> tick |
|||
if a <> b then |
|||
beats_per_minute |> play beats_per_measure (current_beat + 1) |
|||
[<EntryPointAttribute>] |
|||
let main (args : string[]) = |
|||
let tempo, beats = int args.[0], int args.[1] |
|||
Seq.initInfinite (fun i -> i + 1) |
|||
|> Seq.iter (fun _ -> tempo |> play beats 1 |> ignore) |
|||
0 </lang> |
|||
Sample run: |
|||
<pre>$ metronome 120 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 |
|||
TICK tick tick^C |
|||
</pre> |
|||
=={{header|Factor}}== |
=={{header|Factor}}== |
||
Line 383: | Line 430: | ||
MAIN: metronome-main</lang> |
MAIN: metronome-main</lang> |
||
=={{header|F Sharp|F#}}== |
|||
<lang fsharp>open System |
|||
open System.Threading |
|||
// You can use .wav files for your clicks. |
|||
// If used, make sure they are in the same file |
|||
// as this program's executable file. |
|||
let high_pitch = |
|||
new System.Media.SoundPlayer("Ping Hi.wav") |
|||
let low_pitch = |
|||
new System.Media.SoundPlayer("Ping Low.wav") |
|||
let factor x y = x / y |
|||
// Notice that exact bpm would not work by using |
|||
// Thread.Sleep() as there are additional function calls |
|||
// that would consume a miniscule amount of time. |
|||
// This number may need to be adjusted based on the cpu. |
|||
let cpu_error = -750.0 |
|||
let print = function |
|||
| 1 -> high_pitch.Play(); printf "\nTICK " |
|||
| _ -> low_pitch.Play(); printf "tick " |
|||
let wait (time:int) = |
|||
Thread.Sleep(time) |
|||
// Composition of functions |
|||
let tick = float>>factor (60000.0+cpu_error)>>int>>wait |
|||
let rec play beats_per_measure current_beat beats_per_minute = |
|||
match current_beat, beats_per_measure with |
|||
| a, b -> |
|||
current_beat |> print |
|||
beats_per_minute |> tick |
|||
if a <> b then |
|||
beats_per_minute |> play beats_per_measure (current_beat + 1) |
|||
[<EntryPointAttribute>] |
|||
let main (args : string[]) = |
|||
let tempo, beats = int args.[0], int args.[1] |
|||
Seq.initInfinite (fun i -> i + 1) |
|||
|> Seq.iter (fun _ -> tempo |> play beats 1 |> ignore) |
|||
0 </lang> |
|||
Sample run: |
|||
<pre>$ metronome 120 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 |
|||
TICK tick tick^C |
|||
</pre> |
|||
=={{header|Go}}== |
=={{header|Go}}== |
||
Line 475: | Line 475: | ||
TICK tick ^C |
TICK tick ^C |
||
</pre> |
</pre> |
||
=={{header|Haskell}}== |
=={{header|Haskell}}== |
||
Line 592: | Line 593: | ||
bpb \ bpm \ |
bpb \ bpm \ |
||
</pre> |
</pre> |
||
=={{header|Java}}== |
=={{header|Java}}== |
||
<lang java> |
<lang java> |
||
Line 787: | Line 789: | ||
TICK tick tick tick tick tick |
TICK tick tick tick tick tick |
||
^C |
^C |
||
</pre> |
|||
=={{header|Perl 6}}== |
|||
This code only uses textual output, but any noise-generating commands may be substituted; as long as they are executed synchronously, and do not run longer than the specified duration, the timing loop will compensate, since the sequence operator is determining a list of absolute times for each <tt>sleep</tt> to target. |
|||
<lang perl6>sub MAIN ($beats-per-minute = 72, $beats-per-bar = 4) { |
|||
my $duration = 60 / $beats-per-minute; |
|||
my $base-time = now + $duration; |
|||
my $i; |
|||
for $base-time, $base-time + $duration ... * -> $next-time { |
|||
if $i++ %% $beats-per-bar { |
|||
print "\nTICK"; |
|||
} |
|||
else { |
|||
print " tick"; |
|||
} |
|||
sleep $next-time - now; |
|||
} |
|||
}</lang> |
|||
Sample run: |
|||
<pre>$ metronome 120 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 |
|||
TICK tick tick^C |
|||
</pre> |
</pre> |
||
Line 869: | Line 844: | ||
/ |
/ |
||
-> NIL # A key was hit</lang> |
-> NIL # A key was hit</lang> |
||
=={{header|Pure Data}}== |
|||
<pre> |
|||
#N canvas 553 78 360 608 10; |
|||
#X obj 20 20 cnv 15 320 140 empty empty empty 20 12 0 14 -228856 -66577 0; |
|||
#X obj 20 190 cnv 15 320 36 empty empty empty 20 12 0 14 -233017 -66577 0; |
|||
#X obj 67 30 vradio 20 1 0 6 empty beats empty 0 -8 0 10 -86277 -262144 -1 1; |
|||
#X text 40 33 1/1; |
|||
#X text 40 53 2/2; |
|||
#X text 40 73 3/4; |
|||
#X text 40 93 4/4; |
|||
#X text 40 133 6/8; |
|||
#X obj 67 167 + 1; |
|||
#X floatatom 67 201 5 0 0 0 beats - -; |
|||
#X obj 181 32 vsl 20 115 208 40 0 0 empty bpm empty 25 10 0 10 -86277 -262144 -1 5971 0; |
|||
#X text 208 42 Larghetto 60-66; |
|||
#X text 208 58 Adagio 66-76; |
|||
#X text 208 74 Andante 76-108; |
|||
#X text 208 90 Moderato 108-120; |
|||
#X text 208 106 Allegro 120-168; |
|||
#X text 208 122 Presto 168-200; |
|||
#X text 208 138 Prestissimo 200-208; |
|||
#X text 208 26 Largo 40-60; |
|||
#X obj 181 167 int; |
|||
#X floatatom 181 201 5 0 0 1 bpm - -; |
|||
#X obj 149 246 expr 1000 / ($f1/60); |
|||
#X obj 122 125 tgl 25 0 empty on on/off -4 -7 0 10 -261682 -86277 -86277 0 1; |
|||
#X obj 122 270 metro; |
|||
#X obj 122 291 int; |
|||
#X obj 42 249 + 1; |
|||
#X obj 52 275 mod; |
|||
#X obj 122 312 moses 1; |
|||
#X obj 122 347 bng 32 250 50 0 empty empty empty 17 7 0 10 -228856 -258113 -1; |
|||
#X obj 161 347 bng 32 250 50 0 empty empty empty 17 7 0 10 -228856 -260097 -1; |
|||
#X msg 81 399 1 2 \, 1 2 1 \, 0 3 2; |
|||
#X obj 81 420 vline~; |
|||
#X msg 200 399 1 2 \, 1 2 1 \, 0 3 2; |
|||
#X obj 200 420 vline~; |
|||
#X obj 20 420 osc~ 1400; |
|||
#X obj 139 420 osc~ 1230; |
|||
#X obj 65 455 *~; |
|||
#X obj 184 455 *~; |
|||
#X obj 116 559 dac~; |
|||
#X obj 117 523 +~; |
|||
#X obj 278 490 loadbang; |
|||
#X msg 278 511 \; pd dsp 1 \; beats 1 \; bpm 120 \; on 1; |
|||
#X connect 2 0 8 0; |
|||
#X connect 8 0 9 0; |
|||
#X connect 9 0 26 1; |
|||
#X connect 10 0 19 0; |
|||
#X connect 19 0 20 0; |
|||
#X connect 20 0 21 0; |
|||
#X connect 21 0 23 1; |
|||
#X connect 22 0 23 0; |
|||
#X connect 23 0 24 0; |
|||
#X connect 24 0 25 0; |
|||
#X connect 24 0 27 0; |
|||
#X connect 25 0 26 0; |
|||
#X connect 26 0 24 1; |
|||
#X connect 27 0 28 0; |
|||
#X connect 27 1 29 0; |
|||
#X connect 28 0 30 0; |
|||
#X connect 29 0 32 0; |
|||
#X connect 30 0 31 0; |
|||
#X connect 31 0 36 1; |
|||
#X connect 32 0 33 0; |
|||
#X connect 33 0 37 1; |
|||
#X connect 34 0 36 0; |
|||
#X connect 35 0 37 0; |
|||
#X connect 36 0 39 0; |
|||
#X connect 37 0 39 1; |
|||
#X connect 39 0 38 0; |
|||
#X connect 39 0 38 1; |
|||
#X connect 40 0 41 0; |
|||
</pre> |
|||
=={{header|PureBasic}}== |
=={{header|PureBasic}}== |
||
Line 1,204: | Line 1,254: | ||
eClick: |
eClick: |
||
EndDataSection</lang> |
EndDataSection</lang> |
||
=={{header|Pure Data}}== |
|||
<pre> |
|||
#N canvas 553 78 360 608 10; |
|||
#X obj 20 20 cnv 15 320 140 empty empty empty 20 12 0 14 -228856 -66577 0; |
|||
#X obj 20 190 cnv 15 320 36 empty empty empty 20 12 0 14 -233017 -66577 0; |
|||
#X obj 67 30 vradio 20 1 0 6 empty beats empty 0 -8 0 10 -86277 -262144 -1 1; |
|||
#X text 40 33 1/1; |
|||
#X text 40 53 2/2; |
|||
#X text 40 73 3/4; |
|||
#X text 40 93 4/4; |
|||
#X text 40 133 6/8; |
|||
#X obj 67 167 + 1; |
|||
#X floatatom 67 201 5 0 0 0 beats - -; |
|||
#X obj 181 32 vsl 20 115 208 40 0 0 empty bpm empty 25 10 0 10 -86277 -262144 -1 5971 0; |
|||
#X text 208 42 Larghetto 60-66; |
|||
#X text 208 58 Adagio 66-76; |
|||
#X text 208 74 Andante 76-108; |
|||
#X text 208 90 Moderato 108-120; |
|||
#X text 208 106 Allegro 120-168; |
|||
#X text 208 122 Presto 168-200; |
|||
#X text 208 138 Prestissimo 200-208; |
|||
#X text 208 26 Largo 40-60; |
|||
#X obj 181 167 int; |
|||
#X floatatom 181 201 5 0 0 1 bpm - -; |
|||
#X obj 149 246 expr 1000 / ($f1/60); |
|||
#X obj 122 125 tgl 25 0 empty on on/off -4 -7 0 10 -261682 -86277 -86277 0 1; |
|||
#X obj 122 270 metro; |
|||
#X obj 122 291 int; |
|||
#X obj 42 249 + 1; |
|||
#X obj 52 275 mod; |
|||
#X obj 122 312 moses 1; |
|||
#X obj 122 347 bng 32 250 50 0 empty empty empty 17 7 0 10 -228856 -258113 -1; |
|||
#X obj 161 347 bng 32 250 50 0 empty empty empty 17 7 0 10 -228856 -260097 -1; |
|||
#X msg 81 399 1 2 \, 1 2 1 \, 0 3 2; |
|||
#X obj 81 420 vline~; |
|||
#X msg 200 399 1 2 \, 1 2 1 \, 0 3 2; |
|||
#X obj 200 420 vline~; |
|||
#X obj 20 420 osc~ 1400; |
|||
#X obj 139 420 osc~ 1230; |
|||
#X obj 65 455 *~; |
|||
#X obj 184 455 *~; |
|||
#X obj 116 559 dac~; |
|||
#X obj 117 523 +~; |
|||
#X obj 278 490 loadbang; |
|||
#X msg 278 511 \; pd dsp 1 \; beats 1 \; bpm 120 \; on 1; |
|||
#X connect 2 0 8 0; |
|||
#X connect 8 0 9 0; |
|||
#X connect 9 0 26 1; |
|||
#X connect 10 0 19 0; |
|||
#X connect 19 0 20 0; |
|||
#X connect 20 0 21 0; |
|||
#X connect 21 0 23 1; |
|||
#X connect 22 0 23 0; |
|||
#X connect 23 0 24 0; |
|||
#X connect 24 0 25 0; |
|||
#X connect 24 0 27 0; |
|||
#X connect 25 0 26 0; |
|||
#X connect 26 0 24 1; |
|||
#X connect 27 0 28 0; |
|||
#X connect 27 1 29 0; |
|||
#X connect 28 0 30 0; |
|||
#X connect 29 0 32 0; |
|||
#X connect 30 0 31 0; |
|||
#X connect 31 0 36 1; |
|||
#X connect 32 0 33 0; |
|||
#X connect 33 0 37 1; |
|||
#X connect 34 0 36 0; |
|||
#X connect 35 0 37 0; |
|||
#X connect 36 0 39 0; |
|||
#X connect 37 0 39 1; |
|||
#X connect 39 0 38 0; |
|||
#X connect 39 0 38 1; |
|||
#X connect 40 0 41 0; |
|||
</pre> |
|||
=={{header|Python}}== |
=={{header|Python}}== |
||
Line 1,346: | Line 1,321: | ||
(void (thread flip)) |
(void (thread flip)) |
||
</lang> |
</lang> |
||
=={{header|Raku}}== |
|||
(formerly Perl 6) |
|||
This code only uses textual output, but any noise-generating commands may be substituted; as long as they are executed synchronously, and do not run longer than the specified duration, the timing loop will compensate, since the sequence operator is determining a list of absolute times for each <tt>sleep</tt> to target. |
|||
<lang perl6>sub MAIN ($beats-per-minute = 72, $beats-per-bar = 4) { |
|||
my $duration = 60 / $beats-per-minute; |
|||
my $base-time = now + $duration; |
|||
my $i; |
|||
for $base-time, $base-time + $duration ... * -> $next-time { |
|||
if $i++ %% $beats-per-bar { |
|||
print "\nTICK"; |
|||
} |
|||
else { |
|||
print " tick"; |
|||
} |
|||
sleep $next-time - now; |
|||
} |
|||
}</lang> |
|||
Sample run: |
|||
<pre>$ metronome 120 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 |
|||
TICK tick tick^C |
|||
</pre> |
|||
=={{header|REXX}}== |
=={{header|REXX}}== |