File Coverage

blib/lib/MIDI/XML/MetaEvent.pm
Criterion Covered Total %
statement 11 52 21.1
branch 0 20 0.0
condition 0 3 0.0
subroutine 4 9 44.4
pod 5 5 100.0
total 20 89 22.4


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