2 midi-item.cc -- implement Midi items.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--1999 Jan Nieuwenhuizen <janneke@gnu.org>
13 #include "string-convert.hh"
14 #include "command-request.hh"
15 #include "musical-request.hh"
16 #include "midi-item.hh"
17 #include "midi-stream.hh"
18 #include "audio-item.hh"
20 #include "killing-cons.tcc"
22 Midi_chunk::Midi_chunk ()
28 Midi_chunk::set (String header_str, String data_str, String footer_str)
31 footer_str_ = footer_str;
32 header_str_ = header_str;
36 Midi_chunk::data_str () const
42 Midi_chunk::str () const
44 String str = header_str_;
45 String dat = data_str ();
47 String length_str = String_convert::i2hex_str (dat.length_i ()
50 String length_str = String_convert::i2hex_str (data_str_.length_i ()
52 + footer_str_.length_i (), 8, '0');
53 length_str = String_convert::hex2bin_str (length_str);
60 Midi_duration::Midi_duration (Real seconds_f)
63 seconds_f_ = seconds_f;
67 Midi_duration::str () const
69 return String ("<duration: ") + to_str (seconds_f_) + ">";
72 Midi_event::Midi_event (Moment delta_mom, Midi_item* mitem_p)
74 delta_mom_ = delta_mom;
78 Midi_event::~Midi_event ()
85 Midi_event::str () const
87 int delta_i = delta_mom_ * Moment (Duration::division_1_i_s);
88 String delta_str = Midi_item::i2varint_str (delta_i);
89 String mitem_str = mitem_p_->str ();
90 assert (mitem_str.length_i ());
91 return delta_str + mitem_str;
95 Midi_header::Midi_header (int format_i, int tracks_i, int clocks_per_4_i)
100 String format_str = String_convert::i2hex_str (format_i, 4, '0');
101 str += String_convert::hex2bin_str (format_str);
103 String tracks_str = String_convert::i2hex_str (tracks_i, 4, '0');
104 str += String_convert::hex2bin_str (tracks_str);
106 String tempo_str = String_convert::i2hex_str (clocks_per_4_i, 4, '0');
107 str += String_convert::hex2bin_str (tempo_str);
109 set ("MThd", str, "");
112 /* why doesn't this start at 0 ?
114 char const* const instrument_name_sz_a_[ ] = {
115 /* default is usually piano */
119 /* 1 */ "acoustic grand",
120 /* 2 */ "bright acoustic",
121 /* 3 */ "electric grand",
122 /* 4 */ "honky-tonk",
123 /* 5 */ "electric piano 1",
124 /* 6 */ "electric piano 2",
125 /* 7 */ "harpsichord",
128 /* (9-16 chrom percussion) */
130 /* 10 */ "glockenspiel",
131 /* 11 */ "music box",
132 /* 12 */ "vibraphone",
134 /* 14 */ "xylophone",
135 /* 15 */ "tubular bells",
139 /* 17 */ "drawbar organ",
140 /* 18 */ "percussive organ",
141 /* 19 */ "rock organ",
142 /* 20 */ "church organ",
143 /* 21 */ "reed organ",
144 /* 22 */ "accordion",
145 /* 23 */ "harmonica",
146 /* 24 */ "concertina",
149 /* 25 */ "acoustic guitar (nylon)",
150 /* 26 */ "acoustic guitar (steel)",
151 /* 27 */ "electric guitar (jazz)",
152 /* 28 */ "electric guitar (clean)",
153 /* 29 */ "electric guitar (muted)",
154 /* 30 */ "overdriven guitar",
155 /* 31 */ "distorted guitar",
156 /* 32 */ "guitar harmonics",
159 /* 33 */ "acoustic bass",
160 /* 34 */ "electric bass (finger)",
161 /* 35 */ "electric bass (pick)",
162 /* 36 */ "fretless bass",
163 /* 37 */ "slap bass 1",
164 /* 38 */ "slap bass 2",
165 /* 39 */ "synth bass 1",
166 /* 40 */ "synth bass 2",
168 /* (41-48 strings) */
172 /* 44 */ "contrabass",
173 /* 45 */ "tremolo strings",
174 /* 46 */ "pizzicato strings",
175 /* 47 */ "orchestral strings",
178 /* (49-56 ensemble) */
179 /* 49 */ "string ensemble 1",
180 /* 50 */ "string ensemble 2",
181 /* 51 */ "synthstrings 1",
182 /* 52 */ "synthstrings 2",
183 /* 53 */ "choir aahs",
184 /* 54 */ "voice oohs",
185 /* 55 */ "synth voice",
186 /* 56 */ "orchestra hit",
192 /* 60 */ "muted trumpet",
193 /* 61 */ "french horn",
194 /* 62 */ "brass section",
195 /* 63 */ "synthbrass 1",
196 /* 64 */ "synthbrass 2",
199 /* 65 */ "soprano sax",
201 /* 67 */ "tenor sax",
202 /* 68 */ "baritone sax",
204 /* 70 */ "english horn",
212 /* 76 */ "pan flute",
213 /* 77 */ "blown bottle",
214 /* 78 */ "skakuhachi",
218 /* (81-88 synth lead) */
219 /* 81 */ "lead 1 (square)",
220 /* 82 */ "lead 2 (sawtooth)",
221 /* 83 */ "lead 3 (calliope)",
222 /* 84 */ "lead 4 (chiff)",
223 /* 85 */ "lead 5 (charang)",
224 /* 86 */ "lead 6 (voice)",
225 /* 87 */ "lead 7 (fifths)",
226 /* 88 */ "lead 8 (bass+lead)",
228 /* (89-96 synth pad) */
229 /* 89 */ "pad 1 (new age)",
230 /* 90 */ "pad 2 (warm)",
231 /* 91 */ "pad 3 (polysynth)",
232 /* 92 */ "pad 4 (choir)",
233 /* 93 */ "pad 5 (bowed)",
234 /* 94 */ "pad 6 (metallic)",
235 /* 95 */ "pad 7 (halo)",
236 /* 96 */ "pad 8 (sweep)",
238 /* (97-104 synth effects) */
239 /* 97 */ "fx 1 (rain)",
240 /* 98 */ "fx 2 (soundtrack)",
241 /* 99 */ "fx 3 (crystal)",
242 /* 100 */ "fx 4 (atmosphere)",
243 /* 101 */ "fx 5 (brightness)",
244 /* 102 */ "fx 6 (goblins)",
245 /* 103 */ "fx 7 (echoes)",
246 /* 104 */ "fx 8 (sci-fi)",
248 /* (105-112 ethnic) */
251 /* 107 */ "shamisen",
258 /* (113-120 percussive) */
259 /* 113 */ "tinkle bell",
261 /* 115 */ "steel drums",
262 /* 116 */ "woodblock",
263 /* 117 */ "taiko drum",
264 /* 118 */ "melodic tom",
265 /* 119 */ "synth drum",
266 /* 120 */ "reverse cymbal",
268 /* (121-128 sound effects) */
269 /* 121 */ "guitar fret noise",
270 /* 122 */ "breath noise",
271 /* 123 */ "seashore",
272 /* 124 */ "bird tweet",
273 /* 125 */ "telephone ring",
274 /* 126 */ "helicopter",
275 /* 127 */ "applause",
280 Midi_instrument::Midi_instrument (int channel_i, String instrument_str)
283 instrument_str_ = instrument_str;
284 instrument_str_.to_lower ();
285 channel_i_ = channel_i;
288 Midi_item::~Midi_item ()
293 Midi_instrument::str () const
295 Byte program_byte = 0;
297 for (int i = 0; !found && instrument_name_sz_a_[i]; i++)
298 if (instrument_str_ == String (instrument_name_sz_a_[ i ]))
300 program_byte = (Byte)i;
306 warning (_f("No such instrument: `%s'", instrument_str_.ch_C ()));
309 String str = to_str ((char) (0xc0 + channel_i_));
310 str += to_str ((char)program_byte);
314 Midi_item::Midi_item (Audio_item* audio_item_l)
316 audio_item_l_ = audio_item_l;
321 Midi_item::i2varint_str (int i)
323 int buffer_i = i & 0x7f;
324 while ( (i >>= 7) > 0)
328 buffer_i += (i & 0x7f);
334 str += to_str ((char)buffer_i);
343 Midi_key::Midi_key (Audio_item* audio_item_l)
344 : Midi_item (audio_item_l)
349 Midi_key::str () const
351 Key_change_req* k = dynamic_cast <Key_change_req *> (audio_item_l_->req_l_);
352 int sharps_i = k->sharps_i ();
353 int flats_i = k->flats_i ();
355 // midi cannot handle non-conventional keys
356 if (flats_i && sharps_i)
358 String str = _f ("unconventional key: flats: %d, sharps: %d", flats_i,
360 flats_i = sharps_i = 0;
362 int accidentals_i = sharps_i - flats_i;
364 String str = "ff5902";
365 str += String_convert::i2hex_str (accidentals_i, 2, '0');
366 int minor_i = k->minor_b ();
367 str += String_convert::i2hex_str (minor_i, 2, '0');
368 return String_convert::hex2bin_str (str);
371 Midi_time_signature::Midi_time_signature (Audio_item* audio_item_l)
372 : Midi_item (audio_item_l)
374 clocks_per_1_i_ = 18;
378 Midi_time_signature::str () const
380 Time_signature_change_req* m = dynamic_cast <Time_signature_change_req *> (audio_item_l_->req_l_);
381 int num_i = m->beats_i_;
382 int den_i = m->one_beat_i_;
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);
392 Midi_note::Midi_note (Audio_item* audio_item_l)
393 : Midi_item (audio_item_l)
395 dynamic_byte_ = 0x7f;
396 assert (dynamic_cast<Audio_note*> (audio_item_l));
400 Midi_note::length_mom () const
402 Moment m = dynamic_cast <Rhythmic_req *> (audio_item_l_->req_l_)->length_mom ();
403 if (m < Moment (1, 1000))
405 warning (_ ("silly duration"));
412 Midi_note::pitch_i () const
414 int p = dynamic_cast <Melodic_req*> (audio_item_l_->req_l_)->pitch_.semitone_pitch ()
415 + dynamic_cast<Audio_note*>(audio_item_l_)->transposing_i_;
418 warning (_ ("silly pitch"));
425 Midi_note::str () const
427 Byte status_byte = (char) (0x90 + channel_i_);
429 String str = to_str ((char)status_byte);
430 str += to_str ((char) (pitch_i () + c0_pitch_i_c_));
432 str += to_str ((char)dynamic_byte_);
436 Midi_note_off::Midi_note_off (Midi_note* midi_note_l)
437 : Midi_item (midi_note_l->audio_item_l_)
439 // 0x64 is supposed to be neutral, but let's try
440 aftertouch_byte_ = 0x64;
441 channel_i_ = midi_note_l->channel_i_;
445 Midi_note_off::pitch_i () const
447 return dynamic_cast <Melodic_req *> (audio_item_l_->req_l_)->pitch_.semitone_pitch ()
448 + dynamic_cast<Audio_note*>(audio_item_l_)->transposing_i_;
452 Midi_note_off::str () const
454 Byte status_byte = (char) (0x80 + channel_i_);
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_);
462 Midi_tempo::Midi_tempo (Audio_item* audio_item_l)
463 : Midi_item (audio_item_l)
465 per_minute_4_i_ = dynamic_cast<Audio_tempo*>(audio_item_l_)->per_minute_4_i_;
468 Midi_tempo::Midi_tempo (int per_minute_4_i)
471 per_minute_4_i_ = per_minute_4_i;
475 Midi_tempo::str () const
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);
483 Midi_text::Midi_text (Audio_item* audio_item_l)
484 : Midi_item (audio_item_l)
486 text_str_ = dynamic_cast<Audio_text*>(audio_item_l_)->text_str_;
487 type_ = (Type) dynamic_cast<Audio_text*>(audio_item_l_)->type_;
490 Midi_text::Midi_text (Midi_text::Type type, String text_str)
493 text_str_ = text_str;
498 Midi_text::str () const
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 ());
507 Midi_track::Midi_track ()
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
515 // FF 59 02 sf mi Key Signature
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?
531 // "00" "ff59" "02" "00" "00"
532 // key: F (scsii-menuetto)
533 // "00" "ff59" "02" "ff" "00"
537 // only for format 0 (currently using format 1)?
538 data_str += String_convert::hex2bin_str (data_ch_C);
540 char const* footer_ch_C = "00" "ff2f" "00";
541 String footer_str = String_convert::hex2bin_str (footer_ch_C);
543 set ("MTrk", data_str, footer_str);
547 Midi_track::add (Moment delta_time_mom, Midi_item* mitem_p)
549 assert (delta_time_mom >= Moment (0));
551 Midi_event * e = new Midi_event (delta_time_mom, mitem_p);
552 event_p_list_.append (new Killing_cons<Midi_event> (e, 0));
556 Midi_track::data_str () const
558 String str = Midi_chunk::data_str ();
559 if (check_debug && !monitor->silent_b ("Midistrings"))
561 for (Cons<Midi_event> *i=event_p_list_.head_; i; i = i->next_)
563 str += i->car_->str ();
564 if (check_debug && !monitor->silent_b ("Midistrings"))
570 Midi_track::~Midi_track ()