File Coverage

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