Ensure scripts inside chords obey manual directions.
[lilypond/mpolesky.git] / lily / midi-item.cc
blob263c978d07dbc2eba440f0d2e9119842b0f0311b
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2010 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 "main.hh"
25 #include "midi-stream.hh"
26 #include "misc.hh"
27 #include "program-option.hh"
28 #include "string-convert.hh"
29 #include "warn.hh"
31 #define PITCH_WHEEL_TOP 0x3FFF
32 #define PITCH_WHEEL_CENTER 0x2000
33 #define PITCH_WHEEL_BOTTOM 0x0000
34 #define PITCH_WHEEL_RANGE (PITCH_WHEEL_TOP - PITCH_WHEEL_BOTTOM)
36 Midi_item *
37 Midi_item::get_midi (Audio_item *a)
39 if (Audio_key *i = dynamic_cast<Audio_key *> (a))
40 return new Midi_key (i);
41 else if (Audio_instrument *i = dynamic_cast<Audio_instrument *> (a))
42 return i->str_.length () ? new Midi_instrument (i) : 0;
43 else if (Audio_note *i = dynamic_cast<Audio_note *> (a))
44 return new Midi_note (i);
45 else if (Audio_dynamic *i = dynamic_cast<Audio_dynamic *> (a))
46 return new Midi_dynamic (i);
47 else if (Audio_piano_pedal *i = dynamic_cast<Audio_piano_pedal *> (a))
48 return new Midi_piano_pedal (i);
49 else if (Audio_tempo *i = dynamic_cast<Audio_tempo *> (a))
50 return new Midi_tempo (i);
51 else if (Audio_time_signature *i = dynamic_cast<Audio_time_signature *> (a))
52 return new Midi_time_signature (i);
53 else if (Audio_text *i = dynamic_cast<Audio_text *> (a))
54 return new Midi_text (i);
55 else
56 assert (0);
58 return 0;
63 Midi_duration::Midi_duration (Real seconds_f)
65 seconds_ = seconds_f;
68 string
69 Midi_duration::to_string () const
71 return string ("<duration: ") + ::to_string (seconds_) + ">";
74 Midi_instrument::Midi_instrument (Audio_instrument *a)
76 audio_ = a;
77 audio_->str_ = String_convert::to_lower (audio_->str_);
80 string
81 Midi_instrument::to_string () const
83 Byte program_byte = 0;
84 bool found = false;
86 SCM proc = ly_lily_module_constant ("midi-program");
87 SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.c_str ()));
88 found = (program != SCM_BOOL_F);
89 if (found)
90 program_byte = scm_to_int (program);
91 else
92 warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
94 string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
95 str += ::to_string ((char)program_byte);
96 return str;
99 Midi_item::Midi_item ()
103 Midi_channel_item::Midi_channel_item ()
105 channel_ = 0;
108 Midi_item::~Midi_item ()
112 string
113 int2midi_varint_string (int i)
115 int buffer = i & 0x7f;
116 while ((i >>= 7) > 0)
118 buffer <<= 8;
119 buffer |= 0x80;
120 buffer += (i & 0x7f);
123 string str;
124 while (1)
126 str += ::to_string ((char)buffer);
127 if (buffer & 0x80)
128 buffer >>= 8;
129 else
130 break;
132 return str;
135 Midi_key::Midi_key (Audio_key *a)
137 audio_ = a;
140 string
141 Midi_key::to_string () const
143 string str = "ff5902";
144 str += String_convert::int2hex (audio_->accidentals_, 2, '0');
145 if (audio_->major_)
146 str += String_convert::int2hex (0, 2, '0');
147 else
148 str += String_convert::int2hex (1, 2, '0');
149 return String_convert::hex2bin (str);
152 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
154 audio_ = a;
155 clocks_per_1_ = 18;
158 string
159 Midi_time_signature::to_string () const
161 int num = abs (audio_->beats_);
162 if (num > 255)
164 warning ("Time signature with more than 255 beats. Truncating");
165 num = 255;
168 int den = audio_->one_beat_;
172 string str = "ff5804";
173 str += String_convert::int2hex (num, 2, '0');
174 str += String_convert::int2hex (intlog2 (den), 2, '0');
175 str += String_convert::int2hex (clocks_per_1_, 2, '0');
176 str += String_convert::int2hex (8, 2, '0');
177 return String_convert::hex2bin (str);
180 Midi_note::Midi_note (Audio_note *a)
182 audio_ = a;
183 dynamic_byte_ = 0x5a;
188 Midi_note::get_fine_tuning () const
190 Rational tune = (audio_->pitch_.tone_pitch ()
191 + audio_->transposing_.tone_pitch ()) * Rational (2);
192 tune -= Rational (get_semitone_pitch ());
194 tune *= 100;
195 return (int) double (tune);
199 Midi_note::get_semitone_pitch () const
201 return int (double ((audio_->pitch_.tone_pitch ()
202 + audio_->transposing_.tone_pitch ()) * Rational (2)));
205 string
206 Midi_note::to_string () const
208 Byte status_byte = (char) (0x90 + channel_);
209 string str = "";
210 int finetune;
212 // print warning if fine tuning was needed, HJJ
213 if (get_fine_tuning () != 0)
215 finetune = PITCH_WHEEL_CENTER;
216 // Move pitch wheel to a shifted position.
217 // The pitch wheel range (of 4 semitones) is multiplied by the cents.
218 finetune += (PITCH_WHEEL_RANGE *get_fine_tuning ()) / (4 * 100);
220 str += ::to_string ((char) (0xE0 + channel_));
221 str += ::to_string ((char) (finetune & 0x7F));
222 str += ::to_string ((char) (finetune >> 7));
223 str += ::to_string ((char) (0x00));
226 str += ::to_string ((char) status_byte);
227 str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
228 str += ::to_string ((char) dynamic_byte_);
230 return str;
233 Midi_note_off::Midi_note_off (Midi_note *n)
234 : Midi_note (n->audio_)
236 on_ = n;
237 channel_ = n->channel_;
239 // use note_on with velocity=0 instead of note_off
240 aftertouch_byte_ = 0;
243 string
244 Midi_note_off::to_string () const
246 Byte status_byte = (char) (0x90 + channel_);
248 string str = ::to_string ((char)status_byte);
249 str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
250 str += ::to_string ((char)aftertouch_byte_);
252 if (get_fine_tuning () != 0)
254 // Move pitch wheel back to the central position.
255 str += ::to_string ((char) 0x00);
256 str += ::to_string ((char) (0xE0 + channel_));
257 str += ::to_string ((char) (PITCH_WHEEL_CENTER &0x7F));
258 str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
261 return str;
264 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
266 audio_ = a;
269 string
270 Midi_dynamic::to_string () const
272 Byte status_byte = (char) (0xB0 + channel_);
273 string str = ::to_string ((char)status_byte);
276 Main volume controller (per channel):
277 07 MSB
278 27 LSB
280 static Real const full_scale = 127;
282 int volume = (int) (audio_->volume_ * full_scale);
283 if (volume <= 0)
284 volume = 1;
285 if (volume > full_scale)
286 volume = (int)full_scale;
288 str += ::to_string ((char)0x07);
289 str += ::to_string ((char)volume);
290 return str;
293 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
295 audio_ = a;
298 string
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);
313 return str;
316 Midi_tempo::Midi_tempo (Audio_tempo *a)
318 audio_ = a;
321 string
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)
332 audio_ = a;
335 string
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_;
342 return str;
345 char const *
346 Midi_item::name () const
348 return this->class_name ();