]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-item.cc
Performance::remap_grace_durations(): robust way of handling grace
[lilypond.git] / lily / midi-item.cc
1 /*
2   midi-item.cc -- implement Midi items.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "midi-item.hh"
10
11 #include "duration.hh"
12 #include "international.hh"
13 #include "main.hh"
14 #include "midi-stream.hh"
15 #include "misc.hh"
16 #include "program-option.hh"
17 #include "string-convert.hh"
18 #include "warn.hh"
19
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)
24
25 Midi_item *
26 Midi_item::get_midi (Audio_item *a)
27 {
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);
45   else
46     assert (0);
47
48   // isn't C++ grand?
49   return 0;
50 }
51
52 void
53 Midi_chunk::set (string header_string, string data_string, string footer_string)
54 {
55   data_string_ = data_string;
56   footer_string_ = footer_string;
57   header_string_ = header_string;
58 }
59
60 string
61 Midi_chunk::data_string () const
62 {
63   return data_string_;
64 }
65
66 string
67 Midi_chunk::to_string () const
68 {
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);
74
75   str += length_string;
76   str += dat;
77   str += footer_string_;
78
79   return str;
80 }
81
82 Midi_duration::Midi_duration (Real seconds_f)
83 {
84   seconds_ = seconds_f;
85 }
86
87 string
88 Midi_duration::to_string () const
89 {
90   return string ("<duration: ") + ::to_string (seconds_) + ">";
91 }
92
93 Midi_event::Midi_event (Moment delta_mom, Midi_item *midi)
94 {
95   delta_mom_ = delta_mom;
96   midi_ = midi;
97 }
98
99 string
100 Midi_event::to_string () const
101 {
102   assert (delta_mom_.grace_part_ == Rational (0));
103   
104   Rational rat_dt = delta_mom_.main_part_ * Rational (384) * Rational (4);
105   int delta = rat_dt.to_int ();
106
107   string delta_string = Midi_item::i2varint_string (delta);
108   string midi_string = midi_->to_string ();
109   assert (midi_string.length ());
110   return delta_string + midi_string;
111 }
112
113 Midi_header::Midi_header (int format, int tracks, int clocks_per_4)
114 {
115   string str;
116
117   string format_string = String_convert::int2hex (format, 4, '0');
118   str += String_convert::hex2bin (format_string);
119
120   string tracks_string = String_convert::int2hex (tracks, 4, '0');
121   str += String_convert::hex2bin (tracks_string);
122
123   string tempo_string = String_convert::int2hex (clocks_per_4, 4, '0');
124   str += String_convert::hex2bin (tempo_string);
125
126   set ("MThd", str, "");
127 }
128
129 Midi_instrument::Midi_instrument (Audio_instrument *a)
130 {
131   audio_ = a;
132   audio_->str_ = String_convert::to_lower (audio_->str_);
133 }
134
135 string
136 Midi_instrument::to_string () const
137 {
138   Byte program_byte = 0;
139   bool found = false;
140
141   SCM proc = ly_lily_module_constant ("midi-program");
142   SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.c_str ()));
143   found = (program != SCM_BOOL_F);
144   if (found)
145     program_byte = scm_to_int (program);
146   else
147     warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
148
149   string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
150   str += ::to_string ((char)program_byte);
151   return str;
152 }
153
154 Midi_item::Midi_item ()
155 {
156 }
157
158 Midi_channel_item::~Midi_channel_item ()
159 {
160   channel_ = 0;
161 }
162
163 Midi_channel_item::Midi_channel_item ()
164 {
165   channel_ = 0;
166 }
167
168 Midi_item::~Midi_item ()
169 {
170 }
171
172 string
173 Midi_item::i2varint_string (int i)
174 {
175   int buffer = i & 0x7f;
176   while ((i >>= 7) > 0)
177     {
178       buffer <<= 8;
179       buffer |= 0x80;
180       buffer += (i & 0x7f);
181     }
182
183   string str;
184   while (1)
185     {
186       str += ::to_string ((char)buffer);
187       if (buffer & 0x80)
188         buffer >>= 8;
189       else
190         break;
191     }
192   return str;
193 }
194
195 Midi_key::Midi_key (Audio_key *a)
196 {
197   audio_ = a;
198 }
199
200 string
201 Midi_key::to_string () const
202 {
203   string str = "ff5902";
204   str += String_convert::int2hex (audio_->accidentals_, 2, '0');
205   if (audio_->major_)
206     str += String_convert::int2hex (0, 2, '0');
207   else
208     str += String_convert::int2hex (1, 2, '0');
209   return String_convert::hex2bin (str);
210 }
211
212 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
213 {
214   audio_ = a;
215   clocks_per_1_ = 18;
216 }
217
218 string
219 Midi_time_signature::to_string () const
220 {
221   int num = abs (audio_->beats_);
222   if (num > 255)
223     {
224       warning ("Time signature with more than 255 beats. Truncating");
225       num = 255;
226     }
227
228   int den = audio_->one_beat_;
229
230
231   
232   string str = "ff5804";
233   str += String_convert::int2hex (num, 2, '0');
234   str += String_convert::int2hex (intlog2 (den), 2, '0');
235   str += String_convert::int2hex (clocks_per_1_, 2, '0');
236   str += String_convert::int2hex (8, 2, '0');
237   return String_convert::hex2bin (str);
238 }
239
240 Midi_note::Midi_note (Audio_note *a)
241 {
242   audio_ = a;
243   dynamic_byte_ = 0x7f;
244 }
245
246 Moment
247 Midi_note::get_length () const
248 {
249   Moment m = audio_->end_column_->when () - audio_->audio_column_->when ();
250   return m;
251 }
252
253 int
254 Midi_note::get_fine_tuning () const
255 {
256   Rational tune = (audio_->pitch_.tone_pitch ()
257                    + audio_->transposing_.tone_pitch ()) * Rational (2);
258   tune -= Rational (get_semitone_pitch ());
259
260   tune *= 100;
261   return (int) double (tune);
262 }
263
264 int
265 Midi_note::get_semitone_pitch () const
266 {
267   return int (double ((audio_->pitch_.tone_pitch ()
268                        + audio_->transposing_.tone_pitch ()) * Rational (2)));
269 }
270
271 string
272 Midi_note::to_string () const
273 {
274   Byte status_byte = (char) (0x90 + channel_);
275   string str = "";
276   int finetune;
277
278   // print warning if fine tuning was needed, HJJ
279   if (get_fine_tuning () != 0)
280     {
281       warning (_f ("experimental: temporarily fine tuning (of %d cents) a channel.",
282                    get_fine_tuning ()));
283
284       finetune = PITCH_WHEEL_CENTER;
285       // Move pitch wheel to a shifted position.
286       // The pitch wheel range (of 4 semitones) is multiplied by the cents.
287       finetune += (PITCH_WHEEL_RANGE *get_fine_tuning ()) / (4 * 100);
288
289       str += ::to_string ((char) (0xE0 + channel_));
290       str += ::to_string ((char) (finetune & 0x7F));
291       str += ::to_string ((char) (finetune >> 7));
292       str += ::to_string ((char) (0x00));
293     }
294
295   str += ::to_string ((char)status_byte);
296   str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
297   str += ::to_string ((char)dynamic_byte_);
298
299   return str;
300 }
301
302 Midi_note_off::Midi_note_off (Midi_note *n)
303   : Midi_note (n->audio_)
304 {
305   on_ = n;
306   channel_ = n->channel_;
307
308   // Anybody who hears any difference, or knows how this works?
309   //  0 should definitely be avoided, notes stick on some sound cards.
310   // 64 is supposed to be neutral
311
312   aftertouch_byte_ = 64;
313 }
314
315 string
316 Midi_note_off::to_string () const
317 {
318   Byte status_byte = (char) (0x80 + channel_);
319
320   string str = ::to_string ((char)status_byte);
321   str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
322   str += ::to_string ((char)aftertouch_byte_);
323
324   if (get_fine_tuning () != 0)
325     {
326       // Move pitch wheel back to the central position.
327       str += ::to_string ((char) 0x00);
328       str += ::to_string ((char) (0xE0 + channel_));
329       str += ::to_string ((char) (PITCH_WHEEL_CENTER &0x7F));
330       str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
331     }
332
333   return str;
334 }
335
336 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
337 {
338   audio_ = a;
339 }
340
341 string
342 Midi_dynamic::to_string () const
343 {
344   Byte status_byte = (char) (0xB0 + channel_);
345   string str = ::to_string ((char)status_byte);
346
347   /*
348     Main volume controller (per channel):
349     07 MSB
350     27 LSB
351   */
352   static Real const full_scale = 127;
353
354   int volume = (int) (audio_->volume_ * full_scale);
355   if (volume <= 0)
356     volume = 1;
357   if (volume > full_scale)
358     volume = (int)full_scale;
359
360   str += ::to_string ((char)0x07);
361   str += ::to_string ((char)volume);
362   return str;
363 }
364
365 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
366 {
367   audio_ = a;
368 }
369
370 string
371 Midi_piano_pedal::to_string () const
372 {
373   Byte status_byte = (char) (0xB0 + channel_);
374   string str = ::to_string ((char)status_byte);
375
376   if (audio_->type_string_ == "Sostenuto")
377     str += ::to_string ((char)0x42);
378   else if (audio_->type_string_ == "Sustain")
379     str += ::to_string ((char)0x40);
380   else if (audio_->type_string_ == "UnaCorda")
381     str += ::to_string ((char)0x43);
382
383   int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
384   str += ::to_string ((char)pedal);
385   return str;
386 }
387
388 Midi_tempo::Midi_tempo (Audio_tempo *a)
389 {
390   audio_ = a;
391 }
392
393 string
394 Midi_tempo::to_string () const
395 {
396   int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
397   string str = "ff5103";
398   str += String_convert::int2hex (useconds_per_4, 6, '0');
399   return String_convert::hex2bin (str);
400 }
401
402 Midi_text::Midi_text (Audio_text *a)
403 {
404   audio_ = a;
405 }
406
407 string
408 Midi_text::to_string () const
409 {
410   string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
411   str = String_convert::hex2bin (str);
412   str += i2varint_string (audio_->text_string_.length ());
413   str += audio_->text_string_;
414   return str;
415 }
416
417 Midi_track::Midi_track ()
418   : Midi_chunk ()
419 {
420   //                4D 54 72 6B     MTrk
421   //                00 00 00 3B     chunk length (59)
422   //        00      FF 58 04 04 02 18 08    time signature
423   //        00      FF 51 03 07 A1 20       tempo
424
425   // FF 59 02 sf mi  Key Signature
426   //         sf = -7:  7 flats
427   //         sf = -1:  1 flat
428   //         sf = 0:  key of C
429   //         sf = 1:  1 sharp
430   //         sf = 7: 7 sharps
431   //         mi = 0:  major key
432   //         mi = 1:  minor key
433
434   number_ = 0;
435
436   char const *data_str0 = ""
437     //        "00" "ff58" "0404" "0218" "08"
438     //  "00" "ff51" "0307" "a120"
439     // why a key at all, in midi?
440     // key: C
441     //  "00" "ff59" "02" "00" "00"
442     // key: F (scsii-menuetto)
443     //                            "00" "ff59" "02" "ff" "00"
444     ;
445
446   string data_string;
447   // only for format 0 (currently using format 1)?
448   data_string += String_convert::hex2bin (data_str0);
449
450   char const *footer_str0 = "00" "ff2f" "00";
451   string footer_string = String_convert::hex2bin (footer_str0);
452
453   set ("MTrk", data_string, footer_string);
454 }
455
456 void
457 Midi_track::add (Moment delta_time_mom, Midi_item *midi)
458 {
459   assert (delta_time_mom >= Moment (0));
460
461   Midi_event *e = new Midi_event (delta_time_mom, midi);
462   events_.push_back (e);
463 }
464
465 string
466 Midi_track::data_string () const
467 {
468   string str = Midi_chunk::data_string ();
469   if (do_midi_debugging_global)
470     str += "\n";
471
472   for (vector<Midi_event*>::const_iterator i (events_.begin());
473        i != events_.end(); i ++)
474     {
475       str += (*i)->to_string ();
476       if (do_midi_debugging_global)
477         str += "\n";
478     }
479   return str;
480 }
481
482
483 char const *
484 Midi_item::name () const
485 {
486    return this->class_name ();
487 }
488
489 Midi_track::~Midi_track ()
490 {
491   junk_pointers (events_);
492 }