lilypond-1.3.153
[lilypond.git] / mi2mu / my-midi-parser.cc
blob2a7e2d77bcf5216738b9d1e03b44925620c87274
1 //
2 // my-midi-parser.cc -- implement My_midi_parser
3 //
4 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
6 #include "string-convert.hh"
7 #include "duration-convert.hh"
8 #include "mi2mu-global.hh"
9 #include "my-midi-lexer.hh"
10 #include "my-midi-parser.hh"
11 #include "mudela-column.hh"
12 #include "mudela-item.hh"
13 #include "mudela-score.hh"
14 #include "mudela-staff.hh"
16 void
17 yyerror(char const* sz_l)
19 midi_parser_l_g->error (sz_l);
23 My_midi_parser* midi_parser_l_g = 0;
25 My_midi_parser::My_midi_parser (String filename_str, Sources *sources_l)
27 filename_str_ = filename_str;
28 midi_lexer_p_ = new My_midi_lexer (filename_str_,sources_l);
29 midi_lexer_l_g = midi_lexer_p_; // ugh
31 bar_i_ = 1;
33 defined_ch_C_ = 0;
34 fatal_error_i_ = 0;
36 mudela_column_l_ = 0;
37 mudela_score_p_ = new Mudela_score (1, 1, 1);
40 // ugh, belong to Mudela_{score,staff}
41 track_i_ = 0;
42 mudela_staff_l_ = 0;
43 mudela_key_p_ = 0;
44 mudela_tempo_p_ = 0;
45 mudela_meter_p_ = 0;
47 reset();
50 My_midi_parser::~My_midi_parser()
52 midi_lexer_l_g = 0; // ugh
54 delete midi_lexer_p_;
55 delete mudela_key_p_;
56 delete mudela_tempo_p_;
57 delete mudela_meter_p_;
58 delete mudela_score_p_;
61 void
62 My_midi_parser::reset()
64 // open_mudela_note_l_list_.clear();
65 open_mudela_note_l_list_.junk_links();
67 // ugh
68 delete mudela_key_p_;
69 mudela_key_p_ = new Mudela_key (0, 0);
70 // useconds per 4: 250000 === 60 4 per minute
71 delete mudela_tempo_p_;
72 mudela_tempo_p_ = new Mudela_tempo (1000000);
73 delete mudela_meter_p_;
74 mudela_meter_p_ = new Mudela_meter (4, 2, 24, 8);
76 bar_i_ = 1;
77 mudela_column_l_ = mudela_score_p_->mudela_column_l (0);
79 // ugh
80 copyright_str_ = "";
81 track_name_str_ = "";
82 instrument_str_ = "";
85 void
86 My_midi_parser::add_score (Mudela_score* mudela_score_p)
88 assert (mudela_score_p_);
90 #if 0 // ugh, already constructed
91 mudela_score_p_ = mudela_score_p;
92 if (!mudela_column_l_)
93 mudela_column_l_ = mudela_score_p_->mudela_column_l (0);
94 #endif
96 mudela_score_p_->mudela_key_l_ = mudela_key_p_;
97 mudela_score_p_->mudela_meter_l_ = mudela_meter_p_;
98 mudela_score_p_->mudela_tempo_l_ = mudela_tempo_p_;
99 bar_i_ = 1;
102 void
103 My_midi_parser::error (char const* sz_l)
105 midi_lexer_l_g->error (sz_l);
107 if (fatal_error_i_)
108 exit (fatal_error_i_);
111 void
112 My_midi_parser::forward (int i)
114 if (!i)
115 return;
117 Duration dur;
118 dur.durlog_i_ = -100;
119 dur.set_ticks (i);
120 Moment mom = at_mom() + Duration_convert::dur2_mom (dur);
122 mudela_column_l_ = mudela_score_p_->mudela_column_l (mom);
124 if (i)
126 int bars_i = (int) (mom / mudela_meter_p_->bar_mom());
127 if (bars_i > bar_i_)
128 LOGOUT(NORMAL_ver) << '[' << bar_i_ << ']' << flush;
129 bar_i_ = bars_i;
133 Moment
134 My_midi_parser::at_mom()
136 assert (mudela_column_l_);
137 // return mudela_column_l_ ? mudela_column_l_->at_mom() : 0;
138 return mudela_column_l_->at_mom();
141 void
142 My_midi_parser::note_begin (int channel_i, int pitch_i, int dyn_i)
144 // junk dynamics
145 (void)dyn_i;
147 Mudela_note* p = new Mudela_note (mudela_column_l_, channel_i, pitch_i, dyn_i);
148 // ugh, score doesn't know about last staff yet...
149 // mudela_score_p_->add_item (p);
150 mudela_staff_l_->add_item (p);
151 open_mudela_note_l_list_.bottom().add (p);
154 void
155 My_midi_parser::note_end (int channel_i, int pitch_i, int aftertouch_i)
157 // junk dynamics
158 (void)aftertouch_i;
160 // find
161 for (PCursor<Mudela_note*> i (open_mudela_note_l_list_); i.ok(); i++)
163 if ( (i->pitch_i_ == pitch_i) && (i->channel_i_ == channel_i))
165 i->end_column_l_ = mudela_column_l_;
166 LOGOUT(DEBUG_ver) << "Note: " << pitch_i;
167 LOGOUT(DEBUG_ver) << "; " << i->mudela_column_l_->at_mom_;
168 LOGOUT(DEBUG_ver) << ", " << i->end_column_l_->at_mom_ << "\n";
169 i.remove_p();
170 return;
173 warning (String ("junking note-end event: ")
174 + " channel = " + String_convert::i2dec_str (channel_i, 0, ' ')
175 + ", pitch = " + String_convert::i2dec_str (pitch_i, 0, ' '));
178 void
179 My_midi_parser::note_end_all()
181 // find
182 for (PCursor<Mudela_note*> i (open_mudela_note_l_list_); i.ok(); i++)
184 i->end_column_l_ = mudela_column_l_;
185 i.remove_p();
186 // ugh
187 if (!i.ok())
188 break;
193 My_midi_parser::parse()
195 LOGOUT(NORMAL_ver) << "\nParsing..." << flush;
196 int i = ::yyparse();
197 if (!i)
198 note_end_all();
199 return i;
202 void
203 My_midi_parser::set_division_4 (int division_4_i)
205 division_1_i_ = division_4_i * 4;
206 // ugh
207 Duration::division_1_i_s = division_1_i_;
208 if (division_4_i < 0)
209 warning ("seconds iso metrical time");
212 void
213 My_midi_parser::set_key (int accidentals_i, int minor_i)
215 delete mudela_key_p_;
216 mudela_key_p_ = new Mudela_key (accidentals_i, minor_i);
219 void
220 My_midi_parser::set_meter (int num_i, int den_i, int clocks_i, int count_32_i)
222 delete mudela_meter_p_;
223 mudela_meter_p_ = new Mudela_meter (num_i, den_i, clocks_i, count_32_i);
226 void
227 My_midi_parser::set_tempo (int useconds_per_4_i)
229 delete mudela_tempo_p_;
230 mudela_tempo_p_ = new Mudela_tempo (useconds_per_4_i);