File Coverage

blib/lib/Music/Tension.pm
Criterion Covered Total %
statement 28 28 100.0
branch 5 8 62.5
condition 2 6 33.3
subroutine 8 8 100.0
pod 3 3 100.0
total 46 53 86.7


line stmt bran cond sub pod time code
1             # -*- Perl -*-
2             #
3             # Parent class for music tension analysis modules.
4              
5             package Music::Tension;
6              
7 4     4   168930 use 5.010000;
  4         15  
8 4     4   20 use strict;
  4         8  
  4         78  
9 4     4   19 use warnings;
  4         11  
  4         109  
10              
11 4     4   20 use Carp qw/croak/;
  4         12  
  4         202  
12 4     4   21 use Scalar::Util qw/looks_like_number/;
  4         7  
  4         1247  
13              
14             our $VERSION = '0.70';
15              
16             ########################################################################
17             #
18             # SUBROUTINES
19              
20             sub new {
21 6     6 1 25 my ( $class, %param ) = @_;
22 6         14 my $self = {};
23              
24             # just MIDI support here, see Music::Scala for scala scale file support
25 6 100       22 if ( exists $param{reference_frequency} ) {
26             croak "reference_frequency must be a number"
27 3 50       21 if !looks_like_number $param{reference_frequency};
28 3         9 $self->{_reference_frequency} = $param{reference_frequency};
29             } else {
30 3         8 $self->{_reference_frequency} = 440;
31             }
32              
33 6         13 bless $self, $class;
34 6         21 return $self;
35             }
36              
37             sub freq2pitch {
38 4     4 1 906 my ( $self, $freq ) = @_;
39 4 50 33     34 croak "frequency must be a positive number"
40             if !looks_like_number $freq
41             or $freq < 0;
42              
43             return sprintf "%.0f",
44 4         2148 69 + 12 * ( log( $freq / $self->{_reference_frequency} ) / log(2) );
45             }
46              
47             sub pitch2freq {
48 9     9 1 17 my ( $self, $pitch ) = @_;
49 9 50 33     70 croak "pitch must be MIDI number"
50             if !looks_like_number $pitch
51             or $pitch < 0;
52              
53 9         59 return $self->{_reference_frequency} * ( 2**( ( $pitch - 69 ) / 12 ) );
54             }
55              
56             1;
57             __END__