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