]> git.donarmstrong.com Git - lilypond.git/blob - lily/time-signature-engraver.cc
Web-ja: update introduction
[lilypond.git] / lily / time-signature-engraver.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Han-Wen Nienhuys <hanwen@xs4all.nl>
5
6   LilyPond is free software: you can redistribute it and/or modify
7   it under the terms of the GNU General Public License as published by
8   the Free Software Foundation, either version 3 of the License, or
9   (at your option) any later version.
10
11   LilyPond is distributed in the hope that it will be useful,
12   but WITHOUT ANY WARRANTY; without even the implied warranty of
13   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14   GNU General Public License for more details.
15
16   You should have received a copy of the GNU General Public License
17   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
18 */
19
20 #include "engraver-group.hh"
21
22 #include "item.hh"
23 #include "international.hh"
24 #include "misc.hh"
25 #include "moment.hh"
26 #include "stream-event.hh"
27 #include "warn.hh"
28
29 #include "translator.icc"
30
31 /**
32    generate time_signatures.
33 */
34 class Time_signature_engraver : public Engraver
35 {
36   Item *time_signature_;
37   SCM last_time_fraction_;
38   SCM time_cause_;
39
40 protected:
41   virtual void derived_mark () const;
42   void stop_translation_timestep ();
43   void process_music ();
44 public:
45   TRANSLATOR_DECLARATIONS (Time_signature_engraver);
46   void listen_time_signature (Stream_event *);
47 };
48
49 void
50 Time_signature_engraver::derived_mark () const
51 {
52   scm_gc_mark (last_time_fraction_);
53   scm_gc_mark (time_cause_);
54 }
55
56 Time_signature_engraver::Time_signature_engraver (Context *c)
57   : Engraver (c)
58 {
59   time_signature_ = 0;
60   time_cause_ = SCM_EOL;
61   last_time_fraction_ = SCM_BOOL_F;
62 }
63
64 void
65 Time_signature_engraver::listen_time_signature (Stream_event *ev)
66 {
67   time_cause_ = ev->self_scm ();
68 }
69
70 void
71 Time_signature_engraver::process_music ()
72 {
73   if (time_signature_)
74     return;
75
76   SCM fr = get_property ("timeSignatureFraction");
77   if (!scm_is_eq (last_time_fraction_, fr) && scm_is_pair (fr))
78     {
79       time_signature_ = make_item ("TimeSignature", time_cause_);
80       time_signature_->set_property ("fraction", fr);
81
82       if (scm_is_false (last_time_fraction_))
83         time_signature_->set_property ("break-visibility",
84                                        get_property ("initialTimeSignatureVisibility"));
85
86       int den = scm_to_int (scm_cdr (fr));
87       if (den != (1 << intlog2 (den)))
88         {
89           /*
90             Todo: should make typecheck?
91
92             OTOH, Tristan Keuris writes 8/20 in his Intermezzi.
93           */
94           time_signature_->warning (_f ("strange time signature found: %d/%d",
95                                         int (scm_to_int (scm_car (fr))),
96                                         den));
97         }
98
99       last_time_fraction_ = fr;
100     }
101 }
102
103 void
104 Time_signature_engraver::stop_translation_timestep ()
105 {
106   if (time_signature_ && !scm_is_null (time_cause_))
107     {
108       Moment *mp = unsmob<Moment> (get_property ("measurePosition"));
109       if (mp && (mp->main_part_ > Rational (0))
110           && !to_boolean (get_property ("partialBusy")))
111         time_signature_->warning ("mid-measure time signature without \\partial");
112     }
113
114   time_signature_ = 0;
115   time_cause_ = SCM_EOL;
116 }
117
118 void
119 Time_signature_engraver::boot ()
120 {
121   ADD_LISTENER (Time_signature_engraver, time_signature);
122 }
123
124 ADD_TRANSLATOR (Time_signature_engraver,
125                 /* doc */
126                 "Create a @ref{TimeSignature} whenever"
127                 " @code{timeSignatureFraction} changes.",
128
129                 /* create */
130                 "TimeSignature ",
131
132                 /* read */
133                 "initialTimeSignatureVisibility "
134                 "partialBusy "
135                 "timeSignatureFraction ",
136
137                 /* write */
138                 ""
139                );