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