]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-item.cc
Fix compiler warnings.
[lilypond.git] / lily / midi-item.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2010 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 {
76   audio_ = a;
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 proc = ly_lily_module_constant ("midi-program");
87   SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.c_str ()));
88   found = (program != SCM_BOOL_F);
89   if (found)
90     program_byte = (Byte) scm_to_int (program);
91   else
92     warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
93
94   string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
95   str += ::to_string ((char)program_byte);
96   return str;
97 }
98
99 Midi_item::Midi_item ()
100 {
101 }
102
103 Midi_channel_item::Midi_channel_item ()
104 {
105   channel_ = 0;
106 }
107
108 Midi_item::~Midi_item ()
109 {
110 }
111
112 string
113 int2midi_varint_string (int i)
114 {
115   int buffer = i & 0x7f;
116   while ((i >>= 7) > 0)
117     {
118       buffer <<= 8;
119       buffer |= 0x80;
120       buffer += (i & 0x7f);
121     }
122
123   string str;
124   while (1)
125     {
126       str += ::to_string ((char)buffer);
127       if (buffer & 0x80)
128         buffer >>= 8;
129       else
130         break;
131     }
132   return str;
133 }
134
135 Midi_key::Midi_key (Audio_key *a)
136 {
137   audio_ = a;
138 }
139
140 string
141 Midi_key::to_string () const
142 {
143   string str = "ff5902";
144   str += String_convert::int2hex (audio_->accidentals_, 2, '0');
145   if (audio_->major_)
146     str += String_convert::int2hex (0, 2, '0');
147   else
148     str += String_convert::int2hex (1, 2, '0');
149   return String_convert::hex2bin (str);
150 }
151
152 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
153 {
154   audio_ = a;
155   clocks_per_1_ = 18;
156 }
157
158 string
159 Midi_time_signature::to_string () const
160 {
161   int num = abs (audio_->beats_);
162   if (num > 255)
163     {
164       warning ("Time signature with more than 255 beats. Truncating");
165       num = 255;
166     }
167
168   int den = audio_->one_beat_;
169
170
171   
172   string str = "ff5804";
173   str += String_convert::int2hex (num, 2, '0');
174   str += String_convert::int2hex (intlog2 (den), 2, '0');
175   str += String_convert::int2hex (clocks_per_1_, 2, '0');
176   str += String_convert::int2hex (8, 2, '0');
177   return String_convert::hex2bin (str);
178 }
179
180 Midi_note::Midi_note (Audio_note *a)
181 {
182   audio_ = a;
183   dynamic_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 *= 100;
195   return (int) double (tune);
196 }
197
198 int
199 Midi_note::get_semitone_pitch () const
200 {
201   return int (double ((audio_->pitch_.tone_pitch ()
202                        + audio_->transposing_.tone_pitch ()) * Rational (2)));
203 }
204
205 string
206 Midi_note::to_string () const
207 {
208   Byte status_byte = (char) (0x90 + channel_);
209   string str = "";
210   int finetune;
211
212   // print warning if fine tuning was needed, HJJ
213   if (get_fine_tuning () != 0)
214     {
215       finetune = PITCH_WHEEL_CENTER;
216       // Move pitch wheel to a shifted position.
217       // The pitch wheel range (of 4 semitones) is multiplied by the cents.
218       finetune += (PITCH_WHEEL_RANGE *get_fine_tuning ()) / (4 * 100);
219
220       str += ::to_string ((char) (0xE0 + channel_));
221       str += ::to_string ((char) (finetune & 0x7F));
222       str += ::to_string ((char) (finetune >> 7));
223       str += ::to_string ((char) (0x00));
224     }
225
226   str += ::to_string ((char) status_byte);
227   str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
228   str += ::to_string ((char) dynamic_byte_);
229
230   return str;
231 }
232
233 Midi_note_off::Midi_note_off (Midi_note *n)
234   : Midi_note (n->audio_)
235 {
236   on_ = n;
237   channel_ = n->channel_;
238
239   // use note_on with velocity=0 instead of note_off
240   aftertouch_byte_ = 0;
241 }
242
243 string
244 Midi_note_off::to_string () const
245 {
246   Byte status_byte = (char) (0x90 + channel_);
247
248   string str = ::to_string ((char)status_byte);
249   str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
250   str += ::to_string ((char)aftertouch_byte_);
251
252   if (get_fine_tuning () != 0)
253     {
254       // Move pitch wheel back to the central position.
255       str += ::to_string ((char) 0x00);
256       str += ::to_string ((char) (0xE0 + channel_));
257       str += ::to_string ((char) (PITCH_WHEEL_CENTER &0x7F));
258       str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
259     }
260
261   return str;
262 }
263
264 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
265 {
266   audio_ = a;
267 }
268
269 string
270 Midi_dynamic::to_string () const
271 {
272   Byte status_byte = (char) (0xB0 + channel_);
273   string str = ::to_string ((char)status_byte);
274
275   /*
276     Main volume controller (per channel):
277     07 MSB
278     27 LSB
279   */
280   static Real const full_scale = 127;
281
282   int volume = (int) (audio_->volume_ * full_scale);
283   if (volume <= 0)
284     volume = 1;
285   if (volume > full_scale)
286     volume = (int)full_scale;
287
288   str += ::to_string ((char)0x07);
289   str += ::to_string ((char)volume);
290   return str;
291 }
292
293 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
294 {
295   audio_ = a;
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 {
318   audio_ = a;
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 {
332   audio_ = a;
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 }