]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-item.cc
* mf/GNUmakefile: always trace pfa fonts.
[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--2003 Jan Nieuwenhuizen <janneke@gnu.org>
7  */
8
9 #include "warn.hh"
10 #include "main.hh"
11 #include "misc.hh"
12 #include "string.hh"
13 #include "string-convert.hh"
14 #include "midi-item.hh"
15 #include "midi-stream.hh"
16 #include "audio-item.hh"
17 #include "duration.hh"
18 #include "scm-option.hh"
19
20 #include "killing-cons.tcc"
21
22 Midi_item*
23 Midi_item::get_midi (Audio_item* a)
24 {
25   if (Audio_key* i = dynamic_cast<Audio_key*> (a))
26     return new Midi_key (i);
27   else if (Audio_instrument* i = dynamic_cast<Audio_instrument*> (a))
28     return i->str_.length () ? new Midi_instrument (i) : 0;
29   else if (Audio_note* i = dynamic_cast<Audio_note*> (a))
30     return new Midi_note (i);
31   else if (Audio_dynamic* i = dynamic_cast<Audio_dynamic*> (a))
32     return new Midi_dynamic (i);
33   else if (Audio_piano_pedal* i = dynamic_cast<Audio_piano_pedal*> (a))
34     return new Midi_piano_pedal (i);
35   else if (Audio_tempo* i = dynamic_cast<Audio_tempo*> (a))
36     return new Midi_tempo (i);
37   else if (Audio_time_signature* i = dynamic_cast<Audio_time_signature*> (a))
38     return new Midi_time_signature (i);
39   else if (Audio_text* i = dynamic_cast<Audio_text*> (a))
40     //return i->text_string_.length () ? new Midi_text (i) : 0;
41     return new Midi_text (i);
42   else
43     assert (0);
44
45   // isn't C++ grand?
46   return 0;
47 }
48
49 void
50 Midi_chunk::set (String header_string, String data_string, String footer_string)
51 {
52   data_string_ = data_string;
53   footer_string_ = footer_string;
54   header_string_ = header_string;
55 }
56   
57 String
58 Midi_chunk::data_string () const
59 {
60   return data_string_;
61 }
62
63 String
64 Midi_chunk::to_string () const
65 {
66   String str = header_string_;
67   String dat = data_string ();
68   String length_string = String_convert::int2hex (dat.length () 
69     + footer_string_.length (), 8, '0');
70   length_string = String_convert::hex2bin (length_string);
71   str += length_string;
72   str += dat;
73   str += footer_string_;
74   return str;
75 }
76
77 Midi_duration::Midi_duration (Real seconds_f)
78 {
79   seconds_ = seconds_f;
80 }
81
82 String
83 Midi_duration::to_string () const
84 {
85   return String ("<duration: ") + ::to_string (seconds_) + ">";
86 }
87
88 Midi_event::Midi_event (Moment delta_mom, Midi_item* midi)
89 {
90   delta_mom_ = delta_mom;
91   midi_ = midi;
92 }
93
94 /*
95   ugh. midi output badly broken since grace note hackage.
96  */
97 String
98 Midi_event::to_string () const
99 {
100   Rational rat_dt = (delta_mom_.main_part_ * Rational (384) +
101     delta_mom_.grace_part_ * Rational (100))*Rational (4);
102   int delta_i = int (rat_dt);
103
104   String delta_string = Midi_item::i2varint_string (delta_i);
105   String midi_string = midi_->to_string ();
106   assert (midi_string.length ());
107   return delta_string + midi_string;
108 }
109
110
111 Midi_header::Midi_header (int format_i, int tracks_i, int clocks_per_4_i)
112 {
113   String str;
114         
115   String format_string = String_convert::int2hex (format_i, 4, '0');
116   str += String_convert::hex2bin (format_string);
117         
118   String tracks_string = String_convert::int2hex (tracks_i, 4, '0');
119   str += String_convert::hex2bin (tracks_string);
120
121   String tempo_string = String_convert::int2hex (clocks_per_4_i, 4, '0');
122   str += String_convert::hex2bin (tempo_string);
123
124   set ("MThd", str, "");
125 }
126
127 Midi_instrument::Midi_instrument (Audio_instrument* a)
128 {
129   audio_ = a;
130   audio_->str_.to_lower ();
131 }
132
133 String
134 Midi_instrument::to_string () const
135 {
136   Byte program_byte = 0;
137   bool found = false;
138
139   /*
140     UGH. don't use eval.
141    */
142   SCM proc = scm_primitive_eval (ly_symbol2scm ("midi-program")); 
143   SCM program = gh_call1 (proc, ly_symbol2scm (audio_->str_.to_str0 ()));
144   found = (program != SCM_BOOL_F);
145   if (found)
146     program_byte = gh_scm2int(program);
147   else
148       warning (_f ("no such MIDI instrument: `%s'", audio_->str_.to_str0 ()));
149
150   String str = ::to_string ((char) (0xc0 + channel_)); //YIKES! FIXME: Should be track. -rz
151   str += ::to_string ((char)program_byte);
152   return str;
153 }
154
155 Midi_item::Midi_item ()
156 {
157   channel_ = 0;
158 }
159
160 Midi_item::~Midi_item ()
161 {
162 }
163
164 String
165 Midi_item::i2varint_string (int i)
166 {
167   int buffer_i = i & 0x7f;
168   while ((i >>= 7) > 0) 
169     {
170       buffer_i <<= 8;
171       buffer_i |= 0x80;
172       buffer_i += (i & 0x7f);
173     }
174
175   String str;
176   while (1) 
177     {
178       str += ::to_string ((char)buffer_i);
179       if (buffer_i & 0x80)
180         buffer_i >>= 8;
181       else
182         break;
183     }
184   return str;
185 }
186
187 Midi_key::Midi_key (Audio_key*a)
188 {
189   audio_ = a;
190 }
191
192 String
193 Midi_key::to_string () const
194 {
195   String str = "ff5902";
196   str += String_convert::int2hex (audio_->accidentals_, 2, '0');
197   if (audio_->major_)
198     str += String_convert::int2hex (0, 2, '0');
199   else
200     str += String_convert::int2hex (1, 2, '0');
201   return String_convert::hex2bin (str);
202 }
203
204 Midi_time_signature::Midi_time_signature (Audio_time_signature* a)
205 {
206   audio_ = a;
207   clocks_per_1_ = 18;
208 }
209
210 String
211 Midi_time_signature::to_string () const
212 {
213   int num = audio_->beats_;
214   int den = audio_->one_beat_;
215
216   String str = "ff5804";
217   str += String_convert::int2hex (num, 2, '0');
218   str += String_convert::int2hex (intlog2 (den) , 2, '0');
219   str += String_convert::int2hex (clocks_per_1_, 2, '0');
220   str += String_convert::int2hex (8, 2, '0');
221   return String_convert::hex2bin (str);
222 }
223
224 Midi_note::Midi_note (Audio_note* a)
225 {
226   audio_ = a;
227   dynamic_byte_ = 0x7f;
228 }
229
230 Moment
231 Midi_note::get_length () const
232 {
233   Moment m = audio_->length_mom_;
234 #if 0
235   //junkme?
236   if (m < Moment (Rational (1, 1000)))
237     {
238       warning (_ ("silly duration"));
239       m = 1;
240      }
241 #endif
242   return m;
243 }
244
245 int
246 Midi_note::get_pitch () const
247 {
248   int p = audio_->pitch_.semitone_pitch () + audio_->transposing_;
249   if (p == INT_MAX)
250     {
251       warning (_ ("silly pitch"));
252       p = 0;
253      }
254   return p;
255 }
256
257 String
258 Midi_note::to_string () const
259 {
260   Byte status_byte = (char) (0x90 + channel_);
261
262   String str = ::to_string ((char)status_byte);
263   str += ::to_string ((char) (get_pitch () + c0_pitch_i_));
264
265   str += ::to_string ((char)dynamic_byte_);
266   return str;
267 }
268
269 Midi_note_off::Midi_note_off (Midi_note* n)
270   : Midi_note (n->audio_)
271 {
272   on_ = n;
273   channel_ = n->channel_;
274
275   // Anybody who hears any difference, or knows how this works?
276   //  0 should definitely be avoided, notes stick on some sound cards.
277   // 64 is supposed to be neutral
278   
279   aftertouch_byte_ = 64;
280 }
281
282 String
283 Midi_note_off::to_string () const
284 {
285   Byte status_byte = (char) (0x80 + channel_);
286
287   String str = ::to_string ((char)status_byte);
288   str += ::to_string ((char) (get_pitch () + Midi_note::c0_pitch_i_));
289   str += ::to_string ((char)aftertouch_byte_);
290   return str;
291 }
292
293 Midi_dynamic::Midi_dynamic (Audio_dynamic* a)
294 {
295   audio_ = a;
296 }
297
298 String
299 Midi_dynamic::to_string () const
300 {
301   Byte status_byte = (char) (0xB0 + channel_);
302   String str = ::to_string ((char)status_byte);
303
304   /*
305     Main volume controller (per channel):
306     07 MSB
307     27 LSB
308    */
309   static Real const full_scale = 127;
310   
311   int volume = (int) (audio_->volume_*full_scale);
312   if (volume <= 0)
313     volume = 1;
314   if (volume > full_scale)
315     volume = (int)full_scale;
316
317   str += ::to_string ((char)0x07);
318   str += ::to_string ((char)volume);
319   return str;
320 }
321
322 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal* a)
323 {
324   audio_ = a;
325 }
326
327 String
328 Midi_piano_pedal::to_string () const
329 {
330   Byte status_byte = (char) (0xB0 + channel_);
331   String str = ::to_string ((char)status_byte);
332
333   if (audio_->type_string_ == "Sostenuto")
334     str += ::to_string ((char)0x42);
335   else if (audio_->type_string_ == "Sustain")
336     str += ::to_string ((char)0x40);
337   else if (audio_->type_string_ == "UnaCorda")
338     str += ::to_string ((char)0x43);
339
340   int pedal = ((1 - audio_->dir_) / 2) * 0x7f;
341   str += ::to_string ((char)pedal);
342   return str;
343 }
344
345 Midi_tempo::Midi_tempo (Audio_tempo* a)
346 {
347   audio_ = a;
348 }
349
350 String
351 Midi_tempo::to_string () const
352 {
353   int useconds_per_4_i = 60 * (int)1e6 / audio_->per_minute_4_;
354   String str = "ff5103";
355   str += String_convert::int2hex (useconds_per_4_i, 6, '0');
356   return String_convert::hex2bin (str);
357 }
358
359 Midi_text::Midi_text (Audio_text* a)
360 {
361   audio_ = a;
362 }
363
364 String
365 Midi_text::to_string () const
366 {
367   String str = "ff" + String_convert::int2hex (audio_->type_, 2, '0');
368   str = String_convert::hex2bin (str);
369   str += i2varint_string (audio_->text_string_.length ());
370   str += audio_->text_string_;
371   return str;
372 }
373
374 Midi_track::Midi_track ()
375   : Midi_chunk ()
376 {
377   //                4D 54 72 6B     MTrk
378   //                00 00 00 3B     chunk length (59)
379   //        00      FF 58 04 04 02 18 08    time signature
380   //        00      FF 51 03 07 A1 20       tempo
381  
382 // FF 59 02 sf mi  Key Signature
383 //         sf = -7:  7 flats
384 //         sf = -1:  1 flat
385 //         sf = 0:  key of C
386 //         sf = 1:  1 sharp
387 //         sf = 7: 7 sharps
388 //         mi = 0:  major key
389 //         mi = 1:  minor key
390
391   number_ = 0;
392         
393   char const* data_str0 = ""
394     //        "00" "ff58" "0404" "0218" "08"
395     //  "00" "ff51" "0307" "a120"
396     // why a key at all, in midi?
397     // key: C
398     //  "00" "ff59" "02" "00" "00"
399     // key: F (scsii-menuetto)
400     //                            "00" "ff59" "02" "ff" "00"
401         ;
402
403   String data_string;
404   // only for format 0 (currently using format 1)?
405   data_string += String_convert::hex2bin (data_str0);
406
407   char const* footer_str0 = "00" "ff2f" "00";
408   String footer_string = String_convert::hex2bin (footer_str0);
409
410   set ("MTrk", data_string, footer_string);
411 }
412
413 void 
414 Midi_track::add (Moment delta_time_mom, Midi_item* midi)
415 {
416   assert (delta_time_mom >= Moment (0));
417
418   Midi_event * e = new Midi_event (delta_time_mom, midi);
419   event_p_list_.append (new Killing_cons<Midi_event> (e, 0));
420 }
421
422 String
423 Midi_track::data_string () const
424 {
425   String str = Midi_chunk::data_string ();
426   if (midi_debug_global_b)
427     str += "\n";
428   for (Cons<Midi_event> *i=event_p_list_.head_; i; i = i->next_) 
429     {
430       str += i->car_->to_string ();
431       if (midi_debug_global_b)
432         str += "\n";
433     }
434   return str;
435 }