~ubuntu-branches/ubuntu/precise/lilypond/precise

« back to all changes in this revision

Viewing changes to lily/duration.cc

  • Committer: Bazaar Package Importer
  • Author(s): Thomas Bushnell, BSG
  • Date: 2006-12-19 10:18:12 UTC
  • mfrom: (3.1.4 feisty)
  • Revision ID: james.westby@ubuntu.com-20061219101812-7awtjkp0i393wxty
Tags: 2.8.7-3
scripts/midi2ly.py: When setting DATADIR, find Lilypond python files
in the @TOPLEVEL_VERSION@ directory, not 'current'.  Patch thanks to
Chris Lamb (chris@chris-lamb.co.uk).  (Closes: #400550)

Show diffs side-by-side

added added

removed removed

Lines of Context:
3
3
 
4
4
  source file of the LilyPond music typesetter
5
5
 
6
 
  (c) 1997--2004 Jan Nieuwenhuizen <janneke@gnu.org>
7
 
                 Han-Wen Nienhuys <hanwen@cs.uu.nl>
8
 
 
 
6
  (c) 1997--2006 Jan Nieuwenhuizen <janneke@gnu.org>
 
7
  Han-Wen Nienhuys <hanwen@xs4all.nl>
9
8
*/
10
9
 
11
 
#include <assert.h>
 
10
#include "duration.hh"
12
11
 
13
12
#include "misc.hh"
14
13
#include "lily-proto.hh"
15
 
#include "string.hh"
16
 
#include "moment.hh"
17
 
#include "duration.hh"
 
14
 
18
15
#include "ly-smobs.icc"
19
16
 
20
 
 
21
17
int
22
18
Duration::compare (Duration const &left, Duration const &right)
23
19
{
51
47
{
52
48
  Rational mom (1 << abs (durlog_));
53
49
 
54
 
  if (durlog_> 0)
 
50
  if (durlog_ > 0)
55
51
    mom = Rational (1) / mom;
56
52
 
57
53
  Rational delta = mom;
64
60
  return mom * factor_;
65
61
}
66
62
 
67
 
String
 
63
string
68
64
Duration::to_string () const
69
65
{
70
 
  String s;
 
66
  string s;
71
67
 
72
 
  if (durlog_ < 0  )
73
 
    s = "log = "  + ::to_string (durlog_);
 
68
  if (durlog_ < 0)
 
69
    s = "log = " + ::to_string (durlog_);
74
70
  else
75
71
    s = ::to_string (1 << durlog_);
76
72
 
80
76
  return s;
81
77
}
82
78
 
83
 
 
84
79
IMPLEMENT_TYPE_P (Duration, "ly:duration?");
85
80
 
86
81
SCM
93
88
int
94
89
Duration::print_smob (SCM s, SCM port, scm_print_state *)
95
90
{
96
 
  Duration  *r = (Duration *) ly_cdr (s);
 
91
  Duration *r = (Duration *) SCM_CELL_WORD_1 (s);
97
92
 
98
93
  scm_puts ("#<Duration ", port);
99
 
  scm_display (scm_makfrom0str (r->to_string ().to_str0 ()), port);
 
94
  scm_display (scm_makfrom0str (r->to_string ().c_str ()), port);
100
95
  scm_puts (" >", port);
101
96
 
102
97
  return 1;
103
98
}
104
99
 
105
100
SCM
106
 
Duration::equal_p (SCM a , SCM b)
 
101
Duration::equal_p (SCM a, SCM b)
107
102
{
108
 
  Duration  *p = (Duration *) ly_cdr (a);
109
 
  Duration  *q = (Duration *) ly_cdr (b);
 
103
  Duration *p = (Duration *) SCM_CELL_WORD_1 (a);
 
104
  Duration *q = (Duration *) SCM_CELL_WORD_1 (b);
110
105
 
111
106
  bool eq = p->dots_ == q->dots_
112
107
    && p->durlog_ == q->durlog_
115
110
  return eq ? SCM_BOOL_T : SCM_BOOL_F;
116
111
}
117
112
 
118
 
MAKE_SCHEME_CALLBACK (Duration, less_p, 2);
119
 
SCM
120
 
Duration::less_p (SCM p1, SCM p2)
121
 
{
122
 
  Duration *a = unsmob_duration (p1);
123
 
  Duration *b = unsmob_duration (p2);
124
 
 
125
 
  if (compare (*a, *b) < 0)
126
 
    return SCM_BOOL_T;
127
 
  else
128
 
    return SCM_BOOL_F;
129
 
}
130
 
 
131
 
LY_DEFINE (duration_less, "ly:duration<?",
132
 
           2, 0, 0, (SCM p1, SCM p2),
133
 
          "Is @var{p1} shorter than @var{p2}?")
134
 
{
135
 
  Duration *a = unsmob_duration (p1);
136
 
  Duration *b = unsmob_duration (p2);
137
 
 
138
 
  SCM_ASSERT_TYPE (a, p1, SCM_ARG1, __FUNCTION__, "Duration");
139
 
  SCM_ASSERT_TYPE (b, p2, SCM_ARG2, __FUNCTION__, "Duration");
140
 
 
141
 
  if (Duration::compare (*a, *b) < 0)
142
 
    return SCM_BOOL_T;
143
 
  else
144
 
    return SCM_BOOL_F;
145
 
}
146
 
 
147
 
LY_DEFINE (make_duration, "ly:make-duration",
148
 
           1, 3, 0, (SCM length, SCM dotcount, SCM num, SCM den),
149
 
           "@var{length} is the negative logarithm (base 2) of the duration:\n"
150
 
           "1 is a half note, 2 is a quarter note, 3 is an eighth\n"
151
 
           "note, etc.  The number of dots after the note is given by\n"
152
 
           "the optional argument @var{dotcount}.\n"
153
 
           "\n"
154
 
           "The duration factor is optionally given by @var{num}\n"
155
 
           "and @var{den}.\n\n"
156
 
           "A duration is a musical duration, "
157
 
           "i.e. a length of time described by a power of two "
158
 
           "(whole, half, quarter, etc.) and a number of augmentation\n"
159
 
           "dots. \n")
160
 
{
161
 
  SCM_ASSERT_TYPE (scm_integer_p (length) == SCM_BOOL_T,
162
 
                   length, SCM_ARG1, __FUNCTION__, "integer");
163
 
 
164
 
  int dots = 0;
165
 
  if (dotcount != SCM_UNDEFINED)
166
 
    {
167
 
      SCM_ASSERT_TYPE (scm_integer_p (dotcount) == SCM_BOOL_T,
168
 
                       dotcount, SCM_ARG2, __FUNCTION__, "integer");
169
 
      dots = gh_scm2int (dotcount);
170
 
    }
171
 
 
172
 
  bool compress = false;
173
 
  if (num != SCM_UNDEFINED)
174
 
    {
175
 
      SCM_ASSERT_TYPE (gh_number_p (num), length, SCM_ARG3, __FUNCTION__, "integer");
176
 
      compress = true;
177
 
    }
178
 
  else
179
 
    num = gh_int2scm (1);
180
 
 
181
 
  if (den != SCM_UNDEFINED)
182
 
    {
183
 
      SCM_ASSERT_TYPE (gh_number_p (den), length, SCM_ARG4, __FUNCTION__, "integer");
184
 
      compress = true;
185
 
    }
186
 
  else
187
 
    den = gh_int2scm (1);
188
 
 
189
 
  Duration p (gh_scm2int (length), dots);
190
 
  if (compress)
191
 
    p = p.compressed (Rational (gh_scm2int (num), gh_scm2int (den)));
192
 
 
193
 
  return p.smobbed_copy ();
194
 
}
195
 
 
196
 
LY_DEFINE (duration_log, "ly:duration-log",
197
 
           1, 0, 0, (SCM dur),
198
 
          "Extract the duration log from @var{dur}")
199
 
{
200
 
  SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
201
 
  return gh_int2scm (unsmob_duration (dur)->duration_log ());
202
 
}
203
 
 
204
 
LY_DEFINE (dot_count_log, "ly:duration-dot-count", 1, 0, 0, (SCM dur),
205
 
          "Extract the dot count from @var{dur}"
206
 
)
207
 
{
208
 
  SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
209
 
  return gh_int2scm (unsmob_duration (dur)->dot_count ());
210
 
}
211
 
 
212
 
 
213
 
LY_DEFINE (ly_intlog2, "ly:intlog2",
214
 
           1, 0, 0, (SCM d),
215
 
          "The 2-logarithm of 1/@var{d}.")
216
 
{
217
 
  SCM_ASSERT_TYPE (gh_number_p (d), d, SCM_ARG1, __FUNCTION__, "integer");
218
 
  int log = intlog2 (gh_scm2int (d));
219
 
  return gh_int2scm (log);
220
 
}
221
 
 
222
 
LY_DEFINE (compression_factor, "ly:duration-factor",
223
 
           1, 0, 0, (SCM dur),
224
 
          "Extract the compression factor from @var{dur}. Return as a pair.")
225
 
{
226
 
  SCM_ASSERT_TYPE (unsmob_duration (dur), dur, SCM_ARG1, __FUNCTION__, "duration");
227
 
  Rational r = unsmob_duration (dur)->factor ();
228
 
  return gh_cons (gh_int2scm (r.num ()), gh_int2scm (r.den ()));
229
 
}
230
 
 
231
113
int
232
114
Duration::duration_log () const
233
115
{