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