Metronome: Difference between revisions

1,394 bytes added ,  7 years ago
Line 461:
 
=={{header|J}}==
The explicit version (nbars,t) MET (barlengths;bpm) prints a bell character every beat, accompanied by a sequence of slashes, spaces, and backspaces to create a little animation. It includes a beat hand and a measure (or bar) hand.<br>
MET can take several barlengths and bpm values, in which case it will cycle through them individually at each measure, creating (perhaps) interesting patterns. It will stop when nbars measures have been cycled through, or at the end of the current measure if the time limit is exceeded. The clock is self correcting.<br>
MET returns the total number of measures, beats, and elapsed time.<br>
If you leave out the left arguments, it will set them to infinity, so you can go insane without worrying about the metronome ever stopping.
<lang j>
MET=: _ _&$: :(4 : 0)
 
'BEL BS LF CR'=. 7 8 10 13 { a.
'`print stime delay'=. 1!:2&4`(6!:1)`(6!:3)
ticker=. 2 2$'\ /'
'small large'=. (BEL,2#BS) ; 5#BS
clrln=: CR,(79#' '),CR
 
x=. 2 ({.,) x
y=. _1 |.&.> 2 ({.,) y
'i j'=. 0
print 'bpb \ bpm \ ' , 2#BS
delay 1
 
x=. ({. , ('ti t'=. stime'') + {:) x
while. x *./@:> i,t do.
 
'bpb bpm'=. {.@> y=. 1 |.&.> y
dl=. 60 % bpm
 
print clrln,(":bpb),' ',(ticker {~ 2 | i=. >: i),' ',(":bpm),' '
 
for. i. bpb do.
print small ,~ ticker {~ 2 | j=. >: j
delay 0 >. (t=. t + dl) - stime ''
end.
 
end.
 
print clrln
i , j , t - ti
 
)
 
 
NB. Basic tacit version; this is probably considered bad coding style. At least I removed the "magic constants". Sort of.
NB. The above version is by far superior.
'BEL BS LF'=: 7 8 10 { a.
'`print delay'=: 1!:2&4`(6!:3)
Line 469 ⟶ 509:
{{out}}
<pre>
16 60 MET 4;120
NB. (t met f) prints a bell character followed by a little "animation"
4 / 120 /
NB. at f beats per minute for t seconds. It returns the total number
 
NB. of beats.
NB. Variable measure lengths, and corresponding bpm:
10 met 60
21 _ MET 4 3 4 5 ; 120 100 120 150 NB. _ is infinity.
\
5 \ 150 /
10
NB. returns: 21 84 39.2 (21 measures, 84 beats, 39.2 seconds)
 
MET 4 8;120 240 NB. It can almost make music!
7.5 met 120
bpb \ bpm \
/
15
NB. Monadic case sets t to infinity, so you can go insane without
NB. worrying about the metronome ever stopping.
met 42
\ . . .
</pre>
 
Anonymous user