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