2 // midi-event.cc -- implement Midi_event
4 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
8 Midi_event::Midi_event()
19 Midi_event::output_mudela( Lily_stream& lily_stream_r, bool command_mode_bo )
21 lily_stream_r << mudela_str( command_mode_bo ) << String( " " );
24 Midi_key::Midi_key( int accidentals_i, int minor_i )
26 accidentals_i_ = accidentals_i;
28 if ( accidentals_i >= 0 )
29 key_i_ = ( ( accidentals_i % 7 )[ "cgdaebf" ] - 'a' - 2 ) % 7;
31 key_i_ = ( ( -accidentals_i % 7 )[ "cfbeadg" ] - 'a' - 2 ) % 7;
35 Midi_key::mudela_str( bool command_mode_bo )
39 str += String( (char)( ( key_i_ + 2 ) % 7 + 'A' ) );
40 else // heu, -2: should be - 1 1/2: A -> fis
41 str += String( (char)( ( key_i_ + 2 - 2 ) % 7 + 'a' ) );
42 if ( !command_mode_bo )
43 str = String( '\\' ) + str;
44 str = String( "%" ) + str + "\n"; // "\key\F" not supported yet...
49 Midi_key::notename_str( int pitch_i )
51 // this may seem very smart,
52 // but it-s only an excuse not to read a notename table
55 // minor scale: la-la ( = + 5 )
56 static int notename_i_a[ 12 ] = { 0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6 };
57 int notename_i = notename_i_a[ ( minor_i_ * 5 + pitch_i ) % 12 ];
59 static int accidentals_i_a[ 12 ] = { 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 };
60 int accidental_i = accidentals_i_a[ minor_i_ * 5 + pitch_i % 12 ];
61 if ( accidental_i && ( accidentals_i_ < 0 ) ) {
62 accidental_i = - accidental_i;
63 notename_i = ( notename_i + 1 ) % 7;
66 String notename_str = (char)( ( ( notename_i + 2 ) % 7 ) + 'a' );
67 while ( accidental_i-- > 0 )
70 while ( accidental_i++ < 0 )
71 if ( ( notename_str == "a" ) || ( notename_str == "e" ) )
78 octave_str += String( '\'', ( pitch_i - Midi_note::c0_pitch_i_c_ ) / 12 );
79 octave_str += String( '`', ( Midi_note::c0_pitch_i_c_ + 11 - pitch_i ) / 12 );
80 return octave_str + notename_str;
83 Midi_note::Midi_note( String name_str, Duration dur )
85 // do i want pitch too?
91 Midi_note::mudela_str( bool command_mode_bo )
93 // assert( !command_mode_bo );
94 // undefined ref to simple_plet_bo_ ??
95 // if ( simple_plet_bo_ )
96 // return name_str_ + Duration_convert::dur2_str( dur_ );
101 str += String( "\\plet{ " )
102 + String_convert::i2dec_str( dur_.plet_.iso_i_, 0, 0 )
104 + String_convert::i2dec_str( dur_.plet_.type_i_, 0, 0 )
111 str += Duration_convert::dur2_str( dur );
114 str += String( " \\plet{ 1/1 }" );
122 return Duration_convert::dur2_mom( dur_ );
125 Midi_tempo::Midi_tempo( int useconds_per_4_i )
127 useconds_per_4_i_ = useconds_per_4_i;
128 seconds_per_1_f_ = (Real)useconds_per_4_i_ * 4 / 1e6;
132 Midi_tempo::mudela_str( bool command_mode_bo )
134 // assert( command_mode_bo );
135 if ( !command_mode_bo )
137 String str = "tempo 4:";
138 str += String( get_tempo_i( Moment( 1, 4 ) ) );
143 Midi_tempo::useconds_per_4_i()
145 return useconds_per_4_i_;
149 Midi_tempo::get_tempo_i( Moment moment )
151 return Moment( 60 ) / moment / Moment( seconds_per_1_f_ );
154 Midi_text::Midi_text( Midi_text::Type type, String text_str )
157 text_str_ = text_str;
161 Midi_text::mudela_str( bool command_mode_bo )
163 (void)command_mode_bo;
164 if ( !text_str_.length_i()
165 || ( text_str_.length_i() != (int)strlen( text_str_.ch_C() ) ) )
168 return "% " + text_str_ + "\n\t";
171 Midi_time::Midi_time( int num_i, int den_i, int clocks_4_i, int count_32_i )
175 if ( count_32_i != 8 )
176 warning( String( "#32 in quarter: " ) + String( count_32_i ), 0 );
179 clocks_1_i_ = clocks_4_i * 4;
185 return Moment( num_i_ ) * Duration_convert::dur2_mom( Duration( 1 << den_i_ ) );
189 Midi_time::clocks_1_i()
207 Midi_time::mudela_str( bool command_mode_bo )
209 String str = "meter { "
210 + String( num_i_ ) + "*" + String( 1 << den_i_ )
212 if ( !command_mode_bo )
213 str = String( '\\' ) + str;