]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-item.cc
release: 1.5.34
[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--2002 Jan Nieuwenhuizen <janneke@gnu.org>
7  */
8
9 #include "debug.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::midi_p (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_i () ? 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_str_.length_i () ? 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_str, String data_str, String footer_str)
51 {
52   data_str_ = data_str;
53   footer_str_ = footer_str;
54   header_str_ = header_str;
55 }
56   
57 String
58 Midi_chunk::data_str () const
59 {
60   return data_str_;
61 }
62
63 String
64 Midi_chunk::str () const
65 {
66   String str = header_str_;
67   String dat = data_str ();
68   String length_str = String_convert::i2hex_str (dat.length_i () 
69     + footer_str_.length_i (), 8, '0');
70   length_str = String_convert::hex2bin_str (length_str);
71   str += length_str;
72   str += dat;
73   str += footer_str_;
74   return str;
75 }
76
77 Midi_duration::Midi_duration (Real seconds_f)
78 {
79   seconds_f_ = seconds_f;
80 }
81
82 String
83 Midi_duration::str () const
84 {
85   return String ("<duration: ") + to_str (seconds_f_) + ">";
86 }
87
88 Midi_event::Midi_event (Moment delta_mom, Midi_item* midi_p)
89 {
90   delta_mom_ = delta_mom;
91   midi_p_ = midi_p;
92 }
93
94 /*
95   ugh. midi output badly broken since grace note hackage.
96  */
97 String
98 Midi_event::str () 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_str = Midi_item::i2varint_str (delta_i);
105   String midi_str = midi_p_->str ();
106   assert (midi_str.length_i ());
107   return delta_str + midi_str;
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_str = String_convert::i2hex_str (format_i, 4, '0');
116   str += String_convert::hex2bin_str (format_str);
117         
118   String tracks_str = String_convert::i2hex_str (tracks_i, 4, '0');
119   str += String_convert::hex2bin_str (tracks_str);
120
121   String tempo_str = String_convert::i2hex_str (clocks_per_4_i, 4, '0');
122   str += String_convert::hex2bin_str (tempo_str);
123
124   set ("MThd", str, "");
125 }
126
127 Midi_instrument::Midi_instrument (Audio_instrument* a)
128 {
129   audio_l_ = a;
130   audio_l_->str_.to_lower ();
131 }
132
133 String
134 Midi_instrument::str() const
135 {
136   Byte program_byte = 0;
137   bool found = false;
138   SCM proc = scm_primitive_eval (ly_symbol2scm ("midi-program")); 
139   SCM program = gh_call1 (proc, ly_symbol2scm (audio_l_->str_.ch_C()));
140   found = (program != SCM_BOOL_F);
141   if (found)
142     program_byte = gh_scm2int(program);
143   else
144       warning (_f ("no such instrument: `%s'", audio_l_->str_.ch_C ()));
145
146   String str = to_str ((char) (0xc0 + channel_i_)); //YIKES! FIXME: Should be track. -rz
147   str += to_str ((char)program_byte);
148   return str;
149 }
150
151 Midi_item::Midi_item ()
152 {
153   channel_i_ = 0;
154 }
155
156 Midi_item::~Midi_item ()
157 {
158 }
159
160 String
161 Midi_item::i2varint_str (int i)
162 {
163   int buffer_i = i & 0x7f;
164   while ((i >>= 7) > 0) 
165     {
166       buffer_i <<= 8;
167       buffer_i |= 0x80;
168       buffer_i += (i & 0x7f);
169     }
170
171   String str;
172   while (1) 
173     {
174       str += to_str ((char)buffer_i);
175       if (buffer_i & 0x80)
176         buffer_i >>= 8;
177       else
178         break;
179     }
180   return str;
181 }
182
183 Midi_key::Midi_key (Audio_key*a)
184 {
185   audio_l_ = a;
186 }
187
188 String
189 Midi_key::str () const
190 {
191   String str = "ff5902";
192   str += String_convert::i2hex_str (audio_l_->accidentals_, 2, '0');
193   if (audio_l_->major_)
194     str += String_convert::i2hex_str (0, 2, '0');
195   else
196     str += String_convert::i2hex_str (1, 2, '0');
197   return String_convert::hex2bin_str (str);
198 }
199
200 Midi_time_signature::Midi_time_signature (Audio_time_signature* a)
201 {
202   audio_l_ = a;
203   clocks_per_1_i_ = 18;
204 }
205
206 String
207 Midi_time_signature::str () const
208 {
209   int num = audio_l_->beats_i_;
210   int den = audio_l_->one_beat_i_;
211
212   String str = "ff5804";
213   str += String_convert::i2hex_str (num, 2, '0');
214   str += String_convert::i2hex_str (intlog2 (den) , 2, '0');
215   str += String_convert::i2hex_str (clocks_per_1_i_, 2, '0');
216   str += String_convert::i2hex_str (8, 2, '0');
217   return String_convert::hex2bin_str (str);
218 }
219
220 Midi_note::Midi_note (Audio_note* a)
221 {
222   audio_l_ = a;
223   dynamic_byte_ = 0x7f;
224 }
225
226 Moment
227 Midi_note::length_mom () const
228 {
229   Moment m = audio_l_->length_mom_;
230 #if 0
231   //junkme?
232   if (m < Moment (Rational (1, 1000)))
233     {
234       warning (_ ("silly duration"));
235       m = 1;
236      }
237 #endif
238   return m;
239 }
240
241 int
242 Midi_note::pitch_i () const
243 {
244   int p = audio_l_->pitch_.semitone_pitch () + audio_l_->transposing_i_;
245   if (p == INT_MAX)
246     {
247       warning (_ ("silly pitch"));
248       p = 0;
249      }
250   return p;
251 }
252
253 String
254 Midi_note::str () const
255 {
256   Byte status_byte = (char) (0x90 + channel_i_);
257
258   String str = to_str ((char)status_byte);
259   str += to_str ((char) (pitch_i () + c0_pitch_i_c_));
260
261   str += to_str ((char)dynamic_byte_);
262   return str;
263 }
264
265 Midi_note_off::Midi_note_off (Midi_note* n)
266   : Midi_note (n->audio_l_)
267 {
268   on_l_ = n;
269   channel_i_ = n->channel_i_;
270
271   // Anybody who hears any difference, or knows how this works?
272   //  0 should definitely be avoided, notes stick on some sound cards.
273   // 64 is supposed to be neutral
274   
275   aftertouch_byte_ = 64;
276 }
277
278 String
279 Midi_note_off::str () const
280 {
281   Byte status_byte = (char) (0x80 + channel_i_);
282
283   String str = to_str ((char)status_byte);
284   str += to_str ((char) (pitch_i () + Midi_note::c0_pitch_i_c_));
285   str += to_str ((char)aftertouch_byte_);
286   return str;
287 }
288
289 Midi_dynamic::Midi_dynamic (Audio_dynamic* a)
290 {
291   audio_l_ = a;
292 }
293
294 String
295 Midi_dynamic::str () const
296 {
297   Byte status_byte = (char) (0xB0 + channel_i_);
298   String str = to_str ((char)status_byte);
299
300   /*
301     Main volume controller (per channel):
302     07 MSB
303     27 LSB
304    */
305   static Real const full_scale = 127;
306   
307   int volume = (int) (audio_l_->volume_*full_scale);
308   if (volume <= 0)
309     volume = 1;
310   if (volume > full_scale)
311     volume = (int)full_scale;
312
313   str += to_str ((char)0x07);
314   str += to_str ((char)volume);
315   return str;
316 }
317
318 Midi_piano_pedal::Midi_piano_pedal (Audio_piano_pedal* a)
319 {
320   audio_l_ = a;
321 }
322
323 String
324 Midi_piano_pedal::str () const
325 {
326   Byte status_byte = (char) (0xB0 + channel_i_);
327   String str = to_str ((char)status_byte);
328
329   if (audio_l_->type_str_ == "Sostenuto")
330     str += to_str ((char)0x42);
331   else if (audio_l_->type_str_ == "Sustain")
332     str += to_str ((char)0x40);
333   else if (audio_l_->type_str_ == "UnaCorda")
334     str += to_str ((char)0x43);
335
336   int pedal = ((1 - audio_l_->dir_) / 2) * 0x7f;
337   str += to_str ((char)pedal);
338   return str;
339 }
340
341 Midi_tempo::Midi_tempo (Audio_tempo* a)
342 {
343   audio_l_ = a;
344 }
345
346 String
347 Midi_tempo::str () const
348 {
349   int useconds_per_4_i = 60 * (int)1e6 / audio_l_->per_minute_4_i_;
350   String str = "ff5103";
351   str += String_convert::i2hex_str (useconds_per_4_i, 6, '0');
352   return String_convert::hex2bin_str (str);
353 }
354
355 Midi_text::Midi_text (Audio_text* a)
356 {
357   audio_l_ = a;
358 }
359
360 String
361 Midi_text::str () const
362 {
363   String str = "ff" + String_convert::i2hex_str (audio_l_->type_, 2, '0');
364   str = String_convert::hex2bin_str (str);
365   str += i2varint_str (audio_l_->text_str_.length_i ());
366   str += audio_l_->text_str_;
367   return str;
368 }
369
370 Midi_track::Midi_track ()
371   : Midi_chunk ()
372 {
373   //                4D 54 72 6B     MTrk
374   //                00 00 00 3B     chunk length (59)
375   //        00      FF 58 04 04 02 18 08    time signature
376   //        00      FF 51 03 07 A1 20       tempo
377  
378 // FF 59 02 sf mi  Key Signature
379 //         sf = -7:  7 flats
380 //         sf = -1:  1 flat
381 //         sf = 0:  key of C
382 //         sf = 1:  1 sharp
383 //         sf = 7: 7 sharps
384 //         mi = 0:  major key
385 //         mi = 1:  minor key
386
387   number_i_ = 0;
388         
389   char const* data_ch_C = ""
390     //        "00" "ff58" "0404" "0218" "08"
391     //  "00" "ff51" "0307" "a120"
392     // why a key at all, in midi?
393     // key: C
394     //  "00" "ff59" "02" "00" "00"
395     // key: F (scsii-menuetto)
396     //                            "00" "ff59" "02" "ff" "00"
397         ;
398
399   String data_str;
400   // only for format 0 (currently using format 1)?
401   data_str += String_convert::hex2bin_str (data_ch_C);
402
403   char const* footer_ch_C = "00" "ff2f" "00";
404   String footer_str = String_convert::hex2bin_str (footer_ch_C);
405
406   set ("MTrk", data_str, footer_str);
407 }
408
409 void 
410 Midi_track::add (Moment delta_time_mom, Midi_item* midi_p)
411 {
412   assert (delta_time_mom >= Moment (0));
413
414   Midi_event * e = new Midi_event (delta_time_mom, midi_p);
415   event_p_list_.append (new Killing_cons<Midi_event> (e, 0));
416 }
417
418 String
419 Midi_track::data_str () const
420 {
421   String str = Midi_chunk::data_str ();
422   if (midi_debug_global_b)
423     str += "\n";
424   for (Cons<Midi_event> *i=event_p_list_.head_; i; i = i->next_) 
425     {
426       str += i->car_->str ();
427       if (midi_debug_global_b)
428         str += "\n";
429     }
430   return str;
431 }