]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-item.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / midi-item.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5
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.
10
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.
15
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/>.
18 */
19
20 #include "midi-item.hh"
21
22 #include "duration.hh"
23 #include "international.hh"
24 #include "libc-extension.hh"
25 #include "main.hh"
26 #include "midi-stream.hh"
27 #include "misc.hh"
28 #include "program-option.hh"
29 #include "string-convert.hh"
30 #include "warn.hh"
31 #include "lily-imports.hh"
32
33 using std::string;
34
35 #define PITCH_WHEEL_CENTER 0x2000
36 #define PITCH_WHEEL_SEMITONE 0X1000
37
38 Midi_item *
39 Midi_item::get_midi (Audio_item *a)
40 {
41   if (Audio_key *i = dynamic_cast<Audio_key *> (a))
42     return new Midi_key (i);
43   else if (Audio_instrument *i = dynamic_cast<Audio_instrument *> (a))
44     return i->str_.length () ? new Midi_instrument (i) : 0;
45   else if (Audio_note *i = dynamic_cast<Audio_note *> (a))
46     return new Midi_note (i);
47   else if (Audio_dynamic *i = dynamic_cast<Audio_dynamic *> (a))
48     return new Midi_dynamic (i);
49   else if (Audio_piano_pedal *i = dynamic_cast<Audio_piano_pedal *> (a))
50     return new Midi_piano_pedal (i);
51   else if (Audio_tempo *i = dynamic_cast<Audio_tempo *> (a))
52     return new Midi_tempo (i);
53   else if (Audio_time_signature *i = dynamic_cast<Audio_time_signature *> (a))
54     return new Midi_time_signature (i);
55   else if (Audio_text *i = dynamic_cast<Audio_text *> (a))
56     return new Midi_text (i);
57   else if (Audio_control_function_value_change *i
58            = dynamic_cast<Audio_control_function_value_change *> (a))
59     return new Midi_control_function_value_change (i);
60   else
61     assert (0);
62
63   return 0;
64 }
65
66 Midi_duration::Midi_duration (Real seconds_f)
67 {
68   seconds_ = seconds_f;
69 }
70
71 string
72 Midi_duration::to_string () const
73 {
74   return string ("<duration: ") + ::to_string (seconds_) + ">";
75 }
76
77 Midi_instrument::Midi_instrument (Audio_instrument *a)
78   : Midi_channel_item (a),
79     audio_ (a)
80 {
81   audio_->str_ = String_convert::to_lower (audio_->str_);
82 }
83
84 string
85 Midi_instrument::to_string () const
86 {
87   Byte program_byte = 0;
88   bool found = false;
89
90   SCM program = Lily::midi_program (ly_symbol2scm (audio_->str_.c_str ()));
91   found = (scm_is_true (program));
92   if (found)
93     program_byte = (Byte) scm_to_int (program);
94   else
95     warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
96
97   string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
98   str += ::to_string ((char)program_byte);
99   return str;
100 }
101
102 Midi_item::Midi_item ()
103 {
104 }
105
106 Midi_channel_item::Midi_channel_item (Audio_item *ai)
107   : channel_ (ai->channel_)
108 {
109 }
110
111 Midi_control_function_value_change
112 ::Midi_control_function_value_change (Audio_control_function_value_change *ai)
113   : Midi_channel_item (ai), control_ (ai->control_), value_ (ai->value_)
114 {
115 }
116
117 Midi_item::~Midi_item ()
118 {
119 }
120
121 Midi_channel_item::~Midi_channel_item ()
122 {
123 }
124
125 Midi_control_function_value_change::~Midi_control_function_value_change ()
126 {
127 }
128
129 string
130 int2midi_varint_string (int i)
131 {
132   int buffer = i & 0x7f;
133   while ((i >>= 7) > 0)
134     {
135       buffer <<= 8;
136       buffer |= 0x80;
137       buffer += (i & 0x7f);
138     }
139
140   string str;
141   while (1)
142     {
143       str += ::to_string ((char)buffer);
144       if (buffer & 0x80)
145         buffer >>= 8;
146       else
147         break;
148     }
149   return str;
150 }
151
152 Midi_key::Midi_key (Audio_key *a)
153   : audio_ (a)
154 {
155 }
156
157 string
158 Midi_key::to_string () const
159 {
160   string str = "ff5902";
161   str += String_convert::int2hex (audio_->accidentals_, 2, '0');
162   if (audio_->major_)
163     str += String_convert::int2hex (0, 2, '0');
164   else
165     str += String_convert::int2hex (1, 2, '0');
166   return String_convert::hex2bin (str);
167 }
168
169 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
170   : audio_ (a),
171     clocks_per_1_ (18)
172 {
173 }
174
175 string
176 Midi_time_signature::to_string () const
177 {
178   int num = abs (audio_->beats_);
179   if (num > 255)
180     {
181       warning (_ ("Time signature with more than 255 beats.  Truncating"));
182       num = 255;
183     }
184
185   int den = audio_->one_beat_;
186
187   string str = "ff5804";
188   str += String_convert::int2hex (num, 2, '0');
189   str += String_convert::int2hex (intlog2 (den), 2, '0');
190   str += String_convert::int2hex (clocks_per_1_, 2, '0');
191   str += String_convert::int2hex (8, 2, '0');
192   return String_convert::hex2bin (str);
193 }
194
195 Midi_note::Midi_note (Audio_note *a)
196   : Midi_channel_item (a),
197     audio_ (a),
198     dynamic_byte_ (min (max (Byte ((a->dynamic_ && a->dynamic_->volume_ >= 0
199                                     ? a->dynamic_->volume_ * 0x7f : 0x5a)
200                                    + a->extra_velocity_),
201                              Byte (0)), Byte (0x7f)))
202 {
203 }
204
205 int
206 Midi_note::get_fine_tuning () const
207 {
208   Rational tune = (audio_->pitch_.tone_pitch ()
209                    + audio_->transposing_.tone_pitch ()) * Rational (2);
210   tune -= Rational (get_semitone_pitch ());
211
212   tune *= PITCH_WHEEL_SEMITONE;
213   return (int) double (tune);
214 }
215
216 int
217 Midi_note::get_semitone_pitch () const
218 {
219   double tune = double ((audio_->pitch_.tone_pitch ()
220                          + audio_->transposing_.tone_pitch ()) * Rational (2));
221   return int (rint (tune));
222 }
223
224 string
225 Midi_note::to_string () const
226 {
227   Byte status_byte = (char) (0x90 + channel_);
228   string str = "";
229   int finetune;
230
231   // print warning if fine tuning was needed, HJJ
232   if (get_fine_tuning () != 0)
233     {
234       finetune = PITCH_WHEEL_CENTER + get_fine_tuning ();
235
236       str += ::to_string ((char) (0xE0 + channel_));
237       str += ::to_string ((char) (finetune & 0x7F));
238       str += ::to_string ((char) (finetune >> 7));
239       str += ::to_string ((char) (0x00));
240     }
241
242   str += ::to_string ((char) status_byte);
243   str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
244   str += ::to_string ((char) dynamic_byte_);
245
246   return str;
247 }
248
249 Midi_note_off::Midi_note_off (Midi_note *n)
250   : Midi_note (n->audio_)
251 {
252   on_ = n;
253   channel_ = n->channel_;
254
255   // use note_on with velocity=0 instead of note_off
256   aftertouch_byte_ = 0;
257 }
258
259 string
260 Midi_note_off::to_string () const
261 {
262   Byte status_byte = (char) (0x90 + channel_);
263
264   string str = ::to_string ((char)status_byte);
265   str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
266   str += ::to_string ((char)aftertouch_byte_);
267
268   if (get_fine_tuning () != 0)
269     {
270       // Move pitch wheel back to the central position.
271       str += ::to_string ((char) 0x00);
272       str += ::to_string ((char) (0xE0 + channel_));
273       str += ::to_string ((char) (PITCH_WHEEL_CENTER & 0x7F));
274       str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
275     }
276
277   return str;
278 }
279
280 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
281   : Midi_channel_item (a),
282     audio_ (a)
283 {
284 }
285
286 string
287 Midi_dynamic::to_string () const
288 {
289   Byte status_byte = (char) (0xB0 + channel_);
290   string str = ::to_string ((char)status_byte);
291
292   /*
293     Main volume controller (per channel):
294     07 MSB
295     27 LSB
296   */
297   static Real const full_scale = 127;
298
299   int volume = (int) (audio_->volume_ * full_scale);
300   if (volume <= 0)
301     volume = 1;
302   if (volume > full_scale)
303     volume = (int)full_scale;
304
305   int const volume_default = 100;
306   if (audio_->volume_ < 0 || audio_->silent_)
307     volume = volume_default;
308
309   str += ::to_string ((char)0x07);
310   str += ::to_string ((char)volume);
311   return str;
312 }
313
314 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
315   : Midi_channel_item (a),
316     audio_ (a)
317 {
318 }
319
320 string
321 Midi_piano_pedal::to_string () const
322 {
323   Byte status_byte = (char) (0xB0 + channel_);
324   string str = ::to_string ((char)status_byte);
325
326   if (audio_->type_string_ == "Sostenuto")
327     str += ::to_string ((char)0x42);
328   else if (audio_->type_string_ == "Sustain")
329     str += ::to_string ((char)0x40);
330   else if (audio_->type_string_ == "UnaCorda")
331     str += ::to_string ((char)0x43);
332
333   int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
334   str += ::to_string ((char)pedal);
335   return str;
336 }
337
338 Midi_tempo::Midi_tempo (Audio_tempo *a)
339   : audio_ (a)
340 {
341 }
342
343 string
344 Midi_tempo::to_string () const
345 {
346   int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
347   string str = "ff5103";
348   str += String_convert::int2hex (useconds_per_4, 6, '0');
349   return String_convert::hex2bin (str);
350 }
351
352 Midi_text::Midi_text (Audio_text *a)
353   : audio_ (a)
354 {
355 }
356
357 string
358 Midi_text::to_string () const
359 {
360   string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
361   str = String_convert::hex2bin (str);
362   str += int2midi_varint_string (audio_->text_string_.length ());
363   str += audio_->text_string_;
364   return str;
365 }
366
367 string
368 Midi_control_function_value_change::to_string () const
369 {
370   // MIDI control function information.  A MIDI control function may have one
371   // or two assigned control numbers depending on whether it supports coarse
372   // (7-bit) or fine (14-bit) resolution.  If the control function supports
373   // fine resolution, the first (respectively, second) member of the structure
374   // represents the control number for setting the most (least) significant 7
375   // bits of the control function's value.
376   struct Control_function
377   {
378     int msb_control_number_;
379     int lsb_control_number_;
380   };
381
382   // Mapping from supported control functions (enumeration values defined in
383   // Audio_controller_value_change::Control) to the corresponding MIDI control
384   // numbers.
385   static const Control_function control_functions[] =
386     {
387       // When adding support for new control functions, please note the
388       // following:
389       // - The order of the control number definitions should be kept
390       //   consistent with the order of the enumeration values defined in
391       //   Audio_control_function_value_change::Control.
392       // - If the control function has only coarse resolution, the function's
393       //   control number should be stored in the MSB member of the array
394       //   element, and the LSB member should be set to a negative value.
395
396       {  8, 40 }, // balance
397       { 10, 42 }, // pan position
398       { 11, 43 }, // expression
399       { 91, -1 }, // reverb level (only coarse resolution available)
400       { 93, -1 }  // chorus level (only coarse resolution available)
401     };
402
403   string str;
404   const Control_function *control_function = &control_functions[control_];
405   static const Real full_fine_scale = 0x3FFF;
406   static const Real full_coarse_scale = 0x7F;
407   bool fine_resolution = (control_function->lsb_control_number_ >= 0);
408   // value_ is in range [0.0 .. 1.0].  For directional value ranges,
409   // #CENTER will correspond to 0.5 exactly, and my_round rounds
410   // upwards when in case of doubt.  That means that center position
411   // will round to 0x40 or 0x2000 by a hair's breadth.
412   int value = (int) my_round (value_ * (fine_resolution ?
413                                         full_fine_scale : full_coarse_scale));
414   Byte status_byte = (char) (0xB0 + channel_);
415   str += ::to_string ((char)status_byte);
416   str += ::to_string ((char)(control_function->msb_control_number_));
417   str += ::to_string ((char)(fine_resolution ? (value >> 7) : value));
418   if (fine_resolution)
419     {
420       str += ::to_string ((char)0x00);
421       str += ::to_string ((char)status_byte);
422       str += ::to_string ((char)(control_function->lsb_control_number_));
423       str += ::to_string ((char)(value & 0x7F));
424     }
425   return str;
426 }
427
428 char const *
429 Midi_item::name () const
430 {
431   return class_name ();
432 }