line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package MIDI::XML::Parser; |
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
355
|
use 5.006; |
|
1
|
|
|
|
|
3
|
|
4
|
1
|
|
|
1
|
|
6
|
use strict; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
28
|
|
5
|
1
|
|
|
1
|
|
5
|
use warnings; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
30
|
|
6
|
1
|
|
|
1
|
|
3
|
use MIDI::XML::MidiFile; |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
20
|
|
7
|
1
|
|
|
1
|
|
2
|
use vars qw($element2class $MidiFile $Track $Event %attr $cdata $delta $absolute); |
|
1
|
|
|
|
|
1
|
|
|
1
|
|
|
|
|
521
|
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @ISA = qw(); |
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
our $VERSION = 0.02; |
12
|
|
|
|
|
|
|
|
13
|
|
|
|
|
|
|
=head1 NAME |
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
MIDI::XML::Parser - SAX Parser for creating MidiFile objects |
16
|
|
|
|
|
|
|
from XML. |
17
|
|
|
|
|
|
|
|
18
|
|
|
|
|
|
|
=head1 SYNOPSIS |
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
use MIDI::XML::Parser; |
21
|
|
|
|
|
|
|
$MidiFile = MIDI::XML::Parser->parse_MidiXML($file); |
22
|
|
|
|
|
|
|
|
23
|
|
|
|
|
|
|
=head1 DESCRIPTION |
24
|
|
|
|
|
|
|
|
25
|
|
|
|
|
|
|
MIDI::XML::Parser is a class for parsing a file in |
26
|
|
|
|
|
|
|
MidiXML format and storing it as standard MIDI file. Additionally, |
27
|
|
|
|
|
|
|
by subclassing other MIDI-XML classes, it may be converted to |
28
|
|
|
|
|
|
|
another XML format. |
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
=head2 EXPORT |
31
|
|
|
|
|
|
|
|
32
|
|
|
|
|
|
|
None by default. |
33
|
|
|
|
|
|
|
|
34
|
|
|
|
|
|
|
=cut |
35
|
|
|
|
|
|
|
|
36
|
|
|
|
|
|
|
#========================================================================== |
37
|
|
|
|
|
|
|
|
38
|
|
|
|
|
|
|
=head1 METHODS AND ATTRIBUTES |
39
|
|
|
|
|
|
|
|
40
|
|
|
|
|
|
|
=over 4 |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
=item parse_MidiXML($file); |
43
|
|
|
|
|
|
|
|
44
|
|
|
|
|
|
|
Parses a MidiXML file creating and returning a MidiFile object. |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
=cut |
47
|
|
|
|
|
|
|
|
48
|
|
|
|
|
|
|
sub parse_MidiXML { |
49
|
0
|
|
|
0
|
1
|
|
my $class = shift; |
50
|
0
|
|
|
|
|
|
my $file = shift; |
51
|
|
|
|
|
|
|
|
52
|
0
|
|
|
|
|
|
$element2class = { |
53
|
|
|
|
|
|
|
'NoteOff' => 'MIDI::XML::NoteOff', |
54
|
|
|
|
|
|
|
'NoteOn' => 'MIDI::XML::NoteOn', |
55
|
|
|
|
|
|
|
'PolyKeyPressure' => 'MIDI::XML::PolyKeyPressure', |
56
|
|
|
|
|
|
|
'ControlChange' => 'MIDI::XML::ControlChange', |
57
|
|
|
|
|
|
|
'AllSoundOff' => 'MIDI::XML::ControlChange', |
58
|
|
|
|
|
|
|
'ResetAllControllers' => 'MIDI::XML::ControlChange', |
59
|
|
|
|
|
|
|
'LocalControl' => 'MIDI::XML::ControlChange', |
60
|
|
|
|
|
|
|
'AllNotesOff' => 'MIDI::XML::ControlChange', |
61
|
|
|
|
|
|
|
'OmniOff' => 'MIDI::XML::ControlChange', |
62
|
|
|
|
|
|
|
'OmniOn' => 'MIDI::XML::ControlChange', |
63
|
|
|
|
|
|
|
'MonoMode' => 'MIDI::XML::ControlChange', |
64
|
|
|
|
|
|
|
'PolyMode' => 'MIDI::XML::ControlChange', |
65
|
|
|
|
|
|
|
'ProgramChange' => 'MIDI::XML::ProgramChange', |
66
|
|
|
|
|
|
|
'ChannelKeyPressure' => 'MIDI::XML::ChannelKeyPressure', |
67
|
|
|
|
|
|
|
'PitchBendChange' => 'MIDI::XML::PitchBend', |
68
|
|
|
|
|
|
|
'SystemExclusive' => 'MIDI::XML::SystemExclusive', |
69
|
|
|
|
|
|
|
'EndOfExclusive' => 'MIDI::XML::EndOfExclusive', |
70
|
|
|
|
|
|
|
'SequenceNumber' => 'MIDI::XML::SequenceNumber', |
71
|
|
|
|
|
|
|
'TextEvent' => 'MIDI::XML::TextEvent', |
72
|
|
|
|
|
|
|
'CopyrightNotice' => 'MIDI::XML::CopyrightNotice', |
73
|
|
|
|
|
|
|
'TrackName' => 'MIDI::XML::TrackName', |
74
|
|
|
|
|
|
|
'InstrumentName' => 'MIDI::XML::InstrumentName', |
75
|
|
|
|
|
|
|
'Lyric' => 'MIDI::XML::Lyric', |
76
|
|
|
|
|
|
|
'Marker' => 'MIDI::XML::Marker', |
77
|
|
|
|
|
|
|
'CuePoint' => 'MIDI::XML::CuePoint', |
78
|
|
|
|
|
|
|
'ProgramName' => 'MIDI::XML::ProgramName', |
79
|
|
|
|
|
|
|
'DeviceName' => 'MIDI::XML::DeviceName', |
80
|
|
|
|
|
|
|
'MidiChannelPrefix' => 'MIDI::XML::MidiChannelPrefix', |
81
|
|
|
|
|
|
|
'Port' => 'MIDI::XML::Port', |
82
|
|
|
|
|
|
|
'EndOfTrack' => 'MIDI::XML::EndOfTrack', |
83
|
|
|
|
|
|
|
'SetTempo' => 'MIDI::XML::SetTempo', |
84
|
|
|
|
|
|
|
'SmpteOffset' => 'MIDI::XML::SmpteOffset', |
85
|
|
|
|
|
|
|
'TimeSignature' => 'MIDI::XML::TimeSignature', |
86
|
|
|
|
|
|
|
'KeySignature' => 'MIDI::XML::KeySignature', |
87
|
|
|
|
|
|
|
'SequencerSpecific' => 'MIDI::XML::SequencerSpecific', |
88
|
|
|
|
|
|
|
'OtherMetaEvent' => 'MIDI::XML::MetaEvent', |
89
|
|
|
|
|
|
|
}; |
90
|
|
|
|
|
|
|
|
91
|
0
|
|
|
|
|
|
my $parser = XML::Parser->new( Handlers => { |
92
|
|
|
|
|
|
|
# Init => \&start_doc_MidiXML, |
93
|
|
|
|
|
|
|
Start => \&start_elem_MidiXML, |
94
|
|
|
|
|
|
|
Char => \&char_data, |
95
|
|
|
|
|
|
|
End => \&end_elem_MidiXML, |
96
|
|
|
|
|
|
|
# Final => \&end_doc_MidiXML, |
97
|
|
|
|
|
|
|
}); |
98
|
0
|
|
|
|
|
|
$parser->parsefile($file); |
99
|
0
|
|
|
|
|
|
return $MIDI::XML::Parser::MidiFile; |
100
|
|
|
|
|
|
|
|
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
#========================================================================== |
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
=item start_elem($expat, $name, %attr); |
106
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
Handler for XML::Parser start element events. |
108
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
=cut |
110
|
|
|
|
|
|
|
|
111
|
|
|
|
|
|
|
sub start_elem_MidiXML { |
112
|
0
|
|
|
0
|
0
|
|
my $expat; |
113
|
|
|
|
|
|
|
my $name; |
114
|
0
|
|
|
|
|
|
($expat, $name, %MIDI::XML::Parser::attr) = @_; |
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
# print %attr,"\n"; |
117
|
0
|
|
|
|
|
|
$MIDI::XML::Parser::cdata = undef; |
118
|
|
|
|
|
|
|
|
119
|
0
|
0
|
|
|
|
|
if ($name eq 'MIDIFile') { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
120
|
0
|
|
|
|
|
|
$MidiFile = MIDI::XML::MidiFile->new(); |
121
|
|
|
|
|
|
|
} elsif ($name eq 'Format') { |
122
|
|
|
|
|
|
|
} elsif ($name eq 'Tracks') { |
123
|
|
|
|
|
|
|
} elsif ($name eq 'TicksPerBeat') { |
124
|
|
|
|
|
|
|
} elsif ($name eq 'FrameRate') { |
125
|
|
|
|
|
|
|
} elsif ($name eq 'TicksPerFrame') { |
126
|
|
|
|
|
|
|
} elsif ($name eq 'TimestampType') { |
127
|
|
|
|
|
|
|
} elsif ($name eq 'Track') { |
128
|
0
|
|
|
|
|
|
$Track = MIDI::XML::Track->new(); |
129
|
0
|
|
|
|
|
|
$Track->number($attr{'Number'}); |
130
|
|
|
|
|
|
|
} elsif ($name eq 'Event') { |
131
|
0
|
|
|
|
|
|
$delta = undef; |
132
|
0
|
|
|
|
|
|
$absolute = undef; |
133
|
|
|
|
|
|
|
} else { |
134
|
|
|
|
|
|
|
} |
135
|
|
|
|
|
|
|
# print $name, %attr, "\n"; |
136
|
|
|
|
|
|
|
} |
137
|
|
|
|
|
|
|
|
138
|
|
|
|
|
|
|
#========================================================================== |
139
|
|
|
|
|
|
|
|
140
|
|
|
|
|
|
|
=item char_data($expat, $name, $cdata); |
141
|
|
|
|
|
|
|
|
142
|
|
|
|
|
|
|
Handler for XML::Parser character data events. |
143
|
|
|
|
|
|
|
|
144
|
|
|
|
|
|
|
=cut |
145
|
|
|
|
|
|
|
|
146
|
|
|
|
|
|
|
sub char_data { |
147
|
0
|
|
|
0
|
1
|
|
my $expat = shift; |
148
|
0
|
|
|
|
|
|
$MIDI::XML::Parser::cdata = shift; |
149
|
|
|
|
|
|
|
} |
150
|
|
|
|
|
|
|
|
151
|
|
|
|
|
|
|
#========================================================================== |
152
|
|
|
|
|
|
|
|
153
|
|
|
|
|
|
|
=item end_elem($expat, $name); |
154
|
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
Handler for XML::Parser end element events. |
156
|
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
=back |
158
|
|
|
|
|
|
|
|
159
|
|
|
|
|
|
|
=cut |
160
|
|
|
|
|
|
|
|
161
|
|
|
|
|
|
|
sub end_elem_MidiXML { |
162
|
0
|
|
|
0
|
0
|
|
my $expat = shift; |
163
|
0
|
|
|
|
|
|
my $name = shift; |
164
|
|
|
|
|
|
|
|
165
|
0
|
0
|
|
|
|
|
if ($name eq 'MIDIFile') { |
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
|
|
0
|
|
|
|
|
|
166
|
|
|
|
|
|
|
} elsif ($name eq 'Format') { |
167
|
0
|
|
|
|
|
|
$MidiFile->format($cdata); |
168
|
|
|
|
|
|
|
} elsif ($name eq 'Tracks') { |
169
|
0
|
|
|
|
|
|
$MidiFile->track_count($cdata); |
170
|
|
|
|
|
|
|
} elsif ($name eq 'TicksPerBeat') { |
171
|
0
|
|
|
|
|
|
$MidiFile->ticks_per_beat($cdata); |
172
|
|
|
|
|
|
|
} elsif ($name eq 'FrameRate') { |
173
|
0
|
|
|
|
|
|
$MidiFile->frame_rate($cdata); |
174
|
|
|
|
|
|
|
} elsif ($name eq 'TicksPerFrame') { |
175
|
0
|
|
|
|
|
|
$MidiFile->ticks_per_frame($cdata); |
176
|
|
|
|
|
|
|
} elsif ($name eq 'TimestampType') { |
177
|
0
|
|
|
|
|
|
$MidiFile->timestamp_type($cdata); |
178
|
|
|
|
|
|
|
} elsif ($name eq 'Track') { |
179
|
0
|
|
|
|
|
|
$MidiFile->append($Track); |
180
|
|
|
|
|
|
|
} elsif ($name eq 'Delta') { |
181
|
0
|
|
|
|
|
|
$delta = $cdata + 0; |
182
|
|
|
|
|
|
|
} elsif ($name eq 'Absolute') { |
183
|
0
|
|
|
|
|
|
$absolute = $cdata + 0; |
184
|
|
|
|
|
|
|
} else { |
185
|
0
|
|
|
|
|
|
$attr{'_ELEMENT'} = $name; |
186
|
0
|
|
|
|
|
|
$attr{'_CDATA'} = $cdata; |
187
|
0
|
0
|
|
|
|
|
$attr{'Delta'} = $delta if (defined($delta)); |
188
|
0
|
0
|
|
|
|
|
$attr{'Absolute'} = $absolute if (defined($absolute)); |
189
|
0
|
0
|
|
|
|
|
if (${$element2class}{ $name }) { |
|
0
|
|
|
|
|
|
|
190
|
0
|
|
|
|
|
|
$Track->append($element2class->{ $name }->new(\%attr)); |
191
|
|
|
|
|
|
|
} |
192
|
|
|
|
|
|
|
} |
193
|
|
|
|
|
|
|
|
194
|
|
|
|
|
|
|
} |
195
|
|
|
|
|
|
|
|
196
|
|
|
|
|
|
|
1; |
197
|
|
|
|
|
|
|
__END__ |