]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
8cf348d9f7d7e45c6416b62bbea69d04db2b4f23
[lilypond.git] / lily / midi-walker.cc
1 /*
2   midi-walker.cc -- implement Midi_walker
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997 Han-Wen Nienhuys <hanwen@stack.nl>, Jan Nieuwenhuizen <jan@digicash.com>
7   
8   TODO
9
10   Ideally this should also use a register system, to process slurs,
11   dynamics, etc.
12   
13 */
14
15 /* reincluded for code jatting */
16 #if 0
17
18 #include "command-request.hh"
19 #include "musical-request.hh"
20 #include "p-score.hh"
21 #include "staff.hh"
22 #include "midi-walker.hh"
23 #include "midi-item.hh"
24 #include "midi-stream.hh"
25 #include "debug.hh"
26 #include "staff-column.hh"
27
28 Midi_walker::Midi_walker(Staff *st_l, Midi_track* track_l)
29     : PCursor<Staff_column*>(st_l->cols_)
30 {
31     track_l_ = track_l;
32     last_moment_= 0;
33 }
34
35 /**
36   output notestop events for all notes which end before #max_moment#
37  */
38 void
39 Midi_walker::do_stop_notes(Moment max_moment)
40 {
41     while (stop_notes.size() && stop_notes.front().key <= max_moment) {
42         Note_event  ent=stop_notes.get();
43         if (ent.ignore_b_) 
44             continue;
45         
46         Moment stop_moment = ent.key;
47         Melodic_req * req_l = ent.val;
48         
49         Midi_note note(req_l, track_l_->number_i_, false);
50         output_event(note, stop_moment);
51     }
52 }
53 /**
54   Find out if start_note event is needed,  and do it if needed.
55  */
56 void 
57 Midi_walker::do_start_note(Note_req*note_l)
58 {
59     Moment stop = note_l->duration() + ptr()->when();
60     for(int i=0; i < stop_notes.size(); i++) {
61         if (stop_notes[i].val->melodic()->pitch() ==
62             note_l->pitch()) {
63             if ( stop_notes[i].key < stop){
64                 stop_notes[i].ignore_b_=true;
65             }
66             else
67                 return; // skip the stop note 
68         }
69     }
70     Note_event e;
71     e.val = note_l;
72     e.key = stop;
73     
74     stop_notes.insert(e);
75     
76     Midi_note note(note_l, track_l_->number_i_, true);
77     output_event(note, ptr()->when());
78 }
79
80
81 /** advance the track to #now#, output the item, and adjust current
82   "moment".  */
83 void
84 Midi_walker::output_event(Midi_item &i, Moment now)
85 {
86     Moment delta_t = now - last_moment_ ;
87     last_moment_ += delta_t;
88     track_l_->add(delta_t, &i );    
89 }
90
91 void
92 Midi_walker::process_requests()
93 {
94     do_stop_notes(ptr()->when());
95
96     for ( int i = 0; i < ptr()->commandreq_l_arr_.size(); i++ )  {
97         Command_req *c_l = ptr()->commandreq_l_arr_[i]->command();
98         Meter_change_req* meter_l = c_l->meterchange();
99         if ( meter_l )
100             output_event( Midi_time( meter_l->beats_i_, meter_l->one_beat_i_, 18 ),  ptr()->when() );
101         Key_change_req* key_l = c_l->keychange();
102         if ( key_l ) {
103             int sharps_i = key_l->sharps_i();
104             int flats_i = key_l->flats_i();
105             // midi cannot handle non-conventional keys
106             if ( !( flats_i && sharps_i ) )
107                 output_event( Midi_key( sharps_i - flats_i, key_l->minor_b() ), ptr()->when() );
108         }
109     }
110
111     for ( int i = 0; i < ptr()->musicalreq_l_arr_.size(); i++ )  {
112         Musical_req *m = ptr()->musicalreq_l_arr_[i]->musical();
113         if (!m)
114             return;
115         Rhythmic_req *n = m->rhythmic();
116         if ( !n)
117             continue;
118         Note_req * note_l = n->note();
119         if (!note_l)
120             continue;
121         do_start_note(note_l);
122     }
123 }
124
125 Midi_walker::~Midi_walker()
126 {
127     do_stop_notes( last_moment_ + Moment(10,1)); // ugh
128 }
129
130
131 int
132 compare(Note_event const&e1, Note_event const&e2)
133 {
134     return sign(e1.key - e2.key);
135 }
136
137 #endif