File Coverage

blib/lib/MIDI/XML/NoteOff.pm
Criterion Covered Total %
statement 11 54 20.3
branch 0 22 0.0
condition 0 3 0.0
subroutine 4 10 40.0
pod 6 6 100.0
total 21 95 22.1


line stmt bran cond sub pod time code
1             package MIDI::XML::NoteOff;
2              
3 1     1   18 use 5.006;
  1         2  
4 1     1   4 use strict;
  1         1  
  1         19  
5 1     1   3 use warnings;
  1         1  
  1         16  
6              
7 1     1   3 use MIDI::XML::Channel;
  1         1  
  1         494  
8              
9             our @ISA = qw(MIDI::XML::Channel);
10              
11             =head1 NAME
12              
13             MIDI::XML::NoteOff - MIDI Note Off messages.
14              
15             =head1 SYNOPSIS
16              
17             use MIDI::XML::NoteOff;
18             use MIDI::Track;
19              
20             $Note_Off = MIDI::XML::NoteOff->new();
21             $Note_Off->delta(384);
22             $Note_Off->channel(0);
23             $Note_Off->note('F',1,4);
24             $Note_Off->velocity(64);
25             @event = $Note_Off->as_event();
26             $midi_track = MIDI::Track->new();
27             push( @{$midi_track->events_r},\@event;
28             @xml = $Note_Off->as_MidiXML();
29             print join("\n",@xml);
30              
31             =head1 DESCRIPTION
32              
33             MIDI::XML::NoteOff is a class encapsulating MIDI Note Off messages.
34             A Note Off message includes either a delta time or absolute time as
35             implemented by MIDI::XML::Message and the MIDI Note Off event encoded
36             in 3 bytes as follows:
37              
38             1000cccc 0nnnnnnn 0vvvvvvv
39              
40             cccc = channel;
41              
42             nnnnnnn = note number
43              
44             vvvvvvv = velocity
45              
46             The classes for MIDI Note Off 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.01';
56              
57             #==========================================================================
58              
59             =head1 METHODS AND ATTRIBUTES
60              
61             =over 4
62              
63             =item $Note_Off = MIDI::XML::NoteOff->new();
64              
65             This creates a new MIDI::XML::NoteOff object.
66              
67             =item $Note_Off = MIDI::XML::NoteOff->new($event);
68              
69             Creates a new NoteOff object initialized with the values of a
70             MIDI::Event note_off 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             '_Velocity'=> undef,
84             };
85 0 0         if (@_) {
86 0 0         if (ref($_[0]) eq 'ARRAY') {
    0          
    0          
87 0 0         if ($_[0][0] eq 'note_off') {
88 0           $self->{'_Delta'} = $_[0][1];
89 0           $self->{'_Channel'} = $_[0][2] & 0x0F;
90 0           $self->{'_Note'} = $_[0][3] & 0x7F;
91 0           $self->{'_Velocity'} = $_[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 'note_off') {
99 0           $self->{'_Delta'} = $_[1];
100 0           $self->{'_Channel'} = $_[2] & 0x0F;
101 0           $self->{'_Note'} = $_[3] & 0x7F;
102 0           $self->{'_Velocity'} = $_[4] & 0x7F;
103             }
104             }
105             }
106              
107 0           bless($self,$class);
108 0           return $self;
109             }
110              
111             =item $delta_time = $Note_Off->delta() or $Note_Off->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 = $Note_Off->absolute() or $Note_Off->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 = $Note_Off->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 = $Note_Off->channel() or $Note_Off->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 = $Note_Off->note();
147              
148             Returns the MIDI note number. Note numbers are limited
149             to the range 0-127.
150              
151             =item $note_no = $Note_Off->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 = $Note_Off->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             =cut
170              
171             sub note {
172 0     0 1   my $self = shift;
173 0 0         if (@_) {
174 0 0         if ($#_ == 2) {
    0          
175             $self->{'_Note'} =
176 0           (${{'C'=>0,'D'=>2,'E'=>4,'F'=>5,'G'=>7,'A'=>9,'B'=>11}}{$_[0]}
  0            
177             + $_[1]
178             + 12 * $_[2] + 12) & 0x7F;
179             } elsif ($#_ == 1) {
180             $self->{'_Note'} =
181 0           (${{'C'=>0,'D'=>2,'E'=>4,'F'=>5,'G'=>7,'A'=>9,'B'=>11}}{$_[0]}
  0            
182             + 12 * $_[1] + 12) & 0x7F;
183             } else {
184 0           $self->{'_Note'} = (shift) & 0x7F;
185             }
186             }
187 0           return $self->{'_Note'};
188             }
189              
190             #==========================================================================
191              
192             =item $velocity = $Note_Off->velocity() or $Note_Off->velocity($velocity);
193              
194             Returns and optionally sets the velocity. Velocities are limited
195             to the range 0-127.
196              
197             =cut
198              
199             sub velocity {
200 0     0 1   my $self = shift;
201 0 0         if (@_) {
202 0           $self->{'_Velocity'} = (shift) & 0x7F;
203             }
204 0           return $self->{'_Velocity'};
205             }
206              
207             #==========================================================================
208              
209             =item $ordinal = $Seq_No->ordinal();
210              
211             Returns a value to be used to order events that occur at the same time.
212              
213             =cut
214              
215             sub ordinal {
216 0     0 1   my $self = shift;
217 0           return 0x0100 + (127 - $self->{'_Note'});
218             }
219              
220             #==========================================================================
221              
222             =item @event = $Note_Off->as_event();
223              
224             Returns a MIDI::Event note_off array initialized with the values of the
225             NoteOff object. MIDI::Event does not expect absolute times and will interpret
226             them as delta times. Calling this method when the time is absolute will not
227             generate a warning or error but it is unlikely that the results will be
228             satisfactory.
229              
230             =cut
231              
232             sub as_event {
233 0     0 1   my $self = shift;
234              
235             my @event = (
236             'note_off',
237             MIDI::XML::Message::time($self),
238             $self->{'_Channel'} & 0x0F,
239             $self->{'_Note'} & 0x7F,
240 0           $self->{'_Velocity'} & 0x7F
241             );
242 0           return @event;
243             }
244              
245             #==========================================================================
246              
247             =item @xml = $Note_Off->as_MidiXML();
248              
249             Returns an array of elements formatted according to the MidiXML DTD. These
250             elements may be assembled by track into entire documents with the following
251             suggested DOCTYPE declaration:
252              
253            
254             "-//Recordare//DTD MusicXML 0.7 MIDI//EN"
255             "http://www.musicxml.org/dtds/midixml.dtd">
256              
257             =back
258              
259             =cut
260              
261             sub as_MidiXML {
262 0     0 1   my $self = shift;
263 0           my @xml;
264              
265 0           push @xml, MIDI::XML::Channel::as_MidiXML($self);
266 0           $xml[2] = "{'_Note'}\" Velocity=\"$self->{'_Velocity'}\"/>";
267 0           return @xml;
268             }
269              
270             #==========================================================================
271              
272              
273             return 1;
274             __END__