2 midi-item.cc -- implement Midi items.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2006 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "midi-item.hh"
11 #include "duration.hh"
12 #include "international.hh"
14 #include "midi-stream.hh"
16 #include "program-option.hh"
17 #include "string-convert.hh"
20 #include "killing-cons.tcc"
22 #define PITCH_WHEEL_TOP 0x3FFF
23 #define PITCH_WHEEL_CENTER 0x2000
24 #define PITCH_WHEEL_BOTTOM 0x0000
25 #define PITCH_WHEEL_RANGE (PITCH_WHEEL_TOP - PITCH_WHEEL_BOTTOM)
28 Midi_item::get_midi (Audio_item *a)
30 if (Audio_key *i = dynamic_cast<Audio_key *> (a))
31 return new Midi_key (i);
32 else if (Audio_instrument *i = dynamic_cast<Audio_instrument *> (a))
33 return i->str_.length () ? new Midi_instrument (i) : 0;
34 else if (Audio_note *i = dynamic_cast<Audio_note *> (a))
35 return new Midi_note (i);
36 else if (Audio_dynamic *i = dynamic_cast<Audio_dynamic *> (a))
37 return new Midi_dynamic (i);
38 else if (Audio_piano_pedal *i = dynamic_cast<Audio_piano_pedal *> (a))
39 return new Midi_piano_pedal (i);
40 else if (Audio_tempo *i = dynamic_cast<Audio_tempo *> (a))
41 return new Midi_tempo (i);
42 else if (Audio_time_signature *i = dynamic_cast<Audio_time_signature *> (a))
43 return new Midi_time_signature (i);
44 else if (Audio_text *i = dynamic_cast<Audio_text *> (a))
45 //return i->text_string_.length () ? new Midi_text (i) : 0;
46 return new Midi_text (i);
55 Midi_chunk::set (string header_string, string data_string, string footer_string)
57 data_string_ = data_string;
58 footer_string_ = footer_string;
59 header_string_ = header_string;
63 Midi_chunk::data_string () const
69 Midi_chunk::to_string () const
71 string str = header_string_;
72 string dat = data_string ();
73 string length_string = String_convert::int2hex (dat.length ()
74 + footer_string_.length (), 8, '0');
75 length_string = String_convert::hex2bin (length_string);
79 str += footer_string_;
84 Midi_duration::Midi_duration (Real seconds_f)
90 Midi_duration::to_string () const
92 return string ("<duration: ") + ::to_string (seconds_) + ">";
95 Midi_event::Midi_event (Moment delta_mom, Midi_item *midi)
97 delta_mom_ = delta_mom;
102 ugh. midi output badly broken since grace note hackage.
105 Midi_event::to_string () const
107 Rational rat_dt = (delta_mom_.main_part_ * Rational (384)
108 + delta_mom_.grace_part_ * Rational (100)) * Rational (4);
109 int delta_i = rat_dt.to_int ();
111 string delta_string = Midi_item::i2varint_string (delta_i);
112 string midi_string = midi_->to_string ();
113 assert (midi_string.length ());
114 return delta_string + midi_string;
117 Midi_header::Midi_header (int format_i, int tracks_i, int clocks_per_4_i)
121 string format_string = String_convert::int2hex (format_i, 4, '0');
122 str += String_convert::hex2bin (format_string);
124 string tracks_string = String_convert::int2hex (tracks_i, 4, '0');
125 str += String_convert::hex2bin (tracks_string);
127 string tempo_string = String_convert::int2hex (clocks_per_4_i, 4, '0');
128 str += String_convert::hex2bin (tempo_string);
130 set ("MThd", str, "");
133 Midi_instrument::Midi_instrument (Audio_instrument *a)
136 audio_->str_ = String_convert::to_lower (audio_->str_);
140 Midi_instrument::to_string () const
142 Byte program_byte = 0;
148 SCM proc = ly_lily_module_constant ("midi-program");
149 SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.c_str ()));
150 found = (program != SCM_BOOL_F);
152 program_byte = scm_to_int (program);
154 warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
156 string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
157 str += ::to_string ((char)program_byte);
161 Midi_item::Midi_item ()
166 Midi_item::~Midi_item ()
171 Midi_item::i2varint_string (int i)
173 int buffer_i = i & 0x7f;
174 while ((i >>= 7) > 0)
178 buffer_i += (i & 0x7f);
184 str += ::to_string ((char)buffer_i);
193 Midi_key::Midi_key (Audio_key *a)
199 Midi_key::to_string () const
201 string str = "ff5902";
202 str += String_convert::int2hex (audio_->accidentals_, 2, '0');
204 str += String_convert::int2hex (0, 2, '0');
206 str += String_convert::int2hex (1, 2, '0');
207 return String_convert::hex2bin (str);
210 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
217 Midi_time_signature::to_string () const
219 int num = abs (audio_->beats_);
222 warning ("Time signature with more than 255 beats. Truncating");
226 int den = audio_->one_beat_;
230 string str = "ff5804";
231 str += String_convert::int2hex (num, 2, '0');
232 str += String_convert::int2hex (intlog2 (den), 2, '0');
233 str += String_convert::int2hex (clocks_per_1_, 2, '0');
234 str += String_convert::int2hex (8, 2, '0');
235 return String_convert::hex2bin (str);
238 Midi_note::Midi_note (Audio_note *a)
241 dynamic_byte_ = 0x7f;
245 Midi_note::get_length () const
247 Moment m = audio_->length_mom_;
252 Midi_note::get_fine_tuning () const
254 int ft = audio_->pitch_.quartertone_pitch ();
255 ft -= 2 * audio_->pitch_.semitone_pitch ();
256 ft *= 50; // 1 quarter tone = 50 cents
261 Midi_note::get_pitch () const
263 int p = audio_->pitch_.semitone_pitch () + audio_->transposing_;
266 warning (_ ("silly pitch"));
273 Midi_note::to_string () const
275 Byte status_byte = (char) (0x90 + channel_);
279 // print warning if fine tuning was needed, HJJ
280 if (get_fine_tuning () != 0)
282 warning (_f ("experimental: temporarily fine tuning (of %d cents) a channel.",
283 get_fine_tuning ()));
285 finetune = PITCH_WHEEL_CENTER;
286 // Move pitch wheel to a shifted position.
287 // The pitch wheel range (of 4 semitones) is multiplied by the cents.
288 finetune += (PITCH_WHEEL_RANGE *get_fine_tuning ()) / (4 * 100);
290 str += ::to_string ((char) (0xE0 + channel_));
291 str += ::to_string ((char) (finetune & 0x7F));
292 str += ::to_string ((char) (finetune >> 7));
293 str += ::to_string ((char) (0x00));
296 str += ::to_string ((char)status_byte);
297 str += ::to_string ((char) (get_pitch () + c0_pitch_i_));
298 str += ::to_string ((char)dynamic_byte_);
303 Midi_note_off::Midi_note_off (Midi_note *n)
304 : Midi_note (n->audio_)
307 channel_ = n->channel_;
309 // Anybody who hears any difference, or knows how this works?
310 // 0 should definitely be avoided, notes stick on some sound cards.
311 // 64 is supposed to be neutral
313 aftertouch_byte_ = 64;
317 Midi_note_off::to_string () const
319 Byte status_byte = (char) (0x80 + channel_);
321 string str = ::to_string ((char)status_byte);
322 str += ::to_string ((char) (get_pitch () + Midi_note::c0_pitch_i_));
323 str += ::to_string ((char)aftertouch_byte_);
325 if (get_fine_tuning () != 0)
327 // Move pitch wheel back to the central position.
328 str += ::to_string ((char) 0x00);
329 str += ::to_string ((char) (0xE0 + channel_));
330 str += ::to_string ((char) (PITCH_WHEEL_CENTER &0x7F));
331 str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
337 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
343 Midi_dynamic::to_string () const
345 Byte status_byte = (char) (0xB0 + channel_);
346 string str = ::to_string ((char)status_byte);
349 Main volume controller (per channel):
353 static Real const full_scale = 127;
355 int volume = (int) (audio_->volume_ * full_scale);
358 if (volume > full_scale)
359 volume = (int)full_scale;
361 str += ::to_string ((char)0x07);
362 str += ::to_string ((char)volume);
366 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
372 Midi_piano_pedal::to_string () const
374 Byte status_byte = (char) (0xB0 + channel_);
375 string str = ::to_string ((char)status_byte);
377 if (audio_->type_string_ == "Sostenuto")
378 str += ::to_string ((char)0x42);
379 else if (audio_->type_string_ == "Sustain")
380 str += ::to_string ((char)0x40);
381 else if (audio_->type_string_ == "UnaCorda")
382 str += ::to_string ((char)0x43);
384 int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
385 str += ::to_string ((char)pedal);
389 Midi_tempo::Midi_tempo (Audio_tempo *a)
395 Midi_tempo::to_string () const
397 int useconds_per_4_i = 60 * (int)1e6 / audio_->per_minute_4_;
398 string str = "ff5103";
399 str += String_convert::int2hex (useconds_per_4_i, 6, '0');
400 return String_convert::hex2bin (str);
403 Midi_text::Midi_text (Audio_text *a)
409 Midi_text::to_string () const
411 string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
412 str = String_convert::hex2bin (str);
413 str += i2varint_string (audio_->text_string_.length ());
414 str += audio_->text_string_;
418 Midi_track::Midi_track ()
422 // 00 00 00 3B chunk length (59)
423 // 00 FF 58 04 04 02 18 08 time signature
424 // 00 FF 51 03 07 A1 20 tempo
426 // FF 59 02 sf mi Key Signature
437 char const *data_str0 = ""
438 // "00" "ff58" "0404" "0218" "08"
439 // "00" "ff51" "0307" "a120"
440 // why a key at all, in midi?
442 // "00" "ff59" "02" "00" "00"
443 // key: F (scsii-menuetto)
444 // "00" "ff59" "02" "ff" "00"
448 // only for format 0 (currently using format 1)?
449 data_string += String_convert::hex2bin (data_str0);
451 char const *footer_str0 = "00" "ff2f" "00";
452 string footer_string = String_convert::hex2bin (footer_str0);
454 set ("MTrk", data_string, footer_string);
458 Midi_track::add (Moment delta_time_mom, Midi_item *midi)
460 assert (delta_time_mom >= Moment (0));
462 Midi_event *e = new Midi_event (delta_time_mom, midi);
463 event_p_list_.append (new Killing_cons<Midi_event> (e, 0));
467 Midi_track::data_string () const
469 string str = Midi_chunk::data_string ();
470 if (do_midi_debugging_global)
472 for (Cons<Midi_event> *i = event_p_list_.head_; i; i = i->next_)
474 str += i->car_->to_string ();
475 if (do_midi_debugging_global)
483 Midi_item::name () const
485 return this->class_name ();