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