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