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