2 midi-track-parser.cc -- implement
4 source file of the GNU LilyPond music typesetter
6 (c) 1997 Jan Nieuwenhuizen <jan@digicash.com>
10 #include "string-convert.hh"
11 #include "mi2mu-global.hh"
12 #include "midi-track-parser.hh"
13 #include "mudela-column.hh"
14 #include "mudela-item.hh"
15 #include "mudela-score.hh"
16 #include "mudela-staff.hh"
18 Midi_track_parser::Midi_track_parser (Midi_parser_info
* info_l
)
23 mudela_staff_p_
= new Mudela_staff (0, "", "", "");
28 Midi_track_parser::~Midi_track_parser ()
30 delete mudela_staff_p_
;
35 Midi_track_parser::at_mom ()
41 Midi_track_parser::eot ()
43 if ( info_l_
->byte_L_
< info_l_
->end_byte_L_
)
49 Midi_track_parser::note_end (Mudela_column
* col_l
, int channel_i
, int pitch_i
, int aftertouch_i
)
54 for (PCursor
<Mudela_note
*> i (open_note_l_list_
.top ()); i
.ok (); i
++)
56 if ((i
->pitch_i_
== pitch_i
) && (i
->channel_i_
== channel_i
))
58 i
->end_column_l_
= col_l
;
59 // LOGOUT(DEBUG_ver) << "Note: " << pitch_i;
60 // LOGOUT(DEBUG_ver) << "; " << i->mudela_column_l_->at_mom_;
61 // LOGOUT(DEBUG_ver) << ", " << i->end_column_l_->at_mom_ << "\n";
66 warning (String ("junking note-end event: ")
67 + " channel = " + String_convert::i2dec_str (channel_i
, 0, ' ')
68 + ", pitch = " + String_convert::i2dec_str (pitch_i
, 0, ' '));
72 Midi_track_parser::note_end_all (Mudela_column
* col_l
)
75 for (PCursor
<Mudela_note
*> i (open_note_l_list_
.top ()); i
.ok (); i
++)
77 i
->end_column_l_
= col_l
;
86 Midi_track_parser::parse (Mudela_column
* col_l
)
88 Moment mom
= at_mom ();
89 while (!eot () && (mom
== at_mom ()))
91 Mudela_item
* p
= parse_event (col_l
);
93 mudela_staff_p_
->add_item (p
);
99 Mudela_staff
* p
= mudela_staff_p_
;
105 Midi_track_parser::parse_delta_time ()
109 int delta_i
= get_var_i ();
110 at_mom_
+= Moment (delta_i
, info_l_
->division_1_i_
);
114 Midi_track_parser::parse_event (Mudela_column
* col_l
)
116 Byte byte
= peek_byte ();
117 // RUNNING_STATUS [\x00-\x5f]
120 if (running_byte_
<= 0x5f)
121 exit ("Invalid running status");
123 'running status' rather means 'missing status'.
124 we'll just pretend we read the running status byte.
126 byte
= running_byte_
;
131 Mudela_item
* item_p
= 0;
132 // DATA_ENTRY [\x60-\x79]
133 if ((byte
>= 0x60) && (byte
<= 0x79))
137 // ALL_NOTES_OFF [\x7a-\x7f]
138 else if ((byte
>= 0x7a) && (byte
<= 0x7f))
142 note_end_all (col_l
);
144 // NOTE_OFF [\x80-\x8f]
145 else if ((byte
>= 0x80) && (byte
<= 0x8f))
147 running_byte_
= byte
;
148 int channel_i
= byte
& ~0x90;
149 int pitch_i
= (int)next_byte ();
150 int dyn_i
= (int)next_byte ();
151 note_end (col_l
, channel_i
, pitch_i
, dyn_i
);
153 // NOTE_ON [\x90-\x9f]
154 else if ((byte
>= 0x90) && (byte
<= 0x9f))
156 running_byte_
= byte
;
157 int channel_i
= byte
& ~0x90;
158 int pitch_i
= (int)next_byte ();
159 int dyn_i
= (int)next_byte ();
161 sss: some broken devices encode NOTE_OFF as
162 NOTE_ON with zero volume
166 Mudela_note
* p
= new Mudela_note (col_l
, channel_i
, pitch_i
, dyn_i
);
168 open_note_l_list_
.bottom ().add (p
);
172 note_end (col_l
, channel_i
, pitch_i
, dyn_i
);
175 // POLYPHONIC_AFTERTOUCH [\xa0-\xaf]
176 else if ((byte
>= 0xa0) && (byte
<= 0xaf))
178 running_byte_
= byte
;
182 // CONTROLMODE_CHANGE [\xb0-\xbf]
183 else if ((byte
>= 0xb0) && (byte
<= 0xbf))
185 running_byte_
= byte
;
189 // PROGRAM_CHANGE [\xc0-\xcf]
190 else if ((byte
>= 0xc0) && (byte
<= 0xcf))
192 running_byte_
= byte
;
195 // CHANNEL_AFTERTOUCH [\xd0-\xdf]
196 else if ((byte
>= 0xd0) && (byte
<= 0xdf))
198 running_byte_
= byte
;
202 // PITCHWHEEL_RANGE [\xe0-\xef]
203 else if ((byte
>= 0xe0) && (byte
<= 0xef))
205 running_byte_
= byte
;
209 // SYSEX_EVENT1 [\xf0]
210 else if (byte
== 0xf0)
212 int length_i
= get_var_i ();
213 String str
= get_str (length_i
);
215 // SYSEX_EVENT2 [\xf7]
216 else if (byte
== 0xf7)
218 int length_i
= get_var_i ();
219 String str
= get_str (length_i
);
222 else if (byte
== 0xff)
224 // SEQUENCE [\x00][\x02]
232 // YYCOPYRIGHT [\x02]
233 // YYTRACK_NAME [\x03]
234 // YYINSTRUMENT_NAME [\x04]
237 // YYCUE_POINT [\x07]
238 else if ((byte
>= 0x01) && (byte
<= 0x07))
240 // LOGOUT (DEBUG_ver) << "\n% Text(" << (int)byte << "):" << flush;
241 int length_i
= get_var_i ();
242 String str
= get_str (length_i
);
243 // LOGOUT (DEBUG_ver) << str << endl;
244 Mudela_text::Type t
= (Mudela_text::Type
)byte
;
245 Mudela_text
* p
= new Mudela_text (t
, str
);
247 if (t
== Mudela_text::COPYRIGHT
)
248 mudela_staff_p_
->copyright_str_
= p
->text_str_
;
249 else if (t
== Mudela_text::TRACK_NAME
)
250 mudela_staff_p_
->name_str_
= p
->text_str_
;
251 else if (t
== Mudela_text::INSTRUMENT_NAME
)
252 mudela_staff_p_
->instrument_str_
= p
->text_str_
;
254 // END_OF_TRACK [\x2f][\x00]
257 Byte next
= peek_byte ();
258 if ((byte
== 0x2f) && (next
== 0x00))
261 info_l_
->byte_L_
= info_l_
->end_byte_L_
;
263 // TEMPO [\x51][\x03]
264 else if ((byte
== 0x51) && (next
== 0x03))
267 unsigned useconds_per_4_u
= get_u (3);
268 // $$ = new Mudela_tempo ( ($2 << 16) + ($3 << 8) + $4);
269 // LOGOUT (DEBUG_ver) << $$->str() << endl;
270 Mudela_tempo
* p
= new Mudela_tempo ( useconds_per_4_u
);
272 info_l_
->score_l_
->mudela_tempo_l_
= p
;
274 // SMPTE_OFFSET [\x54][\x05]
275 else if ((byte
== 0x54) && (next
== 0x05))
285 else if ((byte
== 0x58) && (next
== 0x04))
288 int num_i
= (int)next_byte ();
289 int den_i
= (int)next_byte ();
290 int clocks_4_i
= (int)next_byte ();
291 int count_32_i
= (int)next_byte ();
292 Mudela_meter
* p
= new Mudela_meter ( num_i
, den_i
, clocks_4_i
, count_32_i
);
294 info_l_
->score_l_
->mudela_meter_l_
= p
;
295 info_l_
->bar_mom_
= p
->bar_mom ();
298 else if ((byte
== 0x59) && (next
== 0x02))
301 int accidentals_i
= (int)next_byte ();
302 int minor_i
= (int)next_byte ();
303 Mudela_key
* p
= new Mudela_key (accidentals_i
, minor_i
);
305 info_l_
->score_l_
->mudela_key_l_
= p
;
307 // SSME [\0x7f][\x03]
308 else if ((byte
== 0x7f) && (next
== 0x03))
311 int length_i
= get_var_i ();
312 String str
= get_str (length_i
);
313 item_p
= new Mudela_text ((Mudela_text::Type
)byte
, str
);
319 warning ("Unimplemented MIDI meta-event");
324 exit ("Invalid MIDI event");
327 item_p
->mudela_column_l_
= col_l
;
335 Midi_track_parser::parse_header ()
337 String str
= get_str (4);
339 exit ("MIDI track expected");
341 int length_i
= get_i (4);
344 exit ("Invalid track length");
345 assert (!track_info_p_
);
346 track_info_p_
= new Midi_parser_info (*info_l_
);
347 track_info_p_
->end_byte_L_
= track_info_p_
->byte_L_
+ length_i
;
348 forward_byte_L (length_i
);
349 // forward_byte_L (length_i-1);
350 info_l_
= track_info_p_
;