2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2015 Jan Nieuwenhuizen <janneke@gnu.org>
6 LilyPond is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
11 LilyPond is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with LilyPond. If not, see <http://www.gnu.org/licenses/>.
20 #include "midi-item.hh"
22 #include "audio-column.hh"
23 #include "duration.hh"
24 #include "international.hh"
25 #include "libc-extension.hh"
27 #include "midi-stream.hh"
29 #include "program-option.hh"
30 #include "string-convert.hh"
32 #include "lily-imports.hh"
34 #define PITCH_WHEEL_CENTER 0x2000
35 #define PITCH_WHEEL_SEMITONE 0X1000
38 Midi_item::get_midi (Audio_item *a)
40 if (Audio_key *i = dynamic_cast<Audio_key *> (a))
41 return new Midi_key (i);
42 else if (Audio_instrument *i = dynamic_cast<Audio_instrument *> (a))
43 return i->str_.length () ? new Midi_instrument (i) : 0;
44 else if (Audio_note *i = dynamic_cast<Audio_note *> (a))
45 return new Midi_note (i);
46 else if (Audio_piano_pedal *i = dynamic_cast<Audio_piano_pedal *> (a))
47 return new Midi_piano_pedal (i);
48 else if (Audio_tempo *i = dynamic_cast<Audio_tempo *> (a))
49 return new Midi_tempo (i);
50 else if (Audio_time_signature *i = dynamic_cast<Audio_time_signature *> (a))
51 return new Midi_time_signature (i);
52 else if (Audio_text *i = dynamic_cast<Audio_text *> (a))
53 return new Midi_text (i);
54 else if (Audio_control_function_value_change *i
55 = dynamic_cast<Audio_control_function_value_change *> (a))
56 return new Midi_control_function_value_change (i);
57 else if (Audio_control_change *i = dynamic_cast<Audio_control_change *> (a))
58 return new Midi_control_change (i);
65 Midi_duration::Midi_duration (Real seconds_f)
71 Midi_duration::to_string () const
73 return string ("<duration: ") + ::to_string (seconds_) + ">";
76 Midi_instrument::Midi_instrument (Audio_instrument *a)
77 : Midi_channel_item (a),
80 audio_->str_ = String_convert::to_lower (audio_->str_);
84 Midi_instrument::to_string () const
86 Byte program_byte = 0;
89 SCM program = Lily::midi_program (ly_symbol2scm (audio_->str_.c_str ()));
90 found = (scm_is_true (program));
92 program_byte = (Byte) scm_to_int (program);
94 warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
96 string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
97 str += ::to_string ((char)program_byte);
101 Midi_item::Midi_item ()
105 Midi_channel_item::Midi_channel_item (Audio_item *ai)
106 : channel_ (ai->channel_)
110 Midi_control_function_value_change
111 ::Midi_control_function_value_change (Audio_control_function_value_change *ai)
112 : Midi_channel_item (ai), control_ (ai->control_), value_ (ai->value_)
116 Midi_control_change::Midi_control_change (Audio_control_change *ai)
117 : Midi_channel_item (ai),
122 Midi_item::~Midi_item ()
126 Midi_channel_item::~Midi_channel_item ()
130 Midi_control_function_value_change::~Midi_control_function_value_change ()
134 Midi_control_change::~Midi_control_change ()
139 int2midi_varint_string (int i)
141 int buffer = i & 0x7f;
142 while ((i >>= 7) > 0)
146 buffer += (i & 0x7f);
152 str += ::to_string ((char)buffer);
161 Midi_key::Midi_key (Audio_key *a)
167 Midi_key::to_string () const
169 string str = "ff5902";
170 str += String_convert::int2hex (audio_->accidentals_, 2, '0');
172 str += String_convert::int2hex (0, 2, '0');
174 str += String_convert::int2hex (1, 2, '0');
175 return String_convert::hex2bin (str);
178 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
185 Midi_time_signature::to_string () const
187 int num = abs (audio_->beats_);
190 warning (_ ("Time signature with more than 255 beats. Truncating"));
194 int den = audio_->one_beat_;
196 string str = "ff5804";
197 str += String_convert::int2hex (num, 2, '0');
198 str += String_convert::int2hex (intlog2 (den), 2, '0');
199 str += String_convert::int2hex (clocks_per_1_, 2, '0');
200 str += String_convert::int2hex (8, 2, '0');
201 return String_convert::hex2bin (str);
204 Midi_note::Midi_note (Audio_note *a)
205 : Midi_channel_item (a),
207 dynamic_byte_ (min (max (Byte ((a->dynamic_
208 ? a->dynamic_->get_volume (a->audio_column_->when ()) * 0x7f : 0x5a)
209 + a->extra_velocity_),
210 Byte (0)), Byte (0x7f)))
215 Midi_note::get_fine_tuning () const
217 Rational tune = (audio_->pitch_.tone_pitch ()
218 + audio_->transposing_.tone_pitch ()) * Rational (2);
219 tune -= Rational (get_semitone_pitch ());
221 tune *= PITCH_WHEEL_SEMITONE;
222 return (int) double (tune);
226 Midi_note::get_semitone_pitch () const
228 double tune = double ((audio_->pitch_.tone_pitch ()
229 + audio_->transposing_.tone_pitch ()) * Rational (2));
230 return int (rint (tune));
234 Midi_note::to_string () const
236 Byte status_byte = (char) (0x90 + channel_);
240 // print warning if fine tuning was needed, HJJ
241 if (get_fine_tuning () != 0)
243 finetune = PITCH_WHEEL_CENTER + get_fine_tuning ();
245 str += ::to_string ((char) (0xE0 + channel_));
246 str += ::to_string ((char) (finetune & 0x7F));
247 str += ::to_string ((char) (finetune >> 7));
248 str += ::to_string ((char) (0x00));
251 str += ::to_string ((char) status_byte);
252 str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
253 str += ::to_string ((char) dynamic_byte_);
258 Midi_note_off::Midi_note_off (Midi_note *n)
259 : Midi_note (n->audio_)
262 channel_ = n->channel_;
264 // use note_on with velocity=0 instead of note_off
265 aftertouch_byte_ = 0;
269 Midi_note_off::to_string () const
271 Byte status_byte = (char) (0x90 + channel_);
273 string str = ::to_string ((char)status_byte);
274 str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
275 str += ::to_string ((char)aftertouch_byte_);
277 if (get_fine_tuning () != 0)
279 // Move pitch wheel back to the central position.
280 str += ::to_string ((char) 0x00);
281 str += ::to_string ((char) (0xE0 + channel_));
282 str += ::to_string ((char) (PITCH_WHEEL_CENTER & 0x7F));
283 str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
289 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
290 : Midi_channel_item (a),
296 Midi_piano_pedal::to_string () const
298 Byte status_byte = (char) (0xB0 + channel_);
299 string str = ::to_string ((char)status_byte);
301 if (audio_->type_string_ == "Sostenuto")
302 str += ::to_string ((char)0x42);
303 else if (audio_->type_string_ == "Sustain")
304 str += ::to_string ((char)0x40);
305 else if (audio_->type_string_ == "UnaCorda")
306 str += ::to_string ((char)0x43);
308 int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
309 str += ::to_string ((char)pedal);
313 Midi_tempo::Midi_tempo (Audio_tempo *a)
319 Midi_tempo::to_string () const
321 int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
322 string str = "ff5103";
323 str += String_convert::int2hex (useconds_per_4, 6, '0');
324 return String_convert::hex2bin (str);
327 Midi_text::Midi_text (Audio_text *a)
333 Midi_text::to_string () const
335 string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
336 str = String_convert::hex2bin (str);
337 str += int2midi_varint_string (audio_->text_string_.length ());
338 str += audio_->text_string_;
343 Midi_control_function_value_change::to_string () const
345 // MIDI control function information. A MIDI control function may have one
346 // or two assigned control numbers depending on whether it supports coarse
347 // (7-bit) or fine (14-bit) resolution. If the control function supports
348 // fine resolution, the first (respectively, second) member of the structure
349 // represents the control number for setting the most (least) significant 7
350 // bits of the control function's value.
351 struct Control_function
353 int msb_control_number_;
354 int lsb_control_number_;
357 // Mapping from supported control functions (enumeration values defined in
358 // Audio_controller_value_change::Control) to the corresponding MIDI control
360 static const Control_function control_functions[] =
362 // When adding support for new control functions, please note the
364 // - The order of the control number definitions should be kept
365 // consistent with the order of the enumeration values defined in
366 // Audio_control_function_value_change::Control.
367 // - If the control function has only coarse resolution, the function's
368 // control number should be stored in the MSB member of the array
369 // element, and the LSB member should be set to a negative value.
371 { 8, 40 }, // balance
372 { 10, 42 }, // pan position
373 { 11, 43 }, // expression
374 { 91, -1 }, // reverb level (only coarse resolution available)
375 { 93, -1 } // chorus level (only coarse resolution available)
379 const Control_function *control_function = &control_functions[control_];
380 static const Real full_fine_scale = 0x3FFF;
381 static const Real full_coarse_scale = 0x7F;
382 bool fine_resolution = (control_function->lsb_control_number_ >= 0);
383 // value_ is in range [0.0 .. 1.0]. For directional value ranges,
384 // #CENTER will correspond to 0.5 exactly, and my_round rounds
385 // upwards when in case of doubt. That means that center position
386 // will round to 0x40 or 0x2000 by a hair's breadth.
387 int value = (int) my_round (value_ * (fine_resolution ?
388 full_fine_scale : full_coarse_scale));
389 Byte status_byte = (char) (0xB0 + channel_);
390 str += ::to_string ((char)status_byte);
391 str += ::to_string ((char)(control_function->msb_control_number_));
392 str += ::to_string ((char)(fine_resolution ? (value >> 7) : value));
395 str += ::to_string ((char)0x00);
396 str += ::to_string ((char)status_byte);
397 str += ::to_string ((char)(control_function->lsb_control_number_));
398 str += ::to_string ((char)(value & 0x7F));
404 Midi_control_change::to_string () const
406 Byte status_byte = (char) (0xB0 + channel_);
407 string str = ::to_string ((char)status_byte);
408 str += ::to_string ((char) (audio_->control_));
409 str += ::to_string ((char) (audio_->value_));
414 Midi_item::name () const
416 return class_name ();