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