2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2012 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"
25 #include "midi-stream.hh"
27 #include "program-option.hh"
28 #include "string-convert.hh"
31 #define PITCH_WHEEL_CENTER 0x2000
32 #define PITCH_WHEEL_SEMITONE 0X1000
35 Midi_item::get_midi (Audio_item *a)
37 if (Audio_key *i = dynamic_cast<Audio_key *> (a))
38 return new Midi_key (i);
39 else if (Audio_instrument *i = dynamic_cast<Audio_instrument *> (a))
40 return i->str_.length () ? new Midi_instrument (i) : 0;
41 else if (Audio_note *i = dynamic_cast<Audio_note *> (a))
42 return new Midi_note (i);
43 else if (Audio_dynamic *i = dynamic_cast<Audio_dynamic *> (a))
44 return new Midi_dynamic (i);
45 else if (Audio_piano_pedal *i = dynamic_cast<Audio_piano_pedal *> (a))
46 return new Midi_piano_pedal (i);
47 else if (Audio_tempo *i = dynamic_cast<Audio_tempo *> (a))
48 return new Midi_tempo (i);
49 else if (Audio_time_signature *i = dynamic_cast<Audio_time_signature *> (a))
50 return new Midi_time_signature (i);
51 else if (Audio_text *i = dynamic_cast<Audio_text *> (a))
52 return new Midi_text (i);
59 Midi_duration::Midi_duration (Real seconds_f)
65 Midi_duration::to_string () const
67 return string ("<duration: ") + ::to_string (seconds_) + ">";
70 Midi_instrument::Midi_instrument (Audio_instrument *a)
71 : Midi_channel_item (a),
74 audio_->str_ = String_convert::to_lower (audio_->str_);
78 Midi_instrument::to_string () const
80 Byte program_byte = 0;
83 SCM proc = ly_lily_module_constant ("midi-program");
84 SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.c_str ()));
85 found = (program != SCM_BOOL_F);
87 program_byte = (Byte) scm_to_int (program);
89 warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
91 string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
92 str += ::to_string ((char)program_byte);
96 Midi_item::Midi_item ()
100 Midi_channel_item::Midi_channel_item (Audio_item *ai)
101 : channel_ (ai->channel_)
105 Midi_item::~Midi_item ()
110 int2midi_varint_string (int i)
112 int buffer = i & 0x7f;
113 while ((i >>= 7) > 0)
117 buffer += (i & 0x7f);
123 str += ::to_string ((char)buffer);
132 Midi_key::Midi_key (Audio_key *a)
138 Midi_key::to_string () const
140 string str = "ff5902";
141 str += String_convert::int2hex (audio_->accidentals_, 2, '0');
143 str += String_convert::int2hex (0, 2, '0');
145 str += String_convert::int2hex (1, 2, '0');
146 return String_convert::hex2bin (str);
149 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
156 Midi_time_signature::to_string () const
158 int num = abs (audio_->beats_);
161 warning (_ ("Time signature with more than 255 beats. Truncating"));
165 int den = audio_->one_beat_;
167 string str = "ff5804";
168 str += String_convert::int2hex (num, 2, '0');
169 str += String_convert::int2hex (intlog2 (den), 2, '0');
170 str += String_convert::int2hex (clocks_per_1_, 2, '0');
171 str += String_convert::int2hex (8, 2, '0');
172 return String_convert::hex2bin (str);
175 Midi_note::Midi_note (Audio_note *a)
176 : Midi_channel_item (a),
178 dynamic_byte_ (a->dynamic_ && a->dynamic_->volume_ >= 0
179 ? Byte (a->dynamic_->volume_ * 0x7f) : Byte (0x5a))
184 Midi_note::get_fine_tuning () const
186 Rational tune = (audio_->pitch_.tone_pitch ()
187 + audio_->transposing_.tone_pitch ()) * Rational (2);
188 tune -= Rational (get_semitone_pitch ());
190 tune *= PITCH_WHEEL_SEMITONE;
191 return (int) double (tune);
195 Midi_note::get_semitone_pitch () const
197 double tune = double ((audio_->pitch_.tone_pitch ()
198 + audio_->transposing_.tone_pitch ()) * Rational (2));
199 return int (rint (tune));
203 Midi_note::to_string () const
205 Byte status_byte = (char) (0x90 + channel_);
209 // print warning if fine tuning was needed, HJJ
210 if (get_fine_tuning () != 0)
212 finetune = PITCH_WHEEL_CENTER + get_fine_tuning ();
214 str += ::to_string ((char) (0xE0 + channel_));
215 str += ::to_string ((char) (finetune & 0x7F));
216 str += ::to_string ((char) (finetune >> 7));
217 str += ::to_string ((char) (0x00));
220 str += ::to_string ((char) status_byte);
221 str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
222 str += ::to_string ((char) dynamic_byte_);
227 Midi_note_off::Midi_note_off (Midi_note *n)
228 : Midi_note (n->audio_)
231 channel_ = n->channel_;
233 // use note_on with velocity=0 instead of note_off
234 aftertouch_byte_ = 0;
238 Midi_note_off::to_string () const
240 Byte status_byte = (char) (0x90 + channel_);
242 string str = ::to_string ((char)status_byte);
243 str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
244 str += ::to_string ((char)aftertouch_byte_);
246 if (get_fine_tuning () != 0)
248 // Move pitch wheel back to the central position.
249 str += ::to_string ((char) 0x00);
250 str += ::to_string ((char) (0xE0 + channel_));
251 str += ::to_string ((char) (PITCH_WHEEL_CENTER & 0x7F));
252 str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
258 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
259 : Midi_channel_item (a),
265 Midi_dynamic::to_string () const
267 Byte status_byte = (char) (0xB0 + channel_);
268 string str = ::to_string ((char)status_byte);
271 Main volume controller (per channel):
275 static Real const full_scale = 127;
277 int volume = (int) (audio_->volume_ * full_scale);
280 if (volume > full_scale)
281 volume = (int)full_scale;
283 int const volume_default = 100;
284 if (audio_->volume_ < 0 || audio_->silent_)
285 volume = volume_default;
287 str += ::to_string ((char)0x07);
288 str += ::to_string ((char)volume);
292 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
293 : Midi_channel_item (a),
299 Midi_piano_pedal::to_string () const
301 Byte status_byte = (char) (0xB0 + channel_);
302 string str = ::to_string ((char)status_byte);
304 if (audio_->type_string_ == "Sostenuto")
305 str += ::to_string ((char)0x42);
306 else if (audio_->type_string_ == "Sustain")
307 str += ::to_string ((char)0x40);
308 else if (audio_->type_string_ == "UnaCorda")
309 str += ::to_string ((char)0x43);
311 int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
312 str += ::to_string ((char)pedal);
316 Midi_tempo::Midi_tempo (Audio_tempo *a)
322 Midi_tempo::to_string () const
324 int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
325 string str = "ff5103";
326 str += String_convert::int2hex (useconds_per_4, 6, '0');
327 return String_convert::hex2bin (str);
330 Midi_text::Midi_text (Audio_text *a)
336 Midi_text::to_string () const
338 string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
339 str = String_convert::hex2bin (str);
340 str += int2midi_varint_string (audio_->text_string_.length ());
341 str += audio_->text_string_;
346 Midi_item::name () const
348 return this->class_name ();