File Coverage

blib/lib/MIDI/XML/PolyKeyPressure.pm
Criterion Covered Total %
statement 11 45 24.4
branch 0 16 0.0
condition 0 3 0.0
subroutine 4 9 44.4
pod 5 5 100.0
total 20 78 25.6


line stmt bran cond sub pod time code
1             package MIDI::XML::PolyKeyPressure;
2              
3 1     1   12 use 5.006;
  1         2  
4 1     1   3 use strict;
  1         1  
  1         20  
5 1     1   13 use warnings;
  1         1  
  1         19  
6              
7 1     1   3 use MIDI::XML::Channel;
  1         1  
  1         388  
8              
9             our @ISA = qw(MIDI::XML::Channel);
10              
11             =head1 NAME
12              
13             MIDI::XML::PolyKeyPressure - MIDI Poly Key Pressure messages.
14              
15             =head1 SYNOPSIS
16              
17             use MIDI::XML::PolyKeyPressure;
18             use MIDI::Track;
19              
20             $Poly_key = MIDI::XML::PolyKeyPressure->new();
21             $Poly_key->delta(384);
22             $Poly_key->channel(0);
23             $Poly_key->note('F',1,4);
24             $Poly_key->pressure(64);
25             @event = $Poly_key->as_event();
26             $midi_track = MIDI::Track->new();
27             push( @{$midi_track->events_r},\@event;
28             @xml = $Poly_key->as_MidiXML();
29             print join("\n",@xml);
30              
31             =head1 DESCRIPTION
32              
33             MIDI::XML::PolyKeyPressure is a class encapsulating MIDI Poly Key Pressure messages.
34             A Poly Key Pressure message includes either a delta time or absolute time as
35             implemented by MIDI::XML::Message and the MIDI Poly Key Pressure event encoded
36             in 3 bytes as follows:
37              
38             1010cccc 0nnnnnnn 0ppppppp
39              
40             cccc = channel;
41              
42             nnnnnnn = note number
43              
44             ppppppp = pressure
45              
46             The classes for MIDI Poly Key Pressure messages and the other six channel
47             messages are derived from MIDI::XML::Channel.
48              
49             =head2 EXPORT
50              
51             None.
52              
53             =cut
54              
55             our $VERSION = '0.02';
56              
57             #==========================================================================
58              
59             =head1 METHODS AND ATTRIBUTES
60              
61             =over 4
62              
63             =item $Poly_key = MIDI::XML::PolyKeyPressure->new();
64              
65             This creates a new MIDI::XML::PolyKeyPressure object.
66              
67             =item $Poly_key = MIDI::XML::PolyKeyPressure->new($event);
68              
69             Creates a new PolyKeyPressure object initialized with the values of a
70             MIDI::Event key_after_touch array.
71              
72             =cut
73              
74             sub new {
75 0     0 1   my $class = shift;
76 0   0       $class = ref($class) || $class;
77              
78 0           my $self = {
79             '_Delta'=> undef,
80             '_Absolute'=> undef,
81             '_Channel'=> undef,
82             '_Note'=> undef,
83             '_Pressure'=> undef,
84             };
85 0 0         if (@_) {
86 0 0         if (ref($_[0]) eq 'ARRAY') {
    0          
    0          
87 0 0         if ($_[0][0] eq 'key_after_touch') {
88 0           $self->{'_Delta'} = $_[0][1];
89 0           $self->{'_Channel'} = $_[0][2] & 0x0F;
90 0           $self->{'_Note'} = $_[0][3] & 0x7F;
91 0           $self->{'_Pressure'} = $_[0][4] & 0x7F;
92             }
93             } elsif (ref($_[0]) eq 'HASH') {
94 0           foreach my $attr (keys %{$_[0]}) {
  0            
95 0 0         $self->{"_$attr"} = $_[0]->{$attr} unless ($attr =~ /^_/);
96             }
97             } elsif (ref($_[0]) eq '') {
98 0 0         if ($_[0] eq 'key_after_touch') {
99 0           $self->{'_Delta'} = $_[1];
100 0           $self->{'_Channel'} = $_[2] & 0x0F;
101 0           $self->{'_Note'} = $_[3] & 0x7F;
102 0           $self->{'_Pressure'} = $_[4] & 0x7F;
103             }
104             }
105             }
106              
107 0           bless($self,$class);
108 0           return $self;
109             }
110              
111             =item $delta_time = $Poly_key->delta() or $Poly_key->delta($delta_time);
112              
113             Returns the message time as a delta time or undef if it is an absolute
114             time. Optionally sets the message time to the specified delta time. To
115             avoid contradictory times, the absolute time is set to undef when a delta time
116             is set.
117              
118             This functionality is provided by the MIDI::XML::Message base class.
119              
120             =item $absolute_time = $Poly_key->absolute() or $Poly_key->absolute($absolute_time);
121              
122             Returns the message time as an absolute time or undef if it is a delta
123             time. Optionally sets the message time to the specified absolute time. To
124             avoid contradictory times, the delta time is set to undef when an absolute time
125             is set.
126              
127             This functionality is provided by the MIDI::XML::Message base class.
128              
129             =item $time = $Poly_key->time();
130              
131             Returns the message time, absolute or delta, whichever was last set.
132              
133             This functionality is provided by the MIDI::XML::Message base class.
134              
135             =item $channel = $Poly_key->channel() or $Poly_key->channel($channel);
136              
137             Returns and optionally sets the channel number. Channel numbers are limited
138             to the range 0-15.
139              
140             This functionality is provided by the MIDI::XML::Channel base class.
141              
142             =cut
143              
144             #==========================================================================
145              
146             =item $note_no = $Poly_key->note();
147              
148             Returns the MIDI note number. Note numbers are limited
149             to the range 0-127.
150              
151             =item $note_no = $$Poly_key->note($note_no);
152              
153             Sets and returns the MIDI note number to the specified number. Note numbers
154             are limited to the range 0-127.
155              
156             =item $note_no = $Poly_key->note($step, $alter, $octave);
157              
158             Sets the MIDI note number to the specified step, alter and octave
159             values. Step is a letter designation of the note, /A|B|C|D|E|F|G/, alter
160             is -1 for flat, 0 for natural, and 1 for sharp, and octave is the octave, -1
161             to 9. Valid values range from ('C',0,-1) to ('G',0,9). Returns the MIDI
162             note number.
163              
164             =item $note_no = $Note_Off->note($step, $octave);
165              
166             Sets and returns the MIDI note number to the specified step and octave
167             values assuming a 0 alter value.
168              
169             This functionality is provided by the MIDI::XML::NoteOff class.
170              
171             =cut
172              
173             #==========================================================================
174              
175             =item $pressure = $Poly_key->pressure() or $Poly_key->pressure($pressure);
176              
177             Returns and optionally sets the pressure. Values are limited
178             to the range 0-127.
179              
180             =cut
181              
182             sub pressure {
183 0     0 1   my $self = shift;
184 0 0         if (@_) {
185 0           $self->{'_Pressure'} = (shift) & 0x7F;
186             }
187 0           return $self->{'_Pressure'};
188             }
189              
190             #==========================================================================
191              
192             =item $ordinal = $Poly_key->ordinal();
193              
194             Returns a value to be used to order events that occur at the same time.
195              
196             =cut
197              
198             sub ordinal {
199 0     0 1   my $self = shift;
200 0           return 0x0800 + (127 - $self->{'_Note'});
201             }
202              
203             #==========================================================================
204              
205             =item @event = $Poly_key->as_event();
206              
207             Returns a MIDI::Event key_after_touch array initialized with the values of the
208             PolyKeyPressure object. MIDI::Event does not expect absolute times and will interpret
209             them as delta times. Calling this method when the time is absolute will not
210             generate a warning or error but it is unlikely that the results will be
211             satisfactory.
212              
213             =cut
214              
215             sub as_event {
216 0     0 1   my $self = shift;
217              
218             my @event = (
219             'key_after_touch',
220             MIDI::XML::Message::time($self),
221             $self->{'_Channel'} & 0x0F,
222             $self->{'_Note'} & 0x7F,
223 0           $self->{'_Pressure'} & 0x7F
224             );
225 0           return @event;
226             }
227              
228             #==========================================================================
229              
230             =item @xml = $Poly_key->as_MidiXML();
231              
232             Returns an array of elements formatted according to the MidiXML DTD. These
233             elements may be assembled by track into entire documents with the following
234             suggested DOCTYPE declaration:
235              
236            
237             "-//Recordare//DTD MusicXML 0.7 MIDI//EN"
238             "http://www.musicxml.org/dtds/midixml.dtd">
239              
240             =back
241              
242             =cut
243              
244             sub as_MidiXML {
245 0     0 1   my $self = shift;
246 0           my @xml;
247              
248 0           push @xml, MIDI::XML::Channel::as_MidiXML($self);
249 0           $xml[2] = "{'_Note'}\" Pressure=\"$self->{'_Pressure'}\"/>";
250 0           return @xml;
251             }
252              
253             #==========================================================================
254              
255              
256             return 1;
257             __END__