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