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