]> git.cworth.org Git - scherzo/blobdiff - pitch.c
Add support for drawing a key signature
[scherzo] / pitch.c
diff --git a/pitch.c b/pitch.c
new file mode 100644 (file)
index 0000000..c628a76
--- /dev/null
+++ b/pitch.c
@@ -0,0 +1,98 @@
+/* scherzo - Music notation training
+ *
+ *     pitch.c - Common structures and functions for pitches, etc.
+ *
+ * Copyright © 2013 Carl Worth
+ *
+ * This program is free software: you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation, either version 3 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program.  If not, see http://www.gnu.org/licenses/ .
+ */
+
+#include "pitch.h"
+
+const char *
+pitch_string (pitch_t pitch)
+{
+    static char double_flat[] = "X𝄫";
+    static char flat[] = "X♭";
+    static char natural[] = "X";
+    static char sharp[] = "X♯";
+    static char double_sharp[] = "X𝄪";
+    char *ret;
+
+    switch (PITCH_ACCIDENTAL (pitch)) {
+    case PITCH_ACCIDENTAL_DOUBLE_FLAT:
+       ret = double_flat;
+       break;
+    case PITCH_ACCIDENTAL_FLAT:
+       ret = flat;
+       break;
+    case PITCH_ACCIDENTAL_NATURAL:
+       ret = natural;
+       break;
+    case PITCH_ACCIDENTAL_SHARP:
+       ret = sharp;
+       break;
+    case PITCH_ACCIDENTAL_DOUBLE_SHARP:
+       ret = double_sharp;
+       break;
+    }
+
+    switch (PITCH_NAME (pitch)) {
+    case PITCH_NAME_C:
+       ret[0] = 'C';
+       break;
+    case PITCH_NAME_D:
+       ret[0] = 'D';
+       break;
+    case PITCH_NAME_E:
+       ret[0] = 'E';
+       break;
+    case PITCH_NAME_F:
+       ret[0] = 'F';
+       break;
+    case PITCH_NAME_G:
+       ret[0] = 'G';
+       break;
+    case PITCH_NAME_A:
+       ret[0] = 'A';
+       break;
+    case PITCH_NAME_B:
+       ret[0] = 'B';
+       break;
+    }
+
+    return ret;
+}
+
+pitch_t
+pitch_raise_by_octaves (pitch_t pitch, int octaves)
+{
+    int new_octave = PITCH_OCTAVE (pitch) + octaves;
+
+    if (new_octave > 8)
+       new_octave = 8;
+
+    return PITCH (PITCH_NAME (pitch), PITCH_ACCIDENTAL (pitch), new_octave);
+}
+
+pitch_t
+pitch_lower_by_octaves (pitch_t pitch, int octaves)
+{
+    int new_octave = PITCH_OCTAVE (pitch) - octaves;
+
+    if (new_octave < 0)
+       new_octave = 0;
+
+    return PITCH (PITCH_NAME (pitch), PITCH_ACCIDENTAL (pitch), new_octave);
+}