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