]> git.donarmstrong.com Git - lilypond.git/blob - mi2mu/midi-event.cc
release: 0.0.45
[lilypond.git] / mi2mu / midi-event.cc
1 //
2 // midi-event.cc -- implement Midi_event
3 //
4 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
5
6 #include "mi2mu.hh"
7
8 Midi_event::Midi_event()
9 {
10 }
11
12 Moment
13 Midi_event::mom()
14 {
15         return Moment( 0 );
16 }
17
18 void
19 Midi_event::output_mudela( Lily_stream& lily_stream_r, bool command_mode_bo )
20 {
21         lily_stream_r << mudela_str( command_mode_bo ) << String( " " );
22 }
23
24 Midi_key::Midi_key( int accidentals_i, int minor_i )
25 {
26         accidentals_i_ = accidentals_i;
27         minor_i_ = minor_i;
28         if ( accidentals_i >= 0 )
29                 key_i_ = ( ( accidentals_i % 7 )[ "cgdaebf" ] - 'a' - 2 ) % 7;
30         else
31                 key_i_ = ( ( -accidentals_i % 7 )[ "cfbeadg" ] - 'a' - 2 ) % 7;
32 }
33
34 String
35 Midi_key::mudela_str( bool command_mode_bo )
36 {
37         String str = "\\key\\";
38         if ( !minor_i_ ) 
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...
45         return str;
46 }
47
48 String
49 Midi_key::notename_str( int pitch_i )
50 {
51         // this may seem very smart,
52         // but it-s only an excuse not to read a notename table
53
54         // major scale: do-do
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 ];
58         
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;
64         }
65
66         String notename_str = (char)( ( ( notename_i + 2 ) % 7 ) + 'a' );
67         while ( accidental_i-- > 0 )
68                 notename_str += "is";
69         accidental_i++;
70         while ( accidental_i++ < 0 )
71                 if ( ( notename_str == "a" ) || ( notename_str == "e" ) )
72                         notename_str += "s";
73                 else
74                         notename_str += "es";
75         accidental_i--;
76
77         String de_octavate_str = String( '\'', ( Midi_note::c0_pitch_i_c_ + 11 - pitch_i ) / 12 );
78         String octavate_str = String( '\'', ( pitch_i - Midi_note::c0_pitch_i_c_ ) / 12 );
79         return de_octavate_str + notename_str + octavate_str;
80 }
81
82 // statics Midi_note
83 bool const Midi_note::simple_plet_b_s = false;
84
85 Midi_note::Midi_note( String name_str, Duration dur )
86 {
87         // do i want pitch too?
88         dur_ = dur;
89         name_str_ = name_str;
90 }
91
92 String
93 Midi_note::mudela_str( bool command_mode_bo )
94 {
95 //      assert( !command_mode_bo );
96         if ( simple_plet_b_s )
97                 return name_str_ + Duration_convert::dur2_str( dur_ );
98
99         //ugh
100         String str;
101         if ( dur_.plet_b() )
102                 str += String( "\\plet{ " )
103                         + String_convert::i2dec_str( dur_.plet_.iso_i_, 0, 0 )
104                         + "/"
105                         + String_convert::i2dec_str( dur_.plet_.type_i_, 0, 0 )
106                         + " } ";
107
108         str += name_str_;
109
110         Duration dur = dur_;
111         dur.set_plet( 1,1 );
112         str += Duration_convert::dur2_str( dur );
113
114         if ( dur_.plet_b() )
115                 str += String( " \\plet{ 1/1 }" );
116                 
117         return str;
118 }
119
120 Moment
121 Midi_note::mom()
122 {
123         return Duration_convert::dur2_mom( dur_ );
124 }
125
126 Midi_tempo::Midi_tempo( int useconds_per_4_i )
127 {
128         useconds_per_4_i_ = useconds_per_4_i;
129         seconds_per_1_f_ = (Real)useconds_per_4_i_ * 4 / 1e6;
130 }
131
132 String
133 Midi_tempo::mudela_str( bool command_mode_bo )
134 {
135 //      assert( command_mode_bo );
136         if ( !command_mode_bo )
137                 return "";
138         String str = "\\tempo 4:";
139         str += String( get_tempo_i( Moment( 1, 4 ) ) );
140         return str;
141 }
142
143 int 
144 Midi_tempo::useconds_per_4_i()
145 {
146         return useconds_per_4_i_;
147 }
148
149 int
150 Midi_tempo::get_tempo_i( Moment moment )
151 {
152         return Moment( 60 ) / moment / Moment( seconds_per_1_f_ );
153 }
154
155 Midi_text::Midi_text( Midi_text::Type type, String text_str )
156 {
157         type_ = type;
158         text_str_ = text_str;
159 }
160
161 String
162 Midi_text::mudela_str( bool command_mode_bo )
163 {
164         (void)command_mode_bo;
165         if ( !text_str_.length_i() 
166                 || ( text_str_.length_i() != (int)strlen( text_str_.ch_C() ) ) )
167                 return "";
168
169         return "% " + text_str_ + "\n\t";
170 }
171
172 Midi_time::Midi_time( int num_i, int den_i, int clocks_4_i, int count_32_i )
173         : sync_dur_( 8 )
174 {
175         sync_f_ = 1.0;
176         if ( count_32_i != 8 )
177                 warning( String( "#32 in quarter: " ) + String( count_32_i ), 0 );
178         num_i_ = num_i;
179         den_i_ = den_i;
180         clocks_1_i_ = clocks_4_i * 4; 
181 }
182
183 Moment
184 Midi_time::bar_mom()
185 {
186         return Moment( num_i_ ) * Duration_convert::dur2_mom( Duration( 1 << den_i_ ) );
187 }
188
189 int
190 Midi_time::clocks_1_i()
191 {
192         return clocks_1_i_;
193 }
194
195 int
196 Midi_time::den_i()
197 {
198         return den_i_;
199 }
200
201 int
202 Midi_time::num_i()
203 {
204         return num_i_;
205 }
206
207 String
208 Midi_time::mudela_str( bool command_mode_bo )
209 {
210         String str = "\\meter{ "
211                 + String( num_i_ ) + "/" + String( 1 << den_i_ ) 
212                 + " }";
213 //      if ( !command_mode_bo )
214 //          str =  String( '\\' ) + str;
215         return str;
216 }
217