2 // midi-event.cc -- implement Midi_event
4 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
9 #include "plist.hh" // all for midi-main.hh
12 #include "source-file.hh"
13 #include "midi-main.hh" // *tors
15 #include "duration.hh"
16 #include "midi-event.hh"
17 #include "lily-stream.hh"
19 Midi_event::Midi_event()
23 Midi_event::~Midi_event()
34 Midi_event::mudela_str()
40 Midi_event::output_mudela( Lily_stream
& lily_stream_r
)
42 lily_stream_r
<< mudela_str_
<< String( " " );
45 Midi_key::Midi_key( int accidentals_i
, int minor_i
)
47 accidentals_i_
= accidentals_i
;
50 key_i_
= ( ( accidentals_i
% 7 )[ "cgdaebf" ] - 'a' + 2 ) % 7;
52 key_i_
= ( ( -accidentals_i
% 7 )[ "fbeadg" ] - 'a' + 2 ) % 7;
53 mudela_str_
= String( "% \\key\\" );
55 mudela_str_
+= String( (char)( key_i_
- 2 + 'A' ) );
57 mudela_str_
+= String( (char)( key_i_
- 2 + 'a' ) );
61 Midi_key::notename_str( int pitch_i
)
63 // this may seem very smart,
64 // but it-s only an excuse not to read a notename table
67 // minor scale: la-la ( = + 5 )
68 static int notename_i_a
[ 12 ] = { 0, 0, 1, 1, 2, 3, 3, 4, 4, 5, 5, 6 };
69 int notename_i
= notename_i_a
[ ( minor_i_
* 5 + pitch_i
) % 12 ];
71 static int accidentals_i_a
[ 12 ] = { 0, 1, 0, 1, 0, 0, 1, 0, 1, 0, 1, 0 };
72 int accidental_i
= accidentals_i_a
[ minor_i_
* 5 + pitch_i
% 12 ];
73 if ( accidentals_i_
< 0 ) {
74 accidental_i
= - accidental_i
;
75 notename_i
= ( notename_i
+ 1 ) % 7;
78 String notename_str
= (char)( ( ( notename_i
+ key_i_
- 2 ) % 7 ) + 'a' );
79 while ( accidental_i
-- > 0 )
82 while ( accidental_i
++ < 0 )
83 if ( ( notename_str
== "a" ) || ( notename_str
== "e" ) )
90 octave_str
+= String( '\'', ( pitch_i
- Midi_note::c0_pitch_i_c_
) / 12 );
91 octave_str
+= String( '`', ( Midi_note::c0_pitch_i_c_
- pitch_i
) / 12 );
92 return octave_str
+ notename_str
;
99 Midi_note::Midi_note( Midi_key
* midi_key_l
, Midi_time
* midi_time_l
, int division_1_i
, int pitch_i
, int time_i
)
101 dur_
= midi_time_l
->i2_dur( time_i
, division_1_i
);
104 mudela_str_
+= String( "\\plet{ " )
105 + String_convert::i2dec_str( dur_
.plet_p_
->iso_i_
, 0, 0 )
107 + String_convert::i2dec_str( dur_
.plet_p_
->type_i_
, 0, 0 )
110 mudela_str_
+= midi_key_l
->notename_str( pitch_i
);
114 mudela_str_
+= Duration_convert::dur2_str( dur
);
117 mudela_str_
+= String( " \\plet{ 1/1 }" );
120 Midi_note::~Midi_note()
127 return Duration_convert::dur2_mom( dur_
);
130 Midi_tempo::Midi_tempo( int useconds_per_4_i
)
132 useconds_per_4_i_
= useconds_per_4_i
;
133 seconds_per_1_f_
= (Real
)useconds_per_4_i_
* 4 / 1e6
;
134 mudela_str_
= "% \\Tempo: ";
135 mudela_str_
+= String( useconds_per_4_i_
);
136 mudela_str_
+= String( ": " )
137 + String( get_tempo_i( Moment( 1, 4 ) ) )
138 + String( " 4 per minute" );
141 Midi_tempo::~Midi_tempo()
146 Midi_tempo::get_tempo_i( Moment moment
)
148 return Moment( 60 ) / moment
/ Moment( seconds_per_1_f_
);
151 Midi_time::Midi_time( int num_i
, int den_i
, int clocks_4_i
, int count_32_i
)
155 if ( count_32_i
!= 8 )
156 warning( String( "#32 in quarter: " ) + String( count_32_i
), 0 );
159 clocks_1_i_
= clocks_4_i
* 4;
160 mudela_str_
= "% \\Time: ";
161 mudela_str_
+= String( num_i_
) + "/" + String( den_i_
)
162 + ", " + String( clocks_1_i_
)
163 + ": " + String( count_32_i
);
166 Midi_time::~Midi_time()
171 Midi_time::i2_dur( int time_i
, int division_1_i
)
173 Moment mom
= Duration_convert::i2_mom( time_i
, division_1_i
);
176 dtor
<< "\n% (" << time_i
<< ", " << mom
<< "): "
179 Duration dur
= Duration_convert::mom2_dur( mom
);
180 if ( !dur
.type_i_
) {
181 vtor
<< "\n% resyncing(" << time_i
<< ", " << mom
<< "): "
182 << sync_f_
<< " -> ";
184 sync_f_
= Duration_convert::sync_f( sync_dur_
, mom
);
185 vtor
<< sync_f_
<< endl
;
187 dur
= Duration_convert::mom2_dur( mom
);
194 Midi_time::clocks_1_i()