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 "midi-item.hh"
15 #include "midi-stream.hh"
16 #include "audio-item.hh"
17 #include "duration.hh"
19 #include "killing-cons.tcc"
22 Midi_item::midi_p (Audio_item
* a
)
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;
44 Midi_chunk::set (String header_str
, String data_str
, String footer_str
)
47 footer_str_
= footer_str
;
48 header_str_
= header_str
;
52 Midi_chunk::data_str () const
58 Midi_chunk::str () const
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
);
71 Midi_duration::Midi_duration (Real seconds_f
)
73 seconds_f_
= seconds_f
;
77 Midi_duration::str () const
79 return String ("<duration: ") + to_str (seconds_f_
) + ">";
82 Midi_event::Midi_event (Moment delta_mom
, Midi_item
* midi_p
)
84 delta_mom_
= delta_mom
;
89 Midi_event::str () const
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
;
99 Midi_header::Midi_header (int format_i
, int tracks_i
, int clocks_per_4_i
)
103 String format_str
= String_convert::i2hex_str (format_i
, 4, '0');
104 str
+= String_convert::hex2bin_str (format_str
);
106 String tracks_str
= String_convert::i2hex_str (tracks_i
, 4, '0');
107 str
+= String_convert::hex2bin_str (tracks_str
);
109 String tempo_str
= String_convert::i2hex_str (clocks_per_4_i
, 4, '0');
110 str
+= String_convert::hex2bin_str (tempo_str
);
112 set ("MThd", str
, "");
115 /* why doesn't this start at 0 ?
117 char const* const instrument_name_sz_a_
[ ] = {
118 /* default is usually piano */
122 /* 1 */ "acoustic grand",
123 /* 2 */ "bright acoustic",
124 /* 3 */ "electric grand",
125 /* 4 */ "honky-tonk",
126 /* 5 */ "electric piano 1",
127 /* 6 */ "electric piano 2",
128 /* 7 */ "harpsichord",
131 /* (9-16 chrom percussion) */
133 /* 10 */ "glockenspiel",
134 /* 11 */ "music box",
135 /* 12 */ "vibraphone",
137 /* 14 */ "xylophone",
138 /* 15 */ "tubular bells",
142 /* 17 */ "drawbar organ",
143 /* 18 */ "percussive organ",
144 /* 19 */ "rock organ",
145 /* 20 */ "church organ",
146 /* 21 */ "reed organ",
147 /* 22 */ "accordion",
148 /* 23 */ "harmonica",
149 /* 24 */ "concertina",
152 /* 25 */ "acoustic guitar (nylon)",
153 /* 26 */ "acoustic guitar (steel)",
154 /* 27 */ "electric guitar (jazz)",
155 /* 28 */ "electric guitar (clean)",
156 /* 29 */ "electric guitar (muted)",
157 /* 30 */ "overdriven guitar",
158 /* 31 */ "distorted guitar",
159 /* 32 */ "guitar harmonics",
162 /* 33 */ "acoustic bass",
163 /* 34 */ "electric bass (finger)",
164 /* 35 */ "electric bass (pick)",
165 /* 36 */ "fretless bass",
166 /* 37 */ "slap bass 1",
167 /* 38 */ "slap bass 2",
168 /* 39 */ "synth bass 1",
169 /* 40 */ "synth bass 2",
171 /* (41-48 strings) */
175 /* 44 */ "contrabass",
176 /* 45 */ "tremolo strings",
177 /* 46 */ "pizzicato strings",
178 /* 47 */ "orchestral strings",
181 /* (49-56 ensemble) */
182 /* 49 */ "string ensemble 1",
183 /* 50 */ "string ensemble 2",
184 /* 51 */ "synthstrings 1",
185 /* 52 */ "synthstrings 2",
186 /* 53 */ "choir aahs",
187 /* 54 */ "voice oohs",
188 /* 55 */ "synth voice",
189 /* 56 */ "orchestra hit",
195 /* 60 */ "muted trumpet",
196 /* 61 */ "french horn",
197 /* 62 */ "brass section",
198 /* 63 */ "synthbrass 1",
199 /* 64 */ "synthbrass 2",
202 /* 65 */ "soprano sax",
204 /* 67 */ "tenor sax",
205 /* 68 */ "baritone sax",
207 /* 70 */ "english horn",
215 /* 76 */ "pan flute",
216 /* 77 */ "blown bottle",
217 /* 78 */ "skakuhachi",
221 /* (81-88 synth lead) */
222 /* 81 */ "lead 1 (square)",
223 /* 82 */ "lead 2 (sawtooth)",
224 /* 83 */ "lead 3 (calliope)",
225 /* 84 */ "lead 4 (chiff)",
226 /* 85 */ "lead 5 (charang)",
227 /* 86 */ "lead 6 (voice)",
228 /* 87 */ "lead 7 (fifths)",
229 /* 88 */ "lead 8 (bass+lead)",
231 /* (89-96 synth pad) */
232 /* 89 */ "pad 1 (new age)",
233 /* 90 */ "pad 2 (warm)",
234 /* 91 */ "pad 3 (polysynth)",
235 /* 92 */ "pad 4 (choir)",
236 /* 93 */ "pad 5 (bowed)",
237 /* 94 */ "pad 6 (metallic)",
238 /* 95 */ "pad 7 (halo)",
239 /* 96 */ "pad 8 (sweep)",
241 /* (97-104 synth effects) */
242 /* 97 */ "fx 1 (rain)",
243 /* 98 */ "fx 2 (soundtrack)",
244 /* 99 */ "fx 3 (crystal)",
245 /* 100 */ "fx 4 (atmosphere)",
246 /* 101 */ "fx 5 (brightness)",
247 /* 102 */ "fx 6 (goblins)",
248 /* 103 */ "fx 7 (echoes)",
249 /* 104 */ "fx 8 (sci-fi)",
251 /* (105-112 ethnic) */
254 /* 107 */ "shamisen",
261 /* (113-120 percussive) */
262 /* 113 */ "tinkle bell",
264 /* 115 */ "steel drums",
265 /* 116 */ "woodblock",
266 /* 117 */ "taiko drum",
267 /* 118 */ "melodic tom",
268 /* 119 */ "synth drum",
269 /* 120 */ "reverse cymbal",
271 /* (121-128 sound effects) */
272 /* 121 */ "guitar fret noise",
273 /* 122 */ "breath noise",
274 /* 123 */ "seashore",
275 /* 124 */ "bird tweet",
276 /* 125 */ "telephone ring",
277 /* 126 */ "helicopter",
278 /* 127 */ "applause",
283 Midi_instrument::Midi_instrument (Audio_instrument
* a
)
286 audio_l_
->str_
.to_lower ();
290 Midi_instrument::str () const
292 Byte program_byte
= 0;
294 for (int i
= 0; !found
&& instrument_name_sz_a_
[i
]; i
++)
295 if (audio_l_
->str_
== String (instrument_name_sz_a_
[ i
]))
297 program_byte
= (Byte
)i
;
303 warning (_f("No such instrument: `%s'", audio_l_
->str_
.ch_C ()));
306 String str
= to_str ((char) (0xc0 + channel_i_
));
307 str
+= to_str ((char)program_byte
);
311 Midi_item::Midi_item ()
316 Midi_item::~Midi_item ()
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_key
*a
)
349 Midi_key::str () const
351 int sharps_i
= audio_l_
->key_
.sharps_i ();
352 int flats_i
= audio_l_
->key_
.flats_i ();
354 // midi cannot handle non-conventional keys
355 if (flats_i
&& sharps_i
)
357 String str
= _f ("unconventional key: flats: %d, sharps: %d", flats_i
,
359 flats_i
= sharps_i
= 0;
361 int accidentals_i
= sharps_i
- flats_i
;
363 String str
= "ff5902";
364 str
+= String_convert::i2hex_str (accidentals_i
, 2, '0');
365 str
+= String_convert::i2hex_str ((int)audio_l_
->key_
.minor_b (), 2, '0');
366 return String_convert::hex2bin_str (str
);
369 Midi_time_signature::Midi_time_signature (Audio_time_signature
* a
)
372 clocks_per_1_i_
= 18;
376 Midi_time_signature::str () const
378 int num_i
= audio_l_
->beats_i_
;
379 int den_i
= audio_l_
->one_beat_i_
;
381 String str
= "ff5804";
382 str
+= String_convert::i2hex_str (num_i
, 2, '0');
383 str
+= String_convert::i2hex_str (intlog2 (den_i
) , 2, '0');
384 str
+= String_convert::i2hex_str (clocks_per_1_i_
, 2, '0');
385 str
+= String_convert::i2hex_str (8, 2, '0');
386 return String_convert::hex2bin_str (str
);
389 Midi_note::Midi_note (Audio_note
* a
)
392 dynamic_byte_
= 0x7f;
396 Midi_note::length_mom () const
398 Moment m
= audio_l_
->length_mom_
;
400 if (m
< Moment (1, 1000))
402 warning (_ ("silly duration"));
410 Midi_note::pitch_i () const
412 int p
= audio_l_
->pitch_
.semitone_pitch () + audio_l_
->transposing_i_
;
415 warning (_ ("silly pitch"));
422 Midi_note::str () const
424 Byte status_byte
= (char) (0x90 + channel_i_
);
426 String str
= to_str ((char)status_byte
);
427 str
+= to_str ((char) (pitch_i () + c0_pitch_i_c_
));
429 str
+= to_str ((char)dynamic_byte_
);
433 Midi_note_off::Midi_note_off (Midi_note
* n
)
434 : Midi_note (n
->audio_l_
)
437 channel_i_
= n
->channel_i_
;
439 // Anybody who hears any difference, or knows how this works?
441 // 0x64 is supposed to be neutral, but let's try
442 //aftertouch_byte_ = 0x64;
445 aftertouch_byte_
= i
;
450 Midi_note_off::str () const
452 Byte status_byte
= (char) (0x80 + channel_i_
);
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_
);
460 Midi_tempo::Midi_tempo (Audio_tempo
* a
)
466 Midi_tempo::str () const
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
);
474 Midi_text::Midi_text (Audio_text
* a
)
480 Midi_text::Midi_text (Midi_text::Type type
, String text_str
)
483 text_str_
= text_str
;
489 Midi_text::str () const
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_
;
498 Midi_track::Midi_track ()
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
506 // FF 59 02 sf mi Key Signature
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?
522 // "00" "ff59" "02" "00" "00"
523 // key: F (scsii-menuetto)
524 // "00" "ff59" "02" "ff" "00"
528 // only for format 0 (currently using format 1)?
529 data_str
+= String_convert::hex2bin_str (data_ch_C
);
531 char const* footer_ch_C
= "00" "ff2f" "00";
532 String footer_str
= String_convert::hex2bin_str (footer_ch_C
);
534 set ("MTrk", data_str
, footer_str
);
538 Midi_track::add (Moment delta_time_mom
, Midi_item
* midi_p
)
540 assert (delta_time_mom
>= Moment (0));
542 Midi_event
* e
= new Midi_event (delta_time_mom
, midi_p
);
543 event_p_list_
.append (new Killing_cons
<Midi_event
> (e
, 0));
547 Midi_track::data_str () const
549 String str
= Midi_chunk::data_str ();
550 if (check_debug
&& !monitor
->silent_b ("Midistrings"))
552 for (Cons
<Midi_event
> *i
=event_p_list_
.head_
; i
; i
= i
->next_
)
554 str
+= i
->car_
->str ();
555 if (check_debug
&& !monitor
->silent_b ("Midistrings"))