]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-item.cc
Merge branch 'master' into lilypond/translation
[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_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
54     assert (0);
55
56   return 0;
57 }
58
59
60
61 Midi_duration::Midi_duration (Real seconds_f)
62 {
63   seconds_ = seconds_f;
64 }
65
66 string
67 Midi_duration::to_string () const
68 {
69   return string ("<duration: ") + ::to_string (seconds_) + ">";
70 }
71
72 Midi_instrument::Midi_instrument (Audio_instrument *a)
73   : Midi_channel_item (a)
74   , audio_ (a)
75 {
76   audio_->str_ = String_convert::to_lower (audio_->str_);
77 }
78
79 string
80 Midi_instrument::to_string () const
81 {
82   Byte program_byte = 0;
83   bool found = false;
84
85   SCM proc = ly_lily_module_constant ("midi-program");
86   SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.c_str ()));
87   found = (program != SCM_BOOL_F);
88   if (found)
89     program_byte = (Byte) scm_to_int (program);
90   else
91     warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
92
93   string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
94   str += ::to_string ((char)program_byte);
95   return str;
96 }
97
98 Midi_item::Midi_item ()
99 {
100 }
101
102 Midi_channel_item::Midi_channel_item (Audio_item *ai)
103   : channel_ (ai->channel_)
104 {
105 }
106
107 Midi_item::~Midi_item ()
108 {
109 }
110
111 string
112 int2midi_varint_string (int i)
113 {
114   int buffer = i & 0x7f;
115   while ((i >>= 7) > 0)
116     {
117       buffer <<= 8;
118       buffer |= 0x80;
119       buffer += (i & 0x7f);
120     }
121
122   string str;
123   while (1)
124     {
125       str += ::to_string ((char)buffer);
126       if (buffer & 0x80)
127         buffer >>= 8;
128       else
129         break;
130     }
131   return str;
132 }
133
134 Midi_key::Midi_key (Audio_key *a)
135   : audio_ (a)
136 {
137 }
138
139 string
140 Midi_key::to_string () const
141 {
142   string str = "ff5902";
143   str += String_convert::int2hex (audio_->accidentals_, 2, '0');
144   if (audio_->major_)
145     str += String_convert::int2hex (0, 2, '0');
146   else
147     str += String_convert::int2hex (1, 2, '0');
148   return String_convert::hex2bin (str);
149 }
150
151 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
152   : audio_ (a)
153   , clocks_per_1_ (18)
154 {
155 }
156
157 string
158 Midi_time_signature::to_string () const
159 {
160   int num = abs (audio_->beats_);
161   if (num > 255)
162     {
163       warning ("Time signature with more than 255 beats. Truncating");
164       num = 255;
165     }
166
167   int den = audio_->one_beat_;
168
169
170   
171   string str = "ff5804";
172   str += String_convert::int2hex (num, 2, '0');
173   str += String_convert::int2hex (intlog2 (den), 2, '0');
174   str += String_convert::int2hex (clocks_per_1_, 2, '0');
175   str += String_convert::int2hex (8, 2, '0');
176   return String_convert::hex2bin (str);
177 }
178
179 Midi_note::Midi_note (Audio_note *a)
180   : Midi_channel_item (a)
181   , audio_ (a)
182   , dynamic_byte_ (a->dynamic_ && a->dynamic_->volume_ >= 0
183                    ? Byte (a->dynamic_->volume_ * 0x7f) : Byte (0x5a))
184 {
185 }
186
187 int
188 Midi_note::get_fine_tuning () const
189 {
190   Rational tune = (audio_->pitch_.tone_pitch ()
191                    + audio_->transposing_.tone_pitch ()) * Rational (2);
192   tune -= Rational (get_semitone_pitch ());
193
194   tune *= PITCH_WHEEL_SEMITONE;
195   return (int) double (tune);
196 }
197
198 int
199 Midi_note::get_semitone_pitch () const
200 {
201   double tune = double ((audio_->pitch_.tone_pitch ()
202                          + audio_->transposing_.tone_pitch ()) * Rational (2));
203   return int (rint (tune));
204 }
205
206 string
207 Midi_note::to_string () const
208 {
209   Byte status_byte = (char) (0x90 + channel_);
210   string str = "";
211   int finetune;
212
213   // print warning if fine tuning was needed, HJJ
214   if (get_fine_tuning () != 0)
215     {
216       finetune = PITCH_WHEEL_CENTER + get_fine_tuning ();
217
218       str += ::to_string ((char) (0xE0 + channel_));
219       str += ::to_string ((char) (finetune & 0x7F));
220       str += ::to_string ((char) (finetune >> 7));
221       str += ::to_string ((char) (0x00));
222     }
223
224   str += ::to_string ((char) status_byte);
225   str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
226   str += ::to_string ((char) dynamic_byte_);
227
228   return str;
229 }
230
231 Midi_note_off::Midi_note_off (Midi_note *n)
232   : Midi_note (n->audio_)
233 {
234   on_ = n;
235   channel_ = n->channel_;
236
237   // use note_on with velocity=0 instead of note_off
238   aftertouch_byte_ = 0;
239 }
240
241 string
242 Midi_note_off::to_string () const
243 {
244   Byte status_byte = (char) (0x90 + channel_);
245
246   string str = ::to_string ((char)status_byte);
247   str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
248   str += ::to_string ((char)aftertouch_byte_);
249
250   if (get_fine_tuning () != 0)
251     {
252       // Move pitch wheel back to the central position.
253       str += ::to_string ((char) 0x00);
254       str += ::to_string ((char) (0xE0 + channel_));
255       str += ::to_string ((char) (PITCH_WHEEL_CENTER &0x7F));
256       str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
257     }
258
259   return str;
260 }
261
262 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
263   : Midi_channel_item (a)
264   , audio_ (a)
265 {
266 }
267
268 string
269 Midi_dynamic::to_string () const
270 {
271   Byte status_byte = (char) (0xB0 + channel_);
272   string str = ::to_string ((char)status_byte);
273
274   /*
275     Main volume controller (per channel):
276     07 MSB
277     27 LSB
278   */
279   static Real const full_scale = 127;
280
281   int volume = (int) (audio_->volume_ * full_scale);
282   if (volume <= 0)
283     volume = 1;
284   if (volume > full_scale)
285     volume = (int)full_scale;
286
287   int const volume_default = 100;
288   if (audio_->volume_ < 0 || audio_->silent_)
289     volume = volume_default;
290   
291   str += ::to_string ((char)0x07);
292   str += ::to_string ((char)volume);
293   return str;
294 }
295
296 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
297   : Midi_channel_item (a)
298   , audio_ (a)
299 {
300 }
301
302 string
303 Midi_piano_pedal::to_string () const
304 {
305   Byte status_byte = (char) (0xB0 + channel_);
306   string str = ::to_string ((char)status_byte);
307
308   if (audio_->type_string_ == "Sostenuto")
309     str += ::to_string ((char)0x42);
310   else if (audio_->type_string_ == "Sustain")
311     str += ::to_string ((char)0x40);
312   else if (audio_->type_string_ == "UnaCorda")
313     str += ::to_string ((char)0x43);
314
315   int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
316   str += ::to_string ((char)pedal);
317   return str;
318 }
319
320 Midi_tempo::Midi_tempo (Audio_tempo *a)
321   : audio_ (a)
322 {
323 }
324
325 string
326 Midi_tempo::to_string () const
327 {
328   int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
329   string str = "ff5103";
330   str += String_convert::int2hex (useconds_per_4, 6, '0');
331   return String_convert::hex2bin (str);
332 }
333
334 Midi_text::Midi_text (Audio_text *a)
335   : audio_ (a)
336 {
337 }
338
339 string
340 Midi_text::to_string () const
341 {
342   string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
343   str = String_convert::hex2bin (str);
344   str += int2midi_varint_string (audio_->text_string_.length ());
345   str += audio_->text_string_;
346   return str;
347 }
348
349 char const *
350 Midi_item::name () const
351 {
352    return this->class_name ();
353 }