Fix \deadNotesOff.
[lilypond/mpolesky.git] / lily / duration-scheme.cc
blob6c29c62917b915d1209b953e426cef931cea85bf
1 /*
2 This file is part of LilyPond, the GNU music typesetter.
4 Copyright (C) 1997--2010 Jan Nieuwenhuizen <janneke@gnu.org>
5 Han-Wen Nienhuys <hanwen@xs4all.nl>
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.
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.
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/>.
21 #include "duration.hh"
22 #include "misc.hh"
24 MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
25 SCM
26 Duration::less_p (SCM p1, SCM p2)
28 Duration *a = unsmob_duration (p1);
29 Duration *b = unsmob_duration (p2);
31 if (compare (*a, *b) < 0)
32 return SCM_BOOL_T;
33 else
34 return SCM_BOOL_F;
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}?")
41 LY_ASSERT_SMOB (Duration, p1, 1);
42 LY_ASSERT_SMOB (Duration, p2, 2);
44 Duration *a = unsmob_duration (p1);
45 Duration *b = unsmob_duration (p2);
47 if (Duration::compare (*a, *b) < 0)
48 return SCM_BOOL_T;
49 else
50 return SCM_BOOL_F;
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 @var{num} and"
61 " @var{den}.\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.")
67 LY_ASSERT_TYPE (scm_is_integer, length, 1);
69 int dots = 0;
70 if (dotcount != SCM_UNDEFINED)
72 LY_ASSERT_TYPE (scm_is_integer, dotcount, 2);
73 dots = scm_to_int (dotcount);
76 bool compress = false;
77 if (num != SCM_UNDEFINED)
79 LY_ASSERT_TYPE (scm_is_number, num, 3);
80 compress = true;
82 else
83 num = scm_from_int (1);
85 if (den != SCM_UNDEFINED)
87 LY_ASSERT_TYPE (scm_is_number, den, 4);
88 compress = true;
90 else
91 den = scm_from_int (1);
93 Duration p (scm_to_int (length), dots);
94 if (compress)
95 p = p.compressed (Rational (scm_to_int (num), scm_to_int (den)));
97 return p.smobbed_copy ();
100 LY_DEFINE (ly_duration_log, "ly:duration-log",
101 1, 0, 0, (SCM dur),
102 "Extract the duration log from @var{dur}.")
104 LY_ASSERT_SMOB (Duration, dur, 1);
105 return scm_from_int (unsmob_duration (dur)->duration_log ());
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}.")
112 LY_ASSERT_SMOB (Duration, dur, 1);
113 return scm_from_int (unsmob_duration (dur)->dot_count ());
116 LY_DEFINE (ly_intlog2, "ly:intlog2",
117 1, 0, 0, (SCM d),
118 "The 2-logarithm of 1/@var{d}.")
120 LY_ASSERT_TYPE (scm_is_number, d, 1);
121 int log = intlog2 (scm_to_int (d));
122 return scm_from_int (log);
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}.")
129 LY_ASSERT_SMOB (Duration, dur, 1);
130 return Moment (unsmob_duration (dur)->get_length ()).smobbed_copy ();
133 LY_DEFINE (ly_duration_2_string, "ly:duration->string",
134 1, 0, 0, (SCM dur),
135 "Convert @var{dur} to a string.")
137 LY_ASSERT_SMOB (Duration, dur, 1);
138 return ly_string2scm (unsmob_duration (dur)->to_string ());
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.")
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 ()));