2 scale.cc -- implement Scale
4 source file of the GNU LilyPond music typesetter
6 (c) 2006--2009 Han-Wen Nienhuys <hanwen@lilypond.org>
7 2007--2008 Rune Zedeler
8 2008 Joe Neeman <joeneeman@gmail.com>
13 #include "ly-smobs.icc"
16 todo: put string <-> pitch here too.
19 LY_DEFINE (ly_make_scale, "ly:make-scale",
22 " The argument is a vector of rational numbers, each of which"
23 " represents the number of 200 cent tones of a pitch above the"
26 bool type_ok = scm_is_vector (steps);
28 vector<Rational> tones;
31 int len = scm_c_vector_length (steps);
32 for (int i = 0 ; i < len; i++)
34 SCM step = scm_c_vector_ref (steps, i);
35 type_ok = type_ok && scm_is_rational (step);
38 Rational from_c (scm_to_int (scm_numerator (step)),
39 scm_to_int (scm_denominator (step)));
40 tones.push_back (from_c);
46 SCM_ASSERT_TYPE (type_ok, steps, SCM_ARG1, __FUNCTION__, "vector of rational");
48 Scale *s = new Scale (tones);
50 SCM retval = s->self_scm ();
56 LY_DEFINE (ly_default_scale, "ly:default-scale",
58 "Get the global default scale.")
60 return default_global_scale
61 ? default_global_scale->self_scm ()
66 Scale * default_global_scale = 0;
68 LY_DEFINE (ly_set_default_scale, "ly:set-default-scale",
70 "Set the global default scale. This determines the tuning of"
71 " pitches with no accidentals or key signatures. The first"
72 " pitch is C. Alterations are calculated relative to this"
73 " scale. The number of pitches in this scale determines the"
74 " number of scale steps that make up an octave. Usually the"
75 " 7-note major scale.")
77 LY_ASSERT_SMOB (Scale, scale, 1);
79 Scale *s = Scale::unsmob (scale);
80 if (default_global_scale)
81 default_global_scale->unprotect ();
82 default_global_scale = s;
85 return SCM_UNSPECIFIED;
89 Scale::step_count () const
91 return step_tones_.size ();
95 Scale::tones_at_step (int step, int octave) const
97 int normalized_step = normalize_step (step);
99 octave += (step - normalized_step) / step_count ();
101 // There are 6 tones in an octave.
102 return step_tones_[normalized_step] + Rational (octave*6);
106 Scale::step_size (int step) const
108 int normalized_step = normalize_step (step);
110 // Wrap around if we are asked for the final note of the
111 // scale (6 is the number of tones of the octave above the
113 if (normalized_step + 1 == step_count ())
114 return Rational(6) - step_tones_[normalized_step];
116 return step_tones_[normalized_step + 1] - step_tones_[normalized_step];
120 Scale::normalize_step (int step) const
122 int ret = step % step_count ();
124 ret += step_count ();
130 Scale::print_smob (SCM /* x */,
134 scm_puts ("#<Scale>", port);
139 Scale::mark_smob (SCM)
141 return SCM_UNSPECIFIED;
144 Scale::Scale (vector<Rational> const &tones)
151 Scale::Scale (Scale const &src)
153 step_tones_ = src.step_tones_;
162 IMPLEMENT_SMOBS (Scale);
163 IMPLEMENT_DEFAULT_EQUAL_P (Scale);