4 // source file of the LilyPond music typesetter
6 // (c) 1997 Jan Nieuwenhuizen <jan@digicash.com>
14 #include "musicalrequest.hh"
16 #include "midiitem.hh"
17 #include "midistream.hh"
19 Midi_chunk::Midi_chunk()
24 Midi_chunk::add( String str
)
30 Midi_chunk::set( String header_str
, String data_str
, String footer_str
)
33 footer_str_
= footer_str
;
34 header_str_
= header_str
;
40 String str
= header_str_
;
41 String length_str
= String_convert::i2hex_str( data_str_
.length_i() + footer_str_
.length_i(), 8, '0' );
42 length_str
= String_convert::hex2bin_str( length_str
);
49 Midi_duration::Midi_duration( Real seconds_f
)
51 seconds_f_
= seconds_f
;
57 return String( "<duration: " ) + String( seconds_f_
) + ">";
60 Midi_header::Midi_header( int format_i
, int tracks_i
, int clocks_per_4_i
)
64 String format_str
= String_convert::i2hex_str( format_i
, 4, '0' );
65 str
+= String_convert::hex2bin_str( format_str
);
67 String tracks_str
= String_convert::i2hex_str( tracks_i
, 4, '0' );
68 str
+= String_convert::hex2bin_str( tracks_str
);
70 String tempo_str
= String_convert::i2hex_str( clocks_per_4_i
, 4, '0' );
71 str
+= String_convert::hex2bin_str( tempo_str
);
73 set( "MThd", str
, "" );
77 Midi_item::i2varint_str( int i
)
79 int buffer_i
= i
& 0x7f;
80 while ( (i
>>= 7) > 0 ) {
83 buffer_i
+= (i
& 0x7f);
88 str
+= (char)buffer_i
;
89 if ( buffer_i
& 0x80 )
98 Midi_item::output_midi( Midi_stream
& midi_stream_r
)
100 midi_stream_r
<< str();
104 Midi_note::Midi_note( Melodic_req
* melreq_l
, int channel_i
, bool on_bo
)
107 pitch_i_
= melreq_l
->pitch() + c0_pitch_i_c_
;
108 channel_i_
= channel_i
;
110 // poor man-s staff dynamics:
111 dynamic_byte_
= (melreq_l
)? 0x64 - 0x10 * channel_i_
:0;
118 if ( pitch_i_
!= INT_MAX
) {
119 Byte status_byte
= ( on_b_
? 0x90 : 0x80 ) + channel_i_
;
120 String str
= String( (char)status_byte
);
121 str
+= (char)pitch_i_
;
122 // poor man-s staff dynamics:
123 str
+= (char)dynamic_byte_
;
129 Midi_tempo::Midi_tempo( int tempo_i
)
137 int useconds_per_4_i
= 60 * (int)1e6
/ tempo_i_
;
138 String str
= "ff5103";
139 str
+= String_convert::i2hex_str( useconds_per_4_i
, 6, '0' );
140 return String_convert::hex2bin_str( str
);
143 Midi_track::Midi_track( int number_i
)
146 // 00 00 00 3B chunk length (59)
147 // 00 FF 58 04 04 02 18 08 time signature
148 // 00 FF 51 03 07 A1 20 tempo
150 // FF 59 02 sf mi Key Signature
159 number_i_
= number_i
;
161 char const* data_ch_c_l
= "00" "ff58" "0404" "0218" "08"
162 // "00" "ff51" "0307" "a120"
163 // why a key at all, in midi?
165 "00" "ff59" "02" "00" "00"
166 // key: F (scsii-menuetto)
167 // "00" "ff59" "02" "ff" "00"
171 // only for format 0 (currently using format 1)?
172 data_str
+= String_convert::hex2bin_str( data_ch_c_l
);
174 char const* footer_ch_c_l
= "00" "ff2f" "00";
175 String footer_str
= String_convert::hex2bin_str( footer_ch_c_l
);
177 set( "MTrk", data_str
, footer_str
);
181 Midi_track::add( int delta_time_i
, String event_str
)
183 assert(delta_time_i
>= 0);
184 Midi_chunk::add( i2varint_str( delta_time_i
) + event_str
);
188 Midi_track::add( Moment delta_time_moment
, Midi_item
* mitem_l
)
190 // use convention of 384 clocks per 4
191 // use Duration_convert
192 int delta_time_i
= delta_time_moment
* Moment( 384 ) / Moment( 1, 4 );
193 add( delta_time_i
, mitem_l
->str() );