File Coverage

blib/lib/Music/Tempo.pm
Criterion Covered Total %
statement 29 29 100.0
branch 8 14 57.1
condition 9 13 69.2
subroutine 8 8 100.0
pod 4 4 100.0
total 58 68 85.2


line stmt bran cond sub pod time code
1             package Music::Tempo;
2 3     3   86100 use strict;
  3         10  
  3         148  
3            
4             BEGIN {
5 3     3   18 use Exporter ();
  3         7  
  3         71  
6 3     3   17 use vars qw ($VERSION @ISA @EXPORT);
  3         18  
  3         328  
7 3     3   6 $VERSION = 0.02;
8 3         59 @ISA = qw (Exporter);
9 3         2578 @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             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             sub italian_to_bpm {
109 2     2 1 5 my $name = uc(shift());
110 2 50       9 return unless defined $tempi{$name};
111 2         13 return int(($tempi{$name}[0] + $tempi{$name}[1])/2) + 1;
112             }
113             sub bpm_to_italian {
114 2     2 1 12 my $bpm = shift();
115 2 50       7 return unless int($bpm);
116 2 50       6 return "Too Slow" if $bpm < 40;
117 2 50       8 return "Too Fast" if $bpm > 220;
118 2         10 foreach (keys %tempi) {
119 14 100 100     80 return ucfirst(lc($_)) if ($tempi{$_}[0] <= $bpm && $bpm <= $tempi{$_}[1]);
120             }
121             }
122            
123             sub bpm_to_ms {
124 2     2 1 8 my $bpm = shift;
125 2   100     10 my $beat = shift || 4;
126 2 50 33     12 return unless (int($bpm) && int($beat));
127 2         13 return 240000 / ($bpm * $beat);
128             }
129             sub ms_to_bpm {
130 2     2 1 4 my $ms = shift;
131 2   100     9 my $beat = shift || 4;
132 2 50 33     11 return unless (int($ms) && int($beat));
133 2         11 return 240000 / ($ms * $beat);
134             }
135            
136            
137             1;