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