]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-performer.cc
tie-performer: Store the moment of the starting note of a tie
[lilypond.git] / lily / tie-performer.cc
1 /*
2   tie-performer.cc -- implement Tie_performer
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1998--2009 Han-Wen Nienhuys <hanwen@xs4all.nl>
7 */
8
9 #include "performer.hh"
10
11 #include "audio-item.hh"
12 #include "context.hh"
13 #include "stream-event.hh"
14 #include "translator.icc"
15
16 struct Head_event_tuple
17 {
18   Audio_element_info head_;
19   Moment moment_;
20   Head_event_tuple () { }
21   Head_event_tuple (Audio_element_info h, Moment m)
22   {
23     head_ = h;
24     moment_ = m;
25   }
26 };
27
28
29 class Tie_performer : public Performer
30 {
31   Stream_event *event_;
32   vector<Head_event_tuple> now_heads_;
33   vector<Head_event_tuple> now_tied_heads_;
34   vector<Head_event_tuple> heads_to_tie_;
35
36 protected:
37   void stop_translation_timestep ();
38   void start_translation_timestep ();
39   virtual void acknowledge_audio_element (Audio_element_info);
40   void process_music ();
41   DECLARE_TRANSLATOR_LISTENER (tie);
42 public:
43   TRANSLATOR_DECLARATIONS (Tie_performer);
44 };
45
46 Tie_performer::Tie_performer ()
47 {
48   event_ = 0;
49 }
50
51 IMPLEMENT_TRANSLATOR_LISTENER (Tie_performer, tie);
52 void
53 Tie_performer::listen_tie (Stream_event *ev)
54 {
55   event_ = ev;
56 }
57
58 void
59 Tie_performer::process_music ()
60 {
61   if (event_)
62     context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
63 }
64
65 void
66 Tie_performer::acknowledge_audio_element (Audio_element_info inf)
67 {
68   if (Audio_note *an = dynamic_cast<Audio_note *> (inf.elem_))
69     {
70       Head_event_tuple inf_mom (inf, now_mom ());
71       if (an->tie_event_)
72         now_tied_heads_.push_back (inf_mom);
73       else
74         now_heads_.push_back (inf_mom);
75
76       // Find a previous note that ties to the current note. If it exists, 
77       // remove it from the heads_to_tie vector and create the tie
78       vector<Head_event_tuple>::iterator it;
79       bool found = false;
80       Stream_event *right_mus = inf.event_;
81       for ( it = heads_to_tie_.begin() ; (!found) && (it < heads_to_tie_.end()); it++ )
82         {
83           Audio_element_info et = (*it).head_;
84           Audio_note *th = dynamic_cast<Audio_note *> (et.elem_);
85           Stream_event *left_mus = et.event_;
86
87           if (th && right_mus && left_mus
88               && ly_is_equal (right_mus->get_property ("pitch"),
89                               left_mus->get_property ("pitch")))
90             {
91               found = true;
92               Moment skip = now_mom() - (*it).moment_ - th->length_mom_;
93               an->tie_to (th, skip);
94               // this invalidates the iterator, we are leaving the loop anyway
95               heads_to_tie_.erase (it);
96             }
97         }
98     }
99 }
100
101 void
102 Tie_performer::start_translation_timestep ()
103 {
104   context ()->set_property ("tieMelismaBusy",
105                             ly_bool2scm (heads_to_tie_.size ()));
106 }
107
108 void
109 Tie_performer::stop_translation_timestep ()
110 {
111   // We might have dangling open ties like c~ d. Close them, unless we have
112   // tieWaitForNote set...
113   if (!to_boolean (get_property ("tieWaitForNote")))
114     {
115       heads_to_tie_.clear ();
116     }
117
118   if (event_)
119     {
120       for (vsize i = now_heads_.size (); i--;)
121         heads_to_tie_.push_back (now_heads_[i]);
122     }
123
124   for (vsize i = now_tied_heads_.size (); i--;)
125     heads_to_tie_.push_back (now_tied_heads_[i]);
126
127   event_ = 0;
128   now_heads_.clear ();
129   now_tied_heads_.clear ();
130 }
131
132 ADD_TRANSLATOR (Tie_performer,
133                 /* doc */
134                 "Generate ties between note heads of equal pitch.",
135
136                 /* create */
137                 "",
138
139                 /* read */
140                 "tieWaitForNote",
141
142                 /* write */
143                 "tieMelismaBusy"
144                 );