]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-item.cc
* lily/modified-font-metric.cc (text_dimension): try
[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--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7  */
8
9 #include "midi-item.hh"
10
11 #include "warn.hh"
12 #include "main.hh"
13 #include "misc.hh"
14 #include "string-convert.hh"
15 #include "midi-stream.hh"
16 #include "duration.hh"
17 #include "scm-option.hh"
18 #include "killing-cons.tcc"
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   str += length_string;
75   str += dat;
76   str += footer_string_;
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 (Moment delta_mom, Midi_item* midi)
92 {
93   delta_mom_ = delta_mom;
94   midi_ = midi;
95 }
96
97 /*
98   ugh. midi output badly broken since grace note hackage.
99  */
100 String
101 Midi_event::to_string () const
102 {
103   Rational rat_dt = (delta_mom_.main_part_ * Rational (384) +
104     delta_mom_.grace_part_ * Rational (100))*Rational (4);
105   int delta_i = int (rat_dt);
106
107   String delta_string = Midi_item::i2varint_string (delta_i);
108   String midi_string = midi_->to_string ();
109   assert (midi_string.length ());
110   return delta_string + midi_string;
111 }
112
113
114 Midi_header::Midi_header (int format_i, int tracks_i, int clocks_per_4_i)
115 {
116   String str;
117         
118   String format_string = String_convert::int2hex (format_i, 4, '0');
119   str += String_convert::hex2bin (format_string);
120         
121   String tracks_string = String_convert::int2hex (tracks_i, 4, '0');
122   str += String_convert::hex2bin (tracks_string);
123
124   String tempo_string = String_convert::int2hex (clocks_per_4_i, 4, '0');
125   str += String_convert::hex2bin (tempo_string);
126
127   set ("MThd", str, "");
128 }
129
130 Midi_instrument::Midi_instrument (Audio_instrument* a)
131 {
132   audio_ = a;
133   audio_->str_.to_lower ();
134 }
135
136 String
137 Midi_instrument::to_string () const
138 {
139   Byte program_byte = 0;
140   bool found = false;
141
142   /*
143     UGH. don't use eval.
144    */
145   SCM proc = ly_lily_module_constant ("midi-program");
146   SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.to_str0 ()));
147   found = (program != SCM_BOOL_F);
148   if (found)
149     program_byte = scm_to_int (program);
150   else
151       warning (_f ("no such MIDI instrument: `%s'", audio_->str_.to_str0 ()));
152
153   String str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME: Should be track. -rz
154   str += ::to_string ((char)program_byte);
155   return str;
156 }
157
158 Midi_item::Midi_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 = i & 0x7f;
171   while ((i >>= 7) > 0) 
172     {
173       buffer_i <<= 8;
174       buffer_i |= 0x80;
175       buffer_i += (i & 0x7f);
176     }
177
178   String str;
179   while (1) 
180     {
181       str += ::to_string ((char)buffer_i);
182       if (buffer_i & 0x80)
183         buffer_i >>= 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 = audio_->beats_;
217   int den = audio_->one_beat_;
218
219   String str = "ff5804";
220   str += String_convert::int2hex (num, 2, '0');
221   str += String_convert::int2hex (intlog2 (den) , 2, '0');
222   str += String_convert::int2hex (clocks_per_1_, 2, '0');
223   str += String_convert::int2hex (8, 2, '0');
224   return String_convert::hex2bin (str);
225 }
226
227 Midi_note::Midi_note (Audio_note* a)
228 {
229   audio_ = a;
230   dynamic_byte_ = 0x7f;
231 }
232
233 Moment
234 Midi_note::get_length () const
235 {
236   Moment m = audio_->length_mom_;
237   return m;
238 }
239
240 int
241 Midi_note::get_fine_tuning () const
242 {
243   int ft = audio_->pitch_.quartertone_pitch ();
244   ft -= 2 * audio_->pitch_.semitone_pitch ();
245   ft *= 50; // 1 quarter tone = 50 cents
246   return ft;
247 }
248
249 int
250 Midi_note::get_pitch () const
251 {
252   int p = audio_->pitch_.semitone_pitch () + audio_->transposing_;
253   if (p == INT_MAX)
254     {
255       warning (_ ("silly pitch"));
256       p = 0;
257      }
258   return p;
259 }
260
261 String
262 Midi_note::to_string () const
263 {
264   Byte status_byte = (char) (0x90 + channel_);
265   String str = "";
266   int finetune;
267
268   // print warning if fine tuning was needed, HJJ
269   if (get_fine_tuning () != 0)
270     {
271       warning (_f ("Experimental: temporarily fine tuning (of %d cents) a channel.", 
272             get_fine_tuning ()));
273
274       finetune = PITCH_WHEEL_CENTER;
275       // Move pitch wheel to a shifted position.
276       // The pitch wheel range (of 4 semitones) is multiplied by the cents.
277       finetune += (PITCH_WHEEL_RANGE * get_fine_tuning ()) / (4 * 100);
278
279       str += ::to_string ((char) (0xE0 + channel_));
280       str += ::to_string ((char) (finetune & 0x7F));
281       str += ::to_string ((char) (finetune >> 7));
282       str += ::to_string ((char) (0x00));
283     }
284
285   str += ::to_string ((char)status_byte);
286   str += ::to_string ((char) (get_pitch () + c0_pitch_i_));
287   str += ::to_string ((char)dynamic_byte_);
288
289   return str;
290 }
291
292 Midi_note_off::Midi_note_off (Midi_note* n)
293   : Midi_note (n->audio_)
294 {
295   on_ = n;
296   channel_ = n->channel_;
297
298   // Anybody who hears any difference, or knows how this works?
299   //  0 should definitely be avoided, notes stick on some sound cards.
300   // 64 is supposed to be neutral
301   
302   aftertouch_byte_ = 64;
303 }
304
305 String
306 Midi_note_off::to_string () const
307 {
308   Byte status_byte = (char) (0x80 + channel_);
309
310   String str = ::to_string ((char)status_byte);
311   str += ::to_string ((char) (get_pitch () + Midi_note::c0_pitch_i_));
312   str += ::to_string ((char)aftertouch_byte_);
313
314   if (get_fine_tuning () != 0)
315     {
316       // Move pitch wheel back to the central position.
317       str += ::to_string ((char) 0x00);
318       str += ::to_string ((char) (0xE0 + channel_));
319       str += ::to_string ((char) (PITCH_WHEEL_CENTER & 0x7F));
320       str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
321     }
322
323   return str;
324 }
325
326 Midi_dynamic::Midi_dynamic (Audio_dynamic* a)
327 {
328   audio_ = a;
329 }
330
331 String
332 Midi_dynamic::to_string () const
333 {
334   Byte status_byte = (char) (0xB0 + channel_);
335   String str = ::to_string ((char)status_byte);
336
337   /*
338     Main volume controller (per channel):
339     07 MSB
340     27 LSB
341    */
342   static Real const full_scale = 127;
343   
344   int volume = (int) (audio_->volume_*full_scale);
345   if (volume <= 0)
346     volume = 1;
347   if (volume > full_scale)
348     volume = (int)full_scale;
349
350   str += ::to_string ((char)0x07);
351   str += ::to_string ((char)volume);
352   return str;
353 }
354
355 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal* a)
356 {
357   audio_ = a;
358 }
359
360 String
361 Midi_piano_pedal::to_string () const
362 {
363   Byte status_byte = (char) (0xB0 + channel_);
364   String str = ::to_string ((char)status_byte);
365
366   if (audio_->type_string_ == "Sostenuto")
367     str += ::to_string ((char)0x42);
368   else if (audio_->type_string_ == "Sustain")
369     str += ::to_string ((char)0x40);
370   else if (audio_->type_string_ == "UnaCorda")
371     str += ::to_string ((char)0x43);
372
373   int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
374   str += ::to_string ((char)pedal);
375   return str;
376 }
377
378 Midi_tempo::Midi_tempo (Audio_tempo* a)
379 {
380   audio_ = a;
381 }
382
383 String
384 Midi_tempo::to_string () const
385 {
386   int useconds_per_4_i = 60 * (int)1e6 / audio_->per_minute_4_;
387   String str = "ff5103";
388   str += String_convert::int2hex (useconds_per_4_i, 6, '0');
389   return String_convert::hex2bin (str);
390 }
391
392 Midi_text::Midi_text (Audio_text* a)
393 {
394   audio_ = a;
395 }
396
397 String
398 Midi_text::to_string () const
399 {
400   String str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
401   str = String_convert::hex2bin (str);
402   str += i2varint_string (audio_->text_string_.length ());
403   str += audio_->text_string_;
404   return str;
405 }
406
407 Midi_track::Midi_track ()
408   : Midi_chunk ()
409 {
410   //                4D 54 72 6B     MTrk
411   //                00 00 00 3B     chunk length (59)
412   //        00      FF 58 04 04 02 18 08    time signature
413   //        00      FF 51 03 07 A1 20       tempo
414  
415 // FF 59 02 sf mi  Key Signature
416 //         sf = -7:  7 flats
417 //         sf = -1:  1 flat
418 //         sf = 0:  key of C
419 //         sf = 1:  1 sharp
420 //         sf = 7: 7 sharps
421 //         mi = 0:  major key
422 //         mi = 1:  minor key
423
424   number_ = 0;
425         
426   char const* data_str0 = ""
427     //        "00" "ff58" "0404" "0218" "08"
428     //  "00" "ff51" "0307" "a120"
429     // why a key at all, in midi?
430     // key: C
431     //  "00" "ff59" "02" "00" "00"
432     // key: F (scsii-menuetto)
433     //                            "00" "ff59" "02" "ff" "00"
434         ;
435
436   String data_string;
437   // only for format 0 (currently using format 1)?
438   data_string += String_convert::hex2bin (data_str0);
439
440   char const* footer_str0 = "00" "ff2f" "00";
441   String footer_string = String_convert::hex2bin (footer_str0);
442
443   set ("MTrk", data_string, footer_string);
444 }
445
446 void 
447 Midi_track::add (Moment delta_time_mom, Midi_item* midi)
448 {
449   assert (delta_time_mom >= Moment (0));
450
451   Midi_event * e = new Midi_event (delta_time_mom, midi);
452   event_p_list_.append (new Killing_cons<Midi_event> (e, 0));
453 }
454
455 String
456 Midi_track::data_string () const
457 {
458   String str = Midi_chunk::data_string ();
459   if (midi_debug_global_b)
460     str += "\n";
461   for (Cons<Midi_event> *i = event_p_list_.head_; i; i = i->next_) 
462     {
463       str += i->car_->to_string ();
464       if (midi_debug_global_b)
465         str += "\n";
466     }
467   return str;
468 }