]> git.donarmstrong.com Git - lilypond.git/blob - lily/tie-performer.cc
35d52ce0d4ed19ed9d9e842c7b501953fd1d6342
[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 class Tie_performer : public Performer
17 {
18   Stream_event *event_;
19   vector<Audio_element_info> now_heads_;
20   vector<Audio_element_info> now_tied_heads_;
21   vector<Audio_element_info> heads_to_tie_;
22
23   bool ties_created_;
24
25 protected:
26   void stop_translation_timestep ();
27   void start_translation_timestep ();
28   virtual void acknowledge_audio_element (Audio_element_info);
29   void process_music ();
30   DECLARE_TRANSLATOR_LISTENER (tie);
31 public:
32   TRANSLATOR_DECLARATIONS (Tie_performer);
33 };
34
35 Tie_performer::Tie_performer ()
36 {
37   event_ = 0;
38   ties_created_ = false;
39 }
40
41 IMPLEMENT_TRANSLATOR_LISTENER (Tie_performer, tie);
42 void
43 Tie_performer::listen_tie (Stream_event *ev)
44 {
45   event_ = ev;
46 }
47
48 void
49 Tie_performer::process_music ()
50 {
51   if (event_)
52     context ()->set_property ("tieMelismaBusy", SCM_BOOL_T);
53 }
54
55 void
56 Tie_performer::acknowledge_audio_element (Audio_element_info inf)
57 {
58   if (Audio_note *an = dynamic_cast<Audio_note *> (inf.elem_))
59     {
60       if (an->tie_event_)
61         now_tied_heads_.push_back (inf);
62       else
63         now_heads_.push_back (inf);
64
65       // Find a previous note that ties to the current note. If it exists, 
66       // remove it from the heads_to_tie vector and create the tie
67       vector<Audio_element_info>::iterator it;
68       bool found = false;
69       Stream_event *right_mus = inf.event_;
70       for ( it = heads_to_tie_.begin() ; (!found) && (it < heads_to_tie_.end()); it++ )
71         {
72           Audio_element_info et = *it;
73           Audio_note *th = dynamic_cast<Audio_note *> (et.elem_);
74           Stream_event *left_mus = et.event_;
75
76           if (th && right_mus && left_mus
77               && ly_is_equal (right_mus->get_property ("pitch"),
78                               left_mus->get_property ("pitch")))
79             {
80               found = true;
81               an->tie_to (th);
82               ties_created_ = true;
83               // this invalidates the iterator, we are leaving the loop anyway
84               heads_to_tie_.erase (it);
85             }
86         }
87     }
88 }
89
90 void
91 Tie_performer::start_translation_timestep ()
92 {
93   context ()->set_property ("tieMelismaBusy",
94                             ly_bool2scm (heads_to_tie_.size ()));
95 }
96
97 void
98 Tie_performer::stop_translation_timestep ()
99 {
100   // We might have dangling open ties like c~ d. Close them, unless we have
101   // tieWaitForNote set...
102   if (!to_boolean (get_property ("tieWaitForNote")))
103     {
104       heads_to_tie_.clear ();
105       ties_created_ = false;
106     }
107
108   if (event_)
109     {
110       heads_to_tie_ = now_heads_;
111     }
112
113   for (vsize i = now_tied_heads_.size (); i--;)
114     heads_to_tie_.push_back (now_tied_heads_[i]);
115
116   event_ = 0;
117   now_heads_.clear ();
118   now_tied_heads_.clear ();
119 }
120
121 ADD_TRANSLATOR (Tie_performer,
122                 /* doc */
123                 "Generate ties between note heads of equal pitch.",
124
125                 /* create */
126                 "",
127
128                 /* read */
129                 "tieWaitForNote",
130
131                 /* write */
132                 "tieMelismaBusy"
133                 );