]> git.donarmstrong.com Git - lilypond.git/blob - lily/misc.cc
85db2468cd3f51116640e95bc62c3433fb31c1b9
[lilypond.git] / lily / misc.cc
1 /*
2   misc.cc -- implement various stuff
3
4   source file of the GNU LilyPond music typesetter
5
6   (c) 1997--2007 Han-Wen Nienhuys <hanwen@xs4all.nl>
7   Jan Nieuwenhuizen <janneke@gnu.org>
8 */
9
10
11 #include "misc.hh"
12
13 /*
14   Return the 2-log, rounded down
15 */
16 int
17 intlog2 (int d)
18 {
19   assert (d);
20   int i = 0;
21   while ((d != 1))
22     {
23       d /= 2;
24       i++;
25     }
26
27   assert (! (d / 2));
28   return i;
29 }
30
31 double
32 log_2 (double x)
33 {
34   return log (x) / log (2.0);
35 }
36
37 Real
38 directed_round (Real f, Direction d)
39 {
40   if (d < 0)
41     return floor (f);
42   else
43     return ceil (f);
44 }
45
46
47
48 /*
49    0 at threshold,  1 at 0, with 1/x falloff.
50  */
51 Real
52 peak_around (Real epsilon,  Real threshold, Real x)
53 {
54   if (x < 0)
55     return 1.0;
56   return max (- epsilon * (x - threshold) / ((x + epsilon) * threshold), 0.0);
57 }
58
59 /*
60   0 at 0,  1 at standard_x, and increasing thereafter. 
61  */
62 Real
63 convex_amplifier (Real standard_x, Real increase_factor, Real x)
64 {
65   return (exp (increase_factor * x / standard_x) - 1.0) / (exp (increase_factor) - 1.0); 
66 }
67
68 string
69 camel_case_to_lisp_identifier (string in)
70 {
71   vector<char> out;
72   
73   /* don't add '-' before first character */
74   out.push_back (char (tolower (in[0])));
75     
76   for (size_t inpos = 1; inpos < in.size (); inpos++)
77     {
78       if (isupper (in[inpos]))
79         out.push_back ('-');
80       out.push_back ( char(tolower (in[inpos])));
81     }
82   
83   string result (&out[0], out.size ());
84   replace_all (&result, '_', '-');
85
86   return result;
87 }
88