]> git.cworth.org Git - scherzo/blob - score.h
Move large portions of score.h to new pitch.h file.
[scherzo] / score.h
1 /* scherzo - Music notation training
2  *
3  *      score - Utilities for drawing (simple) musical scores
4  *
5  * Copyright © 2010 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 #ifndef SCORE_H
22 #define SCORE_H
23
24 #include <talloc.h>
25 #include <cairo.h>
26
27 #include "pitch.h"
28
29 /* Some compatibility naming until the code changes to use pitch_t and
30  * firedns in place of score_pitch_t and friends. */
31 typedef pitch_accidental_t score_pitch_accidental_t;
32 typedef pitch_name_t score_pitch_name_t;
33 typedef pitch_t score_pitch_t;
34
35 #define SCORE_PITCH_ACCIDENTAL(pitch) PITCH_ACCIDENTAL(pitch)
36 #define SCORE_PITCH_NAME(pitch) PITCH_NAME(pitch)
37 #define SCORE_PITCH(pitch, octave) PITCH(pitch, octave)
38
39 #define SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT      PITCH_ACCIDENTAL_DOUBLE_FLAT
40 #define SCORE_PITCH_ACCIDENTAL_FLAT             PITCH_ACCIDENTAL_FLAT
41 #define SCORE_PITCH_ACCIDENTAL_NATURAL          PITCH_ACCIDENTAL_NATURAL
42 #define SCORE_PITCH_ACCIDENTAL_SHARP            PITCH_ACCIDENTAL_SHARP
43 #define SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP     PITCH_ACCIDENTAL_DOUBLE_SHARP
44
45 #define SCORE_PITCH_NAME_C PITCH_NAME_C
46 #define SCORE_PITCH_NAME_D PITCH_NAME_D
47 #define SCORE_PITCH_NAME_E PITCH_NAME_E
48 #define SCORE_PITCH_NAME_F PITCH_NAME_F
49 #define SCORE_PITCH_NAME_G PITCH_NAME_G
50 #define SCORE_PITCH_NAME_A PITCH_NAME_A
51 #define SCORE_PITCH_NAME_B PITCH_NAME_B
52
53 #define SCORE_PITCH_Cff PITCH_Cff
54 #define SCORE_PITCH_Cf PITCH_Cf
55 #define SCORE_PITCH_C PITCH_C
56 #define SCORE_PITCH_Cs PITCH_Cs
57 #define SCORE_PITCH_Css PITCH_Css
58 #define SCORE_PITCH_Dff PITCH_Dff
59 #define SCORE_PITCH_Df PITCH_Df
60 #define SCORE_PITCH_D PITCH_D
61 #define SCORE_PITCH_Ds PITCH_Ds
62 #define SCORE_PITCH_Dss PITCH_Dss
63 #define SCORE_PITCH_Eff PITCH_Eff
64 #define SCORE_PITCH_Ef PITCH_Ef
65 #define SCORE_PITCH_E PITCH_E
66 #define SCORE_PITCH_Es PITCH_Es
67 #define SCORE_PITCH_Ess PITCH_Ess
68 #define SCORE_PITCH_Fff PITCH_Fff
69 #define SCORE_PITCH_Ff PITCH_Ff
70 #define SCORE_PITCH_F PITCH_F
71 #define SCORE_PITCH_Fs PITCH_Fs
72 #define SCORE_PITCH_Fss PITCH_Fss
73 #define SCORE_PITCH_Gff PITCH_Gff
74 #define SCORE_PITCH_Gf PITCH_Gf
75 #define SCORE_PITCH_G PITCH_G
76 #define SCORE_PITCH_Gs PITCH_Gs
77 #define SCORE_PITCH_Gss PITCH_Gss
78 #define SCORE_PITCH_Aff PITCH_Aff
79 #define SCORE_PITCH_Af PITCH_Af
80 #define SCORE_PITCH_A PITCH_A
81 #define SCORE_PITCH_As PITCH_As
82 #define SCORE_PITCH_Ass PITCH_Ass
83 #define SCORE_PITCH_Bff PITCH_Bff
84 #define SCORE_PITCH_Bf PITCH_Bf
85 #define SCORE_PITCH_B PITCH_B
86 #define SCORE_PITCH_Bs PITCH_Bs
87 #define SCORE_PITCH_Bss PITCH_Bss
88
89 typedef enum score_duration
90 {
91     SCORE_DURATION_WHOLE = 1,
92     SCORE_DURATION_1 = 1,
93     SCORE_DURATION_HALF = 2,
94     SCORE_DURATION_2 = 2,
95     SCORE_DURATION_QUARTER = 4,
96     SCORE_DURATION_4 = 4,
97     SCORE_DURATION_EIGHTH = 8,
98     SCORE_DURATION_8 = 8,
99     SCORE_DURATION_SIXTEENTH = 16,
100     SCORE_DURATION_16 = 16,
101     SCORE_DURATION_THIRTYSECOND = 32,
102     SCORE_DURATION_32 = 32,
103     SCORE_DURATION_SIXTYFOURTH = 64,
104     SCORE_DURATION_64 = 64,
105     SCORE_DURATION_ONEHUNDREDTWENTYEIGHTH = 128,
106     SCORE_DURATION_128 = 128
107 } score_duration_t;
108
109 #define SCORE_BUILD_NOTE(pitch, octave, duration) SCORE_PITCH_##pitch, (octave), SCORE_DURATION_##duration
110
111 typedef struct score score_t;
112 typedef struct score_staff score_staff_t;
113
114 typedef struct score_note
115 {
116     score_staff_t *staff;
117     score_pitch_t pitch;
118     int octave;
119     score_duration_t duration;
120
121     struct {
122         double r;
123         double g;
124         double b;
125     } color;
126 } score_note_t;
127
128 typedef struct score_chord
129 {
130     score_staff_t *staff;
131
132     char *name;
133     double width;
134 } score_chord_t;
135
136 typedef enum score_clef
137 {
138     SCORE_CLEF_G,
139     SCORE_CLEF_TREBLE = SCORE_CLEF_G,
140     SCORE_CLEF_F,
141     SCORE_CLEF_BASS = SCORE_CLEF_F
142 } score_clef_t;
143
144 /* Allocate a new, empty score object, (with optional ctx as talloc
145  * owner). If ctx is NULL, the caller should call talloc_free on the
146  * score_t* when done with it. Otherwise, the object will be freed
147  * when ctx is freed. */
148 score_t *
149 score_create (void *ctx);
150
151 /* Set an (approximate) staff height. The actual staff height may
152  * differ due to rounding to achieve evenly spaced, sharply rendered
153  * lines. the actual staff height is returned. */
154 int
155 score_set_staff_height (score_t *score, int height);
156
157 /* Set the total width available for drawing the score. */
158 void
159 score_set_width (score_t *score, int width);
160
161 /* Add a brace to the score, connecting the given number of staves.
162  *
163  * The staves to be connected are those that will next be added to the
164  * score. */
165 void
166 score_add_brace (score_t *score, int staves);
167
168 /* Add a new staff to the score */
169 score_staff_t *
170 score_add_staff (score_t *score, score_clef_t clef);
171
172 /* Add a note to a staff of the given pitch, octave, and duration.
173  *
174  * Octave numbers are ISO octave numbers [0:8], (so Octave 4 is from
175  * middle C to the B above middle C).
176  *
177  * Duration values can be symbolic (SCORE_DURATION_WHOLE, _QUARTER,
178  * _EIGHTH, etc.) or numerical as simply the denominator (WHOLE=1,
179  * QUARTER=4, EIGHTH=8, etc.)
180  */
181 score_note_t *
182 score_add_note (score_staff_t *staff,
183                 score_pitch_t pitch,
184                 int octave,
185                 score_duration_t);
186
187 /* Add a chord symbol of 'name' to a staff.
188  *
189  * For now, the chord symbols are free-form names.
190  *
191  * The chord name must be a talloc'ed string, which the returned
192  * score_chord_t will talloc_steal.
193  */
194 score_chord_t *
195 score_add_chord (score_staff_t *staff,
196                  const char * name);
197
198 /* Remove the given chord from its staff. */
199 void
200 score_remove_chord (score_chord_t *chord);
201
202 /* Remove the given note from its staff. */
203 void
204 score_remove_note (score_note_t *note);
205
206 void
207 score_set_note_color_rgb (score_note_t *note,
208                           double r,
209                           double g,
210                           double b);
211
212 /* Return the first note on the given staff with the given pitch,
213  * octave, and durations. Returns NULL if no match is found. */
214 score_note_t *
215 score_staff_find_note (score_staff_t *staff,
216                        score_pitch_t pitch,
217                        int octave,
218                        score_duration_t duration);
219
220 /* Draw the given score_t onto the given cairo_t.
221  *
222  * The caller can call cairo_translate before calling score_draw to
223  * position the result as desired, (and can call cairo_clip to clip it
224  * if desired). */
225 void
226 score_draw (score_t *score, cairo_t *cr);
227
228 #endif