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_change *i = dynamic_cast<Audio_control_change *> (a))
55 return new Midi_control_change (i);
62 Midi_duration::Midi_duration (Real seconds_f)
68 Midi_duration::to_string () const
70 return string ("<duration: ") + ::to_string (seconds_) + ">";
73 Midi_instrument::Midi_instrument (Audio_instrument *a)
74 : Midi_channel_item (a),
77 audio_->str_ = String_convert::to_lower (audio_->str_);
81 Midi_instrument::to_string () const
83 Byte program_byte = 0;
86 SCM program = Lily::midi_program (ly_symbol2scm (audio_->str_.c_str ()));
87 found = (scm_is_true (program));
89 program_byte = (Byte) scm_to_int (program);
91 warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
93 string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
94 str += ::to_string ((char)program_byte);
98 Midi_item::Midi_item ()
102 Midi_channel_item::Midi_channel_item (Audio_item *ai)
103 : channel_ (ai->channel_)
107 Midi_control_change::Midi_control_change (Audio_control_change *ai)
108 : Midi_channel_item (ai),
113 Midi_item::~Midi_item ()
117 Midi_channel_item::~Midi_channel_item ()
121 Midi_control_change::~Midi_control_change ()
126 int2midi_varint_string (int i)
128 int buffer = i & 0x7f;
129 while ((i >>= 7) > 0)
133 buffer += (i & 0x7f);
139 str += ::to_string ((char)buffer);
148 Midi_key::Midi_key (Audio_key *a)
154 Midi_key::to_string () const
156 string str = "ff5902";
157 str += String_convert::int2hex (audio_->accidentals_, 2, '0');
159 str += String_convert::int2hex (0, 2, '0');
161 str += String_convert::int2hex (1, 2, '0');
162 return String_convert::hex2bin (str);
165 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
172 Midi_time_signature::to_string () const
174 int num = abs (audio_->beats_);
177 warning (_ ("Time signature with more than 255 beats. Truncating"));
181 int den = audio_->one_beat_;
183 string str = "ff5804";
184 str += String_convert::int2hex (num, 2, '0');
185 str += String_convert::int2hex (intlog2 (den), 2, '0');
186 str += String_convert::int2hex (clocks_per_1_, 2, '0');
187 str += String_convert::int2hex (8, 2, '0');
188 return String_convert::hex2bin (str);
191 Midi_note::Midi_note (Audio_note *a)
192 : Midi_channel_item (a),
194 dynamic_byte_ (min (max (Byte ((a->dynamic_
195 ? a->dynamic_->get_volume (a->audio_column_->when ()) * 0x7f : 0x5a)
196 + a->extra_velocity_),
197 Byte (0)), Byte (0x7f)))
202 Midi_note::get_fine_tuning () const
204 Rational tune = (audio_->pitch_.tone_pitch ()
205 + audio_->transposing_.tone_pitch ()) * Rational (2);
206 tune -= Rational (get_semitone_pitch ());
208 tune *= PITCH_WHEEL_SEMITONE;
209 return (int) double (tune);
213 Midi_note::get_semitone_pitch () const
215 double tune = double ((audio_->pitch_.tone_pitch ()
216 + audio_->transposing_.tone_pitch ()) * Rational (2));
217 return int (rint (tune));
221 Midi_note::to_string () const
223 Byte status_byte = (char) (0x90 + channel_);
227 // print warning if fine tuning was needed, HJJ
228 if (get_fine_tuning () != 0)
230 finetune = PITCH_WHEEL_CENTER + get_fine_tuning ();
232 str += ::to_string ((char) (0xE0 + channel_));
233 str += ::to_string ((char) (finetune & 0x7F));
234 str += ::to_string ((char) (finetune >> 7));
235 str += ::to_string ((char) (0x00));
238 str += ::to_string ((char) status_byte);
239 str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
240 str += ::to_string ((char) dynamic_byte_);
245 Midi_note_off::Midi_note_off (Midi_note *n)
246 : Midi_note (n->audio_)
249 channel_ = n->channel_;
251 // use note_on with velocity=0 instead of note_off
252 aftertouch_byte_ = 0;
256 Midi_note_off::to_string () const
258 Byte status_byte = (char) (0x90 + channel_);
260 string str = ::to_string ((char)status_byte);
261 str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
262 str += ::to_string ((char)aftertouch_byte_);
264 if (get_fine_tuning () != 0)
266 // Move pitch wheel back to the central position.
267 str += ::to_string ((char) 0x00);
268 str += ::to_string ((char) (0xE0 + channel_));
269 str += ::to_string ((char) (PITCH_WHEEL_CENTER & 0x7F));
270 str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
276 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
277 : Midi_channel_item (a),
283 Midi_piano_pedal::to_string () const
285 Byte status_byte = (char) (0xB0 + channel_);
286 string str = ::to_string ((char)status_byte);
288 if (audio_->type_string_ == "Sostenuto")
289 str += ::to_string ((char)0x42);
290 else if (audio_->type_string_ == "Sustain")
291 str += ::to_string ((char)0x40);
292 else if (audio_->type_string_ == "UnaCorda")
293 str += ::to_string ((char)0x43);
295 int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
296 str += ::to_string ((char)pedal);
300 Midi_tempo::Midi_tempo (Audio_tempo *a)
306 Midi_tempo::to_string () const
308 int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
309 string str = "ff5103";
310 str += String_convert::int2hex (useconds_per_4, 6, '0');
311 return String_convert::hex2bin (str);
314 Midi_text::Midi_text (Audio_text *a)
320 Midi_text::to_string () const
322 string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
323 str = String_convert::hex2bin (str);
324 str += int2midi_varint_string (audio_->text_string_.length ());
325 str += audio_->text_string_;
330 Midi_control_change::to_string () const
332 Byte status_byte = (char) (0xB0 + channel_);
333 string str = ::to_string ((char)status_byte);
334 str += ::to_string ((char) (audio_->control_));
335 str += ::to_string ((char) (audio_->value_));
340 Midi_item::name () const
342 return class_name ();