]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-item.cc
MIDI: always output all midi events. Fixes #1593.
[lilypond.git] / lily / midi-item.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2011 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_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)
35
36 Midi_item *
37 Midi_item::get_midi (Audio_item *a)
38 {
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);
57
58   return 0;
59 }
60
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_item::~Midi_item ()
110 {
111 }
112
113 string
114 int2midi_varint_string (int i)
115 {
116   int buffer = i & 0x7f;
117   while ((i >>= 7) > 0)
118     {
119       buffer <<= 8;
120       buffer |= 0x80;
121       buffer += (i & 0x7f);
122     }
123
124   string str;
125   while (1)
126     {
127       str += ::to_string ((char)buffer);
128       if (buffer & 0x80)
129         buffer >>= 8;
130       else
131         break;
132     }
133   return str;
134 }
135
136 Midi_key::Midi_key (Audio_key *a)
137   : audio_ (a)
138 {
139 }
140
141 string
142 Midi_key::to_string () const
143 {
144   string str = "ff5902";
145   str += String_convert::int2hex (audio_->accidentals_, 2, '0');
146   if (audio_->major_)
147     str += String_convert::int2hex (0, 2, '0');
148   else
149     str += String_convert::int2hex (1, 2, '0');
150   return String_convert::hex2bin (str);
151 }
152
153 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
154   : audio_ (a)
155   , clocks_per_1_ (18)
156 {
157 }
158
159 string
160 Midi_time_signature::to_string () const
161 {
162   int num = abs (audio_->beats_);
163   if (num > 255)
164     {
165       warning ("Time signature with more than 255 beats. Truncating");
166       num = 255;
167     }
168
169   int den = audio_->one_beat_;
170
171
172   
173   string str = "ff5804";
174   str += String_convert::int2hex (num, 2, '0');
175   str += String_convert::int2hex (intlog2 (den), 2, '0');
176   str += String_convert::int2hex (clocks_per_1_, 2, '0');
177   str += String_convert::int2hex (8, 2, '0');
178   return String_convert::hex2bin (str);
179 }
180
181 Midi_note::Midi_note (Audio_note *a)
182   : Midi_channel_item (a)
183   , audio_ (a)
184   , dynamic_byte_ (a->dynamic_ && a->dynamic_->volume_ > 0
185                    ? Byte (a->dynamic_->volume_ * 0x7f) : Byte (0x5a))
186 {
187 }
188
189 int
190 Midi_note::get_fine_tuning () const
191 {
192   Rational tune = (audio_->pitch_.tone_pitch ()
193                    + audio_->transposing_.tone_pitch ()) * Rational (2);
194   tune -= Rational (get_semitone_pitch ());
195
196   tune *= 100;
197   return (int) double (tune);
198 }
199
200 int
201 Midi_note::get_semitone_pitch () const
202 {
203   return int (double ((audio_->pitch_.tone_pitch ()
204                        + audio_->transposing_.tone_pitch ()) * Rational (2)));
205 }
206
207 string
208 Midi_note::to_string () const
209 {
210   Byte status_byte = (char) (0x90 + channel_);
211   string str = "";
212   int finetune;
213
214   // print warning if fine tuning was needed, HJJ
215   if (get_fine_tuning () != 0)
216     {
217       finetune = PITCH_WHEEL_CENTER;
218       // Move pitch wheel to a shifted position.
219       // The pitch wheel range (of 4 semitones) is multiplied by the cents.
220       finetune += (PITCH_WHEEL_RANGE *get_fine_tuning ()) / (4 * 100);
221
222       str += ::to_string ((char) (0xE0 + channel_));
223       str += ::to_string ((char) (finetune & 0x7F));
224       str += ::to_string ((char) (finetune >> 7));
225       str += ::to_string ((char) (0x00));
226     }
227
228   str += ::to_string ((char) status_byte);
229   str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
230   str += ::to_string ((char) dynamic_byte_);
231
232   return str;
233 }
234
235 Midi_note_off::Midi_note_off (Midi_note *n)
236   : Midi_note (n->audio_)
237 {
238   on_ = n;
239   channel_ = n->channel_;
240
241   // use note_on with velocity=0 instead of note_off
242   aftertouch_byte_ = 0;
243 }
244
245 string
246 Midi_note_off::to_string () const
247 {
248   Byte status_byte = (char) (0x90 + channel_);
249
250   string str = ::to_string ((char)status_byte);
251   str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
252   str += ::to_string ((char)aftertouch_byte_);
253
254   if (get_fine_tuning () != 0)
255     {
256       // Move pitch wheel back to the central position.
257       str += ::to_string ((char) 0x00);
258       str += ::to_string ((char) (0xE0 + channel_));
259       str += ::to_string ((char) (PITCH_WHEEL_CENTER &0x7F));
260       str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
261     }
262
263   return str;
264 }
265
266 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
267   : Midi_channel_item (a)
268   , audio_ (a)
269 {
270 }
271
272 string
273 Midi_dynamic::to_string () const
274 {
275   Byte status_byte = (char) (0xB0 + channel_);
276   string str = ::to_string ((char)status_byte);
277
278   /*
279     Main volume controller (per channel):
280     07 MSB
281     27 LSB
282   */
283   static Real const full_scale = 127;
284
285   int volume = (int) (audio_->volume_ * full_scale);
286   if (volume <= 0)
287     volume = 1;
288   if (volume > full_scale)
289     volume = (int)full_scale;
290
291   int const volume_default = 100;
292   if (audio_->volume_ < 0 || audio_->silent_)
293     volume = volume_default;
294   
295   str += ::to_string ((char)0x07);
296   str += ::to_string ((char)volume);
297   return str;
298 }
299
300 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
301   : Midi_channel_item (a)
302   , audio_ (a)
303 {
304 }
305
306 string
307 Midi_piano_pedal::to_string () const
308 {
309   Byte status_byte = (char) (0xB0 + channel_);
310   string str = ::to_string ((char)status_byte);
311
312   if (audio_->type_string_ == "Sostenuto")
313     str += ::to_string ((char)0x42);
314   else if (audio_->type_string_ == "Sustain")
315     str += ::to_string ((char)0x40);
316   else if (audio_->type_string_ == "UnaCorda")
317     str += ::to_string ((char)0x43);
318
319   int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
320   str += ::to_string ((char)pedal);
321   return str;
322 }
323
324 Midi_tempo::Midi_tempo (Audio_tempo *a)
325   : audio_ (a)
326 {
327 }
328
329 string
330 Midi_tempo::to_string () const
331 {
332   int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
333   string str = "ff5103";
334   str += String_convert::int2hex (useconds_per_4, 6, '0');
335   return String_convert::hex2bin (str);
336 }
337
338 Midi_text::Midi_text (Audio_text *a)
339   : audio_ (a)
340 {
341 }
342
343 string
344 Midi_text::to_string () const
345 {
346   string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
347   str = String_convert::hex2bin (str);
348   str += int2midi_varint_string (audio_->text_string_.length ());
349   str += audio_->text_string_;
350   return str;
351 }
352
353 char const *
354 Midi_item::name () const
355 {
356    return this->class_name ();
357 }