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