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 #define PITCH_WHEEL_TOP 0x3FFF
21 #define PITCH_WHEEL_CENTER 0x2000
22 #define PITCH_WHEEL_BOTTOM 0x0000
23 #define PITCH_WHEEL_RANGE (PITCH_WHEEL_TOP - PITCH_WHEEL_BOTTOM)
26 Midi_item::get_midi (Audio_item *a)
28 if (Audio_key *i = dynamic_cast<Audio_key *> (a))
29 return new Midi_key (i);
30 else if (Audio_instrument *i = dynamic_cast<Audio_instrument *> (a))
31 return i->str_.length () ? new Midi_instrument (i) : 0;
32 else if (Audio_note *i = dynamic_cast<Audio_note *> (a))
33 return new Midi_note (i);
34 else if (Audio_dynamic *i = dynamic_cast<Audio_dynamic *> (a))
35 return new Midi_dynamic (i);
36 else if (Audio_piano_pedal *i = dynamic_cast<Audio_piano_pedal *> (a))
37 return new Midi_piano_pedal (i);
38 else if (Audio_tempo *i = dynamic_cast<Audio_tempo *> (a))
39 return new Midi_tempo (i);
40 else if (Audio_time_signature *i = dynamic_cast<Audio_time_signature *> (a))
41 return new Midi_time_signature (i);
42 else if (Audio_text *i = dynamic_cast<Audio_text *> (a))
43 //return i->text_string_.length () ? new Midi_text (i) : 0;
44 return new Midi_text (i);
53 Midi_chunk::set (string header_string, string data_string, string footer_string)
55 data_string_ = data_string;
56 footer_string_ = footer_string;
57 header_string_ = header_string;
61 Midi_chunk::data_string () const
67 Midi_chunk::to_string () const
69 string str = header_string_;
70 string dat = data_string ();
71 string length_string = String_convert::int2hex (dat.length ()
72 + footer_string_.length (), 8, '0');
73 length_string = String_convert::hex2bin (length_string);
77 str += footer_string_;
82 Midi_duration::Midi_duration (Real seconds_f)
88 Midi_duration::to_string () const
90 return string ("<duration: ") + ::to_string (seconds_) + ">";
93 Midi_event::Midi_event (Moment delta_mom, Midi_item *midi)
95 delta_mom_ = delta_mom;
100 ugh. midi output badly broken since grace note hackage.
103 Midi_event::to_string () const
105 Rational rat_dt = (delta_mom_.main_part_ * Rational (384)
106 + delta_mom_.grace_part_ * Rational (100)) * Rational (4);
107 int delta = rat_dt.to_int ();
109 string delta_string = Midi_item::i2varint_string (delta);
110 string midi_string = midi_->to_string ();
111 assert (midi_string.length ());
112 return delta_string + midi_string;
115 Midi_header::Midi_header (int format, int tracks, int clocks_per_4)
119 string format_string = String_convert::int2hex (format, 4, '0');
120 str += String_convert::hex2bin (format_string);
122 string tracks_string = String_convert::int2hex (tracks, 4, '0');
123 str += String_convert::hex2bin (tracks_string);
125 string tempo_string = String_convert::int2hex (clocks_per_4, 4, '0');
126 str += String_convert::hex2bin (tempo_string);
128 set ("MThd", str, "");
131 Midi_instrument::Midi_instrument (Audio_instrument *a)
134 audio_->str_ = String_convert::to_lower (audio_->str_);
138 Midi_instrument::to_string () const
140 Byte program_byte = 0;
146 SCM proc = ly_lily_module_constant ("midi-program");
147 SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.c_str ()));
148 found = (program != SCM_BOOL_F);
150 program_byte = scm_to_int (program);
152 warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
154 string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
155 str += ::to_string ((char)program_byte);
159 Midi_item::Midi_item ()
163 Midi_channel_item::~Midi_channel_item ()
168 Midi_channel_item::Midi_channel_item ()
173 Midi_item::~Midi_item ()
178 Midi_item::i2varint_string (int i)
180 int buffer = i & 0x7f;
181 while ((i >>= 7) > 0)
185 buffer += (i & 0x7f);
191 str += ::to_string ((char)buffer);
200 Midi_key::Midi_key (Audio_key *a)
206 Midi_key::to_string () const
208 string str = "ff5902";
209 str += String_convert::int2hex (audio_->accidentals_, 2, '0');
211 str += String_convert::int2hex (0, 2, '0');
213 str += String_convert::int2hex (1, 2, '0');
214 return String_convert::hex2bin (str);
217 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
224 Midi_time_signature::to_string () const
226 int num = abs (audio_->beats_);
229 warning ("Time signature with more than 255 beats. Truncating");
233 int den = audio_->one_beat_;
237 string str = "ff5804";
238 str += String_convert::int2hex (num, 2, '0');
239 str += String_convert::int2hex (intlog2 (den), 2, '0');
240 str += String_convert::int2hex (clocks_per_1_, 2, '0');
241 str += String_convert::int2hex (8, 2, '0');
242 return String_convert::hex2bin (str);
245 Midi_note::Midi_note (Audio_note *a)
248 dynamic_byte_ = 0x7f;
252 Midi_note::get_length () const
254 Moment m = audio_->length_mom_;
259 Midi_note::get_fine_tuning () const
261 int ft = audio_->pitch_.quartertone_pitch ();
262 ft -= 2 * audio_->pitch_.semitone_pitch ();
263 ft *= 50; // 1 quarter tone = 50 cents
268 Midi_note::get_pitch () const
270 int p = audio_->pitch_.semitone_pitch () + audio_->transposing_;
273 warning (_ ("silly pitch"));
280 Midi_note::to_string () const
282 Byte status_byte = (char) (0x90 + channel_);
286 // print warning if fine tuning was needed, HJJ
287 if (get_fine_tuning () != 0)
289 warning (_f ("experimental: temporarily fine tuning (of %d cents) a channel.",
290 get_fine_tuning ()));
292 finetune = PITCH_WHEEL_CENTER;
293 // Move pitch wheel to a shifted position.
294 // The pitch wheel range (of 4 semitones) is multiplied by the cents.
295 finetune += (PITCH_WHEEL_RANGE *get_fine_tuning ()) / (4 * 100);
297 str += ::to_string ((char) (0xE0 + channel_));
298 str += ::to_string ((char) (finetune & 0x7F));
299 str += ::to_string ((char) (finetune >> 7));
300 str += ::to_string ((char) (0x00));
303 str += ::to_string ((char)status_byte);
304 str += ::to_string ((char) (get_pitch () + c0_pitch_));
305 str += ::to_string ((char)dynamic_byte_);
310 Midi_note_off::Midi_note_off (Midi_note *n)
311 : Midi_note (n->audio_)
314 channel_ = n->channel_;
316 // Anybody who hears any difference, or knows how this works?
317 // 0 should definitely be avoided, notes stick on some sound cards.
318 // 64 is supposed to be neutral
320 aftertouch_byte_ = 64;
324 Midi_note_off::to_string () const
326 Byte status_byte = (char) (0x80 + channel_);
328 string str = ::to_string ((char)status_byte);
329 str += ::to_string ((char) (get_pitch () + Midi_note::c0_pitch_));
330 str += ::to_string ((char)aftertouch_byte_);
332 if (get_fine_tuning () != 0)
334 // Move pitch wheel back to the central position.
335 str += ::to_string ((char) 0x00);
336 str += ::to_string ((char) (0xE0 + channel_));
337 str += ::to_string ((char) (PITCH_WHEEL_CENTER &0x7F));
338 str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
344 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
350 Midi_dynamic::to_string () const
352 Byte status_byte = (char) (0xB0 + channel_);
353 string str = ::to_string ((char)status_byte);
356 Main volume controller (per channel):
360 static Real const full_scale = 127;
362 int volume = (int) (audio_->volume_ * full_scale);
365 if (volume > full_scale)
366 volume = (int)full_scale;
368 str += ::to_string ((char)0x07);
369 str += ::to_string ((char)volume);
373 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
379 Midi_piano_pedal::to_string () const
381 Byte status_byte = (char) (0xB0 + channel_);
382 string str = ::to_string ((char)status_byte);
384 if (audio_->type_string_ == "Sostenuto")
385 str += ::to_string ((char)0x42);
386 else if (audio_->type_string_ == "Sustain")
387 str += ::to_string ((char)0x40);
388 else if (audio_->type_string_ == "UnaCorda")
389 str += ::to_string ((char)0x43);
391 int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
392 str += ::to_string ((char)pedal);
396 Midi_tempo::Midi_tempo (Audio_tempo *a)
402 Midi_tempo::to_string () const
404 int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
405 string str = "ff5103";
406 str += String_convert::int2hex (useconds_per_4, 6, '0');
407 return String_convert::hex2bin (str);
410 Midi_text::Midi_text (Audio_text *a)
416 Midi_text::to_string () const
418 string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
419 str = String_convert::hex2bin (str);
420 str += i2varint_string (audio_->text_string_.length ());
421 str += audio_->text_string_;
425 Midi_track::Midi_track ()
429 // 00 00 00 3B chunk length (59)
430 // 00 FF 58 04 04 02 18 08 time signature
431 // 00 FF 51 03 07 A1 20 tempo
433 // FF 59 02 sf mi Key Signature
444 char const *data_str0 = ""
445 // "00" "ff58" "0404" "0218" "08"
446 // "00" "ff51" "0307" "a120"
447 // why a key at all, in midi?
449 // "00" "ff59" "02" "00" "00"
450 // key: F (scsii-menuetto)
451 // "00" "ff59" "02" "ff" "00"
455 // only for format 0 (currently using format 1)?
456 data_string += String_convert::hex2bin (data_str0);
458 char const *footer_str0 = "00" "ff2f" "00";
459 string footer_string = String_convert::hex2bin (footer_str0);
461 set ("MTrk", data_string, footer_string);
465 Midi_track::add (Moment delta_time_mom, Midi_item *midi)
467 assert (delta_time_mom >= Moment (0));
469 Midi_event *e = new Midi_event (delta_time_mom, midi);
470 events_.push_back (e);
474 Midi_track::data_string () const
476 string str = Midi_chunk::data_string ();
477 if (do_midi_debugging_global)
480 for (vector<Midi_event*>::const_iterator i (events_.begin());
481 i != events_.end(); i ++)
483 str += (*i)->to_string ();
484 if (do_midi_debugging_global)
492 Midi_item::name () const
494 return this->class_name ();
497 Midi_track::~Midi_track ()
499 junk_pointers (events_);