File Coverage

blib/lib/MIDI/XML/KeySignature.pm
Criterion Covered Total %
statement 11 51 21.5
branch 0 22 0.0
condition 0 3 0.0
subroutine 4 10 40.0
pod 6 6 100.0
total 21 92 22.8


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