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