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