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