blib/lib/Music/Tempo.pm

filter: Statementstmt 100% 31 / 31 Branchbran 57.1% 8 / 14 Conditioncond 69.2% 9 / 13 MC/DCmcdc 50.0% 4 / 8 Subroutinesub 100% 8 / 8 Podpod 100% 4 / 4 totaltotal 82.0% 64 / 78 CC 14 SCAR 14.9
CC
14
Cov
83%
bpm_to_italian
7.3
bpm_to_ms
4.3
ms_to_bpm
4.3
CRAP 4.5
↑ Summary
1 package Music::Tempo;
2 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3
4 3 ▶BEGIN {
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶ use Exporter ();
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 ▶ use vars qw ($VERSION @ISA @EXPORT);
Subroutine BEGIN: called CC=1 SCAR=0.0
7 3 $VERSION = 0.02;
8 3 @ISA = qw (Exporter);
9 3 @EXPORT = qw (bpm_to_italian italian_to_bpm bpm_to_ms ms_to_bpm);
10 }
11
12 =head1 NAME
13
14 Tempo - various conversions to and from BPM
15
16 =head1 SYNOPSIS
17
18 use Music::Tempo;
19
20 my $marking = bpm_to_italian(50); # 'Largo'
21 my $bpm = italian_to_bpm('Allegro'); # 120
22 my $ms = bpm_to_ms(100); # 600
23 my $bpm = ms_to_bpm(200,8); # 120
24
25
26 =head1 DESCRIPTION
27
28 Includes two main functions, converting BPM (Beats Per Minute) to and from ms (milliseconds) and Italian metonome markings.
29
30 =head1 METHODS
31
32 =head2 bpm_to_italian($bpm)
33
34 Takes a BPM marking, and returns an appropriate Italian metronome marking (Lento, Allegro etc. - see below for full list).
35
36 =head2 italian_to_bpm($marking)
37
38 Takes an Italian metronome marking (Lento, Allegro, Presto etc.) and returns an *average* BPM.
39
40 =head2 bpm_to_ms($bpm,$beat)
41
42 Converts from BPM to ms. The 'beat' parameter (which defaults to 4) acts as an extra divisor.
43 For instance, 120 BPM would normally mean 1 crotchet (or 1/4 note) =500ms.
44 Passing a beat of '16' would return 125ms, referring to semiquavers (or 1/16 notes).
45
46 =head2 ms_to_bpm($ms,$beat)
47
48 The reverse of bpm_to_ms.
49
50 =head1 TEMPI
51
52 The italian tempi are of course approximations. The ranges below have been greatly reduced, and are
53 presented only as a 'last resort' for automatic machine translation etc. They're roughly based on an
54 average between Maetzel and Quantz, tweaked for 'standard modern' usage (Allegro=120 etc.).
55
56 Largo 40-59
57 Larghetto 60-66
58 Adagio 67-72
59 Lento 73-78
60 Andante 79-88
61 Moderato 89-109
62 Allegro 110-129
63 Vivace 130-149
64 Presto 150-190
65 Prestissimo 190-220
66
67 =head1 TODO
68
69 =head1 AUTHOR
70
71 Ben Daglish (bdaglish@surfnet-ds.co.uk)
72
73 =head1 BUGS
74
75 None known
76 All feedback most welcome.
77
78 =head1 COPYRIGHT
79
80 Copyright (c) 2003, Ben Daglish. All Rights Reserved.
81 This program is free software; you can redistribute
82 it and/or modify it under the same terms as Perl itself.
83
84 The full text of the license can be found in the
85 LICENSE file included with this module.
86
87 =head1 SEE ALSO
88
89 perl(1).
90
91 =cut
92
93
94 3 my %tempi = (
95 'LARGO' => [40,59],
96 'LARGHETTO' => [60,66],
97 'ADAGIO' => [67,72],
98 'LENTO' => [73,78],
99 'ANDANTE' => [79,88],
100 'MODERATO' => [89,109],
101 'ALLEGRO' => [110,129],
102 'VIVACE' => [130,149],
103 'PRESTO' => [150,190],
104 'PRESTISSIMO' => [190,220],
105 );
106
107
108 2 ▶sub italian_to_bpm {
Subroutine italian_to_bpm: called CC=2 SCAR=7.1
Pod documented
109 2 my $name = uc(shift());
110 2 ▶ return unless defined $tempi{$name};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $tempi{$name} 0 2 2
111 2 return int(($tempi{$name}[0] + $tempi{$name}[1])/2) + 1;
112 }
113 2 ▶sub bpm_to_italian {
Subroutine bpm_to_italian: called CC=7 SCAR=19.8
Pod documented
114 2 my $bpm = shift();
115 2 ▶ return unless int($bpm);
Branch: 1 branch
BranchTrueFalseTotal
unless int $bpm 0 2 2
116 2 ▶ return "Too Slow" if $bpm < 40;
Branch: 1 branch
BranchTrueFalseTotal
if $bpm < 40 0 2 2
117 2 ▶ return "Too Fast" if $bpm > 220;
Branch: 1 branch
BranchTrueFalseTotal
if $bpm > 220 0 2 2
118 2 foreach (keys %tempi) {
119 12 ▶ return ucfirst(lc($_)) if ($tempi{$_}[0] <= $bpm && $bpm <= $tempi{$_}[1]);
Branch: 1 branch
BranchTrueFalseTotal
if $tempi{$_}[0] <= $bpm and $bpm <= $tempi{$_}[1] 2 10 12
Condition100%3/3
$tempi{$_}[0] <= $bpm and $bpm <= $tempi{$_}[1]
!ll&&!rl&&r
462
MC/DC100%2/2
$tempi{$_}[0] <= $bpm and $bpm <= $tempi{$_}[1]
$tempi{$_}[0] <= $bpm $bpm <= $tempi{$_}[1]
Truth table100%3/3
A and B
A= $tempi{$_}[0] <= $bpmB= $bpm <= $tempi{$_}[1]
ABresult
0X0
100
111
120 }
121 }
122
123 2 ▶sub bpm_to_ms {
Subroutine bpm_to_ms: called CC=4 SCAR=14.6
Pod documented
124 2 my $bpm = shift;
125 2 ▶ my $beat = shift || 4;
Condition100%2/2
shift() || 4
l!l
11
MC/DC100%1/1
shift() || 4
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
126 2 ▶ return unless (int($bpm) && int($beat));
Branch: 1 branch
BranchTrueFalseTotal
unless int $bpm and int $beat 0 2 2
Condition33%1/3
int $bpm and int $beat
!ll&&!rl&&r
002
MC/DC0%0/2
int $bpm and int $beat
int $bpm int $beat
Truth table33%1/3
A and B
A= int $bpmB= int $beat
ABresult
0X0
100
111
127 2 return 240000 / ($bpm * $beat);
128 }
129 2 ▶sub ms_to_bpm {
Subroutine ms_to_bpm: called CC=4 SCAR=14.6
Pod documented
130 2 my $ms = shift;
131 2 ▶ my $beat = shift || 4;
Condition100%2/2
shift() || 4
l!l
11
MC/DC100%1/1
shift() || 4
shift()
Truth table100%2/2
A
A= shift()
Aresult
11
00
132 2 ▶ return unless (int($ms) && int($beat));
Branch: 1 branch
BranchTrueFalseTotal
unless int $ms and int $beat 0 2 2
Condition33%1/3
int $ms and int $beat
!ll&&!rl&&r
002
MC/DC0%0/2
int $ms and int $beat
int $ms int $beat
Truth table33%1/3
A and B
A= int $msB= int $beat
ABresult
0X0
100
111
133 2 return 240000 / ($ms * $beat);
134 }
135
136
137 3 1;
↑ Summary