]> git.cworth.org Git - scherzo/blob - score.c
Integrate some simple mnemon quizzing into scherzo.
[scherzo] / score.c
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 #include <string.h>
22
23 #include "score.h"
24
25 struct score_staff
26 {
27     score_clef_t clef;
28
29     score_note_t **notes;
30     int num_notes;
31 };
32
33 typedef struct score_brace
34 {
35     int first_staff;
36     int num_staves;
37 } score_brace_t;
38
39 struct score
40 {
41     /* Height of a single staff */
42     int staff_height;
43
44     /* Height of one space within a staff */
45     int space_height;
46
47     /* Minimal line width for staff lines */
48     int line_width;
49
50     /* Full width of staff */
51     int width;
52
53     score_brace_t **braces;
54     int num_braces;
55     int brace_width;
56
57     score_staff_t **staves;
58     int num_staves;
59 };
60
61 score_t *
62 score_create (void *ctx)
63 {
64     score_t *score;
65
66     score = talloc (ctx, score_t);
67     if (score == NULL)
68         return NULL;
69
70     /* Also sets space_height and line_width */
71     score_set_staff_height (score, 24);
72
73     /* Just to have some nominal width. */
74     score->width = 800;
75
76     score->braces = NULL;
77     score->num_braces = 0;
78
79     score->staves = NULL;
80     score->num_staves = 0;
81
82     return score;
83 }
84
85 int
86 score_set_staff_height (score_t *score, int height)
87 {
88     score->space_height = (int) height / 4;
89     score->staff_height = score->space_height * 4;
90
91     score->line_width = score->space_height / 10;
92     if (score->line_width == 0)
93         score->line_width = 1;
94
95     return score->staff_height;
96 }
97
98 void
99 score_set_width (score_t *score, int width)
100 {
101     score->width = width;
102 }
103
104 /* Returns in brace_width the width of the brace */
105 static void
106 _draw_brace (score_t *score, cairo_t *cr,
107              score_brace_t *brace, int *brace_width)
108 {
109     cairo_glyph_t brace_glyph;
110     cairo_text_extents_t brace_extents;
111
112     cairo_save (cr);
113
114     cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
115
116     /* XXX: This hard-coded glyph index is pretty ugly. We should
117      * figure out how to lookup the glyph we want, (though, as it
118      * turns out, this brace font pretty much just has numbered glyph
119      * names for different sizes, so it wouldn't be all that different
120      * than just the bare index here). */
121     brace_glyph.index = 300;
122     brace_glyph.x = 0;
123     brace_glyph.y = score->staff_height * (brace->first_staff + (2 * brace->num_staves - 1) / 2.0) + 1;
124
125     /* XXX: This font size (in conjunction with the glyph selection)
126      * is a rough guess at best. We should figure out how the brace
127      * font is intended to be used and actually measure to find the
128      * correctly-sized glyph. */
129     cairo_set_font_size (cr, (score->staff_height * 3) / 3.85);
130
131     cairo_glyph_extents (cr, &brace_glyph, 1, &brace_extents);
132
133     /* Subtract space for brace itself */
134     cairo_translate (cr, -brace_extents.x_bearing, 0);
135
136     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
137     cairo_show_glyphs (cr, &brace_glyph, 1);
138
139     cairo_restore (cr);
140
141     *brace_width = (int) -brace_extents.x_bearing;
142 }
143
144 /* Line containing middle C for the given clef. */
145 static int
146 _score_clef_c_line (score_clef_t clef)
147 {
148     switch (clef)
149     {
150     default:
151     case SCORE_CLEF_G:
152         return 5;
153     case SCORE_CLEF_F:
154         return -1;
155     }
156 }
157
158 static double
159 _score_note_to_line (score_staff_t *staff, score_note_t *note)
160 {
161     score_pitch_name_t name = SCORE_PITCH_NAME (note->pitch);
162     int c_line = _score_clef_c_line (staff->clef);
163
164     return c_line - (name - SCORE_PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4);
165 }
166
167 static void
168 _draw_note (score_t *score, cairo_t *cr,
169             score_staff_t *staff, score_note_t *note)
170 {
171     double line;
172     cairo_glyph_t note_glyph;
173
174     cairo_save (cr);
175
176     /* Which line should the note appear on? Line 0 is the top line of
177      * the staff and increasing downwards. (Negative values indicate a
178      * note on a ledger line above the staff). Values half way between
179      * integers indicate notes appearing on a space between two staff
180      * lines (or ledger lines). */
181     line = _score_note_to_line (staff, note);
182
183     cairo_select_font_face (cr, "Gonville-26", 0, 0);
184     cairo_set_font_size (cr, score->staff_height);
185
186     /* XXX: The hard-coded glyph indices here are very ugly. We should
187      * figure out how to lookup glyphs by name from this font. */
188     switch (note->duration) {
189     case SCORE_DURATION_1:
190         note_glyph.index = 127;
191         break;
192     case SCORE_DURATION_2:
193         note_glyph.index = 85;
194         break;
195     case SCORE_DURATION_4:
196     case SCORE_DURATION_8:
197     case SCORE_DURATION_16:
198     case SCORE_DURATION_32:
199     case SCORE_DURATION_64:
200     case SCORE_DURATION_128:
201     default:
202         note_glyph.index = 84;
203     }
204
205     note_glyph.x = 0;
206     note_glyph.y = score->space_height * line + score->line_width / 2.0;
207
208     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
209     cairo_show_glyphs (cr, &note_glyph, 1);
210
211     cairo_restore (cr);
212 }
213
214 static void
215 _draw_staff (score_t *score, cairo_t *cr,
216              score_staff_t *staff, int staff_width)
217 {
218     int i;
219     cairo_glyph_t clef_glyph;
220
221     cairo_save (cr);
222
223     cairo_select_font_face (cr, "Gonville-26", 0, 0);
224
225     cairo_set_font_size (cr, score->staff_height);
226
227     /* XXX: The hard-coded glyph indices here are very ugly. We should
228      * figure out how to lookup glyphs by name from this font. */
229     switch (staff->clef) {
230     case SCORE_CLEF_G:
231     default:
232         clef_glyph.index = 46;
233         clef_glyph.y = 3 * score->space_height;
234         break;
235     case SCORE_CLEF_F:
236         clef_glyph.index = 45;
237         clef_glyph.y = 1 * score->space_height;
238         break;
239     }
240     clef_glyph.x = 3 * score->line_width;
241     clef_glyph.y += 1;
242
243     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
244     cairo_show_glyphs (cr, &clef_glyph, 1);
245
246     /* Draw staff lines */
247     for (i = 0; i < 5; i++) {
248         cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
249         cairo_rel_line_to (cr, staff_width, 0);
250     }
251
252     cairo_set_line_width (cr, score->line_width);
253
254     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
255     cairo_stroke (cr);
256
257     /* Make space for clef before drawing notes */
258     cairo_translate (cr, (int) (4 * score->space_height), 0);
259
260     /* Draw notes */
261     for (i = 0; i < staff->num_notes; i++) {
262         _draw_note (score, cr, staff, staff->notes[i]);
263         /* Draw all notes concurrent for now (as a chord)
264         cairo_translate (cr, score->space_height * 2.0, 0);
265         */
266     }
267
268     cairo_restore (cr);
269 }
270
271 void
272 score_draw (score_t *score, cairo_t *cr)
273 {
274     int i;
275     int staff_width = score->width;
276
277     cairo_save (cr);
278
279     if (score->num_braces)
280     {
281         int brace_width;
282
283         for (i = 0; i < score->num_braces; i++)
284             _draw_brace (score, cr, score->braces[i], &brace_width);
285
286         /* Subtract space for brace itself */
287         cairo_translate (cr, brace_width, 0);
288         staff_width -= brace_width;
289
290         /* As well as some padding */
291         cairo_translate (cr, 2, 0);
292         staff_width -= 2;
293     }
294
295     /* Vertical lines at each end */
296     cairo_rectangle (cr,
297                      score->line_width / 2.0,
298                      score->line_width / 2.0,
299                      staff_width - score->line_width,
300                      score->staff_height * 3);
301     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
302     cairo_set_line_width (cr, score->line_width);
303     cairo_stroke (cr);
304
305     for (i = 0; i < score->num_staves; i++) {
306         _draw_staff (score, cr, score->staves[i], staff_width);
307         cairo_translate (cr, 0, 2 * score->staff_height);
308     }
309
310     cairo_restore (cr);
311 }
312
313 void
314 score_add_brace (score_t *score, int staves)
315 {
316     score_brace_t *brace;
317
318     brace = talloc (score, score_brace_t);
319     if (brace == NULL)
320         return;
321
322     brace->first_staff = score->num_staves;
323     brace->num_staves = staves;
324
325     score->num_braces++;
326     score->braces = talloc_realloc (score,
327                                     score->braces,
328                                     score_brace_t*,
329                                     score->num_braces);
330     if (score->braces == NULL) {
331         score->num_braces = 0;
332         return;
333     }
334
335     score->braces[score->num_braces - 1] = brace;
336
337 }
338
339 score_staff_t *
340 score_add_staff (score_t *score, score_clef_t clef)
341 {
342     score_staff_t *staff;
343
344     staff = talloc (score, score_staff_t);
345     if (staff == NULL)
346         return NULL;
347
348     staff->clef = clef;
349
350     staff->notes = NULL;
351     staff->num_notes = 0;
352
353     score->num_staves++;
354     score->staves = talloc_realloc (score,
355                                     score->staves,
356                                     score_staff_t*,
357                                     score->num_staves);
358     if (score->staves == NULL) {
359         score->num_staves = 0;
360         return NULL;
361     }
362
363     score->staves[score->num_staves - 1] = staff;
364
365     return staff;
366 }
367
368 score_note_t *
369 score_staff_add_note (score_staff_t *staff,
370                       score_pitch_t pitch,
371                       int octave,
372                       score_duration_t duration)
373 {
374     score_note_t *note;
375
376     note = talloc (staff, score_note_t);
377     if (note == NULL)
378         return NULL;
379
380     note->pitch = pitch;
381     note->octave = octave;
382     note->duration = duration;
383
384     staff->num_notes++;
385     staff->notes = talloc_realloc (staff,
386                                    staff->notes,
387                                    score_note_t*,
388                                    staff->num_notes);
389     if (staff->notes == NULL) {
390         staff->num_notes = 0;
391         return NULL;
392     }
393
394     staff->notes[staff->num_notes - 1] = note;
395
396     return note;
397 }
398
399 void
400 score_staff_remove_note (score_staff_t *staff, score_note_t *note)
401 {
402     int i;
403
404     for (i = 0; i < staff->num_notes; i++)
405         if (staff->notes[i] == note)
406             break;
407
408     if (i == staff->num_notes)
409         return;
410
411     if (i < staff->num_notes - 1)
412     {
413         memmove (staff->notes + i,
414                  staff->notes + i + 1, 
415                  (staff->num_notes - 1 - i) * sizeof (score_note_t *));
416     }
417
418     staff->num_notes -= 1;
419 }
420
421 score_note_t *
422 score_staff_find_note (score_staff_t *staff,
423                        score_pitch_t pitch,
424                        int octave,
425                        score_duration_t duration)
426 {
427     int i;
428     score_note_t *note;
429
430     for (i = 0; i < staff->num_notes; i++) {
431         note = staff->notes[i];
432         if (note->pitch == pitch &&
433             note->octave == octave &&
434             note->duration == duration)
435         {
436             return note;
437         }
438     }
439
440     return NULL;
441 }
442