lilypond-0.1.16
[lilypond.git] / m2m / midi-event.cc
blob8ec23f9f95e767280d3ca637e83045556ba48ee4
1 //
2 // midi-event.cc -- implement Midi_event
3 //
4 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
6 #include "m2m.hh"
8 Midi_event::Midi_event()
12 Moment
13 Midi_event::mom()
15 return Moment( 0 );
18 void
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;
27 minor_i_ = minor_i;
28 if ( !minor_i_ )
29 key_i_ = ( ( accidentals_i % 7 )[ "cgdaebf" ] - 'a' + 2 ) % 7;
30 else
31 key_i_ = ( ( -accidentals_i % 7 )[ "fbeadg" ] - 'a' + 2 ) % 7;
34 String
35 Midi_key::mudela_str( bool command_mode_bo )
37 String str = "key\\";
38 if ( !minor_i_ )
39 str += String( (char)( key_i_ - 2 + 'A' ) );
40 else
41 str += String( (char)( key_i_ - 2 + 'a' ) );
42 if ( !command_mode_bo )
43 str = String( '\\' ) + str;
44 return str;
47 String
48 Midi_key::notename_str( int pitch_i )
50 // this may seem very smart,
51 // but it-s only an excuse not to read a notename table
53 // major scale: do-do
54 // minor scale: la-la ( = + 5 )
55 static int notename_i_a[ 12 ] = { 0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6 };
56 int notename_i = notename_i_a[ ( minor_i_ * 5 + pitch_i ) % 12 ];
58 static int accidentals_i_a[ 12 ] = { 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 };
59 int accidental_i = accidentals_i_a[ minor_i_ * 5 + pitch_i % 12 ];
60 if ( accidentals_i_ < 0 ) {
61 accidental_i = - accidental_i;
62 notename_i = ( notename_i + 1 ) % 7;
65 String notename_str = (char)( ( ( notename_i + key_i_ - 2 ) % 7 ) + 'a' );
66 while ( accidental_i-- > 0 )
67 notename_str += "is";
68 accidental_i++;
69 while ( accidental_i++ < 0 )
70 if ( ( notename_str == "a" ) || ( notename_str == "e" ) )
71 notename_str += "s";
72 else
73 notename_str += "es";
74 accidental_i--;
75 String octave_str;
77 octave_str += String( '\'', ( pitch_i - Midi_note::c0_pitch_i_c_ ) / 12 );
78 octave_str += String( '`', ( Midi_note::c0_pitch_i_c_ - pitch_i ) / 12 );
79 return octave_str + notename_str;
82 Midi_note::Midi_note( String name_str, Duration dur )
84 // do i want pitch too?
85 dur_ = dur;
86 name_str_ = name_str;
89 String
90 Midi_note::mudela_str( bool command_mode_bo )
92 // assert( !command_mode_bo );
93 // undefined ref to simple_plet_bo_ ??
94 // if ( simple_plet_bo_ )
95 // return name_str_ + Duration_convert::dur2_str( dur_ );
97 //ugh
98 String str;
99 if ( dur_.plet_p_ )
100 str += String( "\\plet{ " )
101 + String_convert::i2dec_str( dur_.plet_p_->iso_i_, 0, 0 )
102 + "/"
103 + String_convert::i2dec_str( dur_.plet_p_->type_i_, 0, 0 )
104 + " } ";
106 str += name_str_;
108 Duration dur = dur_;
109 dur.set_plet( 0 );
110 str += Duration_convert::dur2_str( dur );
112 if ( dur_.plet_p_ )
113 str += String( " \\plet{ 1/1 }" );
115 return str;
118 Moment
119 Midi_note::mom()
121 return Duration_convert::dur2_mom( dur_ );
124 Midi_tempo::Midi_tempo( int useconds_per_4_i )
126 useconds_per_4_i_ = useconds_per_4_i;
127 seconds_per_1_f_ = (Real)useconds_per_4_i_ * 4 / 1e6;
130 String
131 Midi_tempo::mudela_str( bool command_mode_bo )
133 // assert( command_mode_bo );
134 if ( !command_mode_bo )
135 return "";
136 String str = "tempo 4:";
137 str += String( get_tempo_i( Moment( 1, 4 ) ) );
138 return str;
142 Midi_tempo::get_tempo_i( Moment moment )
144 return Moment( 60 ) / moment / Moment( seconds_per_1_f_ );
147 Midi_text::Midi_text( Midi_text::Type type, String text_str )
149 type_ = type;
150 text_str_ = text_str;
153 String
154 Midi_text::mudela_str( bool command_mode_bo )
156 (void)command_mode_bo;
157 if ( !text_str_.length_i()
158 || ( text_str_.length_i() != (int)strlen( text_str_.ch_c_l() ) ) )
159 return "";
161 return "% " + text_str_ + "\n\t";
164 Midi_time::Midi_time( int num_i, int den_i, int clocks_4_i, int count_32_i )
165 : sync_dur_( 8 )
167 sync_f_ = 1.0;
168 if ( count_32_i != 8 )
169 warning( String( "#32 in quarter: " ) + String( count_32_i ), 0 );
170 num_i_ = num_i;
171 den_i_ = 2 << den_i;
172 clocks_1_i_ = clocks_4_i * 4;
176 Midi_time::clocks_1_i()
178 return clocks_1_i_;
181 Duration
182 Midi_time::i2_dur( int time_i, int division_1_i )
184 Moment mom = Duration_convert::i2_mom( time_i, division_1_i );
185 mom /= sync_f_;
187 dtor << "\n% (" << time_i << ", " << mom << "): "
188 << sync_f_ << endl;
190 Duration dur = Duration_convert::mom2_dur( mom );
191 if ( !dur.type_i_ ) {
192 vtor << "\n% resyncing(" << time_i << ", " << mom << "): "
193 << sync_f_ << " -> ";
194 mom *= sync_f_;
195 sync_f_ = Duration_convert::sync_f( sync_dur_, mom );
196 vtor << sync_f_ << endl;
197 mom /= sync_f_;
198 dur = Duration_convert::mom2_dur( mom );
201 return dur;
204 String
205 Midi_time::mudela_str( bool command_mode_bo )
207 String str = "meter { "
208 + String( num_i_ ) + "*" + String( den_i_ )
209 + " }";
210 if ( !command_mode_bo )
211 str = String( '\\' ) + str;
212 return str;