]> git.cworth.org Git - scherzo/blob - score.h
Add voicings for 7th chords without 3rd and without 5th
[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 typedef struct score score_t;
28 typedef struct score_staff score_staff_t;
29
30 #define SCORE_PITCH_ACCIDENTAL_MASK 0x07
31 #define SCORE_PITCH_ACCIDENTAL_SHIFT 0
32
33 typedef enum score_pitch_accidental
34 {
35     SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT,
36     SCORE_PITCH_ACCIDENTAL_FLAT,
37     SCORE_PITCH_ACCIDENTAL_NATURAL,
38     SCORE_PITCH_ACCIDENTAL_SHARP,
39     SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP
40 } score_pitch_accidental_t;
41
42 #define SCORE_PITCH_ACCIDENTAL(pitch) (((pitch) & SCORE_PITCH_ACCIDENTAL_MASK) >> SCORE_PITCH_ACCIDENTAL_SHIFT)
43
44 #define SCORE_PITCH_NAME_MASK 0x38
45 #define SCORE_PITCH_NAME_SHIFT 3
46
47 typedef enum score_pitch_name
48 {
49     SCORE_PITCH_NAME_C,
50     SCORE_PITCH_NAME_D,
51     SCORE_PITCH_NAME_E,
52     SCORE_PITCH_NAME_F,
53     SCORE_PITCH_NAME_G,
54     SCORE_PITCH_NAME_A,
55     SCORE_PITCH_NAME_B,
56 } score_pitch_name_t;
57
58 #define SCORE_PITCH_NAME(pitch) (((pitch) & SCORE_PITCH_NAME_MASK) >> SCORE_PITCH_NAME_SHIFT)
59
60 #define SCORE_PITCH(name, accidental) (((name) << SCORE_PITCH_NAME_SHIFT) | (accidental))
61
62 #define SCORE_PITCH_LITERAL(literal_name, literal_accidental) SCORE_PITCH(SCORE_PITCH_NAME_##literal_name, SCORE_PITCH_ACCIDENTAL_##literal_accidental)
63
64 typedef enum score_pitch
65 {
66     SCORE_PITCH_Cff = SCORE_PITCH_LITERAL (C, DOUBLE_FLAT), 
67     SCORE_PITCH_Cf  = SCORE_PITCH_LITERAL (C, FLAT),      
68     SCORE_PITCH_C   = SCORE_PITCH_LITERAL (C, NATURAL),   
69     SCORE_PITCH_Cs  = SCORE_PITCH_LITERAL (C, SHARP),     
70     SCORE_PITCH_Css = SCORE_PITCH_LITERAL (C, DOUBLE_SHARP),
71
72     SCORE_PITCH_Dff = SCORE_PITCH_LITERAL (D, DOUBLE_FLAT), 
73     SCORE_PITCH_Df  = SCORE_PITCH_LITERAL (D, FLAT),      
74     SCORE_PITCH_D   = SCORE_PITCH_LITERAL (D, NATURAL),   
75     SCORE_PITCH_Ds  = SCORE_PITCH_LITERAL (D, SHARP),     
76     SCORE_PITCH_Dss = SCORE_PITCH_LITERAL (D, DOUBLE_SHARP),
77
78     SCORE_PITCH_Eff = SCORE_PITCH_LITERAL (E, DOUBLE_FLAT), 
79     SCORE_PITCH_Ef  = SCORE_PITCH_LITERAL (E, FLAT),      
80     SCORE_PITCH_E   = SCORE_PITCH_LITERAL (E, NATURAL),   
81     SCORE_PITCH_Es  = SCORE_PITCH_LITERAL (E, SHARP),     
82     SCORE_PITCH_Ess = SCORE_PITCH_LITERAL (E, DOUBLE_SHARP),
83
84     SCORE_PITCH_Fff = SCORE_PITCH_LITERAL (F, DOUBLE_FLAT), 
85     SCORE_PITCH_Ff  = SCORE_PITCH_LITERAL (F, FLAT),      
86     SCORE_PITCH_F   = SCORE_PITCH_LITERAL (F, NATURAL),   
87     SCORE_PITCH_Fs  = SCORE_PITCH_LITERAL (F, SHARP),     
88     SCORE_PITCH_Fss = SCORE_PITCH_LITERAL (F, DOUBLE_SHARP),
89
90     SCORE_PITCH_Gff = SCORE_PITCH_LITERAL (G, DOUBLE_FLAT), 
91     SCORE_PITCH_Gf  = SCORE_PITCH_LITERAL (G, FLAT),      
92     SCORE_PITCH_G   = SCORE_PITCH_LITERAL (G, NATURAL),   
93     SCORE_PITCH_Gs  = SCORE_PITCH_LITERAL (G, SHARP),     
94     SCORE_PITCH_Gss = SCORE_PITCH_LITERAL (G, DOUBLE_SHARP),
95
96     SCORE_PITCH_Aff = SCORE_PITCH_LITERAL (A, DOUBLE_FLAT), 
97     SCORE_PITCH_Af  = SCORE_PITCH_LITERAL (A, FLAT),      
98     SCORE_PITCH_A   = SCORE_PITCH_LITERAL (A, NATURAL),   
99     SCORE_PITCH_As  = SCORE_PITCH_LITERAL (A, SHARP),     
100     SCORE_PITCH_Ass = SCORE_PITCH_LITERAL (A, DOUBLE_SHARP),
101
102     SCORE_PITCH_Bff = SCORE_PITCH_LITERAL (B, DOUBLE_FLAT), 
103     SCORE_PITCH_Bf  = SCORE_PITCH_LITERAL (B, FLAT),      
104     SCORE_PITCH_B   = SCORE_PITCH_LITERAL (B, NATURAL),   
105     SCORE_PITCH_Bs  = SCORE_PITCH_LITERAL (B, SHARP),     
106     SCORE_PITCH_Bss = SCORE_PITCH_LITERAL (B, DOUBLE_SHARP)
107 } score_pitch_t;
108
109 typedef enum score_duration
110 {
111     SCORE_DURATION_WHOLE = 1,
112     SCORE_DURATION_1 = 1,
113     SCORE_DURATION_HALF = 2,
114     SCORE_DURATION_2 = 2,
115     SCORE_DURATION_QUARTER = 4,
116     SCORE_DURATION_4 = 4,
117     SCORE_DURATION_EIGHTH = 8,
118     SCORE_DURATION_8 = 8,
119     SCORE_DURATION_SIXTEENTH = 16,
120     SCORE_DURATION_16 = 16,
121     SCORE_DURATION_THIRTYSECOND = 32,
122     SCORE_DURATION_32 = 32,
123     SCORE_DURATION_SIXTYFOURTH = 64,
124     SCORE_DURATION_64 = 64,
125     SCORE_DURATION_ONEHUNDREDTWENTYEIGHTH = 128,
126     SCORE_DURATION_128 = 128
127 } score_duration_t;
128
129 #define SCORE_BUILD_NOTE(pitch, octave, duration) SCORE_PITCH_##pitch, (octave), SCORE_DURATION_##duration
130
131 typedef struct score_note
132 {
133     score_staff_t *staff;
134     score_pitch_t pitch;
135     int octave;
136     score_duration_t duration;
137
138     struct {
139         double r;
140         double g;
141         double b;
142     } color;
143 } score_note_t;
144
145 typedef struct score_chord
146 {
147     score_staff_t *staff;
148
149     char *name;
150     double width;
151 } score_chord_t;
152
153 typedef enum score_clef
154 {
155     SCORE_CLEF_G,
156     SCORE_CLEF_TREBLE = SCORE_CLEF_G,
157     SCORE_CLEF_F,
158     SCORE_CLEF_BASS = SCORE_CLEF_F
159 } score_clef_t;
160
161 /* Allocate a new, empty score object, (with optional ctx as talloc
162  * owner). If ctx is NULL, the caller should call talloc_free on the
163  * score_t* when done with it. Otherwise, the object will be freed
164  * when ctx is freed. */
165 score_t *
166 score_create (void *ctx);
167
168 /* Set an (approximate) staff height. The actual staff height may
169  * differ due to rounding to achieve evenly spaced, sharply rendered
170  * lines. the actual staff height is returned. */
171 int
172 score_set_staff_height (score_t *score, int height);
173
174 /* Set the total width available for drawing the score. */
175 void
176 score_set_width (score_t *score, int width);
177
178 /* Add a brace to the score, connecting the given number of staves.
179  *
180  * The staves to be connected are those that will next be added to the
181  * score. */
182 void
183 score_add_brace (score_t *score, int staves);
184
185 /* Add a new staff to the score */
186 score_staff_t *
187 score_add_staff (score_t *score, score_clef_t clef);
188
189 /* Add a note to a staff of the given pitch, octave, and duration.
190  *
191  * Octave numbers are ISO octave numbers [0:8], (so Octave 4 is from
192  * middle C to the B above middle C).
193  *
194  * Duration values can be symbolic (SCORE_DURATION_WHOLE, _QUARTER,
195  * _EIGHTH, etc.) or numerical as simply the denominator (WHOLE=1,
196  * QUARTER=4, EIGHTH=8, etc.)
197  */
198 score_note_t *
199 score_add_note (score_staff_t *staff,
200                 score_pitch_t pitch,
201                 int octave,
202                 score_duration_t);
203
204 /* Add a chord symbol of 'name' to a staff.
205  *
206  * For now, the chord symbols are free-form names.
207  *
208  * The chord name must be a talloc'ed string, which the returned
209  * score_chord_t will talloc_steal.
210  */
211 score_chord_t *
212 score_add_chord (score_staff_t *staff,
213                  const char * name);
214
215 /* Remove the given chord from its staff. */
216 void
217 score_remove_chord (score_chord_t *chord);
218
219 /* Remove the given note from its staff. */
220 void
221 score_remove_note (score_note_t *note);
222
223 void
224 score_set_note_color_rgb (score_note_t *note,
225                           double r,
226                           double g,
227                           double b);
228
229 /* Return the first note on the given staff with the given pitch,
230  * octave, and durations. Returns NULL if no match is found. */
231 score_note_t *
232 score_staff_find_note (score_staff_t *staff,
233                        score_pitch_t pitch,
234                        int octave,
235                        score_duration_t duration);
236
237 /* Draw the given score_t onto the given cairo_t.
238  *
239  * The caller can call cairo_translate before calling score_draw to
240  * position the result as desired, (and can call cairo_clip to clip it
241  * if desired). */
242 void
243 score_draw (score_t *score, cairo_t *cr);
244
245 #endif