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