]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-item.cc
patch::: 1.3.132.jcn1
[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--2001 Jan Nieuwenhuizen <janneke@gnu.org>
7  */
8
9 #include "debug.hh"
10 #include "misc.hh"
11 #include "string.hh"
12 #include "string-convert.hh"
13 #include "midi-item.hh"
14 #include "midi-stream.hh"
15 #include "audio-item.hh"
16 #include "duration.hh"
17
18 #include "killing-cons.tcc"
19
20 Midi_item*
21 Midi_item::midi_p (Audio_item* a)
22 {
23   if (Audio_key* i = dynamic_cast<Audio_key*> (a))
24     return new Midi_key (i);
25   else if (Audio_instrument* i = dynamic_cast<Audio_instrument*> (a))
26     return i->str_.length_i () ? new Midi_instrument (i) : 0;
27   else if (Audio_note* i = dynamic_cast<Audio_note*> (a))
28     return new Midi_note (i);
29   else if (Audio_dynamic* i = dynamic_cast<Audio_dynamic*> (a))
30     return new Midi_dynamic (i);
31   else if (Audio_piano_pedal* i = dynamic_cast<Audio_piano_pedal*> (a))
32     return new Midi_piano_pedal (i);
33   else if (Audio_tempo* i = dynamic_cast<Audio_tempo*> (a))
34     return new Midi_tempo (i);
35   else if (Audio_time_signature* i = dynamic_cast<Audio_time_signature*> (a))
36     return new Midi_time_signature (i);
37   else if (Audio_text* i = dynamic_cast<Audio_text*> (a))
38     //return i->text_str_.length_i () ? new Midi_text (i) : 0;
39     return new Midi_text (i);
40   else
41     assert (0);
42
43   // isn't C++ grand?
44   return 0;
45 }
46
47 void
48 Midi_chunk::set (String header_str, String data_str, String footer_str)
49 {
50   data_str_ = data_str;
51   footer_str_ = footer_str;
52   header_str_ = header_str;
53 }
54   
55 String
56 Midi_chunk::data_str () const
57 {
58   return data_str_;
59 }
60
61 String
62 Midi_chunk::str () const
63 {
64   String str = header_str_;
65   String dat = data_str ();
66   String length_str = String_convert::i2hex_str (dat.length_i () 
67     + footer_str_.length_i (), 8, '0');
68   length_str = String_convert::hex2bin_str (length_str);
69   str += length_str;
70   str += dat;
71   str += footer_str_;
72   return str;
73 }
74
75 Midi_duration::Midi_duration (Real seconds_f)
76 {
77   seconds_f_ = seconds_f;
78 }
79
80 String
81 Midi_duration::str () const
82 {
83   return String ("<duration: ") + to_str (seconds_f_) + ">";
84 }
85
86 Midi_event::Midi_event (Moment delta_mom, Midi_item* midi_p)
87 {
88   delta_mom_ = delta_mom;
89   midi_p_ = midi_p;
90 }
91
92 String
93 Midi_event::str () const
94 {
95   int delta_i = delta_mom_ * Moment (384 * 4); // ugh.
96
97   String delta_str = Midi_item::i2varint_str (delta_i);
98   String midi_str = midi_p_->str ();
99   assert (midi_str.length_i ());
100   return delta_str + midi_str;
101 }
102
103
104 Midi_header::Midi_header (int format_i, int tracks_i, int clocks_per_4_i)
105 {
106   String str;
107         
108   String format_str = String_convert::i2hex_str (format_i, 4, '0');
109   str += String_convert::hex2bin_str (format_str);
110         
111   String tracks_str = String_convert::i2hex_str (tracks_i, 4, '0');
112   str += String_convert::hex2bin_str (tracks_str);
113
114   String tempo_str = String_convert::i2hex_str (clocks_per_4_i, 4, '0');
115   str += String_convert::hex2bin_str (tempo_str);
116
117   set ("MThd", str, "");
118 }
119
120
121 /* why doesn't this start at 0 ?
122
123    TODO: -> IN GUILE!
124  */
125 char const* const instrument_name_sz_a_[ ] = {
126   /* default is usually piano */
127   /* 0 "piano", */
128
129   /* (1-8 piano) */
130   /* 1 */ "acoustic grand",
131           /* 2 */ "bright acoustic",
132           /* 3 */ "electric grand",
133           /* 4 */ "honky-tonk",
134           /* 5 */ "electric piano 1",
135           /* 6 */ "electric piano 2",
136           /* 7 */ "harpsichord",
137           /* 8 */ "clav",
138
139           /* (9-16 chrom percussion) */
140           /* 9 */ "celesta",
141           /* 10 */ "glockenspiel",
142           /* 11 */ "music box",
143           /* 12 */ "vibraphone",
144           /* 13 */ "marimba",
145           /* 14 */ "xylophone",
146           /* 15 */ "tubular bells",
147           /* 16 */ "dulcimer",
148
149           /* (17-24 organ) */
150           /* 17 */ "drawbar organ",
151           /* 18 */ "percussive organ",
152           /* 19 */ "rock organ",
153           /* 20 */ "church organ",
154           /* 21 */ "reed organ",
155           /* 22 */ "accordion",
156           /* 23 */ "harmonica",
157           /* 24 */ "concertina",
158
159           /* (25-32 guitar) */
160           /* 25 */ "acoustic guitar (nylon)",
161           /* 26 */ "acoustic guitar (steel)",
162           /* 27 */ "electric guitar (jazz)",
163           /* 28 */ "electric guitar (clean)",
164           /* 29 */ "electric guitar (muted)",
165           /* 30 */ "overdriven guitar",
166           /* 31 */ "distorted guitar",
167           /* 32 */ "guitar harmonics",
168
169           /* (33-40 bass) */
170           /* 33 */ "acoustic bass",
171           /* 34 */ "electric bass (finger)",
172           /* 35 */ "electric bass (pick)",
173           /* 36 */ "fretless bass",
174           /* 37 */ "slap bass 1",
175           /* 38 */ "slap bass 2",
176           /* 39 */ "synth bass 1",
177           /* 40 */ "synth bass 2",
178
179           /* (41-48 strings) */
180           /* 41 */ "violin",
181           /* 42 */ "viola",
182           /* 43 */ "cello",
183           /* 44 */ "contrabass",
184           /* 45 */ "tremolo strings",
185           /* 46 */ "pizzicato strings",
186           /* 47 */ "orchestral strings",
187           /* 48 */ "timpani",
188
189           /* (49-56 ensemble) */
190           /* 49 */ "string ensemble 1",
191           /* 50 */ "string ensemble 2",
192           /* 51 */ "synthstrings 1",
193           /* 52 */ "synthstrings 2",
194           /* 53 */ "choir aahs",
195           /* 54 */ "voice oohs",
196           /* 55 */ "synth voice",
197           /* 56 */ "orchestra hit",
198
199           /* (57-64 brass) */
200           /* 57 */ "trumpet",
201           /* 58 */ "trombone",
202           /* 59 */ "tuba",
203           /* 60 */ "muted trumpet",
204           /* 61 */ "french horn",
205           /* 62 */ "brass section",
206           /* 63 */ "synthbrass 1",
207           /* 64 */ "synthbrass 2",
208
209           /* (65-72 reed) */
210           /* 65 */ "soprano sax",
211           /* 66 */ "alto sax",
212           /* 67 */ "tenor sax",
213           /* 68 */ "baritone sax",
214           /* 69 */ "oboe",
215           /* 70 */ "english horn",
216           /* 71 */ "bassoon",
217           /* 72 */ "clarinet",
218
219           /* (73-80 pipe) */
220           /* 73 */ "piccolo",
221           /* 74 */ "flute",
222           /* 75 */ "recorder",
223           /* 76 */ "pan flute",
224           /* 77 */ "blown bottle",
225           /* 78 */ "skakuhachi",
226           /* 79 */ "whistle",
227           /* 80 */ "ocarina",
228
229           /* (81-88 synth lead) */
230           /* 81 */ "lead 1 (square)",
231           /* 82 */ "lead 2 (sawtooth)",
232           /* 83 */ "lead 3 (calliope)",
233           /* 84 */ "lead 4 (chiff)",
234           /* 85 */ "lead 5 (charang)",
235           /* 86 */ "lead 6 (voice)",
236           /* 87 */ "lead 7 (fifths)",
237           /* 88 */ "lead 8 (bass+lead)",
238
239           /* (89-96 synth pad) */
240           /* 89 */ "pad 1 (new age)",
241           /* 90 */ "pad 2 (warm)",
242           /* 91 */ "pad 3 (polysynth)",
243           /* 92 */ "pad 4 (choir)",
244           /* 93 */ "pad 5 (bowed)",
245           /* 94 */ "pad 6 (metallic)",
246           /* 95 */ "pad 7 (halo)",
247           /* 96 */ "pad 8 (sweep)",
248
249           /* (97-104 synth effects) */
250           /* 97 */ "fx 1 (rain)",
251           /* 98 */ "fx 2 (soundtrack)",
252           /* 99 */ "fx 3 (crystal)",
253           /* 100 */ "fx 4 (atmosphere)",
254           /* 101 */ "fx 5 (brightness)",
255           /* 102 */ "fx 6 (goblins)",
256           /* 103 */ "fx 7 (echoes)",
257           /* 104 */ "fx 8 (sci-fi)",
258
259           /* (105-112 ethnic) */
260           /* 105 */ "sitar",
261           /* 106 */ "banjo",
262           /* 107 */ "shamisen",
263           /* 108 */ "koto",
264           /* 109 */ "kalimba",
265           /* 110 */ "bagpipe",
266           /* 111 */ "fiddle",
267           /* 112 */ "shanai",
268
269           /* (113-120 percussive) */
270           /* 113 */ "tinkle bell",
271           /* 114 */ "agogo",
272           /* 115 */ "steel drums",
273           /* 116 */ "woodblock",
274           /* 117 */ "taiko drum",
275           /* 118 */ "melodic tom",
276           /* 119 */ "synth drum",
277           /* 120 */ "reverse cymbal",
278
279           /* (121-128 sound effects) */
280           /* 121 */ "guitar fret noise",
281           /* 122 */ "breath noise",
282           /* 123 */ "seashore",
283           /* 124 */ "bird tweet",
284           /* 125 */ "telephone ring",
285           /* 126 */ "helicopter",
286           /* 127 */ "applause",
287           /* 128 */ "gunshot",
288           0
289 }; 
290
291 Midi_instrument::Midi_instrument (Audio_instrument* a)
292 {
293   audio_l_ = a;
294   audio_l_->str_.to_lower ();
295 }
296
297 String
298 Midi_instrument::str () const
299 {
300   Byte program_byte = 0;
301   bool found = false;
302   for (int i = 0; !found && instrument_name_sz_a_[i]; i++)
303     if (audio_l_->str_ == String (instrument_name_sz_a_[ i ])) 
304       {
305         program_byte = (Byte)i;
306         found = true;
307       }
308
309   if (!found)
310     {
311       warning (_f ("no such instrument: `%s'", audio_l_->str_.ch_C ()));
312     }
313   
314   String str = to_str ((char) (0xc0 + channel_i_));
315   str += to_str ((char)program_byte);
316   return str;
317 }
318
319 Midi_item::Midi_item ()
320 {
321   channel_i_ = 0;
322 }
323
324 Midi_item::~Midi_item ()
325 {
326 }
327
328 String
329 Midi_item::i2varint_str (int i)
330 {
331   int buffer_i = i & 0x7f;
332   while ( (i >>= 7) > 0) 
333     {
334       buffer_i <<= 8;
335       buffer_i |= 0x80;
336       buffer_i += (i & 0x7f);
337     }
338
339   String str;
340   while (1) 
341     {
342       str += to_str ((char)buffer_i);
343       if (buffer_i & 0x80)
344         buffer_i >>= 8;
345       else
346         break;
347     }
348   return str;
349 }
350
351 Midi_key::Midi_key (Audio_key*a)
352 {
353   audio_l_ = a;
354 }
355
356 String
357 Midi_key::str () const
358 {
359   // fxime.
360   int sharps_i = 0; //audio_l_->key_.sharps_i ();
361   int flats_i = 0; //audio_l_->key_.flats_i ();
362
363   // midi cannot handle non-conventional keys
364   if (flats_i && sharps_i)
365     {
366       String str = _f ("unconventional key: flats: %d, sharps: %d", flats_i, 
367         sharps_i);
368       flats_i = sharps_i = 0;
369     }
370   int accidentals_i = sharps_i - flats_i;
371
372   String str = "ff5902";
373   str += String_convert::i2hex_str (accidentals_i, 2, '0');
374
375   // (int)audio_l_->key_.minor_b ()
376   str += String_convert::i2hex_str (0, 2, '0');
377   return String_convert::hex2bin_str (str);
378 }
379
380 Midi_time_signature::Midi_time_signature (Audio_time_signature* a)
381 {
382   audio_l_ = a;
383   clocks_per_1_i_ = 18;
384 }
385
386 String
387 Midi_time_signature::str () const
388 {
389   int num_i = audio_l_->beats_i_;
390   int den_i = audio_l_->one_beat_i_;
391
392   String str = "ff5804";
393   str += String_convert::i2hex_str (num_i, 2, '0');
394   str += String_convert::i2hex_str (intlog2 (den_i) , 2, '0');
395   str += String_convert::i2hex_str (clocks_per_1_i_, 2, '0');
396   str += String_convert::i2hex_str (8, 2, '0');
397   return String_convert::hex2bin_str (str);
398 }
399
400 Midi_note::Midi_note (Audio_note* a)
401 {
402   audio_l_ = a;
403   dynamic_byte_ = 0x7f;
404 }
405
406 Moment
407 Midi_note::length_mom () const
408 {
409   Moment m = audio_l_->length_mom_;
410 #if 0
411   if (m < Moment (1, 1000))
412     {
413       warning (_ ("silly duration"));
414       m = 1;
415      }
416 #endif
417   return m;
418 }
419
420 int
421 Midi_note::pitch_i () const
422 {
423   int p = audio_l_->pitch_.semitone_pitch () + audio_l_->transposing_i_;
424   if (p == INT_MAX)
425     {
426       warning (_ ("silly pitch"));
427       p = 0;
428      }
429   return p;
430 }
431
432 String
433 Midi_note::str () const
434 {
435   Byte status_byte = (char) (0x90 + channel_i_);
436
437   String str = to_str ((char)status_byte);
438   str += to_str ((char) (pitch_i () + c0_pitch_i_c_));
439
440   str += to_str ((char)dynamic_byte_);
441   return str;
442 }
443
444 Midi_note_off::Midi_note_off (Midi_note* n)
445   : Midi_note (n->audio_l_)
446 {
447   on_l_ = n;
448   channel_i_ = n->channel_i_;
449
450   // Anybody who hears any difference, or knows how this works?
451   //  0 should definitely be avoided, notes stick on some sound cards.
452   // 64 is supposed to be neutral
453   
454   aftertouch_byte_ = 64;
455 }
456
457 String
458 Midi_note_off::str () const
459 {
460   Byte status_byte = (char) (0x80 + channel_i_);
461
462   String str = to_str ((char)status_byte);
463   str += to_str ((char) (pitch_i () + Midi_note::c0_pitch_i_c_));
464   str += to_str ((char)aftertouch_byte_);
465   return str;
466 }
467
468 Midi_dynamic::Midi_dynamic (Audio_dynamic* a)
469 {
470   audio_l_ = a;
471 }
472
473 String
474 Midi_dynamic::str () const
475 {
476   Byte status_byte = (char) (0xB0 + channel_i_);
477   String str = to_str ((char)status_byte);
478
479   /*
480     Main volume controller (per channel):
481     07 MSB
482     27 LSB
483    */
484   static Real const full_scale = 127;
485   
486   int volume = (int)(audio_l_->volume_*full_scale);
487   if (volume <= 0)
488     volume = 1;
489   if (volume > full_scale)
490     volume = (int)full_scale;
491
492   str += to_str ((char)0x07);
493   str += to_str ((char)volume);
494   return str;
495 }
496
497 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal* a)
498 {
499   audio_l_ = a;
500 }
501
502 String
503 Midi_piano_pedal::str () const
504 {
505   Byte status_byte = (char) (0xB0 + channel_i_);
506   String str = to_str ((char)status_byte);
507
508   if (audio_l_->type_str_ == "Sostenuto")
509     str += to_str ((char)0x42);
510   else if (audio_l_->type_str_ == "Sustain")
511     str += to_str ((char)0x40);
512   else if (audio_l_->type_str_ == "UnaChorda")
513     str += to_str ((char)0x43);
514
515   int pedal = ((1 - audio_l_->dir_) / 2 ) * 0x7f;
516   str += to_str ((char)pedal);
517   return str;
518 }
519
520 Midi_tempo::Midi_tempo (Audio_tempo* a)
521 {
522   audio_l_ = a;
523 }
524
525 String
526 Midi_tempo::str () const
527 {
528   int useconds_per_4_i = 60 * (int)1e6 / audio_l_->per_minute_4_i_;
529   String str = "ff5103";
530   str += String_convert::i2hex_str (useconds_per_4_i, 6, '0');
531   return String_convert::hex2bin_str (str);
532 }
533
534 Midi_text::Midi_text (Audio_text* a)
535 {
536   audio_l_ = a;
537 }
538
539 String
540 Midi_text::str () const
541 {
542   String str = "ff" + String_convert::i2hex_str (audio_l_->type_, 2, '0');
543   str = String_convert::hex2bin_str (str);
544   str += i2varint_str (audio_l_->text_str_.length_i ());
545   str += audio_l_->text_str_;
546   return str;
547 }
548
549 Midi_track::Midi_track ()
550   : Midi_chunk ()
551 {
552   //                4D 54 72 6B     MTrk
553   //                00 00 00 3B     chunk length (59)
554   //        00      FF 58 04 04 02 18 08    time signature
555   //        00      FF 51 03 07 A1 20       tempo
556  
557 // FF 59 02 sf mi  Key Signature
558 //         sf = -7:  7 flats
559 //         sf = -1:  1 flat
560 //         sf = 0:  key of C
561 //         sf = 1:  1 sharp
562 //         sf = 7: 7 sharps
563 //         mi = 0:  major key
564 //         mi = 1:  minor key
565
566   number_i_ = 0;
567         
568   char const* data_ch_C = ""
569     //        "00" "ff58" "0404" "0218" "08"
570     //  "00" "ff51" "0307" "a120"
571     // why a key at all, in midi?
572     // key: C
573     //  "00" "ff59" "02" "00" "00"
574     // key: F (scsii-menuetto)
575     //                            "00" "ff59" "02" "ff" "00"
576         ;
577
578   String data_str;
579   // only for format 0 (currently using format 1)?
580   data_str += String_convert::hex2bin_str (data_ch_C);
581
582   char const* footer_ch_C = "00" "ff2f" "00";
583   String footer_str = String_convert::hex2bin_str (footer_ch_C);
584
585   set ("MTrk", data_str, footer_str);
586 }
587
588 void 
589 Midi_track::add (Moment delta_time_mom, Midi_item* midi_p)
590 {
591   assert (delta_time_mom >= Moment (0));
592
593   Midi_event * e = new Midi_event (delta_time_mom, midi_p);
594   event_p_list_.append (new Killing_cons<Midi_event> (e, 0));
595 }
596
597 extern bool verbose_global_b;
598
599 String
600 Midi_track::data_str () const
601 {
602   String str = Midi_chunk::data_str ();
603 #if 0
604   if (flower_dstream && !flower_dstream->silent_b ("Midistrings"))
605 #else
606   if (verbose_global_b)
607 #endif
608     str += "\n";
609   for (Cons<Midi_event> *i=event_p_list_.head_; i; i = i->next_) 
610     {
611       str += i->car_->str ();
612 #if 0
613       if (flower_dstream && !flower_dstream->silent_b ("Midistrings"))
614 #else   
615       if (verbose_global_b)
616 #endif  
617         str += "\n";
618     }
619   return str;
620 }