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