File Coverage

blib/lib/MIDI/XML/PitchBend.pm
Criterion Covered Total %
statement 11 43 25.5
branch 0 16 0.0
condition 0 3 0.0
subroutine 4 9 44.4
pod 5 5 100.0
total 20 76 26.3


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