*** empty log message ***
[lilypond/patrick.git] / lily / midi-item.cc
blob48f0061d709c294290caa465ff50d5a7be09a385
1 /*
2 midi-item.cc -- implement Midi items.
4 source file of the GNU LilyPond music typesetter
6 (c) 1997--2005 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
9 #include "midi-item.hh"
11 #include "warn.hh"
12 #include "main.hh"
13 #include "misc.hh"
14 #include "string-convert.hh"
15 #include "midi-stream.hh"
16 #include "duration.hh"
17 #include "scm-option.hh"
18 #include "killing-cons.tcc"
20 #define PITCH_WHEEL_TOP 0x3FFF
21 #define PITCH_WHEEL_CENTER 0x2000
22 #define PITCH_WHEEL_BOTTOM 0x0000
23 #define PITCH_WHEEL_RANGE (PITCH_WHEEL_TOP - PITCH_WHEEL_BOTTOM)
25 Midi_item *
26 Midi_item::get_midi (Audio_item *a)
28 if (Audio_key *i = dynamic_cast<Audio_key *> (a))
29 return new Midi_key (i);
30 else if (Audio_instrument *i = dynamic_cast<Audio_instrument *> (a))
31 return i->str_.length () ? new Midi_instrument (i) : 0;
32 else if (Audio_note *i = dynamic_cast<Audio_note *> (a))
33 return new Midi_note (i);
34 else if (Audio_dynamic *i = dynamic_cast<Audio_dynamic *> (a))
35 return new Midi_dynamic (i);
36 else if (Audio_piano_pedal *i = dynamic_cast<Audio_piano_pedal *> (a))
37 return new Midi_piano_pedal (i);
38 else if (Audio_tempo *i = dynamic_cast<Audio_tempo *> (a))
39 return new Midi_tempo (i);
40 else if (Audio_time_signature *i = dynamic_cast<Audio_time_signature *> (a))
41 return new Midi_time_signature (i);
42 else if (Audio_text *i = dynamic_cast<Audio_text *> (a))
43 //return i->text_string_.length () ? new Midi_text (i) : 0;
44 return new Midi_text (i);
45 else
46 assert (0);
48 // isn't C++ grand?
49 return 0;
52 void
53 Midi_chunk::set (String header_string, String data_string, String footer_string)
55 data_string_ = data_string;
56 footer_string_ = footer_string;
57 header_string_ = header_string;
60 String
61 Midi_chunk::data_string () const
63 return data_string_;
66 String
67 Midi_chunk::to_string () const
69 String str = header_string_;
70 String dat = data_string ();
71 String length_string = String_convert::int2hex (dat.length ()
72 + footer_string_.length (), 8, '0');
73 length_string = String_convert::hex2bin (length_string);
74 str += length_string;
75 str += dat;
76 str += footer_string_;
77 return str;
80 Midi_duration::Midi_duration (Real seconds_f)
82 seconds_ = seconds_f;
85 String
86 Midi_duration::to_string () const
88 return String ("<duration: ") + ::to_string (seconds_) + ">";
91 Midi_event::Midi_event (Moment delta_mom, Midi_item *midi)
93 delta_mom_ = delta_mom;
94 midi_ = midi;
98 ugh. midi output badly broken since grace note hackage.
100 String
101 Midi_event::to_string () const
103 Rational rat_dt = (delta_mom_.main_part_ * Rational (384)
104 + delta_mom_.grace_part_ * Rational (100)) * Rational (4);
105 int delta_i = int (rat_dt);
107 String delta_string = Midi_item::i2varint_string (delta_i);
108 String midi_string = midi_->to_string ();
109 assert (midi_string.length ());
110 return delta_string + midi_string;
113 Midi_header::Midi_header (int format_i, int tracks_i, int clocks_per_4_i)
115 String str;
117 String format_string = String_convert::int2hex (format_i, 4, '0');
118 str += String_convert::hex2bin (format_string);
120 String tracks_string = String_convert::int2hex (tracks_i, 4, '0');
121 str += String_convert::hex2bin (tracks_string);
123 String tempo_string = String_convert::int2hex (clocks_per_4_i, 4, '0');
124 str += String_convert::hex2bin (tempo_string);
126 set ("MThd", str, "");
129 Midi_instrument::Midi_instrument (Audio_instrument *a)
131 audio_ = a;
132 audio_->str_.to_lower ();
135 String
136 Midi_instrument::to_string () const
138 Byte program_byte = 0;
139 bool found = false;
142 UGH. don't use eval.
144 SCM proc = ly_lily_module_constant ("midi-program");
145 SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.to_str0 ()));
146 found = (program != SCM_BOOL_F);
147 if (found)
148 program_byte = scm_to_int (program);
149 else
150 warning (_f ("no such MIDI instrument: `%s'", audio_->str_.to_str0 ()));
152 String str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
153 str += ::to_string ((char)program_byte);
154 return str;
157 Midi_item::Midi_item ()
159 channel_ = 0;
162 Midi_item::~Midi_item ()
166 String
167 Midi_item::i2varint_string (int i)
169 int buffer_i = i & 0x7f;
170 while ((i >>= 7) > 0)
172 buffer_i <<= 8;
173 buffer_i |= 0x80;
174 buffer_i += (i & 0x7f);
177 String str;
178 while (1)
180 str += ::to_string ((char)buffer_i);
181 if (buffer_i & 0x80)
182 buffer_i >>= 8;
183 else
184 break;
186 return str;
189 Midi_key::Midi_key (Audio_key *a)
191 audio_ = a;
194 String
195 Midi_key::to_string () const
197 String str = "ff5902";
198 str += String_convert::int2hex (audio_->accidentals_, 2, '0');
199 if (audio_->major_)
200 str += String_convert::int2hex (0, 2, '0');
201 else
202 str += String_convert::int2hex (1, 2, '0');
203 return String_convert::hex2bin (str);
206 Midi_time_signature::Midi_time_signature (Audio_time_signature* a)
208 audio_ = a;
209 clocks_per_1_ = 18;
212 String
213 Midi_time_signature::to_string () const
215 int num = audio_->beats_;
216 int den = audio_->one_beat_;
218 String str = "ff5804";
219 str += String_convert::int2hex (num, 2, '0');
220 str += String_convert::int2hex (intlog2 (den) , 2, '0');
221 str += String_convert::int2hex (clocks_per_1_, 2, '0');
222 str += String_convert::int2hex (8, 2, '0');
223 return String_convert::hex2bin (str);
226 Midi_note::Midi_note (Audio_note* a)
228 audio_ = a;
229 dynamic_byte_ = 0x7f;
232 Moment
233 Midi_note::get_length () const
235 Moment m = audio_->length_mom_;
236 return m;
240 Midi_note::get_fine_tuning () const
242 int ft = audio_->pitch_.quartertone_pitch ();
243 ft -= 2 * audio_->pitch_.semitone_pitch ();
244 ft *= 50; // 1 quarter tone = 50 cents
245 return ft;
249 Midi_note::get_pitch () const
251 int p = audio_->pitch_.semitone_pitch () + audio_->transposing_;
252 if (p == INT_MAX)
254 warning (_ ("silly pitch"));
255 p = 0;
257 return p;
260 String
261 Midi_note::to_string () const
263 Byte status_byte = (char) (0x90 + channel_);
264 String str = "";
265 int finetune;
267 // print warning if fine tuning was needed, HJJ
268 if (get_fine_tuning () != 0)
270 warning (_f ("experimental: temporarily fine tuning (of %d cents) a channel.",
271 get_fine_tuning ()));
273 finetune = PITCH_WHEEL_CENTER;
274 // Move pitch wheel to a shifted position.
275 // The pitch wheel range (of 4 semitones) is multiplied by the cents.
276 finetune += (PITCH_WHEEL_RANGE *get_fine_tuning ()) / (4 * 100);
278 str += ::to_string ((char) (0xE0 + channel_));
279 str += ::to_string ((char) (finetune & 0x7F));
280 str += ::to_string ((char) (finetune >> 7));
281 str += ::to_string ((char) (0x00));
284 str += ::to_string ((char)status_byte);
285 str += ::to_string ((char) (get_pitch () + c0_pitch_i_));
286 str += ::to_string ((char)dynamic_byte_);
288 return str;
291 Midi_note_off::Midi_note_off (Midi_note *n)
292 : Midi_note (n->audio_)
294 on_ = n;
295 channel_ = n->channel_;
297 // Anybody who hears any difference, or knows how this works?
298 // 0 should definitely be avoided, notes stick on some sound cards.
299 // 64 is supposed to be neutral
301 aftertouch_byte_ = 64;
304 String
305 Midi_note_off::to_string () const
307 Byte status_byte = (char) (0x80 + channel_);
309 String str = ::to_string ((char)status_byte);
310 str += ::to_string ((char) (get_pitch () + Midi_note::c0_pitch_i_));
311 str += ::to_string ((char)aftertouch_byte_);
313 if (get_fine_tuning () != 0)
315 // Move pitch wheel back to the central position.
316 str += ::to_string ((char) 0x00);
317 str += ::to_string ((char) (0xE0 + channel_));
318 str += ::to_string ((char) (PITCH_WHEEL_CENTER &0x7F));
319 str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
322 return str;
325 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
327 audio_ = a;
330 String
331 Midi_dynamic::to_string () const
333 Byte status_byte = (char) (0xB0 + channel_);
334 String str = ::to_string ((char)status_byte);
337 Main volume controller (per channel):
338 07 MSB
339 27 LSB
341 static Real const full_scale = 127;
343 int volume = (int) (audio_->volume_ * full_scale);
344 if (volume <= 0)
345 volume = 1;
346 if (volume > full_scale)
347 volume = (int)full_scale;
349 str += ::to_string ((char)0x07);
350 str += ::to_string ((char)volume);
351 return str;
354 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
356 audio_ = a;
359 String
360 Midi_piano_pedal::to_string () const
362 Byte status_byte = (char) (0xB0 + channel_);
363 String str = ::to_string ((char)status_byte);
365 if (audio_->type_string_ == "Sostenuto")
366 str += ::to_string ((char)0x42);
367 else if (audio_->type_string_ == "Sustain")
368 str += ::to_string ((char)0x40);
369 else if (audio_->type_string_ == "UnaCorda")
370 str += ::to_string ((char)0x43);
372 int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
373 str += ::to_string ((char)pedal);
374 return str;
377 Midi_tempo::Midi_tempo (Audio_tempo *a)
379 audio_ = a;
382 String
383 Midi_tempo::to_string () const
385 int useconds_per_4_i = 60 * (int)1e6 / audio_->per_minute_4_;
386 String str = "ff5103";
387 str += String_convert::int2hex (useconds_per_4_i, 6, '0');
388 return String_convert::hex2bin (str);
391 Midi_text::Midi_text (Audio_text* a)
393 audio_ = a;
396 String
397 Midi_text::to_string () const
399 String str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
400 str = String_convert::hex2bin (str);
401 str += i2varint_string (audio_->text_string_.length ());
402 str += audio_->text_string_;
403 return str;
406 Midi_track::Midi_track ()
407 : Midi_chunk ()
409 // 4D 54 72 6B MTrk
410 // 00 00 00 3B chunk length (59)
411 // 00 FF 58 04 04 02 18 08 time signature
412 // 00 FF 51 03 07 A1 20 tempo
414 // FF 59 02 sf mi Key Signature
415 // sf = -7: 7 flats
416 // sf = -1: 1 flat
417 // sf = 0: key of C
418 // sf = 1: 1 sharp
419 // sf = 7: 7 sharps
420 // mi = 0: major key
421 // mi = 1: minor key
423 number_ = 0;
425 char const *data_str0 = ""
426 // "00" "ff58" "0404" "0218" "08"
427 // "00" "ff51" "0307" "a120"
428 // why a key at all, in midi?
429 // key: C
430 // "00" "ff59" "02" "00" "00"
431 // key: F (scsii-menuetto)
432 // "00" "ff59" "02" "ff" "00"
435 String data_string;
436 // only for format 0 (currently using format 1)?
437 data_string += String_convert::hex2bin (data_str0);
439 char const *footer_str0 = "00" "ff2f" "00";
440 String footer_string = String_convert::hex2bin (footer_str0);
442 set ("MTrk", data_string, footer_string);
445 void
446 Midi_track::add (Moment delta_time_mom, Midi_item *midi)
448 assert (delta_time_mom >= Moment (0));
450 Midi_event *e = new Midi_event (delta_time_mom, midi);
451 event_p_list_.append (new Killing_cons<Midi_event> (e, 0));
454 String
455 Midi_track::data_string () const
457 String str = Midi_chunk::data_string ();
458 if (midi_debug_global_b)
459 str += "\n";
460 for (Cons<Midi_event> *i = event_p_list_.head_; i; i = i->next_)
462 str += i->car_->to_string ();
463 if (midi_debug_global_b)
464 str += "\n";
466 return str;