]> git.donarmstrong.com Git - lilypond.git/blob - lily/scale.cc
Run grand-replace (issue 3765)
[lilypond.git] / lily / scale.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2014 Han-Wen Nienhuys <hanwen@lilypond.org>
5       2007--2008 Rune Zedeler
6       2008       Joe Neeman <joeneeman@gmail.com>
7
8   LilyPond is free software: you can redistribute it and/or modify
9   it under the terms of the GNU General Public License as published by
10   the Free Software Foundation, either version 3 of the License, or
11   (at your option) any later version.
12
13   LilyPond is distributed in the hope that it will be useful,
14   but WITHOUT ANY WARRANTY; without even the implied warranty of
15   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16   GNU General Public License for more details.
17
18   You should have received a copy of the GNU General Public License
19   along with LilyPond.  If not, see <http://www.gnu.org/licenses/>.
20 */
21
22 #include "scale.hh"
23
24 #include "ly-smobs.icc"
25
26 /*
27   todo: put string <-> pitch here too.
28 */
29 LY_DEFINE (ly_make_scale, "ly:make-scale",
30            1, 0, 0, (SCM steps),
31            "Create a scale."
32            "  The argument is a vector of rational numbers, each of which"
33            " represents the number of 200 cent tones of a pitch above the"
34            " tonic.")
35 {
36   bool type_ok = scm_is_vector (steps);
37
38   vector<Rational> tones;
39   if (type_ok)
40     {
41       int len = scm_c_vector_length (steps);
42       for (int i = 0; i < len; i++)
43         {
44           SCM step = scm_c_vector_ref (steps, i);
45           type_ok = type_ok && scm_is_rational (step);
46           if (type_ok)
47             {
48               Rational from_c (scm_to_int (scm_numerator (step)),
49                                scm_to_int (scm_denominator (step)));
50               tones.push_back (from_c);
51             }
52         }
53     }
54
55   SCM_ASSERT_TYPE (type_ok, steps, SCM_ARG1, __FUNCTION__, "vector of rational");
56
57   Scale *s = new Scale (tones);
58
59   SCM retval = s->self_scm ();
60   s->unprotect ();
61
62   return retval;
63 }
64
65 LY_DEFINE (ly_default_scale, "ly:default-scale",
66            0, 0, 0, (),
67            "Get the global default scale.")
68 {
69   return default_global_scale
70          ? default_global_scale->self_scm ()
71          : SCM_BOOL_F;
72 }
73
74 Scale *default_global_scale = 0;
75
76 LY_DEFINE (ly_set_default_scale, "ly:set-default-scale",
77            1, 0, 0, (SCM scale),
78            "Set the global default scale.  This determines the tuning of"
79            " pitches with no accidentals or key signatures.  The first"
80            " pitch is C.  Alterations are calculated relative to this"
81            " scale.  The number of pitches in this scale determines the"
82            " number of scale steps that make up an octave.  Usually the"
83            " 7-note major scale.")
84 {
85   LY_ASSERT_SMOB (Scale, scale, 1);
86
87   Scale *s = Scale::unsmob (scale);
88   if (default_global_scale)
89     default_global_scale->unprotect ();
90   default_global_scale = s;
91   s->protect ();
92
93   return SCM_UNSPECIFIED;
94 }
95
96 int
97 Scale::step_count () const
98 {
99   return step_tones_.size ();
100 }
101
102 Rational
103 Scale::tones_at_step (int step, int octave) const
104 {
105   int normalized_step = normalize_step (step);
106
107   octave += (step - normalized_step) / step_count ();
108
109   // There are 6 tones in an octave.
110   return step_tones_[normalized_step] + Rational (octave * 6);
111 }
112
113 Rational
114 Scale::step_size (int step) const
115 {
116   int normalized_step = normalize_step (step);
117
118   // Wrap around if we are asked for the final note of the
119   // scale (6 is the number of tones of the octave above the
120   // first note).
121   if (normalized_step + 1 == step_count ())
122     return Rational (6) - step_tones_[normalized_step];
123
124   return step_tones_[normalized_step + 1] - step_tones_[normalized_step];
125 }
126
127 int
128 Scale::normalize_step (int step) const
129 {
130   int ret = step % step_count ();
131   if (ret < 0)
132     ret += step_count ();
133
134   return ret;
135 }
136
137 int
138 Scale::print_smob (SCM /* x */,
139                    SCM port,
140                    scm_print_state *)
141 {
142   scm_puts ("#<Scale>", port);
143   return 1;
144 }
145
146 SCM
147 Scale::mark_smob (SCM)
148 {
149   return SCM_UNSPECIFIED;
150 }
151
152 Scale::Scale (vector<Rational> const &tones)
153 {
154   step_tones_ = tones;
155
156   smobify_self ();
157 }
158
159 Scale::Scale (Scale const &src)
160 {
161   step_tones_ = src.step_tones_;
162   smobify_self ();
163 }
164
165 Scale::~Scale ()
166 {
167 }
168
169 IMPLEMENT_SMOBS (Scale);
170 IMPLEMENT_DEFAULT_EQUAL_P (Scale);