2 midi-item.cc -- implement MIDI items.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2007 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 new Midi_text (i);
51 Midi_chunk::set (string header_string, string data_string, string footer_string)
53 data_string_ = data_string;
54 footer_string_ = footer_string;
55 header_string_ = header_string;
59 Midi_chunk::data_string () const
65 Midi_chunk::to_string () const
67 string str = header_string_;
68 string dat = data_string ();
69 string length_string = String_convert::int2hex (dat.length ()
70 + footer_string_.length (), 8, '0');
71 length_string = String_convert::hex2bin (length_string);
75 str += footer_string_;
80 Midi_duration::Midi_duration (Real seconds_f)
86 Midi_duration::to_string () const
88 return string ("<duration: ") + ::to_string (seconds_) + ">";
91 Midi_event::Midi_event (int delta_ticks, Midi_item *midi)
93 delta_ticks_ = delta_ticks;
98 Midi_event::to_string () const
100 string delta_string = Midi_item::i2varint_string (delta_ticks_);
101 string midi_string = midi_->to_string ();
102 assert (midi_string.length ());
103 return delta_string + midi_string;
106 Midi_header::Midi_header (int format, int tracks, int clocks_per_4)
110 string format_string = String_convert::int2hex (format, 4, '0');
111 str += String_convert::hex2bin (format_string);
113 string tracks_string = String_convert::int2hex (tracks, 4, '0');
114 str += String_convert::hex2bin (tracks_string);
116 string tempo_string = String_convert::int2hex (clocks_per_4, 4, '0');
117 str += String_convert::hex2bin (tempo_string);
119 set ("MThd", str, "");
122 Midi_instrument::Midi_instrument (Audio_instrument *a)
125 audio_->str_ = String_convert::to_lower (audio_->str_);
129 Midi_instrument::to_string () const
131 Byte program_byte = 0;
134 SCM proc = ly_lily_module_constant ("midi-program");
135 SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.c_str ()));
136 found = (program != SCM_BOOL_F);
138 program_byte = scm_to_int (program);
140 warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
142 string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
143 str += ::to_string ((char)program_byte);
147 Midi_item::Midi_item ()
151 Midi_channel_item::Midi_channel_item ()
156 Midi_item::~Midi_item ()
161 Midi_item::i2varint_string (int i)
163 int buffer = i & 0x7f;
164 while ((i >>= 7) > 0)
168 buffer += (i & 0x7f);
174 str += ::to_string ((char)buffer);
183 Midi_key::Midi_key (Audio_key *a)
189 Midi_key::to_string () const
191 string str = "ff5902";
192 str += String_convert::int2hex (audio_->accidentals_, 2, '0');
194 str += String_convert::int2hex (0, 2, '0');
196 str += String_convert::int2hex (1, 2, '0');
197 return String_convert::hex2bin (str);
200 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
207 Midi_time_signature::to_string () const
209 int num = abs (audio_->beats_);
212 warning ("Time signature with more than 255 beats. Truncating");
216 int den = audio_->one_beat_;
220 string str = "ff5804";
221 str += String_convert::int2hex (num, 2, '0');
222 str += String_convert::int2hex (intlog2 (den), 2, '0');
223 str += String_convert::int2hex (clocks_per_1_, 2, '0');
224 str += String_convert::int2hex (8, 2, '0');
225 return String_convert::hex2bin (str);
228 Midi_note::Midi_note (Audio_note *a)
231 dynamic_byte_ = 0x7f;
236 Midi_note::get_fine_tuning () const
238 Rational tune = (audio_->pitch_.tone_pitch ()
239 + audio_->transposing_.tone_pitch ()) * Rational (2);
240 tune -= Rational (get_semitone_pitch ());
243 return (int) double (tune);
247 Midi_note::get_semitone_pitch () const
249 return int (double ((audio_->pitch_.tone_pitch ()
250 + audio_->transposing_.tone_pitch ()) * Rational (2)));
254 Midi_note::to_string () const
256 Byte status_byte = (char) (0x90 + channel_);
260 // print warning if fine tuning was needed, HJJ
261 if (get_fine_tuning () != 0)
263 warning (_f ("experimental: temporarily fine tuning (of %d cents) a channel.",
264 get_fine_tuning ()));
266 finetune = PITCH_WHEEL_CENTER;
267 // Move pitch wheel to a shifted position.
268 // The pitch wheel range (of 4 semitones) is multiplied by the cents.
269 finetune += (PITCH_WHEEL_RANGE *get_fine_tuning ()) / (4 * 100);
271 str += ::to_string ((char) (0xE0 + channel_));
272 str += ::to_string ((char) (finetune & 0x7F));
273 str += ::to_string ((char) (finetune >> 7));
274 str += ::to_string ((char) (0x00));
277 str += ::to_string ((char) status_byte);
278 str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
279 str += ::to_string ((char)dynamic_byte_);
284 Midi_note_off::Midi_note_off (Midi_note *n)
285 : Midi_note (n->audio_)
288 channel_ = n->channel_;
290 // Anybody who hears any difference, or knows how this works?
291 // 0 should definitely be avoided, notes stick on some sound cards.
292 // 64 is supposed to be neutral
294 aftertouch_byte_ = 64;
298 Midi_note_off::to_string () const
300 Byte status_byte = (char) (0x80 + channel_);
302 string str = ::to_string ((char)status_byte);
303 str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
304 str += ::to_string ((char)aftertouch_byte_);
306 if (get_fine_tuning () != 0)
308 // Move pitch wheel back to the central position.
309 str += ::to_string ((char) 0x00);
310 str += ::to_string ((char) (0xE0 + channel_));
311 str += ::to_string ((char) (PITCH_WHEEL_CENTER &0x7F));
312 str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
318 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
324 Midi_dynamic::to_string () const
326 Byte status_byte = (char) (0xB0 + channel_);
327 string str = ::to_string ((char)status_byte);
330 Main volume controller (per channel):
334 static Real const full_scale = 127;
336 int volume = (int) (audio_->volume_ * full_scale);
339 if (volume > full_scale)
340 volume = (int)full_scale;
342 str += ::to_string ((char)0x07);
343 str += ::to_string ((char)volume);
347 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
353 Midi_piano_pedal::to_string () const
355 Byte status_byte = (char) (0xB0 + channel_);
356 string str = ::to_string ((char)status_byte);
358 if (audio_->type_string_ == "Sostenuto")
359 str += ::to_string ((char)0x42);
360 else if (audio_->type_string_ == "Sustain")
361 str += ::to_string ((char)0x40);
362 else if (audio_->type_string_ == "UnaCorda")
363 str += ::to_string ((char)0x43);
365 int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
366 str += ::to_string ((char)pedal);
370 Midi_tempo::Midi_tempo (Audio_tempo *a)
376 Midi_tempo::to_string () const
378 int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
379 string str = "ff5103";
380 str += String_convert::int2hex (useconds_per_4, 6, '0');
381 return String_convert::hex2bin (str);
384 Midi_text::Midi_text (Audio_text *a)
390 Midi_text::to_string () const
392 string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
393 str = String_convert::hex2bin (str);
394 str += i2varint_string (audio_->text_string_.length ());
395 str += audio_->text_string_;
399 Midi_track::Midi_track ()
403 // 00 00 00 3B chunk length (59)
404 // 00 FF 58 04 04 02 18 08 time signature
405 // 00 FF 51 03 07 A1 20 tempo
407 // FF 59 02 sf mi Key Signature
418 char const *data_str0 = ""
419 // "00" "ff58" "0404" "0218" "08"
420 // "00" "ff51" "0307" "a120"
421 // why a key at all, in midi?
423 // "00" "ff59" "02" "00" "00"
424 // key: F (scsii-menuetto)
425 // "00" "ff59" "02" "ff" "00"
429 // only for format 0 (currently using format 1)?
430 data_string += String_convert::hex2bin (data_str0);
432 char const *footer_str0 = "00" "ff2f" "00";
433 string footer_string = String_convert::hex2bin (footer_str0);
435 set ("MTrk", data_string, footer_string);
439 Midi_track::add (int delta_ticks, Midi_item *midi)
441 assert (delta_ticks >= 0);
443 Midi_event *e = new Midi_event (delta_ticks, midi);
444 events_.push_back (e);
448 Midi_track::data_string () const
450 string str = Midi_chunk::data_string ();
452 for (vector<Midi_event*>::const_iterator i (events_.begin());
453 i != events_.end(); i ++)
455 str += (*i)->to_string ();
462 Midi_item::name () const
464 return this->class_name ();
467 Midi_track::~Midi_track ()
469 junk_pointers (events_);