]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
release: 0.0.46.jcn1
[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_idx() <= max_moment) {
33         Moment stop_moment = stop_notes.front_idx();
34         Melodic_req * req_l = stop_notes.get();
35         
36         Midi_note note(req_l, track_l_->number_i_, false);
37         output_event(note, stop_moment);
38     }
39 }
40 /**
41   Find out if start_note event is needed,  and do it if needed.
42  */
43 void 
44 Midi_walker::do_start_note(Note_req*note_l)
45 {
46     Moment stop=note_l->duration() + ptr()->when();
47     for(int i=0; i < stop_notes.size(); i++)
48         if (stop_notes.value_arr_[i]->melodic()->pitch() ==
49             note_l->pitch()) {
50              if ( stop_notes.indices_arr_[i] < stop){
51                  
52                  stop_notes.del(i);
53                  return ;  // removing this gives a feature ( ${c2 c4}$ output correctly)
54              }
55              else
56                  return; // skip the stop note 
57              break;// do the stop note               
58         }
59     
60     stop_notes.enter(note_l,  stop);
61     Midi_note note(note_l, track_l_->number_i_, true);
62     output_event(note, ptr()->when());
63 }
64
65
66 /** advance the track to #now#, output the item, and adjust current
67   "moment".  */
68 void
69 Midi_walker::output_event(Midi_item &i, Moment now)
70 {
71     Moment delta_t = now - last_moment_ ;
72     last_moment_ += delta_t;
73     track_l_->add(delta_t, &i );    
74 }
75
76 void
77 Midi_walker::process_requests()
78 {
79     do_stop_notes(ptr()->when());
80
81     for ( int i = 0; i < ptr()->commandreq_l_arr_.size(); i++ )  {
82         Command_req *c_l = ptr()->commandreq_l_arr_[i]->command();
83         Meter_change_req* meter_l = c_l->meterchange();
84         if ( meter_l )
85             output_event( Midi_time( meter_l->beats_i_, meter_l->one_beat_i_, 18 ),  0 );
86         Key_change_req* key_l = c_l->keychange();
87         if ( key_l ) {
88             int sharps_i = key_l->sharps_i();
89             int flats_i = key_l->flats_i();
90             // midi cannot handle non-conventional keys
91             if ( !( flats_i && sharps_i ) )
92                 output_event( Midi_key( sharps_i - flats_i, key_l->minor_b() ), 0 );
93         }
94     }
95
96     for ( int i = 0; i < ptr()->musicalreq_l_arr_.size(); i++ )  {
97
98         Rhythmic_req *n = ptr()->musicalreq_l_arr_[i]->rhythmic();
99         if ( !n)
100             continue;
101         Note_req * note_l = n->note();
102         if (!note_l)
103             continue;
104         do_start_note(note_l);
105         
106     }
107 }
108
109 Midi_walker::~Midi_walker()
110 {
111     do_stop_notes( last_moment_ + Moment(10,1)); // ugh
112 }