]> git.donarmstrong.com Git - lilypond.git/blob - lily/scale.cc
Issue 4550 (1/2) Avoid "using namespace std;" in included files
[lilypond.git] / lily / scale.cc
1 /*
2   This file is part of LilyPond, the GNU music typesetter.
3
4   Copyright (C) 2006--2015 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 #include "protected-scm.hh"
24
25 using std::vector;
26
27 /*
28   todo: put string <-> pitch here too.
29 */
30 LY_DEFINE (ly_make_scale, "ly:make-scale",
31            1, 0, 0, (SCM steps),
32            "Create a scale."
33            "  The argument is a vector of rational numbers, each of which"
34            " represents the number of 200 cent tones of a pitch above the"
35            " tonic.")
36 {
37   bool type_ok = scm_is_vector (steps);
38
39   vector<Rational> tones;
40   if (type_ok)
41     {
42       int len = scm_c_vector_length (steps);
43       for (int i = 0; i < len; i++)
44         {
45           SCM step = scm_c_vector_ref (steps, i);
46           type_ok = type_ok && scm_is_rational (step);
47           if (type_ok)
48             {
49               Rational from_c (scm_to_int (scm_numerator (step)),
50                                scm_to_int (scm_denominator (step)));
51               tones.push_back (from_c);
52             }
53         }
54     }
55
56   SCM_ASSERT_TYPE (type_ok, steps, SCM_ARG1, __FUNCTION__, "vector of rational");
57
58   return (new Scale (tones))->unprotect ();
59 }
60
61 Scale *default_global_scale = 0;
62 Protected_scm default_global_scale_scm (SCM_BOOL_F);
63
64 // TODO: This is somewhat fishy: pitches protect their scale via a
65 // mark_smob hook.  But since pitches are of Simple_smob variety, they
66 // are unknown to GUILE unless a smobbed_copy has been created.  So
67 // changing the default scale might cause some existing pitches to
68 // lose their scale's protection.
69
70 LY_DEFINE (ly_default_scale, "ly:default-scale",
71            0, 0, 0, (),
72            "Get the global default scale.")
73 {
74   return default_global_scale_scm;
75 }
76
77 LY_DEFINE (ly_set_default_scale, "ly:set-default-scale",
78            1, 0, 0, (SCM scale),
79            "Set the global default scale.  This determines the tuning of"
80            " pitches with no accidentals or key signatures.  The first"
81            " pitch is C.  Alterations are calculated relative to this"
82            " scale.  The number of pitches in this scale determines the"
83            " number of scale steps that make up an octave.  Usually the"
84            " 7-note major scale.")
85 {
86   LY_ASSERT_SMOB (Scale, scale, 1);
87
88   default_global_scale_scm = scale;
89   default_global_scale = unsmob<Scale> (scale);
90
91   return SCM_UNSPECIFIED;
92 }
93
94 int
95 Scale::step_count () const
96 {
97   return step_tones_.size ();
98 }
99
100 Rational
101 Scale::tones_at_step (int step, int octave) const
102 {
103   int normalized_step = normalize_step (step);
104
105   octave += (step - normalized_step) / step_count ();
106
107   // There are 6 tones in an octave.
108   return step_tones_[normalized_step] + Rational (octave * 6);
109 }
110
111 Rational
112 Scale::step_size (int step) const
113 {
114   int normalized_step = normalize_step (step);
115
116   // Wrap around if we are asked for the final note of the
117   // scale (6 is the number of tones of the octave above the
118   // first note).
119   if (normalized_step + 1 == step_count ())
120     return Rational (6) - step_tones_[normalized_step];
121
122   return step_tones_[normalized_step + 1] - step_tones_[normalized_step];
123 }
124
125 int
126 Scale::normalize_step (int step) const
127 {
128   int ret = step % step_count ();
129   if (ret < 0)
130     ret += step_count ();
131
132   return ret;
133 }
134
135
136
137 Scale::Scale (vector<Rational> const &tones)
138 {
139   step_tones_ = tones;
140
141   smobify_self ();
142 }
143
144 Scale::Scale (Scale const &src)
145   : Smob<Scale> ()
146 {
147   step_tones_ = src.step_tones_;
148   smobify_self ();
149 }
150
151 Scale::~Scale ()
152 {
153 }