lilypond-0.1.16
[lilypond.git] / src / midi-event.cc
blob36d6694779d3006e37256dbfa3372c81a78d9ab2
1 //
2 // midi-event.cc -- implement Midi_event
3 //
4 // copyright 1997 Jan Nieuwenhuizen <jan@digicash.com>
6 #include <assert.h>
8 #include "proto.hh"
9 #include "plist.hh" // all for midi-main.hh
10 #include "string.hh"
11 #include "source.hh"
12 #include "source-file.hh"
13 #include "midi-main.hh" // *tors
14 #include "moment.hh"
15 #include "duration.hh"
16 #include "midi-event.hh"
17 #include "lily-stream.hh"
19 Midi_event::Midi_event()
23 Midi_event::~Midi_event()
27 Moment
28 Midi_event::mom()
30 return Moment( 0 );
33 String
34 Midi_event::mudela_str()
36 return mudela_str_;
39 void
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;
48 minor_i_ = minor_i;
49 if ( !minor_i_ )
50 key_i_ = ( ( accidentals_i % 7 )[ "cgdaebf" ] - 'a' + 2 ) % 7;
51 else
52 key_i_ = ( ( -accidentals_i % 7 )[ "fbeadg" ] - 'a' + 2 ) % 7;
53 mudela_str_ = String( "% \\key\\" );
54 if ( !minor_i_ )
55 mudela_str_ += String( (char)( key_i_ - 2 + 'A' ) );
56 else
57 mudela_str_ += String( (char)( key_i_ - 2 + 'a' ) );
60 String
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
66 // major scale: do-do
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 )
80 notename_str += "is";
81 accidental_i++;
82 while ( accidental_i++ < 0 )
83 if ( ( notename_str == "a" ) || ( notename_str == "e" ) )
84 notename_str += "s";
85 else
86 notename_str += "es";
87 accidental_i--;
88 String octave_str;
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;
95 Midi_key::~Midi_key()
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 );
103 if ( dur_.plet_p_ )
104 mudela_str_ += String( "\\plet{ " )
105 + String_convert::i2dec_str( dur_.plet_p_->iso_i_, 0, 0 )
106 + "/"
107 + String_convert::i2dec_str( dur_.plet_p_->type_i_, 0, 0 )
108 + " } ";
110 mudela_str_ += midi_key_l->notename_str( pitch_i );
112 Duration dur = dur_;
113 dur.set_plet( 0 );
114 mudela_str_ += Duration_convert::dur2_str( dur );
116 if ( dur_.plet_p_ )
117 mudela_str_ += String( " \\plet{ 1/1 }" );
120 Midi_note::~Midi_note()
124 Moment
125 Midi_note::mom()
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 )
152 : sync_dur_( 8 )
154 sync_f_ = 1.0;
155 if ( count_32_i != 8 )
156 warning( String( "#32 in quarter: " ) + String( count_32_i ), 0 );
157 num_i_ = num_i;
158 den_i_ = 2 << den_i;
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()
170 Duration
171 Midi_time::i2_dur( int time_i, int division_1_i )
173 Moment mom = Duration_convert::i2_mom( time_i, division_1_i );
174 mom /= sync_f_;
176 dtor << "\n% (" << time_i << ", " << mom << "): "
177 << sync_f_ << endl;
179 Duration dur = Duration_convert::mom2_dur( mom );
180 if ( !dur.type_i_ ) {
181 vtor << "\n% resyncing(" << time_i << ", " << mom << "): "
182 << sync_f_ << " -> ";
183 mom *= sync_f_;
184 sync_f_ = Duration_convert::sync_f( sync_dur_, mom );
185 vtor << sync_f_ << endl;
186 mom /= sync_f_;
187 dur = Duration_convert::mom2_dur( mom );
190 return dur;
194 Midi_time::clocks_1_i()
196 return clocks_1_i_;