]> git.donarmstrong.com Git - lilypond.git/blob - lily/scale.cc
Merge branch 'master' of ssh+git://hanwen@git.sv.gnu.org/srv/git/lilypond
[lilypond.git] / lily / scale.cc
1 /* 
2   scale.cc -- implement Scale
3   
4   source file of the GNU LilyPond music typesetter
5   
6   (c) 2006--2007 Han-Wen Nienhuys <hanwen@lilypond.org>
7   
8 */
9
10 #include "scale.hh"
11
12 #include "ly-smobs.icc"
13
14 /*
15   todo: put string <-> pitch here too.
16
17 */
18 LY_DEFINE (ly_make_scale, "ly:make-scale",
19            1, 0, 0, (SCM steps),
20            "Create a scale. Takes a vector of ints as argument")
21 {
22   bool type_ok = scm_is_vector (steps);
23
24   vector<Rational> semitones; 
25   if (type_ok)
26     {
27       int len = scm_c_vector_length (steps);
28       for (int i = 0 ; i < len; i++)
29         {
30           SCM step = scm_c_vector_ref (steps, i);
31           type_ok = type_ok && scm_is_rational (step);
32           if (type_ok)
33             {
34               Rational from_c (scm_to_int (scm_numerator (step)),
35                                scm_to_int (scm_denominator (step)));
36               semitones.push_back (from_c);
37             }
38         }
39     }
40   
41   SCM_ASSERT_TYPE (type_ok, steps, SCM_ARG1, __FUNCTION__, "vector of int");
42
43   Scale *s = new Scale;
44   s->step_tones_ = semitones;
45
46   SCM retval =  s->self_scm ();
47
48   s->unprotect ();
49   
50   return retval;
51 }
52
53 LY_DEFINE (ly_default_scale, "ly:default-scale",
54            0, 0, 0, (),
55            "Get the global default scale.")
56 {
57   return default_global_scale
58     ? SCM_BOOL_F
59     : default_global_scale->self_scm ();
60 }
61
62
63 Scale * default_global_scale = 0;
64
65 LY_DEFINE (ly_set_default_scale, "ly:set-default-scale",
66            1, 0, 0, (SCM scale),
67            "Set the global default scale.")
68 {
69   LY_ASSERT_SMOB (Scale, scale, 1);
70
71   Scale *s = Scale::unsmob (scale);
72   if (default_global_scale)
73     default_global_scale->unprotect ();
74   default_global_scale = s;
75   s->protect ();
76   
77   return SCM_UNSPECIFIED;
78 }
79
80
81 int
82 Scale::print_smob (SCM x, SCM port, scm_print_state *)
83 {
84   (void) x;
85   
86   scm_puts ("#<Scale>", port); 
87   return 1;
88 }
89
90
91 SCM
92 Scale::mark_smob (SCM x)
93 {
94   (void) x;
95   return SCM_UNSPECIFIED;
96 }
97
98 Scale::Scale ()
99 {
100   smobify_self ();
101 }
102
103 Scale::Scale (Scale const &src)
104 {
105   step_tones_ = src.step_tones_;
106   smobify_self ();
107 }
108
109
110 Scale::~Scale ()
111 {
112 }
113
114 IMPLEMENT_SMOBS (Scale);
115 IMPLEMENT_DEFAULT_EQUAL_P (Scale);