4 // source file of the LilyPond music typesetter
6 // (c) 1997 Jan Nieuwenhuizen <jan@digicash.com>
14 #include "musicalrequest.hh"
16 #include "midi-item.hh"
17 #include "midi-stream.hh"
18 #include "string-convert.hh"
20 Midi_chunk::Midi_chunk()
25 Midi_chunk::add( String str
)
31 Midi_chunk::set( String header_str
, String data_str
, String footer_str
)
34 footer_str_
= footer_str
;
35 header_str_
= header_str
;
41 String str
= header_str_
;
42 String length_str
= String_convert::i2hex_str( data_str_
.length_i() + footer_str_
.length_i(), 8, '0' );
43 length_str
= String_convert::hex2bin_str( length_str
);
50 Midi_duration::Midi_duration( Real seconds_f
)
52 seconds_f_
= seconds_f
;
58 return String( "<duration: " ) + String( seconds_f_
) + ">";
61 Midi_header::Midi_header( int format_i
, int tracks_i
, int clocks_per_4_i
)
65 String format_str
= String_convert::i2hex_str( format_i
, 4, '0' );
66 str
+= String_convert::hex2bin_str( format_str
);
68 String tracks_str
= String_convert::i2hex_str( tracks_i
, 4, '0' );
69 str
+= String_convert::hex2bin_str( tracks_str
);
71 String tempo_str
= String_convert::i2hex_str( clocks_per_4_i
, 4, '0' );
72 str
+= String_convert::hex2bin_str( tempo_str
);
74 set( "MThd", str
, "" );
78 Midi_item::i2varint_str( int i
)
80 int buffer_i
= i
& 0x7f;
81 while ( (i
>>= 7) > 0 ) {
84 buffer_i
+= (i
& 0x7f);
89 str
+= (char)buffer_i
;
90 if ( buffer_i
& 0x80 )
99 Midi_item::output_midi( Midi_stream
& midi_stream_r
)
101 midi_stream_r
<< str();
105 Midi_note::Midi_note( Melodic_req
* melreq_l
, int channel_i
, bool on_bo
)
108 pitch_i_
= melreq_l
->pitch() + c0_pitch_i_c_
;
109 channel_i_
= channel_i
;
111 // poor man-s staff dynamics:
112 dynamic_byte_
= (melreq_l
)? 0x64 - 0x10 * channel_i_
:0;
119 if ( pitch_i_
!= INT_MAX
) {
120 Byte status_byte
= ( on_b_
? 0x90 : 0x80 ) + channel_i_
;
121 String str
= String( (char)status_byte
);
122 str
+= (char)pitch_i_
;
123 // poor man-s staff dynamics:
124 str
+= (char)dynamic_byte_
;
130 Midi_tempo::Midi_tempo( int tempo_i
)
138 int useconds_per_4_i
= 60 * (int)1e6
/ tempo_i_
;
139 String str
= "ff5103";
140 str
+= String_convert::i2hex_str( useconds_per_4_i
, 6, '0' );
141 return String_convert::hex2bin_str( str
);
144 Midi_track::Midi_track( int number_i
)
147 // 00 00 00 3B chunk length (59)
148 // 00 FF 58 04 04 02 18 08 time signature
149 // 00 FF 51 03 07 A1 20 tempo
151 // FF 59 02 sf mi Key Signature
160 number_i_
= number_i
;
162 char const* data_ch_c_l
= "00" "ff58" "0404" "0218" "08"
163 // "00" "ff51" "0307" "a120"
164 // why a key at all, in midi?
166 "00" "ff59" "02" "00" "00"
167 // key: F (scsii-menuetto)
168 // "00" "ff59" "02" "ff" "00"
172 // only for format 0 (currently using format 1)?
173 data_str
+= String_convert::hex2bin_str( data_ch_c_l
);
175 char const* footer_ch_c_l
= "00" "ff2f" "00";
176 String footer_str
= String_convert::hex2bin_str( footer_ch_c_l
);
178 set( "MTrk", data_str
, footer_str
);
182 Midi_track::add( int delta_time_i
, String event_str
)
184 assert(delta_time_i
>= 0);
185 Midi_chunk::add( i2varint_str( delta_time_i
) + event_str
);
189 Midi_track::add( Moment delta_time_moment
, Midi_item
* mitem_l
)
191 // use convention of 384 clocks per 4
192 // use Duration_convert
193 int delta_time_i
= delta_time_moment
* Moment( 384 ) / Moment( 1, 4 );
194 add( delta_time_i
, mitem_l
->str() );