]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-item.cc
Web-ja: update introduction
[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_change *i = dynamic_cast<Audio_control_change *> (a))
55     return new Midi_control_change (i);
56   else
57     assert (0);
58
59   return 0;
60 }
61
62 Midi_duration::Midi_duration (Real seconds_f)
63 {
64   seconds_ = seconds_f;
65 }
66
67 string
68 Midi_duration::to_string () const
69 {
70   return string ("<duration: ") + ::to_string (seconds_) + ">";
71 }
72
73 Midi_instrument::Midi_instrument (Audio_instrument *a)
74   : Midi_channel_item (a),
75     audio_ (a)
76 {
77   audio_->str_ = String_convert::to_lower (audio_->str_);
78 }
79
80 string
81 Midi_instrument::to_string () const
82 {
83   Byte program_byte = 0;
84   bool found = false;
85
86   SCM program = Lily::midi_program (ly_symbol2scm (audio_->str_.c_str ()));
87   found = (scm_is_true (program));
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_control_change::Midi_control_change (Audio_control_change *ai)
108   : Midi_channel_item (ai),
109     audio_ (ai)
110 {
111 }
112
113 Midi_item::~Midi_item ()
114 {
115 }
116
117 Midi_channel_item::~Midi_channel_item ()
118 {
119 }
120
121 Midi_control_change::~Midi_control_change ()
122 {
123 }
124
125 string
126 int2midi_varint_string (int i)
127 {
128   int buffer = i & 0x7f;
129   while ((i >>= 7) > 0)
130     {
131       buffer <<= 8;
132       buffer |= 0x80;
133       buffer += (i & 0x7f);
134     }
135
136   string str;
137   while (1)
138     {
139       str += ::to_string ((char)buffer);
140       if (buffer & 0x80)
141         buffer >>= 8;
142       else
143         break;
144     }
145   return str;
146 }
147
148 Midi_key::Midi_key (Audio_key *a)
149   : audio_ (a)
150 {
151 }
152
153 string
154 Midi_key::to_string () const
155 {
156   string str = "ff5902";
157   str += String_convert::int2hex (audio_->accidentals_, 2, '0');
158   if (audio_->major_)
159     str += String_convert::int2hex (0, 2, '0');
160   else
161     str += String_convert::int2hex (1, 2, '0');
162   return String_convert::hex2bin (str);
163 }
164
165 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
166   : audio_ (a),
167     clocks_per_1_ (18)
168 {
169 }
170
171 string
172 Midi_time_signature::to_string () const
173 {
174   int num = abs (audio_->beats_);
175   if (num > 255)
176     {
177       warning (_ ("Time signature with more than 255 beats.  Truncating"));
178       num = 255;
179     }
180
181   int den = audio_->one_beat_;
182
183   string str = "ff5804";
184   str += String_convert::int2hex (num, 2, '0');
185   str += String_convert::int2hex (intlog2 (den), 2, '0');
186   str += String_convert::int2hex (clocks_per_1_, 2, '0');
187   str += String_convert::int2hex (8, 2, '0');
188   return String_convert::hex2bin (str);
189 }
190
191 Midi_note::Midi_note (Audio_note *a)
192   : Midi_channel_item (a),
193     audio_ (a),
194     dynamic_byte_ (min (max (Byte ((a->dynamic_
195                                     ? a->dynamic_->get_volume (a->audio_column_->when ()) * 0x7f : 0x5a)
196                                    + a->extra_velocity_),
197                              Byte (0)), Byte (0x7f)))
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_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
277   : Midi_channel_item (a),
278     audio_ (a)
279 {
280 }
281
282 string
283 Midi_piano_pedal::to_string () const
284 {
285   Byte status_byte = (char) (0xB0 + channel_);
286   string str = ::to_string ((char)status_byte);
287
288   if (audio_->type_string_ == "Sostenuto")
289     str += ::to_string ((char)0x42);
290   else if (audio_->type_string_ == "Sustain")
291     str += ::to_string ((char)0x40);
292   else if (audio_->type_string_ == "UnaCorda")
293     str += ::to_string ((char)0x43);
294
295   int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
296   str += ::to_string ((char)pedal);
297   return str;
298 }
299
300 Midi_tempo::Midi_tempo (Audio_tempo *a)
301   : audio_ (a)
302 {
303 }
304
305 string
306 Midi_tempo::to_string () const
307 {
308   int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
309   string str = "ff5103";
310   str += String_convert::int2hex (useconds_per_4, 6, '0');
311   return String_convert::hex2bin (str);
312 }
313
314 Midi_text::Midi_text (Audio_text *a)
315   : audio_ (a)
316 {
317 }
318
319 string
320 Midi_text::to_string () const
321 {
322   string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
323   str = String_convert::hex2bin (str);
324   str += int2midi_varint_string (audio_->text_string_.length ());
325   str += audio_->text_string_;
326   return str;
327 }
328
329 string
330 Midi_control_change::to_string () const
331 {
332   Byte status_byte = (char) (0xB0 + channel_);
333   string str = ::to_string ((char)status_byte);
334   str += ::to_string ((char) (audio_->control_));
335   str += ::to_string ((char) (audio_->value_));
336   return str;
337 }
338
339 char const *
340 Midi_item::name () const
341 {
342   return class_name ();
343 }