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