File Coverage

blib/lib/MIDI/XML/TextEvent.pm
Criterion Covered Total %
statement 14 48 29.1
branch 0 16 0.0
condition 0 3 0.0
subroutine 5 10 50.0
pod 5 5 100.0
total 24 82 29.2


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