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 "duration.hh"
23 #include "international.hh"
24 #include "libc-extension.hh"
26 #include "midi-stream.hh"
28 #include "program-option.hh"
29 #include "string-convert.hh"
32 #define PITCH_WHEEL_CENTER 0x2000
33 #define PITCH_WHEEL_SEMITONE 0X1000
36 Midi_item::get_midi (Audio_item *a)
38 if (Audio_key *i = dynamic_cast<Audio_key *> (a))
39 return new Midi_key (i);
40 else if (Audio_instrument *i = dynamic_cast<Audio_instrument *> (a))
41 return i->str_.length () ? new Midi_instrument (i) : 0;
42 else if (Audio_note *i = dynamic_cast<Audio_note *> (a))
43 return new Midi_note (i);
44 else if (Audio_dynamic *i = dynamic_cast<Audio_dynamic *> (a))
45 return new Midi_dynamic (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);
63 Midi_duration::Midi_duration (Real seconds_f)
69 Midi_duration::to_string () const
71 return string ("<duration: ") + ::to_string (seconds_) + ">";
74 Midi_instrument::Midi_instrument (Audio_instrument *a)
75 : Midi_channel_item (a),
78 audio_->str_ = String_convert::to_lower (audio_->str_);
82 Midi_instrument::to_string () const
84 Byte program_byte = 0;
87 SCM proc = ly_lily_module_constant ("midi-program");
88 SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.c_str ()));
89 found = (scm_is_true (program));
91 program_byte = (Byte) scm_to_int (program);
93 warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
95 string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
96 str += ::to_string ((char)program_byte);
100 Midi_item::Midi_item ()
104 Midi_channel_item::Midi_channel_item (Audio_item *ai)
105 : channel_ (ai->channel_)
109 Midi_control_function_value_change
110 ::Midi_control_function_value_change (Audio_control_function_value_change *ai)
111 : Midi_channel_item (ai), control_ (ai->control_), value_ (ai->value_)
115 Midi_item::~Midi_item ()
119 Midi_channel_item::~Midi_channel_item ()
123 Midi_control_function_value_change::~Midi_control_function_value_change ()
128 int2midi_varint_string (int i)
130 int buffer = i & 0x7f;
131 while ((i >>= 7) > 0)
135 buffer += (i & 0x7f);
141 str += ::to_string ((char)buffer);
150 Midi_key::Midi_key (Audio_key *a)
156 Midi_key::to_string () const
158 string str = "ff5902";
159 str += String_convert::int2hex (audio_->accidentals_, 2, '0');
161 str += String_convert::int2hex (0, 2, '0');
163 str += String_convert::int2hex (1, 2, '0');
164 return String_convert::hex2bin (str);
167 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
174 Midi_time_signature::to_string () const
176 int num = abs (audio_->beats_);
179 warning (_ ("Time signature with more than 255 beats. Truncating"));
183 int den = audio_->one_beat_;
185 string str = "ff5804";
186 str += String_convert::int2hex (num, 2, '0');
187 str += String_convert::int2hex (intlog2 (den), 2, '0');
188 str += String_convert::int2hex (clocks_per_1_, 2, '0');
189 str += String_convert::int2hex (8, 2, '0');
190 return String_convert::hex2bin (str);
193 Midi_note::Midi_note (Audio_note *a)
194 : Midi_channel_item (a),
196 dynamic_byte_ (min (max (Byte ((a->dynamic_ && a->dynamic_->volume_ >= 0
197 ? a->dynamic_->volume_ * 0x7f : 0x5a)
198 + a->extra_velocity_),
199 Byte (0)), Byte (0x7f)))
204 Midi_note::get_fine_tuning () const
206 Rational tune = (audio_->pitch_.tone_pitch ()
207 + audio_->transposing_.tone_pitch ()) * Rational (2);
208 tune -= Rational (get_semitone_pitch ());
210 tune *= PITCH_WHEEL_SEMITONE;
211 return (int) double (tune);
215 Midi_note::get_semitone_pitch () const
217 double tune = double ((audio_->pitch_.tone_pitch ()
218 + audio_->transposing_.tone_pitch ()) * Rational (2));
219 return int (rint (tune));
223 Midi_note::to_string () const
225 Byte status_byte = (char) (0x90 + channel_);
229 // print warning if fine tuning was needed, HJJ
230 if (get_fine_tuning () != 0)
232 finetune = PITCH_WHEEL_CENTER + get_fine_tuning ();
234 str += ::to_string ((char) (0xE0 + channel_));
235 str += ::to_string ((char) (finetune & 0x7F));
236 str += ::to_string ((char) (finetune >> 7));
237 str += ::to_string ((char) (0x00));
240 str += ::to_string ((char) status_byte);
241 str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
242 str += ::to_string ((char) dynamic_byte_);
247 Midi_note_off::Midi_note_off (Midi_note *n)
248 : Midi_note (n->audio_)
251 channel_ = n->channel_;
253 // use note_on with velocity=0 instead of note_off
254 aftertouch_byte_ = 0;
258 Midi_note_off::to_string () const
260 Byte status_byte = (char) (0x90 + channel_);
262 string str = ::to_string ((char)status_byte);
263 str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
264 str += ::to_string ((char)aftertouch_byte_);
266 if (get_fine_tuning () != 0)
268 // Move pitch wheel back to the central position.
269 str += ::to_string ((char) 0x00);
270 str += ::to_string ((char) (0xE0 + channel_));
271 str += ::to_string ((char) (PITCH_WHEEL_CENTER & 0x7F));
272 str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
278 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
279 : Midi_channel_item (a),
285 Midi_dynamic::to_string () const
287 Byte status_byte = (char) (0xB0 + channel_);
288 string str = ::to_string ((char)status_byte);
291 Main volume controller (per channel):
295 static Real const full_scale = 127;
297 int volume = (int) (audio_->volume_ * full_scale);
300 if (volume > full_scale)
301 volume = (int)full_scale;
303 int const volume_default = 100;
304 if (audio_->volume_ < 0 || audio_->silent_)
305 volume = volume_default;
307 str += ::to_string ((char)0x07);
308 str += ::to_string ((char)volume);
312 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
313 : Midi_channel_item (a),
319 Midi_piano_pedal::to_string () const
321 Byte status_byte = (char) (0xB0 + channel_);
322 string str = ::to_string ((char)status_byte);
324 if (audio_->type_string_ == "Sostenuto")
325 str += ::to_string ((char)0x42);
326 else if (audio_->type_string_ == "Sustain")
327 str += ::to_string ((char)0x40);
328 else if (audio_->type_string_ == "UnaCorda")
329 str += ::to_string ((char)0x43);
331 int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
332 str += ::to_string ((char)pedal);
336 Midi_tempo::Midi_tempo (Audio_tempo *a)
342 Midi_tempo::to_string () const
344 int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
345 string str = "ff5103";
346 str += String_convert::int2hex (useconds_per_4, 6, '0');
347 return String_convert::hex2bin (str);
350 Midi_text::Midi_text (Audio_text *a)
356 Midi_text::to_string () const
358 string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
359 str = String_convert::hex2bin (str);
360 str += int2midi_varint_string (audio_->text_string_.length ());
361 str += audio_->text_string_;
366 Midi_control_function_value_change::to_string () const
368 // MIDI control function information. A MIDI control function may have one
369 // or two assigned control numbers depending on whether it supports coarse
370 // (7-bit) or fine (14-bit) resolution. If the control function supports
371 // fine resolution, the first (respectively, second) member of the structure
372 // represents the control number for setting the most (least) significant 7
373 // bits of the control function's value.
374 struct Control_function
376 int msb_control_number_;
377 int lsb_control_number_;
380 // Mapping from supported control functions (enumeration values defined in
381 // Audio_controller_value_change::Control) to the corresponding MIDI control
383 static const Control_function control_functions[] =
385 // When adding support for new control functions, please note the
387 // - The order of the control number definitions should be kept
388 // consistent with the order of the enumeration values defined in
389 // Audio_control_function_value_change::Control.
390 // - If the control function has only coarse resolution, the function's
391 // control number should be stored in the MSB member of the array
392 // element, and the LSB member should be set to a negative value.
394 { 8, 40 }, // balance
395 { 10, 42 }, // pan position
396 { 11, 43 }, // expression
397 { 91, -1 }, // reverb level (only coarse resolution available)
398 { 93, -1 } // chorus level (only coarse resolution available)
402 const Control_function *control_function = &control_functions[control_];
403 static const Real full_fine_scale = 0x3FFF;
404 static const Real full_coarse_scale = 0x7F;
405 bool fine_resolution = (control_function->lsb_control_number_ >= 0);
406 // value_ is in range [0.0 .. 1.0]. For directional value ranges,
407 // #CENTER will correspond to 0.5 exactly, and my_round rounds
408 // upwards when in case of doubt. That means that center position
409 // will round to 0x40 or 0x2000 by a hair's breadth.
410 int value = (int) my_round (value_ * (fine_resolution ?
411 full_fine_scale : full_coarse_scale));
412 Byte status_byte = (char) (0xB0 + channel_);
413 str += ::to_string ((char)status_byte);
414 str += ::to_string ((char)(control_function->msb_control_number_));
415 str += ::to_string ((char)(fine_resolution ? (value >> 7) : value));
418 str += ::to_string ((char)0x00);
419 str += ::to_string ((char)status_byte);
420 str += ::to_string ((char)(control_function->lsb_control_number_));
421 str += ::to_string ((char)(value & 0x7F));
427 Midi_item::name () const
429 return this->class_name ();