]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-item.cc
Merge http://git.sv.gnu.org/r/lilypond
[lilypond.git] / lily / midi-item.cc
1 /*
2   midi-item.cc -- implement MIDI items.
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Jan Nieuwenhuizen <janneke@gnu.org>
7 */
8
9 #include "midi-item.hh"
10
11 #include "duration.hh"
12 #include "international.hh"
13 #include "main.hh"
14 #include "midi-stream.hh"
15 #include "misc.hh"
16 #include "program-option.hh"
17 #include "string-convert.hh"
18 #include "warn.hh"
19
20 #define PITCH_WHEEL_TOP 0x3FFF
21 #define PITCH_WHEEL_CENTER 0x2000
22 #define PITCH_WHEEL_BOTTOM 0x0000
23 #define PITCH_WHEEL_RANGE (PITCH_WHEEL_TOP - PITCH_WHEEL_BOTTOM)
24
25 Midi_item *
26 Midi_item::get_midi (Audio_item *a)
27 {
28   if (Audio_key *i = dynamic_cast<Audio_key *> (a))
29     return new Midi_key (i);
30   else if (Audio_instrument *i = dynamic_cast<Audio_instrument *> (a))
31     return i->str_.length () ? new Midi_instrument (i) : 0;
32   else if (Audio_note *i = dynamic_cast<Audio_note *> (a))
33     return new Midi_note (i);
34   else if (Audio_dynamic *i = dynamic_cast<Audio_dynamic *> (a))
35     return new Midi_dynamic (i);
36   else if (Audio_piano_pedal *i = dynamic_cast<Audio_piano_pedal *> (a))
37     return new Midi_piano_pedal (i);
38   else if (Audio_tempo *i = dynamic_cast<Audio_tempo *> (a))
39     return new Midi_tempo (i);
40   else if (Audio_time_signature *i = dynamic_cast<Audio_time_signature *> (a))
41     return new Midi_time_signature (i);
42   else if (Audio_text *i = dynamic_cast<Audio_text *> (a))
43     return new Midi_text (i);
44   else
45     assert (0);
46
47   return 0;
48 }
49
50
51
52 Midi_duration::Midi_duration (Real seconds_f)
53 {
54   seconds_ = seconds_f;
55 }
56
57 string
58 Midi_duration::to_string () const
59 {
60   return string ("<duration: ") + ::to_string (seconds_) + ">";
61 }
62
63 Midi_instrument::Midi_instrument (Audio_instrument *a)
64 {
65   audio_ = a;
66   audio_->str_ = String_convert::to_lower (audio_->str_);
67 }
68
69 string
70 Midi_instrument::to_string () const
71 {
72   Byte program_byte = 0;
73   bool found = false;
74
75   SCM proc = ly_lily_module_constant ("midi-program");
76   SCM program = scm_call_1 (proc, ly_symbol2scm (audio_->str_.c_str ()));
77   found = (program != SCM_BOOL_F);
78   if (found)
79     program_byte = scm_to_int (program);
80   else
81     warning (_f ("no such MIDI instrument: `%s'", audio_->str_.c_str ()));
82
83   string str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME : Should be track. -rz
84   str += ::to_string ((char)program_byte);
85   return str;
86 }
87
88 Midi_item::Midi_item ()
89 {
90 }
91
92 Midi_channel_item::Midi_channel_item ()
93 {
94   channel_ = 0;
95 }
96
97 Midi_item::~Midi_item ()
98 {
99 }
100
101 string
102 int2midi_varint_string (int i)
103 {
104   int buffer = i & 0x7f;
105   while ((i >>= 7) > 0)
106     {
107       buffer <<= 8;
108       buffer |= 0x80;
109       buffer += (i & 0x7f);
110     }
111
112   string str;
113   while (1)
114     {
115       str += ::to_string ((char)buffer);
116       if (buffer & 0x80)
117         buffer >>= 8;
118       else
119         break;
120     }
121   return str;
122 }
123
124 Midi_key::Midi_key (Audio_key *a)
125 {
126   audio_ = a;
127 }
128
129 string
130 Midi_key::to_string () const
131 {
132   string str = "ff5902";
133   str += String_convert::int2hex (audio_->accidentals_, 2, '0');
134   if (audio_->major_)
135     str += String_convert::int2hex (0, 2, '0');
136   else
137     str += String_convert::int2hex (1, 2, '0');
138   return String_convert::hex2bin (str);
139 }
140
141 Midi_time_signature::Midi_time_signature (Audio_time_signature *a)
142 {
143   audio_ = a;
144   clocks_per_1_ = 18;
145 }
146
147 string
148 Midi_time_signature::to_string () const
149 {
150   int num = abs (audio_->beats_);
151   if (num > 255)
152     {
153       warning ("Time signature with more than 255 beats. Truncating");
154       num = 255;
155     }
156
157   int den = audio_->one_beat_;
158
159
160   
161   string str = "ff5804";
162   str += String_convert::int2hex (num, 2, '0');
163   str += String_convert::int2hex (intlog2 (den), 2, '0');
164   str += String_convert::int2hex (clocks_per_1_, 2, '0');
165   str += String_convert::int2hex (8, 2, '0');
166   return String_convert::hex2bin (str);
167 }
168
169 Midi_note::Midi_note (Audio_note *a)
170 {
171   audio_ = a;
172   dynamic_byte_ = 0x7f;
173 }
174
175
176 int
177 Midi_note::get_fine_tuning () const
178 {
179   Rational tune = (audio_->pitch_.tone_pitch ()
180                    + audio_->transposing_.tone_pitch ()) * Rational (2);
181   tune -= Rational (get_semitone_pitch ());
182
183   tune *= 100;
184   return (int) double (tune);
185 }
186
187 int
188 Midi_note::get_semitone_pitch () const
189 {
190   return int (double ((audio_->pitch_.tone_pitch ()
191                        + audio_->transposing_.tone_pitch ()) * Rational (2)));
192 }
193
194 string
195 Midi_note::to_string () const
196 {
197   Byte status_byte = (char) (0x90 + channel_);
198   string str = "";
199   int finetune;
200
201   // print warning if fine tuning was needed, HJJ
202   if (get_fine_tuning () != 0)
203     {
204       warning (_f ("experimental: temporarily fine tuning (of %d cents) a channel.",
205                    get_fine_tuning ()));
206
207       finetune = PITCH_WHEEL_CENTER;
208       // Move pitch wheel to a shifted position.
209       // The pitch wheel range (of 4 semitones) is multiplied by the cents.
210       finetune += (PITCH_WHEEL_RANGE *get_fine_tuning ()) / (4 * 100);
211
212       str += ::to_string ((char) (0xE0 + channel_));
213       str += ::to_string ((char) (finetune & 0x7F));
214       str += ::to_string ((char) (finetune >> 7));
215       str += ::to_string ((char) (0x00));
216     }
217
218   str += ::to_string ((char) status_byte);
219   str += ::to_string ((char) (get_semitone_pitch () + c0_pitch_));
220   str += ::to_string ((char)dynamic_byte_);
221
222   return str;
223 }
224
225 Midi_note_off::Midi_note_off (Midi_note *n)
226   : Midi_note (n->audio_)
227 {
228   on_ = n;
229   channel_ = n->channel_;
230
231   // Anybody who hears any difference, or knows how this works?
232   //  0 should definitely be avoided, notes stick on some sound cards.
233   // 64 is supposed to be neutral
234
235   aftertouch_byte_ = 64;
236 }
237
238 string
239 Midi_note_off::to_string () const
240 {
241   Byte status_byte = (char) (0x80 + channel_);
242
243   string str = ::to_string ((char)status_byte);
244   str += ::to_string ((char) (get_semitone_pitch () + Midi_note::c0_pitch_));
245   str += ::to_string ((char)aftertouch_byte_);
246
247   if (get_fine_tuning () != 0)
248     {
249       // Move pitch wheel back to the central position.
250       str += ::to_string ((char) 0x00);
251       str += ::to_string ((char) (0xE0 + channel_));
252       str += ::to_string ((char) (PITCH_WHEEL_CENTER &0x7F));
253       str += ::to_string ((char) (PITCH_WHEEL_CENTER >> 7));
254     }
255
256   return str;
257 }
258
259 Midi_dynamic::Midi_dynamic (Audio_dynamic *a)
260 {
261   audio_ = a;
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   str += ::to_string ((char)0x07);
284   str += ::to_string ((char)volume);
285   return str;
286 }
287
288 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal *a)
289 {
290   audio_ = a;
291 }
292
293 string
294 Midi_piano_pedal::to_string () const
295 {
296   Byte status_byte = (char) (0xB0 + channel_);
297   string str = ::to_string ((char)status_byte);
298
299   if (audio_->type_string_ == "Sostenuto")
300     str += ::to_string ((char)0x42);
301   else if (audio_->type_string_ == "Sustain")
302     str += ::to_string ((char)0x40);
303   else if (audio_->type_string_ == "UnaCorda")
304     str += ::to_string ((char)0x43);
305
306   int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
307   str += ::to_string ((char)pedal);
308   return str;
309 }
310
311 Midi_tempo::Midi_tempo (Audio_tempo *a)
312 {
313   audio_ = a;
314 }
315
316 string
317 Midi_tempo::to_string () const
318 {
319   int useconds_per_4 = 60 * (int)1e6 / audio_->per_minute_4_;
320   string str = "ff5103";
321   str += String_convert::int2hex (useconds_per_4, 6, '0');
322   return String_convert::hex2bin (str);
323 }
324
325 Midi_text::Midi_text (Audio_text *a)
326 {
327   audio_ = a;
328 }
329
330 string
331 Midi_text::to_string () const
332 {
333   string str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
334   str = String_convert::hex2bin (str);
335   str += int2midi_varint_string (audio_->text_string_.length ());
336   str += audio_->text_string_;
337   return str;
338 }
339
340 char const *
341 Midi_item::name () const
342 {
343    return this->class_name ();
344 }