File Coverage

blib/lib/idi.pm
Criterion Covered Total %
statement 61 62 98.3
branch 3 6 50.0
condition n/a
subroutine 25 25 100.0
pod 12 13 92.3
total 101 106 95.2


line stmt bran cond sub pod time code
1             package idi;
2              
3             # ABSTRACT: Easy, command-line MIDI
4              
5 1     1   813 use strict;
  1         2  
  1         41  
6 1     1   5 use warnings;
  1         2  
  1         37  
7              
8 1     1   528 use File::Slurper qw(read_binary);
  1         14950  
  1         63  
9 1     1   747 use File::Temp qw(tempfile);
  1         18564  
  1         60  
10 1     1   700 use MIDI::Simple ();
  1         23009  
  1         37  
11 1     1   484 use Music::Tempo qw(bpm_to_ms);
  1         522  
  1         60  
12 1     1   1473 use Moo;
  1         16884  
  1         5  
13 1     1   2711 use strictures 2;
  1         1859  
  1         50  
14 1     1   678 use namespace::clean;
  1         18131  
  1         8  
15              
16 1     1   366 use Exporter 'import';
  1         3  
  1         279  
17             our @EXPORT = qw(
18             get_score
19             b
20             c
21             d
22             e
23             n
24             o
25             p
26             r
27             t
28             v
29             w
30             x
31             );
32              
33             our $VERSION = '0.0302';
34              
35             my $self;
36              
37             sub BEGIN {
38 1     1   15 has filename => (
39             is => 'rw',
40             builder => 1,
41             );
42             sub _build_filename {
43 1     1   1615 my ($fh, $filename) = tempfile('idi-XXXX',
44             DIR => '.',
45             SUFFIX => '.mid',
46             UNLINK => 1,
47             );
48 1         673 return $filename;
49             }
50              
51             has score => (
52             is => 'ro',
53 1         12 default => sub { MIDI::Simple->new_score },
54 1         24838 );
55              
56             has play => (
57             is => 'rw',
58 1         19 default => sub { 1 },
59 1         304 );
60              
61             has is_written => (
62             is => 'rw',
63 1         30 default => sub { 0 },
64 1         282 );
65              
66 1         274 $self = __PACKAGE__->new;
67             }
68              
69             sub END {
70             if ($self->play) {
71             $self->score->write_score($self->filename) unless $self->is_written;
72             my $content = read_binary($self->filename);
73             print $content;
74             }
75             }
76              
77             sub get_score {
78 1     1 0 644 return $self->score;
79             }
80              
81             sub b {
82 1     1 1 4 my ($bpm) = @_;
83 1         5 $self->score->set_tempo(bpm_to_ms($bpm) * 1000);
84             }
85              
86             sub c {
87 1     1 1 1306 $self->score->Channel(@_);
88             }
89              
90             sub d {
91 1     1 1 1431 $self->score->Duration(@_);
92             }
93              
94             sub e {
95 1     1 1 5 my ($value) = @_;
96 1         5 $self->play($value);
97             }
98              
99             sub n {
100 2     2 1 580 $self->score->n(@_);
101             }
102              
103             sub o {
104 1     1 1 1191 $self->score->Octave(@_);
105             }
106              
107             sub p {
108 1     1 1 538 $self->score->patch_change(@_);
109             }
110              
111             sub r {
112 1     1 1 688 $self->score->r(@_);
113             }
114              
115             sub t {
116 1     1 1 583 my ($signature) = @_;
117 1         6 my ($beats, $divisions) = split /\//, $signature;
118 1 50       12 $self->score->time_signature(
    50          
119             $beats,
120             ($divisions == 8 ? 3 : 2),
121             ($divisions == 8 ? 24 : 18 ),
122             8
123             );
124             }
125              
126             sub v {
127 1     1 1 1164 $self->score->Volume(@_);
128             }
129              
130             sub w {
131 1     1 1 527 my ($name) = @_;
132 1 50       4 if ($name) {
133 1         5 $self->filename($name);
134             }
135             else {
136 0         0 $name = $self->filename;
137             }
138 1         12 $self->score->write_score($name);
139 1         869 $self->is_written(1);
140             }
141              
142             sub x {
143 1     1 1 391 $self->score->noop(@_);
144             }
145              
146             1;
147              
148             =head1 NAME
149              
150             idi - Easy, command-line MIDI
151              
152             =head1 SYNOPSIS
153              
154             $ perl -Midi -E'x(qw(c1 f o5)); n(qw(qn Cs)); n("F"); n("Ds"); n(qw(hn Gs_d1))' | timidity -
155              
156             # Compare with:
157             $ perl -MMIDI::Simple -E'new_score; noop qw(c1 f o5); n qw(qn Cs); n "F"; n "Ds"; n qw(hn Gs_d1); write_score shift()' idi.mid
158             $ timidity idi.mid
159              
160             =head1 DESCRIPTION
161              
162             Easy, command-line MIDI!
163              
164             =head1 FUNCTIONS
165              
166             =head2 b
167              
168             b(100)
169              
170             Set BPM
171              
172             =head2 c
173              
174             c(15)
175              
176             Channel
177              
178             Default: C<0>
179              
180             =head2 d
181              
182             d(128)
183              
184             Duration
185              
186             Default: C<96>
187              
188             =head2 e
189              
190             e(0)
191              
192             Play at end
193              
194             Default: C<1>
195              
196             =head2 n
197              
198             n(@note_spec)
199              
200             Add note. See the L documentation for what a
201             "note_spec" is.
202              
203             =head2 o
204              
205             o(3)
206              
207             Octave
208              
209             Default: C<5>
210              
211             =head2 p
212              
213             p($channel, $patch_number)
214              
215             Patch
216              
217             Default: C<0, 0> (channel 0, piano)
218              
219             =head2 r
220              
221             r($note_duration)
222              
223             Add rest. See the L documentation for what
224             "note_durations" are valid.
225              
226             =head2 t
227              
228             t("$numerator/$denominator")
229              
230             Time signature
231              
232             Default: C
233              
234             =head2 v
235              
236             v(127)
237              
238             Volume
239              
240             Default: C<64>
241              
242             =head2 w
243              
244             w('filename.mid')
245              
246             Write score to a file.
247              
248             =head2 x
249              
250             No-op (with C)
251              
252             =for Pod::Coverage filename
253             =for Pod::Coverage score
254             =for Pod::Coverage play
255             =for Pod::Coverage is_written
256              
257             =head1 SEE ALSO
258              
259             The F file in this distribution
260              
261             L
262              
263             L
264              
265             L
266              
267             L
268              
269             L
270              
271             L
272              
273             L
274              
275             L
276              
277             =head1 AUTHOR
278              
279             Gene Boggs
280              
281             =head1 COPYRIGHT AND LICENSE
282              
283             This software is Copyright (c) 2022 by Gene Boggs.
284              
285             This is free software, licensed under: The Artistic License 2.0 (GPL Compatible)
286              
287             =cut