2 midi-item.cc -- implement Midi items.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2001 Jan Nieuwenhuizen <janneke@gnu.org>
13 #include "string-convert.hh"
14 #include "midi-item.hh"
15 #include "midi-stream.hh"
16 #include "audio-item.hh"
17 #include "duration.hh"
18 #include "scm-option.hh"
20 #include "killing-cons.tcc"
23 Midi_item::midi_p (Audio_item* a)
25 if (Audio_key* i = dynamic_cast<Audio_key*> (a))
26 return new Midi_key (i);
27 else if (Audio_instrument* i = dynamic_cast<Audio_instrument*> (a))
28 return i->str_.length_i () ? new Midi_instrument (i) : 0;
29 else if (Audio_note* i = dynamic_cast<Audio_note*> (a))
30 return new Midi_note (i);
31 else if (Audio_dynamic* i = dynamic_cast<Audio_dynamic*> (a))
32 return new Midi_dynamic (i);
33 else if (Audio_piano_pedal* i = dynamic_cast<Audio_piano_pedal*> (a))
34 return new Midi_piano_pedal (i);
35 else if (Audio_tempo* i = dynamic_cast<Audio_tempo*> (a))
36 return new Midi_tempo (i);
37 else if (Audio_time_signature* i = dynamic_cast<Audio_time_signature*> (a))
38 return new Midi_time_signature (i);
39 else if (Audio_text* i = dynamic_cast<Audio_text*> (a))
40 //return i->text_str_.length_i () ? new Midi_text (i) : 0;
41 return new Midi_text (i);
50 Midi_chunk::set (String header_str, String data_str, String footer_str)
53 footer_str_ = footer_str;
54 header_str_ = header_str;
58 Midi_chunk::data_str () const
64 Midi_chunk::str () const
66 String str = header_str_;
67 String dat = data_str ();
68 String length_str = String_convert::i2hex_str (dat.length_i ()
69 + footer_str_.length_i (), 8, '0');
70 length_str = String_convert::hex2bin_str (length_str);
77 Midi_duration::Midi_duration (Real seconds_f)
79 seconds_f_ = seconds_f;
83 Midi_duration::str () const
85 return String ("<duration: ") + to_str (seconds_f_) + ">";
88 Midi_event::Midi_event (Moment delta_mom, Midi_item* midi_p)
90 delta_mom_ = delta_mom;
95 ugh. midi output badly broken since grace note hackage.
98 Midi_event::str () const
100 Rational rat_dt = (delta_mom_.main_part_ * Rational (384) +
101 delta_mom_.grace_part_ * Rational (100))*Rational (4);
102 int delta_i = int (rat_dt);
104 String delta_str = Midi_item::i2varint_str (delta_i);
105 String midi_str = midi_p_->str ();
106 assert (midi_str.length_i ());
107 return delta_str + midi_str;
111 Midi_header::Midi_header (int format_i, int tracks_i, int clocks_per_4_i)
115 String format_str = String_convert::i2hex_str (format_i, 4, '0');
116 str += String_convert::hex2bin_str (format_str);
118 String tracks_str = String_convert::i2hex_str (tracks_i, 4, '0');
119 str += String_convert::hex2bin_str (tracks_str);
121 String tempo_str = String_convert::i2hex_str (clocks_per_4_i, 4, '0');
122 str += String_convert::hex2bin_str (tempo_str);
124 set ("MThd", str, "");
127 Midi_instrument::Midi_instrument (Audio_instrument* a)
130 audio_l_->str_.to_lower ();
134 Midi_instrument::str() const
136 Byte program_byte = 0;
138 SCM proc = scm_primitive_eval (ly_symbol2scm ("midi-program"));
139 SCM program = gh_call1 (proc, ly_symbol2scm (audio_l_->str_.ch_C()));
140 found = (program != SCM_BOOL_F);
142 program_byte = gh_scm2int(program);
144 warning (_f ("no such instrument: `%s'", audio_l_->str_.ch_C ()));
146 String str = to_str ((char) (0xc0 + channel_i_)); //YIKES! FIXME: Should be track. -rz
147 str += to_str ((char)program_byte);
151 Midi_item::Midi_item ()
156 Midi_item::~Midi_item ()
161 Midi_item::i2varint_str (int i)
163 int buffer_i = i & 0x7f;
164 while ((i >>= 7) > 0)
168 buffer_i += (i & 0x7f);
174 str += to_str ((char)buffer_i);
183 Midi_key::Midi_key (Audio_key*a)
189 Midi_key::str () const
191 String str = "ff5902";
192 str += String_convert::i2hex_str (audio_l_->accidentals_, 2, '0');
193 if (audio_l_->major_)
194 str += String_convert::i2hex_str (0, 2, '0');
196 str += String_convert::i2hex_str (1, 2, '0');
197 return String_convert::hex2bin_str (str);
200 Midi_time_signature::Midi_time_signature (Audio_time_signature* a)
203 clocks_per_1_i_ = 18;
207 Midi_time_signature::str () const
209 int num = audio_l_->beats_i_;
210 int den = audio_l_->one_beat_i_;
212 String str = "ff5804";
213 str += String_convert::i2hex_str (num, 2, '0');
214 str += String_convert::i2hex_str (intlog2 (den) , 2, '0');
215 str += String_convert::i2hex_str (clocks_per_1_i_, 2, '0');
216 str += String_convert::i2hex_str (8, 2, '0');
217 return String_convert::hex2bin_str (str);
220 Midi_note::Midi_note (Audio_note* a)
223 dynamic_byte_ = 0x7f;
227 Midi_note::length_mom () const
229 Moment m = audio_l_->length_mom_;
232 if (m < Moment (Rational (1, 1000)))
234 warning (_ ("silly duration"));
242 Midi_note::pitch_i () const
244 int p = audio_l_->pitch_.semitone_pitch () + audio_l_->transposing_i_;
247 warning (_ ("silly pitch"));
254 Midi_note::str () const
256 Byte status_byte = (char) (0x90 + channel_i_);
258 String str = to_str ((char)status_byte);
259 str += to_str ((char) (pitch_i () + c0_pitch_i_c_));
261 str += to_str ((char)dynamic_byte_);
265 Midi_note_off::Midi_note_off (Midi_note* n)
266 : Midi_note (n->audio_l_)
269 channel_i_ = n->channel_i_;
271 // Anybody who hears any difference, or knows how this works?
272 // 0 should definitely be avoided, notes stick on some sound cards.
273 // 64 is supposed to be neutral
275 aftertouch_byte_ = 64;
279 Midi_note_off::str () const
281 Byte status_byte = (char) (0x80 + channel_i_);
283 String str = to_str ((char)status_byte);
284 str += to_str ((char) (pitch_i () + Midi_note::c0_pitch_i_c_));
285 str += to_str ((char)aftertouch_byte_);
289 Midi_dynamic::Midi_dynamic (Audio_dynamic* a)
295 Midi_dynamic::str () const
297 Byte status_byte = (char) (0xB0 + channel_i_);
298 String str = to_str ((char)status_byte);
301 Main volume controller (per channel):
305 static Real const full_scale = 127;
307 int volume = (int) (audio_l_->volume_*full_scale);
310 if (volume > full_scale)
311 volume = (int)full_scale;
313 str += to_str ((char)0x07);
314 str += to_str ((char)volume);
318 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal* a)
324 Midi_piano_pedal::str () const
326 Byte status_byte = (char) (0xB0 + channel_i_);
327 String str = to_str ((char)status_byte);
329 if (audio_l_->type_str_ == "Sostenuto")
330 str += to_str ((char)0x42);
331 else if (audio_l_->type_str_ == "Sustain")
332 str += to_str ((char)0x40);
333 else if (audio_l_->type_str_ == "UnaChorda")
334 str += to_str ((char)0x43);
336 int pedal = ((1 - audio_l_->dir_) / 2) * 0x7f;
337 str += to_str ((char)pedal);
341 Midi_tempo::Midi_tempo (Audio_tempo* a)
347 Midi_tempo::str () const
349 int useconds_per_4_i = 60 * (int)1e6 / audio_l_->per_minute_4_i_;
350 String str = "ff5103";
351 str += String_convert::i2hex_str (useconds_per_4_i, 6, '0');
352 return String_convert::hex2bin_str (str);
355 Midi_text::Midi_text (Audio_text* a)
361 Midi_text::str () const
363 String str = "ff" + String_convert::i2hex_str (audio_l_->type_, 2, '0');
364 str = String_convert::hex2bin_str (str);
365 str += i2varint_str (audio_l_->text_str_.length_i ());
366 str += audio_l_->text_str_;
370 Midi_track::Midi_track ()
374 // 00 00 00 3B chunk length (59)
375 // 00 FF 58 04 04 02 18 08 time signature
376 // 00 FF 51 03 07 A1 20 tempo
378 // FF 59 02 sf mi Key Signature
389 char const* data_ch_C = ""
390 // "00" "ff58" "0404" "0218" "08"
391 // "00" "ff51" "0307" "a120"
392 // why a key at all, in midi?
394 // "00" "ff59" "02" "00" "00"
395 // key: F (scsii-menuetto)
396 // "00" "ff59" "02" "ff" "00"
400 // only for format 0 (currently using format 1)?
401 data_str += String_convert::hex2bin_str (data_ch_C);
403 char const* footer_ch_C = "00" "ff2f" "00";
404 String footer_str = String_convert::hex2bin_str (footer_ch_C);
406 set ("MTrk", data_str, footer_str);
410 Midi_track::add (Moment delta_time_mom, Midi_item* midi_p)
412 assert (delta_time_mom >= Moment (0));
414 Midi_event * e = new Midi_event (delta_time_mom, midi_p);
415 event_p_list_.append (new Killing_cons<Midi_event> (e, 0));
419 Midi_track::data_str () const
421 String str = Midi_chunk::data_str ();
422 if (midi_debug_global_b)
424 for (Cons<Midi_event> *i=event_p_list_.head_; i; i = i->next_)
426 str += i->car_->str ();
427 if (midi_debug_global_b)