2 midi-item.cc -- implement Midi items.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Jan Nieuwenhuizen <janneke@gnu.org>
9 #include "midi-item.hh"
14 #include "string-convert.hh"
15 #include "midi-stream.hh"
16 #include "duration.hh"
17 #include "program-option.hh"
19 #include "killing-cons.tcc"
21 #define PITCH_WHEEL_TOP 0x3FFF
22 #define PITCH_WHEEL_CENTER 0x2000
23 #define PITCH_WHEEL_BOTTOM 0x0000
24 #define PITCH_WHEEL_RANGE (PITCH_WHEEL_TOP - PITCH_WHEEL_BOTTOM)
27 Midi_item::get_midi (Audio_item *a)
29 if (Audio_key *i = dynamic_cast<Audio_key *> (a))
30 return new Midi_key (i);
31 else if (Audio_instrument *i = dynamic_cast<Audio_instrument *> (a))
32 return i->str_.length () ? new Midi_instrument (i) : 0;
33 else if (Audio_note *i = dynamic_cast<Audio_note *> (a))
34 return new Midi_note (i);
35 else if (Audio_dynamic *i = dynamic_cast<Audio_dynamic *> (a))
36 return new Midi_dynamic (i);
37 else if (Audio_piano_pedal *i = dynamic_cast<Audio_piano_pedal *> (a))
38 return new Midi_piano_pedal (i);
39 else if (Audio_tempo *i = dynamic_cast<Audio_tempo *> (a))
40 return new Midi_tempo (i);
41 else if (Audio_time_signature *i = dynamic_cast<Audio_time_signature *> (a))
42 return new Midi_time_signature (i);
43 else if (Audio_text *i = dynamic_cast<Audio_text *> (a))
44 //return i->text_string_.length () ? new Midi_text (i) : 0;
45 return new Midi_text (i);
54 Midi_chunk::set (String header_string, String data_string, String footer_string)
56 data_string_ = data_string;
57 footer_string_ = footer_string;
58 header_string_ = header_string;
62 Midi_chunk::data_string () const
68 Midi_chunk::to_string () const
70 String str = header_string_;
71 String dat = data_string ();
72 String length_string = String_convert::int2hex (dat.length ()
73 + footer_string_.length (), 8, '0');
74 length_string = String_convert::hex2bin (length_string);
78 str += footer_string_;
83 Midi_duration::Midi_duration (Real seconds_f)
89 Midi_duration::to_string () const
91 return String ("<duration: ") + ::to_string (seconds_) + ">";
94 Midi_event::Midi_event (Moment delta_mom, Midi_item *midi)
96 delta_mom_ = delta_mom;
101 ugh. midi output badly broken since grace note hackage.
104 Midi_event::to_string () const
106 Rational rat_dt = (delta_mom_.main_part_ * Rational (384)
107 + delta_mom_.grace_part_ * Rational (100)) * Rational (4);
108 int delta_i = rat_dt.to_int ();
110 String delta_string = Midi_item::i2varint_string (delta_i);
111 String midi_string = midi_->to_string ();
112 assert (midi_string.length ());
113 return delta_string + midi_string;
116 Midi_header::Midi_header (int format_i, int tracks_i, int clocks_per_4_i)
120 String format_string = String_convert::int2hex (format_i, 4, '0');
121 str += String_convert::hex2bin (format_string);
123 String tracks_string = String_convert::int2hex (tracks_i, 4, '0');
124 str += String_convert::hex2bin (tracks_string);
126 String tempo_string = String_convert::int2hex (clocks_per_4_i, 4, '0');
127 str += String_convert::hex2bin (tempo_string);
129 set ("MThd", str, "");
132 Midi_instrument::Midi_instrument (Audio_instrument *a)
135 audio_->str_.to_lower ();
139 Midi_instrument::to_string () const
141 Byte program_byte = 0;
147 SCM proc = ly_lily_module_constant ("midi-program");
148 SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.to_str0 ()));
149 found = (program != SCM_BOOL_F);
151 program_byte = scm_to_int (program);
153 warning (_f ("no such MIDI instrument: `%s'", audio_->str_.to_str0 ()));
155 String str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
156 str += ::to_string ((char)program_byte);
160 Midi_item::Midi_item ()
165 Midi_item::~Midi_item ()
170 Midi_item::i2varint_string (int i)
172 int buffer_i = i & 0x7f;
173 while ((i >>= 7) > 0)
177 buffer_i += (i & 0x7f);
183 str += ::to_string ((char)buffer_i);
192 Midi_key::Midi_key (Audio_key *a)
198 Midi_key::to_string () const
200 String str = "ff5902";
201 str += String_convert::int2hex (audio_->accidentals_, 2, '0');
203 str += String_convert::int2hex (0, 2, '0');
205 str += String_convert::int2hex (1, 2, '0');
206 return String_convert::hex2bin (str);
209 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
216 Midi_time_signature::to_string () const
218 int num = audio_->beats_;
219 int den = audio_->one_beat_;
221 String str = "ff5804";
222 str += String_convert::int2hex (num, 2, '0');
223 str += String_convert::int2hex (intlog2 (den), 2, '0');
224 str += String_convert::int2hex (clocks_per_1_, 2, '0');
225 str += String_convert::int2hex (8, 2, '0');
226 return String_convert::hex2bin (str);
229 Midi_note::Midi_note (Audio_note *a)
232 dynamic_byte_ = 0x7f;
236 Midi_note::get_length () const
238 Moment m = audio_->length_mom_;
243 Midi_note::get_fine_tuning () const
245 int ft = audio_->pitch_.quartertone_pitch ();
246 ft -= 2 * audio_->pitch_.semitone_pitch ();
247 ft *= 50; // 1 quarter tone = 50 cents
252 Midi_note::get_pitch () const
254 int p = audio_->pitch_.semitone_pitch () + audio_->transposing_;
257 warning (_ ("silly pitch"));
264 Midi_note::to_string () const
266 Byte status_byte = (char) (0x90 + channel_);
270 // print warning if fine tuning was needed, HJJ
271 if (get_fine_tuning () != 0)
273 warning (_f ("experimental: temporarily fine tuning (of %d cents) a channel.",
274 get_fine_tuning ()));
276 finetune = PITCH_WHEEL_CENTER;
277 // Move pitch wheel to a shifted position.
278 // The pitch wheel range (of 4 semitones) is multiplied by the cents.
279 finetune += (PITCH_WHEEL_RANGE *get_fine_tuning ()) / (4 * 100);
281 str += ::to_string ((char) (0xE0 + channel_));
282 str += ::to_string ((char) (finetune & 0x7F));
283 str += ::to_string ((char) (finetune >> 7));
284 str += ::to_string ((char) (0x00));
287 str += ::to_string ((char)status_byte);
288 str += ::to_string ((char) (get_pitch () + c0_pitch_i_));
289 str += ::to_string ((char)dynamic_byte_);
294 Midi_note_off::Midi_note_off (Midi_note *n)
295 : Midi_note (n->audio_)
298 channel_ = n->channel_;
300 // Anybody who hears any difference, or knows how this works?
301 // 0 should definitely be avoided, notes stick on some sound cards.
302 // 64 is supposed to be neutral
304 aftertouch_byte_ = 64;
308 Midi_note_off::to_string () const
310 Byte status_byte = (char) (0x80 + channel_);
312 String str = ::to_string ((char)status_byte);
313 str += ::to_string ((char) (get_pitch () + Midi_note::c0_pitch_i_));
314 str += ::to_string ((char)aftertouch_byte_);
316 if (get_fine_tuning () != 0)
318 // Move pitch wheel back to the central position.
319 str += ::to_string ((char) 0x00);
320 str += ::to_string ((char) (0xE0 + channel_));
321 str += ::to_string ((char) (PITCH_WHEEL_CENTER &0x7F));
322 str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
328 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
334 Midi_dynamic::to_string () const
336 Byte status_byte = (char) (0xB0 + channel_);
337 String str = ::to_string ((char)status_byte);
340 Main volume controller (per channel):
344 static Real const full_scale = 127;
346 int volume = (int) (audio_->volume_ * full_scale);
349 if (volume > full_scale)
350 volume = (int)full_scale;
352 str += ::to_string ((char)0x07);
353 str += ::to_string ((char)volume);
357 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
363 Midi_piano_pedal::to_string () const
365 Byte status_byte = (char) (0xB0 + channel_);
366 String str = ::to_string ((char)status_byte);
368 if (audio_->type_string_ == "Sostenuto")
369 str += ::to_string ((char)0x42);
370 else if (audio_->type_string_ == "Sustain")
371 str += ::to_string ((char)0x40);
372 else if (audio_->type_string_ == "UnaCorda")
373 str += ::to_string ((char)0x43);
375 int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
376 str += ::to_string ((char)pedal);
380 Midi_tempo::Midi_tempo (Audio_tempo *a)
386 Midi_tempo::to_string () const
388 int useconds_per_4_i = 60 * (int)1e6 / audio_->per_minute_4_;
389 String str = "ff5103";
390 str += String_convert::int2hex (useconds_per_4_i, 6, '0');
391 return String_convert::hex2bin (str);
394 Midi_text::Midi_text (Audio_text *a)
400 Midi_text::to_string () const
402 String str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
403 str = String_convert::hex2bin (str);
404 str += i2varint_string (audio_->text_string_.length ());
405 str += audio_->text_string_;
409 Midi_track::Midi_track ()
413 // 00 00 00 3B chunk length (59)
414 // 00 FF 58 04 04 02 18 08 time signature
415 // 00 FF 51 03 07 A1 20 tempo
417 // FF 59 02 sf mi Key Signature
428 char const *data_str0 = ""
429 // "00" "ff58" "0404" "0218" "08"
430 // "00" "ff51" "0307" "a120"
431 // why a key at all, in midi?
433 // "00" "ff59" "02" "00" "00"
434 // key: F (scsii-menuetto)
435 // "00" "ff59" "02" "ff" "00"
439 // only for format 0 (currently using format 1)?
440 data_string += String_convert::hex2bin (data_str0);
442 char const *footer_str0 = "00" "ff2f" "00";
443 String footer_string = String_convert::hex2bin (footer_str0);
445 set ("MTrk", data_string, footer_string);
449 Midi_track::add (Moment delta_time_mom, Midi_item *midi)
451 assert (delta_time_mom >= Moment (0));
453 Midi_event *e = new Midi_event (delta_time_mom, midi);
454 event_p_list_.append (new Killing_cons<Midi_event> (e, 0));
458 Midi_track::data_string () const
460 String str = Midi_chunk::data_string ();
461 if (do_midi_debugging_global)
463 for (Cons<Midi_event> *i = event_p_list_.head_; i; i = i->next_)
465 str += i->car_->to_string ();
466 if (do_midi_debugging_global)
474 Midi_item::name () const
476 return this->class_name ();