]> git.cworth.org Git - scherzo/blob - pitch.c
Add support for drawing a key signature
[scherzo] / pitch.c
1 /* scherzo - Music notation training
2  *
3  *      pitch.c - Common structures and functions for pitches, etc.
4  *
5  * Copyright © 2013 Carl Worth
6  *
7  * This program is free software: you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation, either version 3 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program.  If not, see http://www.gnu.org/licenses/ .
19  */
20
21 #include "pitch.h"
22
23 const char *
24 pitch_string (pitch_t pitch)
25 {
26     static char double_flat[] = "X𝄫";
27     static char flat[] = "X♭";
28     static char natural[] = "X";
29     static char sharp[] = "X♯";
30     static char double_sharp[] = "X𝄪";
31     char *ret;
32
33     switch (PITCH_ACCIDENTAL (pitch)) {
34     case PITCH_ACCIDENTAL_DOUBLE_FLAT:
35         ret = double_flat;
36         break;
37     case PITCH_ACCIDENTAL_FLAT:
38         ret = flat;
39         break;
40     case PITCH_ACCIDENTAL_NATURAL:
41         ret = natural;
42         break;
43     case PITCH_ACCIDENTAL_SHARP:
44         ret = sharp;
45         break;
46     case PITCH_ACCIDENTAL_DOUBLE_SHARP:
47         ret = double_sharp;
48         break;
49     }
50
51     switch (PITCH_NAME (pitch)) {
52     case PITCH_NAME_C:
53         ret[0] = 'C';
54         break;
55     case PITCH_NAME_D:
56         ret[0] = 'D';
57         break;
58     case PITCH_NAME_E:
59         ret[0] = 'E';
60         break;
61     case PITCH_NAME_F:
62         ret[0] = 'F';
63         break;
64     case PITCH_NAME_G:
65         ret[0] = 'G';
66         break;
67     case PITCH_NAME_A:
68         ret[0] = 'A';
69         break;
70     case PITCH_NAME_B:
71         ret[0] = 'B';
72         break;
73     }
74
75     return ret;
76 }
77
78 pitch_t
79 pitch_raise_by_octaves (pitch_t pitch, int octaves)
80 {
81     int new_octave = PITCH_OCTAVE (pitch) + octaves;
82
83     if (new_octave > 8)
84         new_octave = 8;
85
86     return PITCH (PITCH_NAME (pitch), PITCH_ACCIDENTAL (pitch), new_octave);
87 }
88
89 pitch_t
90 pitch_lower_by_octaves (pitch_t pitch, int octaves)
91 {
92     int new_octave = PITCH_OCTAVE (pitch) - octaves;
93
94     if (new_octave < 0)
95         new_octave = 0;
96
97     return PITCH (PITCH_NAME (pitch), PITCH_ACCIDENTAL (pitch), new_octave);
98 }