blib/lib/Types/MIDI.pm

filter: Statementstmt 100% 34 / 34 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 9 / 9 Podpod n/a totaltotal 100% 43 / 43 CC 1 SCAR 0.0
CC
1
Cov
100%
BEGIN
1.0
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
↑ Summary
1 ## no critic (TestingAndDebugging::RequireUseStrict)
2 ## no critic (TestingAndDebugging::RequireUseWarnings)
3 package Types::MIDI;
4 3 our $AUTHORITY = 'cpan:MJGARDNER';
5 ## use critic (TestingAndDebugging::RequireUseStrict)
6 ## use critic (TestingAndDebugging::RequireUseWarnings)
7  
8 3 ▶use 5.016;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
10 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 # ABSTRACT: Type library for MIDI
13  
14 #<<<
15 3 our $VERSION = 'v0.601.0';
16 #>>>
17  
18 3 ▶use Type::Library 2.000000 -extends => [ qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
19 Types::Common::Numeric
20 Types::Common::String
21 ) ],
22 -declare => qw(
23 Channel
24 Velocity
25 Note
26 PercussionNote
27 3 );
28 3 ▶use Type::Utils 2.000000 -all;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 3 ▶use MIDI;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 3 ▶use Readonly;
Subroutine BEGIN: called CC=1 SCAR=0.0
31  
32 #pod =head1 SYNOPSIS
33 #pod
34 #pod use Moo;
35 #pod use Types::MIDI -all;
36 #pod
37 #pod has volume => (
38 #pod is => 'ro',
39 #pod isa => Velocity,
40 #pod default => 100,
41 #pod );
42 #pod has a440 => (
43 #pod is => 'ro',
44 #pod isa => Note,
45 #pod default => 69,
46 #pod );
47 #pod has electric_snare => (
48 #pod is => 'ro',
49 #pod isa => PercussionNote,
50 #pod coerce => 1,
51 #pod default => 'Electric Snare',
52 #pod );
53 #pod
54 #pod =head1 DESCRIPTION
55 #pod
56 #pod This is a type constraint library intended to be useful for those
57 #pod developing music software using the MIDI (Musical Instrument Digital
58 #pod Interface) specification.
59 #pod
60 #pod It is a work in progress driven by real-world usage, and as such
61 #pod B<does not yet necessarily have a stable interface>. Once it reaches
62 #pod version 1.0, though, the author does not intend to introduce any
63 #pod breaking changes without a corresponding increase in the major version
64 #pod number.
65 #pod
66 #pod =head1 OVERVIEW
67 #pod
68 #pod Because this leverages L<Type::Library>, it should be usable in a
69 #pod variety of Perl object systems, including L<Moo> and L<Moose>. By
70 #pod default, it exports nothing into the consumer's namespace; however, in
71 #pod addition to specifying individual functions in the L<perlfunc/use>
72 #pod statement, you can also provide or combine the following tags to export
73 #pod groups of functions:
74 #pod
75 #pod =over
76 #pod
77 #pod =item C<use Types::MIDI qw(:types);>
78 #pod
79 #pod Exports all types by name into the namespace.
80 #pod
81 #pod =item C<use Types::MIDI qw(:is);>
82 #pod
83 #pod Exports all C<is_>I<TypeName> functions into the namespace.
84 #pod
85 #pod =item C<use Types::MIDI qw(:assert);>
86 #pod
87 #pod Exports all C<assert_>I<TypeName> functions into the namespace.
88 #pod
89 #pod =item C<use Types::MIDI qw(:to);>
90 #pod
91 #pod Exports all C<to_>I<TypeName> functions into the namespace.
92 #pod
93 #pod =item C<use Types::MIDI qw(+>I<TypeName>C<);>
94 #pod
95 #pod Exports I<TypeName> and all related functions into the namespace.
96 #pod
97 #pod =item C<use Types::MIDI qw(:all);>
98 #pod
99 #pod Exports everything.
100 #pod
101 #pod =back
102 #pod
103 #pod This library also inherits from L<Exporter::Tiny>; consult
104 #pod L<Exporter::Tiny::Manual::Importing> for ways to customize how functions
105 #pod are imported, such as renaming or omitting certain names.
106 #pod
107 #pod Also note that the tags listed above may be preceded with a C<->
108 #pod (hyphen) instead of a C<:> (colon); Perl's auto-quoting of barewords
109 #pod thus enables you to import a tag group of functions like so:
110 #pod
111 #pod use Types::MIDI -all;
112 #pod
113 #pod =cut
114  
115 ## no critic (ValuesAndExpressions::ProhibitMagicNumbers)
116  
117 #pod =type Channel
118 #pod
119 #pod An L<integer from|Types::Common::Numeric/Types> 0 to 15 corresponding
120 #pod to a MIDI Channel.
121 #pod
122 #pod =func is_Channel
123 #pod
124 #pod Returns true if the passed value can be used as a L</Channel>.
125 #pod
126 #pod =func assert_Channel
127 #pod
128 #pod Returns the passed value if and only if it can be used as a L</Channel>;
129 #pod otherwise it throws an exception.
130 #pod
131 #pod =cut
132  
133 3 declare Channel, as IntRange [ 0, 15 ];
134  
135 #pod =type Velocity
136 #pod
137 #pod An L<integer from|Types::Common::Numeric/Types> 0 to 127 corresponding
138 #pod to a MIDI velocity.
139 #pod
140 #pod =func is_Velocity
141 #pod
142 #pod Returns true if the passed value can be used as a L</Velocity>.
143 #pod
144 #pod =func assert_Velocity
145 #pod
146 #pod Returns the passed value if and only if it can be used as a
147 #pod L</Velocity>; otherwise it throws an exception.
148 #pod
149 #pod =cut
150  
151 3 declare Velocity, as IntRange [ 0, 127 ];
152  
153 #pod =type Note
154 #pod
155 #pod An L<integer from|Types::Common::Numeric/Types> 0 to 127 corresponding
156 #pod to a MIDI note number.
157 #pod
158 #pod =func is_Note
159 #pod
160 #pod Returns true if the passed value can be used as a L</Note>.
161 #pod
162 #pod =func assert_Note
163 #pod
164 #pod Returns the passed value if and only if it can be used as a L</Note>;
165 #pod otherwise it throws an exception.
166 #pod
167 #pod =cut
168  
169 3 declare Note, as IntRange [ 0, 127 ];
170  
171 #pod =type PercussionNote
172 #pod
173 #pod A L</Note> from 27 through 87, corresponding to a L</Note> number in the
174 #pod General MIDI 2 Percussion Sound Set.
175 #pod
176 #pod This type can also coerce case-insensitive
177 #pod L<Types::Common::String/NonEmptySimpleStr>s of instrument names in the
178 #pod General MIDI 2 Percussion Sound Set, returning the corresponding
179 #pod L</Note>.
180 #pod
181 #pod =func is_PercussionNote
182 #pod
183 #pod Returns true if the passed value can be used as a L</PercussionNote>.
184 #pod
185 #pod =func assert_PercussionNote
186 #pod
187 #pod Returns the passed value if and only if it can be used as a
188 #pod L</PercussionNote>; otherwise it throws an exception.
189 #pod
190 #pod =func to_PercussionNote
191 #pod
192 #pod Coerces the passed value to a L</PercussionNote>.
193 #pod
194 #pod =cut
195  
196 3 my %notenum2percussion = %MIDI::notenum2percussion; ## no critic (Variables::ProhibitPackageVars)
197 3 @notenum2percussion{ 27 .. 34, 82 .. 87 } = (
198 'High Q',
199 'Slap',
200 'Scratch Push',
201 'Scratch Pull',
202 'Sticks',
203 'Square Click',
204 'Metronome Click',
205 'Metronome Bell',
206  
207 'Shaker',
208 'Jingle Bell',
209 'Bell Tree',
210 'Castanets',
211 'Mute Surdo',
212 'Open Surdo',
213 );
214 Readonly my %NOTENUM2PERCUSSION_FC =>
215 3 map { fc $notenum2percussion{$_} => $_ } keys %notenum2percussion;
216  
217 declare PercussionNote, as Note,
218 3 ▶ where { exists $notenum2percussion{$_} };
Subroutine __ANON__: called CC=1 SCAR=0.0
219 coerce PercussionNote, from NonEmptySimpleStr,
220 50 ▶ via { $NOTENUM2PERCUSSION_FC{"\F$_"} };
Subroutine __ANON__: called CC=1 SCAR=0.0
221  
222 #pod =head1 SEE ALSO
223 #pod
224 #pod =over
225 #pod
226 #pod =item *
227 #pod
228 #pod I<MIDI 1.0 Detailed Specification (Document Version 4.2.1)>,
229 #pod revised February 1996 by the MIDI Manufacturers Association:
230 #pod L<https://midi.org/midi-1-0-core-specifications>
231 #pod
232 #pod =item *
233 #pod
234 #pod B<Appendix B: GM 2 Percussion Sound Set> in
235 #pod I<General MIDI 2 (Version 1.2a)>,
236 #pod published February 6, 2007 by the MIDI Manufacturers Association:
237 #pod L<https://midi.org/general-midi-2>
238 #pod
239 #pod =back
240 #pod
241 #pod =cut
242  
243 3 1;
244  
245 __END__
↑ Summary