]> git.donarmstrong.com Git - lilypond.git/blob - lily/midi-walker.cc
release: 0.0.40
[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 "musicalrequest.hh"
10 #include "pscore.hh"
11 #include "staff.hh"
12 #include "midi-walker.hh"
13 #include "midi-item.hh"
14 #include "midi-stream.hh"
15 #include "debug.hh"
16 #include "staff-column.hh"
17
18 Midi_walker::Midi_walker(Staff *st_l, Midi_track* track_l)
19     : PCursor<Staff_column*>(st_l->cols_)
20 {
21     track_l_ = track_l;
22     last_moment_= 0;
23 }
24
25 /**
26   output notestop events for all notes which end before #max_moment#
27  */
28 void
29 Midi_walker::do_stop_notes(Moment max_moment)
30 {
31     while (stop_notes.size() && stop_notes.front_idx() <= max_moment) {
32         Moment stop_moment = stop_notes.front_idx();
33         Melodic_req * req_l = stop_notes.get();
34         
35         Midi_note note(req_l, track_l_->number_i_, false);
36         output_event(note, stop_moment);
37     }
38 }
39 /**
40   Find out if start_note event is needed,  and do it if needed.
41  */
42 void 
43 Midi_walker::do_start_note(Note_req*note_l)
44 {
45     Moment stop=note_l->duration() + ptr()->when();
46     for(int i=0; i < stop_notes.size(); i++)
47         if (stop_notes.value_arr_[i]->melodic()->pitch() ==
48             note_l->pitch()) {
49              if ( stop_notes.indices_arr_[i] < stop){
50                  
51                  stop_notes.del(i);
52                  return ;  // removing this gives a feature ( ${c2 c4}$ output correctly)
53              }
54              else
55                  return; // skip the stop note 
56              break;// do the stop note               
57         }
58     
59     stop_notes.enter(note_l,  stop);
60     Midi_note note(note_l, track_l_->number_i_, true);
61     output_event(note, ptr()->when());
62 }
63
64
65 /** advance the track to #now#, output the item, and adjust current
66   "moment".  */
67 void
68 Midi_walker::output_event(Midi_item &i, Moment now)
69 {
70     Moment delta_t = now - last_moment_ ;
71     last_moment_ += delta_t;
72     track_l_->add(delta_t, &i );    
73 }
74
75 void
76 Midi_walker::process_requests()
77 {
78     do_stop_notes(ptr()->when());
79     for ( int i = 0; i < ptr()->musicalreq_l_arr_.size(); i++ )  {
80
81         Rhythmic_req *n = ptr()->musicalreq_l_arr_[i]->rhythmic();
82         if ( !n)
83             continue;
84         Note_req * note_l = n->note();
85         if (!note_l)
86             continue;
87         do_start_note(note_l);
88         
89     }
90 }
91
92 Midi_walker::~Midi_walker()
93 {
94     do_stop_notes( last_moment_ + Moment(10,1)); // ugh
95 }