ChordBot/Song.pm

filter: Statementstmt 98.1% 53 / 54 Branchbran 80.0% 8 / 10 Conditioncond n/a MC/DCmcdc n/a Subroutinesub 94.4% 17 / 18 Podpod 71.4% 5 / 7 totaltotal 93.2% 83 / 89 CC 4 SCAR 3.0
CC
4
Cov
95%
END
2.1
song
2.0
_chord
2.0
CRAP 1.4
« ChordBot/Opus/Section/Style/Track.pm ↑ Summary ChordBot/Opus/Base.pm »
1 #! perl
2  
3 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 3 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 package Music::ChordBot::Song;
8  
9 =head1 NAME
10  
11 Music::ChordBot::Song - Generate ChordBot songs.
12  
13 =head1 SYNOPSIS
14  
15 use Music::ChordBot::Song;
16 song "All Of Me";
17 tempo 105;
18  
19 section "All Of Me 1";
20 style "Chicago";
21  
22 C 4; C; E7; E7; A7; A7; Dm7; Dm7;
23 E7; E7; Am7; Am7; D7; D7; Dm7; G7;
24  
25 section "All Of Me 2";
26 style "Swingatron";
27  
28 C 4; C; E7; E7; A7; A7; Dm7; Dm7;
29 Dm7; Ebdim7; Em7; A9; Dm7b5; G13;
30 C 2; Ebdim7; Dm7; G7;
31  
32 =head1 DESCRIPTION
33  
34 Music::ChordBot::Song exports a number of subroutines that can be used
35 to construct a CordBot song. Upon program termination, the song is
36 written out to standard output in JSON format, suitable for import into
37 the ChordBot app.
38  
39 =cut
40  
41 3 our $VERSION = 0.01;
42  
43 3 ▶use Music::ChordBot::Opus;
Subroutine BEGIN: called CC=1 SCAR=0.0
44 3 ▶use Music::ChordBot::Opus::Section;
Subroutine BEGIN: called CC=1 SCAR=0.0
45  
46 3 our @EXPORT = qw( song chord section tempo style );
47 3 ▶use base 'Exporter';
Subroutine BEGIN: called CC=1 SCAR=0.0
48  
49 3 my $song;
50 my $section;
51  
52 =head1 SUBROUTINES
53  
54 =head2 song I<title>
55  
56 Starts a new song with the given title.
57  
58 =cut
59  
60 3 ▶sub song($) {
Subroutine song: called CC=2 SCAR=7.1
Pod documented
61 3 ▶ _export() if $song;
Branch: 1 branch
BranchTrueFalseTotal
if $song 0 3 3
62 3 $song = Music::ChordBot::Opus->new( name => shift );
63 3 undef $section;
64 }
65  
66 =head2 tempo I<bpm>
67  
68 Sets the tempo in beats per minute.
69  
70 =cut
71  
72 3 ▶sub tempo($) {
Subroutine tempo: called CC=1 SCAR=0.0
Pod documented
73 3 $song->tempo(@_);
74 }
75  
76 =head2 section I<name>
77  
78 Starts a song section. A section groups a number of bars with chords.
79 Each section can have a style associated although it is common to have
80 a single style for the whole song.
81  
82 =cut
83  
84 4 ▶sub section($) {
Subroutine section: called CC=1 SCAR=0.0
Pod documented
85 4 $section = Music::ChordBot::Opus::Section->new( name => shift );
86 4 $song->add_section( $section );
87 }
88  
89 =head2 style I<preset>
90  
91 Associate the given style to the current section. For a list of
92 presets, see http://chordbot.com/style-lookup.php .
93  
94 =cut
95  
96 4 ▶sub style($) {
Subroutine style: called CC=1 SCAR=0.0
Pod documented
97 4 $section->set_style(@_);
98 }
99  
100 =head2 chord I<key>, I<type>, I<duration>
101  
102 Append a chord with given key, type and duration. Note that duration
103 is measured in number of beats. The three arguments may also be
104 specified in a single string argument, space separated.
105  
106 You can specify a bass note for the chord by separating the key and
107 bass with a slash. E.g., C<"C/B"> denotes a C chord with B bass.
108  
109 =cut
110  
111 14 ▶sub chord($) {
Subroutine chord: called CC=1 SCAR=0.0
Pod documented
112 14 $section->add_chord( @_ );
113 }
114  
115 # Automatically export the song at the end of the program.
116 3 ▶sub END {
Subroutine END: called CC=2 SCAR=7.6
117 3 ▶ _export() if $song;
Branch: 1 branch
BranchTrueFalseTotal
if $song 3 0 3
118 }
119  
120 3 ▶sub json { $song->json }
Subroutine json: called CC=1 SCAR=0.0
Pod undocumented
121  
122 3 ▶sub _export {
Subroutine _export: called CC=1 SCAR=0.0
123 3 binmode( STDOUT, ':utf8');
124 3 print STDOUT $song->json, "\n";
125 }
126  
127 =head1 QUICK ACCESS CHORDS
128  
129 For convenience, subroutines are exported for quick access to chords.
130 So instead of
131  
132 chord "C", "Maj", 4;
133  
134 you can also write:
135  
136 C 4;
137  
138 If you omit the duration it will use the duration of the previous
139 chord:
140  
141 C 4; C; F; G; # same as C 4; C 4; F 4; G 4;
142  
143 The subroutine name is the key of the chord, optionally followed by a
144 chord modifier. So C<C> is C major, C<Cm> is C minor, and so on.
145  
146 Chord keys are A B C D E F G Ab Bb Db Eb Gb Ais Cis Dis Fis Gis.
147  
148 Modifiers are m 7 m7 maj7 9 11 13 auf 7b5 m7b5 dim dim7.
149  
150 =cut
151  
152 3 my $_key = "C";
153 3 my $_mod = "Maj";
154 3 my $_dur = 4;
155  
156 34 ▶sub _chord {
Subroutine _chord: called CC=2 SCAR=6.9
157 34 my ( $key, $mod, $dur ) = @_;
158 34 ▶ $_dur = $dur if $dur;
Branch: 1 branch
BranchTrueFalseTotal
if $dur 3 31 34
159 34 $section->add_chord( $key, $mod, $_dur );
160 }
161  
162 3 for my $key ( qw( A B C D E F G
163 Ab Bb Db Eb Gb
164 Ais Cis Dis Fis Gis
165 ) ) {
166 51 ▶ my $k2 = $key =~ /^(.)is$/ ? "$1#" : $key;
Branch: 1 branch
BranchTrueFalseTotal
$key =~ /^(.)is$/ ? : 15 36 51
167  
168 51 my %m = (
169 "" => "Maj",
170 "m" => "Min",
171 "7" => "7",
172 "m7" => "Min7",
173 "maj7" => "Maj7",
174 "9" => "9",
175 "11" => "11",
176 "13" => "13",
177 "aug" => "Aug",
178 "7b5" => "7(b5)",
179 "m7b5" => "Min7(b5)",
180 "dim" => "Dim",
181 "dim7" => "Dim7",
182 );
183  
184 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
185 51 ▶ while ( my ($k, $t) = each( %m ) ) {
Branch: 1 branch
BranchTrueFalseTotal
if (() = each %m) 663 51 714
186 663 *{ __PACKAGE__ . '::' . $key.$k } =
187 34 ▶ sub { &_chord( $k2, $t, $_[0] ) };
Subroutine __ANON__: called CC=1 SCAR=0.0
188 663 push( @EXPORT, $key.$k );
189 }
190 }
191  
192 # Rest 'chord'.
193 0 ▶sub NC { &_chord( "A", "Silence", $_[0] ) }
Subroutine NC: not called CC=1 SCAR=6.9
Pod undocumented
194 3 *S = \&NC;
195 3 push( @EXPORT, "S", "NC" );
196  
197 =head1 DISCLAIMER
198  
199 There is currently NO VALIDATION of argument values. Illegal values
200 will result in program crashes and songs that cannot be imported, or
201 played, by ChordBot.
202  
203 =head1 AUTHOR, COPYRIGHT & LICENSE
204  
205 See L<Music::ChordBot>.
206  
207 =cut
208  
209 3 1;
« ChordBot/Opus/Section/Style/Track.pm ↑ Summary ChordBot/Opus/Base.pm »