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