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