]> git.donarmstrong.com Git - lilypond.git/blob - mi2mu/midi-track-parser.cc
patch::: 0.1.17.jcn1: pats
[lilypond.git] / mi2mu / midi-track-parser.cc
1 /*
2   midi-track-parser.cc -- implement 
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997 Jan Nieuwenhuizen <jan@digicash.com>
7 */
8
9 #include <assert.h>
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"
17
18 Midi_track_parser::Midi_track_parser (Midi_parser_info* info_l)
19 {
20   info_l_ = info_l;
21   at_mom_ = 0;
22   track_info_p_ = 0;
23   mudela_staff_p_ = new Mudela_staff (0, "", "", "");
24   parse_header ();
25   parse_delta_time ();
26 }
27
28 Midi_track_parser::~Midi_track_parser ()
29 {
30   delete mudela_staff_p_;
31   delete track_info_p_;
32 }
33
34 Moment
35 Midi_track_parser::at_mom ()
36 {
37   return at_mom_;
38 }
39
40 bool
41 Midi_track_parser::eot ()
42 {
43   if ( info_l_->byte_L_ < info_l_->end_byte_L_ )
44     return false;
45   return true;
46 }
47
48 void
49 Midi_track_parser::note_end (Mudela_column* col_l, int channel_i, int pitch_i, int aftertouch_i )
50 {
51   // junk dynamics
52   (void)aftertouch_i;
53
54   assert (col_l);
55
56   for (PCursor<Mudela_note*> i (open_note_l_list_.top ()); i.ok (); i++) 
57     {
58       if ((i->pitch_i_ == pitch_i) && (i->channel_i_ == channel_i)) 
59         {
60           i->end_column_l_ = col_l;
61           // LOGOUT(DEBUG_ver) << "Note: " << pitch_i;
62           // LOGOUT(DEBUG_ver) << "; " << i->mudela_column_l_->at_mom_;
63           // LOGOUT(DEBUG_ver) << ", " << i->end_column_l_->at_mom_ << "\n";
64           i.remove_p();
65           return;
66         }
67     }
68   warning (String ("junking note-end event: ")
69            + " channel = " + String_convert::i2dec_str (channel_i, 0, ' ')
70            + ", pitch = " + String_convert::i2dec_str (pitch_i, 0, ' '));
71 }
72
73 void
74 Midi_track_parser::note_end_all (Mudela_column* col_l) 
75 {
76   // find 
77   assert (col_l);
78   for (PCursor<Mudela_note*> i (open_note_l_list_.top ()); i.ok (); i++) 
79     {
80       i->end_column_l_ = col_l;
81       i.remove_p();
82       // ugh
83       if (!i.ok())
84         break;
85     }
86 }
87
88 Mudela_staff*
89 Midi_track_parser::parse (Mudela_column* col_l)
90 {
91   Moment mom = at_mom ();
92   while (!eot () && (mom == at_mom ()))
93     {
94       Mudela_item* p = parse_event (col_l);
95       if (p)
96         mudela_staff_p_->add_item (p);
97     }
98
99   if (!eot())
100     return 0;
101
102   // vangnet
103   note_end_all (col_l);
104
105   Mudela_staff* p = mudela_staff_p_;
106   mudela_staff_p_ = 0;
107   return p;
108 }
109
110 void
111 Midi_track_parser::parse_delta_time ()
112 {
113   if (eot ())
114     return;
115   int delta_i = get_var_i ();
116   at_mom_ += Moment (delta_i, info_l_->division_1_i_);  
117 }
118
119 Mudela_item*
120 Midi_track_parser::parse_event (Mudela_column* col_l)
121
122   Byte byte = peek_byte ();
123   // RUNNING_STATUS     [\x00-\x5f]
124   if (byte <= 0x5f) 
125     {
126       if (running_byte_ <= 0x5f) 
127         exit ("Invalid running status");
128       /*
129         'running status' rather means 'missing status'.
130         we'll just pretend we read the running status byte.
131         */
132       byte = running_byte_;
133     }
134   else
135     byte = next_byte ();
136
137   Mudela_item* item_p = 0;
138   // DATA_ENTRY [\x60-\x79]
139   if ((byte >= 0x60) && (byte <= 0x79))
140     {
141             next_byte ();
142     }
143   // ALL_NOTES_OFF      [\x7a-\x7f]
144   else if ((byte >= 0x7a) && (byte <= 0x7f))
145     {
146       next_byte ();
147       next_byte ();
148       note_end_all (col_l);
149     }
150   // NOTE_OFF   [\x80-\x8f]
151   else if ((byte >= 0x80) && (byte <= 0x8f))
152     {
153       running_byte_ = byte;
154       int channel_i = byte & ~0x90;
155       int pitch_i = (int)next_byte ();
156       int dyn_i = (int)next_byte ();
157       note_end (col_l, channel_i, pitch_i, dyn_i);
158     }
159   // NOTE_ON            [\x90-\x9f]
160   else if ((byte >= 0x90) && (byte <= 0x9f))
161     {
162       running_byte_ = byte;
163       int channel_i = byte & ~0x90;
164       int pitch_i = (int)next_byte ();
165       int dyn_i = (int)next_byte ();
166       /*
167         sss: some broken devices encode NOTE_OFF as 
168         NOTE_ON with zero volume
169         */
170       if (dyn_i)
171         {
172           Mudela_note* p = new Mudela_note (col_l, channel_i, pitch_i, dyn_i);
173           item_p = p;
174           open_note_l_list_.bottom ().add (p);
175         }
176       else
177         {
178           note_end (col_l, channel_i, pitch_i, dyn_i);
179         }
180     }
181   // POLYPHONIC_AFTERTOUCH      [\xa0-\xaf]
182   else if ((byte >= 0xa0) && (byte <= 0xaf))
183     {
184       running_byte_ = byte;
185       next_byte ();
186       next_byte ();
187     }
188   // CONTROLMODE_CHANGE [\xb0-\xbf]
189   else if ((byte >= 0xb0) && (byte <= 0xbf))
190     {
191       running_byte_ = byte;
192       next_byte ();
193       next_byte ();
194     }
195   // PROGRAM_CHANGE     [\xc0-\xcf]
196   else if ((byte >= 0xc0) && (byte <= 0xcf))
197     {
198       running_byte_ = byte;
199       next_byte ();
200     }
201   // CHANNEL_AFTERTOUCH [\xd0-\xdf]
202   else if ((byte >= 0xd0) && (byte <= 0xdf))
203     {
204       running_byte_ = byte;
205       next_byte ();
206       next_byte ();
207     }
208   // PITCHWHEEL_RANGE   [\xe0-\xef]
209   else if ((byte >= 0xe0) && (byte <= 0xef))
210     {
211       running_byte_ = byte;
212       next_byte ();
213       next_byte ();
214     }
215   // SYSEX_EVENT1       [\xf0]
216   else if (byte == 0xf0)
217     {
218       int length_i = get_var_i ();
219       String str = get_str (length_i);
220     }
221   // SYSEX_EVENT2       [\xf7]
222   else if (byte == 0xf7)
223     {
224       int length_i = get_var_i ();
225       String str = get_str (length_i);
226     }
227   // META_EVENT [\xff]
228   else if (byte == 0xff)
229     {
230       // SEQUENCE       [\x00][\x02]
231       byte = next_byte ();
232       if (byte == 0)
233         {
234           next_byte ();
235           get_i (2);
236         }
237       // YYTEXT         [\x01] 
238       // YYCOPYRIGHT    [\x02]
239       // YYTRACK_NAME   [\x03]
240       // YYINSTRUMENT_NAME      [\x04]
241       // YYLYRIC                [\x05]
242       // YYMARKER               [\x06]
243       // YYCUE_POINT    [\x07]
244       else if ((byte >= 0x01) && (byte <= 0x07))
245         {
246           // LOGOUT (DEBUG_ver) << "\n% Text(" << (int)byte << "):" << flush;
247           int length_i = get_var_i ();
248           String str = get_str (length_i);
249           // LOGOUT (DEBUG_ver) << str << endl;
250           Mudela_text::Type t = (Mudela_text::Type)byte;
251           Mudela_text* p = new Mudela_text (t, str);
252           item_p = p;
253           if (t == Mudela_text::COPYRIGHT) 
254              mudela_staff_p_->copyright_str_ = p->text_str_;
255           else if (t == Mudela_text::TRACK_NAME)
256              mudela_staff_p_->name_str_ = p->text_str_;
257           else if (t == Mudela_text::INSTRUMENT_NAME)
258              mudela_staff_p_->instrument_str_ = p->text_str_;
259         }
260       // END_OF_TRACK   [\x2f][\x00]
261       else
262         {
263           Byte next = peek_byte ();
264           if ((byte == 0x2f) && (next == 0x00))
265             {
266               next_byte ();
267               info_l_->byte_L_ = info_l_->end_byte_L_;
268             }
269           // TEMPO              [\x51][\x03]
270           else if ((byte == 0x51) && (next == 0x03))
271             {
272               next_byte ();
273               unsigned useconds_per_4_u = get_u (3);
274               // $$ = new Mudela_tempo ( ($2 << 16) + ($3 << 8) + $4);
275               // LOGOUT (DEBUG_ver) << $$->str() << endl;
276               Mudela_tempo* p = new Mudela_tempo ( useconds_per_4_u );
277               item_p = p;
278               info_l_->score_l_->mudela_tempo_l_ = p;
279             }
280           // SMPTE_OFFSET       [\x54][\x05]
281           else if ((byte == 0x54) && (next == 0x05))
282             {
283               next_byte ();
284               (int)next_byte ();
285               (int)next_byte ();
286               (int)next_byte ();
287               (int)next_byte ();
288               (int)next_byte ();
289             }
290           // TIME               [\x58][\x04]
291           else if ((byte == 0x58) && (next == 0x04))
292             {
293               next_byte ();
294               int num_i = (int)next_byte ();
295               int den_i = (int)next_byte ();
296               int clocks_4_i = (int)next_byte ();
297               int count_32_i = (int)next_byte ();
298               Mudela_meter* p = new Mudela_meter ( num_i, den_i, clocks_4_i, count_32_i );
299               item_p = p;
300               info_l_->score_l_->mudela_meter_l_ = p;
301               info_l_->bar_mom_ = p->bar_mom ();
302             }
303           // KEY                [\x59][\x02]
304           else if ((byte == 0x59) && (next == 0x02))
305             {
306               next_byte ();
307               int accidentals_i = (int)next_byte ();
308               int minor_i = (int)next_byte ();
309               Mudela_key* p = new Mudela_key (accidentals_i, minor_i);
310               item_p = p;
311               info_l_->score_l_->mudela_key_l_ = p;
312             }
313           // SSME               [\0x7f][\x03]
314           else if ((byte == 0x7f) && (next == 0x03))
315             {
316               next_byte ();
317               int length_i = get_var_i ();
318               String str = get_str (length_i);
319               item_p = new Mudela_text ((Mudela_text::Type)byte, str);
320             }
321           else
322             {
323               next_byte ();
324               next_byte ();
325               warning ("Unimplemented MIDI meta-event");
326             }
327         }
328     }
329   else
330     exit ("Invalid MIDI event");
331
332   if (item_p)
333     item_p->mudela_column_l_ = col_l;
334
335   parse_delta_time ();
336
337   return item_p;
338 }
339
340 void
341 Midi_track_parser::parse_header ()
342
343   String str = get_str (4);
344   if ( str != "MTrk" )
345     exit ("MIDI track expected");
346
347   int length_i = get_i (4);
348   // is this signed?
349   if (length_i < 0)
350     exit ("Invalid track length");
351   assert (!track_info_p_);
352   track_info_p_ = new Midi_parser_info (*info_l_);
353   track_info_p_->end_byte_L_ = track_info_p_->byte_L_ + length_i;
354   forward_byte_L (length_i);
355 //  forward_byte_L (length_i-1);
356   info_l_ = track_info_p_;
357 }
358