]> git.donarmstrong.com Git - lilypond.git/blob - lily/duration-scheme.cc
Web-ja: update introduction
[lilypond.git] / lily / duration-scheme.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 1997--2015 Jan Nieuwenhuizen <janneke@gnu.org>
5   Han-Wen Nienhuys <hanwen@xs4all.nl>
6
7   LilyPond is free software: you can redistribute it and/or modify
8   it under the terms of the GNU General Public License as published by
9   the Free Software Foundation, either version 3 of the License, or
10   (at your option) any later version.
11
12   LilyPond is distributed in the hope that it will be useful,
13   but WITHOUT ANY WARRANTY; without even the implied warranty of
14   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   GNU General Public License for more details.
16
17   You should have received a copy of the GNU General Public License
18   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
19 */
20
21 #include "duration.hh"
22 #include "misc.hh"
23
24 MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
25 SCM
26 Duration::less_p (SCM p1, SCM p2)
27 {
28   Duration *a = unsmob<Duration> (p1);
29   Duration *b = unsmob<Duration> (p2);
30
31   if (compare (*a, *b) < 0)
32     return SCM_BOOL_T;
33   else
34     return SCM_BOOL_F;
35 }
36
37 LY_DEFINE (ly_duration_less_p, "ly:duration<?",
38            2, 0, 0, (SCM p1, SCM p2),
39            "Is @var{p1} shorter than @var{p2}?")
40 {
41   LY_ASSERT_SMOB (Duration, p1, 1);
42   LY_ASSERT_SMOB (Duration, p2, 2);
43
44   Duration *a = unsmob<Duration> (p1);
45   Duration *b = unsmob<Duration> (p2);
46
47   if (Duration::compare (*a, *b) < 0)
48     return SCM_BOOL_T;
49   else
50     return SCM_BOOL_F;
51 }
52
53 LY_DEFINE (ly_make_duration, "ly:make-duration",
54            1, 3, 0, (SCM length, SCM dotcount, SCM num, SCM den),
55            "@var{length} is the negative logarithm (base 2) of the duration:"
56            " 1@tie{}is a half note, 2@tie{}is a quarter note, 3@tie{}is an"
57            " eighth note, etc.  The number of dots after the note is given by"
58            " the optional argument @var{dotcount}.\n"
59            "\n"
60            "The duration factor is optionally given by integers @var{num} and"
61            " @var{den}, alternatively by a single rational number.\n"
62            "\n"
63            "A duration is a musical duration, i.e., a length of time"
64            " described by a power of two (whole, half, quarter, etc.) and a"
65            " number of augmentation dots.")
66 {
67   LY_ASSERT_TYPE (scm_is_integer, length, 1);
68
69   int dots = 0;
70   if (!SCM_UNBNDP (dotcount))
71     {
72       LY_ASSERT_TYPE (scm_is_integer, dotcount, 2);
73       dots = scm_to_int (dotcount);
74     }
75
76   bool compress = false;
77   if (!SCM_UNBNDP (num))
78     {
79       LY_ASSERT_TYPE (ly_is_rational, num, 3);
80       compress = true;
81     }
82   else
83     num = scm_from_int (1);
84
85   if (!SCM_UNBNDP (den))
86     {
87       LY_ASSERT_TYPE (scm_is_integer, den, 4);
88       compress = true;
89     }
90   else
91     den = scm_from_int (1);
92
93   Duration p (scm_to_int (length), dots);
94   if (compress)
95     p = p.compressed (ly_scm2rational (scm_divide (num, den)));
96
97   return p.smobbed_copy ();
98 }
99
100 LY_DEFINE (ly_duration_log, "ly:duration-log",
101            1, 0, 0, (SCM dur),
102            "Extract the duration log from @var{dur}.")
103 {
104   LY_ASSERT_SMOB (Duration, dur, 1);
105   return scm_from_int (unsmob<Duration> (dur)->duration_log ());
106 }
107
108 LY_DEFINE (ly_duration_dot_count, "ly:duration-dot-count",
109            1, 0, 0, (SCM dur),
110            "Extract the dot count from @var{dur}.")
111 {
112   LY_ASSERT_SMOB (Duration, dur, 1);
113   return scm_from_int (unsmob<Duration> (dur)->dot_count ());
114 }
115
116 LY_DEFINE (ly_intlog2, "ly:intlog2",
117            1, 0, 0, (SCM d),
118            "The 2-logarithm of 1/@var{d}.")
119 {
120   LY_ASSERT_TYPE (scm_is_number, d, 1);
121   int log = intlog2 (scm_to_int (d));
122   return scm_from_int (log);
123 }
124
125 LY_DEFINE (ly_duration_length, "ly:duration-length",
126            1, 0, 0, (SCM dur),
127            "The length of the duration as a @code{moment}.")
128 {
129   LY_ASSERT_SMOB (Duration, dur, 1);
130   return Moment (unsmob<Duration> (dur)->get_length ()).smobbed_copy ();
131 }
132
133 LY_DEFINE (ly_duration_2_string, "ly:duration->string",
134            1, 0, 0, (SCM dur),
135            "Convert @var{dur} to a string.")
136 {
137   LY_ASSERT_SMOB (Duration, dur, 1);
138   return ly_string2scm (unsmob<Duration> (dur)->to_string ());
139 }
140
141 LY_DEFINE (ly_duration_factor, "ly:duration-factor",
142            1, 0, 0, (SCM dur),
143            "Extract the compression factor from @var{dur}."
144            "  Return it as a pair.")
145 {
146   LY_ASSERT_SMOB (Duration, dur, 1);
147   Rational r = unsmob<Duration> (dur)->factor ();
148   return scm_cons (scm_from_int64 (r.num ()), scm_from_int64 (r.den ()));
149 }
150
151 // This is likely what ly:duration-factor should have been in the
152 // first place.
153 LY_DEFINE (ly_duration_scale, "ly:duration-scale",
154            1, 0, 0, (SCM dur),
155            "Extract the compression factor from @var{dur}."
156            "  Return it as a rational.")
157 {
158   LY_ASSERT_SMOB (Duration, dur, 1);
159   Rational r = unsmob<Duration> (dur)->factor ();
160
161   return ly_rational2scm (r);
162 }