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