]> git.cworth.org Git - scherzo/blob - score.c
Fix the broken SCORE_PITCH_NAME macro to work correctly
[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     /* How many ledger lines are needed for current notes */
33     int upper_ledger_lines;
34     int lower_ledger_lines;
35
36     /* Y position of top full line of staff */
37     int y_pos;
38 };
39
40 typedef struct score_brace
41 {
42     int first_staff;
43     int num_staves;
44 } score_brace_t;
45
46 struct score
47 {
48     /* Nominal height of a single staff (ledger lines may make it larger) */
49     int staff_height;
50
51     /* Height of one space within a staff */
52     int space_height;
53
54     /* Minimal line width for staff lines */
55     int line_width;
56
57     /* Full width of staff */
58     int width;
59
60     score_brace_t **braces;
61     int num_braces;
62     int brace_width;
63
64     score_staff_t **staves;
65     int num_staves;
66 };
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, 76);
79
80     /* Just to have some nominal width. */
81     score->width = 1000;
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     double top, bottom;
119
120     if (brace->num_staves == 0)
121         return;
122
123     cairo_save (cr);
124
125     top = score->staves[brace->first_staff]->y_pos;
126     bottom = score->staves[brace->first_staff + brace->num_staves - 1]->y_pos + score->staff_height;
127
128     cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
129
130     /* XXX: This hard-coded glyph index is pretty ugly. We should
131      * figure out how to lookup the glyph we want, (though, as it
132      * turns out, this brace font pretty much just has numbered glyph
133      * names for different sizes, so it wouldn't be all that different
134      * than just the bare index here). */
135     brace_glyph.index = 300;
136     brace_glyph.x = 0;
137     brace_glyph.y = top + (bottom - top) / 2.0 + score->line_width / 2.0;
138
139     /* XXX: This font size (in conjunction with the glyph selection)
140      * is a rough guess at best. We should figure out how the brace
141      * font is intended to be used and actually measure to find the
142      * correctly-sized glyph. */
143     cairo_set_font_size (cr, (bottom - top) / 3.85);
144
145     cairo_glyph_extents (cr, &brace_glyph, 1, &brace_extents);
146
147     /* Subtract space for brace itself */
148     cairo_translate (cr, -brace_extents.x_bearing, 0);
149
150     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
151     cairo_show_glyphs (cr, &brace_glyph, 1);
152
153     cairo_restore (cr);
154
155     *brace_width = (int) -brace_extents.x_bearing;
156 }
157
158 /* Line containing middle C for the given clef. */
159 static int
160 _score_clef_c_line (score_clef_t clef)
161 {
162     switch (clef)
163     {
164     default:
165     case SCORE_CLEF_G:
166         return 5;
167     case SCORE_CLEF_F:
168         return -1;
169     }
170 }
171
172 static double
173 _score_note_to_line (score_staff_t *staff, score_note_t *note)
174 {
175     score_pitch_name_t name = SCORE_PITCH_NAME (note->pitch);
176     int c_line = _score_clef_c_line (staff->clef);
177
178     return c_line - (name - SCORE_PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4);
179 }
180
181 static void
182 _draw_note (score_t *score, cairo_t *cr,
183             score_staff_t *staff, score_note_t *note)
184 {
185     double line;
186     cairo_glyph_t note_glyph;
187     static double extend_factor = 0.25;
188
189     void _draw_ledger_line (double line, double width) {
190         cairo_move_to (cr,
191                        - width * extend_factor / 2.0,
192                        score->space_height * line + score->line_width / 2.0);
193         cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
194         cairo_stroke (cr);
195     }
196
197     cairo_save (cr);
198
199     /* Which line should the note appear on? Line 0 is the top line of
200      * the staff and increasing downwards. (Negative values indicate a
201      * note on a ledger line above the staff). Values half way between
202      * integers indicate notes appearing on a space between two staff
203      * lines (or ledger lines). */
204     line = _score_note_to_line (staff, note);
205
206     cairo_select_font_face (cr, "Gonville-26", 0, 0);
207     cairo_set_font_size (cr, score->staff_height);
208
209     /* XXX: The hard-coded glyph indices here are very ugly. We should
210      * figure out how to lookup glyphs by name from this font. */
211     switch (note->duration) {
212     case SCORE_DURATION_1:
213         note_glyph.index = 127;
214         break;
215     case SCORE_DURATION_2:
216         note_glyph.index = 85;
217         break;
218     case SCORE_DURATION_4:
219     case SCORE_DURATION_8:
220     case SCORE_DURATION_16:
221     case SCORE_DURATION_32:
222     case SCORE_DURATION_64:
223     case SCORE_DURATION_128:
224     default:
225         note_glyph.index = 84;
226     }
227
228     note_glyph.x = 0;
229     note_glyph.y = score->space_height * line;
230
231     if (line < 0 || line > 4) {
232         int i;
233         cairo_text_extents_t note_extents;
234
235         cairo_glyph_extents (cr, &note_glyph, 1, &note_extents);
236
237         if (line < 0) {
238             for (i = -1; i >= line; i--)
239                 _draw_ledger_line (i, note_extents.width);
240         } else {
241             for (i = 5; i <= line; i++)
242                 _draw_ledger_line (i, note_extents.width);
243         }
244     }
245
246     cairo_set_source_rgb (cr,
247                           note->color.r,
248                           note->color.g,
249                           note->color.b);
250     cairo_show_glyphs (cr, &note_glyph, 1);
251
252     cairo_restore (cr);
253 }
254
255 static void
256 _draw_staff (score_t *score, cairo_t *cr,
257              score_staff_t *staff, int staff_width)
258 {
259     int i;
260     cairo_glyph_t clef_glyph;
261
262     cairo_save (cr);
263
264     cairo_translate (cr, 0, staff->y_pos);
265
266     cairo_select_font_face (cr, "Gonville-26", 0, 0);
267
268     cairo_set_font_size (cr, score->staff_height);
269
270     /* XXX: The hard-coded glyph indices here are very ugly. We should
271      * figure out how to lookup glyphs by name from this font. */
272     switch (staff->clef) {
273     case SCORE_CLEF_G:
274     default:
275         clef_glyph.index = 46;
276         clef_glyph.y = 3 * score->space_height;
277         break;
278     case SCORE_CLEF_F:
279         clef_glyph.index = 45;
280         clef_glyph.y = 1 * score->space_height;
281         break;
282     }
283     clef_glyph.x = 3 * score->line_width;
284     clef_glyph.y += score->line_width / 2.0;
285
286     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
287     cairo_show_glyphs (cr, &clef_glyph, 1);
288
289     /* Draw staff lines */
290     for (i = 0; i < 5; i++) {
291         cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
292         cairo_rel_line_to (cr, staff_width, 0);
293     }
294
295     cairo_set_line_width (cr, score->line_width);
296
297     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
298     cairo_stroke (cr);
299
300     /* Make space for clef before drawing notes */
301     cairo_translate (cr, (int) (4 * score->space_height), 0);
302
303     /* Draw notes */
304     for (i = 0; i < staff->num_notes; i++) {
305         _draw_note (score, cr, staff, staff->notes[i]);
306         /* Draw all notes concurrent for now (as a chord)
307         cairo_translate (cr, score->space_height * 2.0, 0);
308         */
309     }
310
311     cairo_restore (cr);
312 }
313
314 void
315 score_draw (score_t *score, cairo_t *cr)
316 {
317     int i;
318     int staff_width = score->width;
319     int staff_y_pos;
320
321     if (score->num_staves == 0)
322         return;
323
324     cairo_save (cr);
325
326     /* Before drawing anything, position each staff based on the size
327      * of each (including ledger lines) */
328     staff_y_pos = 0;
329     for (i = 0; i < score->num_staves; i++) {
330         score_staff_t *staff = score->staves[i];
331         staff_y_pos += staff->upper_ledger_lines * score->space_height;
332         staff->y_pos = staff_y_pos;
333         staff_y_pos += (score->staff_height +
334                         staff->lower_ledger_lines * score->space_height +
335                         score->staff_height);
336     }
337
338     if (score->num_braces)
339     {
340         int brace_width;
341
342         for (i = 0; i < score->num_braces; i++)
343             _draw_brace (score, cr, score->braces[i], &brace_width);
344
345         /* Subtract space for brace itself */
346         cairo_translate (cr, brace_width, 0);
347         staff_width -= brace_width;
348
349         /* As well as some padding */
350         cairo_translate (cr, 2, 0);
351         staff_width -= 2;
352     }
353
354     /* Vertical lines at each end */
355     cairo_rectangle (cr,
356                      score->line_width / 2.0,
357                      score->staves[0]->y_pos + score->line_width / 2.0,
358                      staff_width - score->line_width,
359                      score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
360     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
361     cairo_set_line_width (cr, score->line_width);
362     cairo_stroke (cr);
363
364     for (i = 0; i < score->num_staves; i++) {
365         score_staff_t *staff = score->staves[i];
366         _draw_staff (score, cr, staff, staff_width);
367     }
368
369     cairo_restore (cr);
370 }
371
372 void
373 score_add_brace (score_t *score, int staves)
374 {
375     score_brace_t *brace;
376
377     brace = talloc (score, score_brace_t);
378     if (brace == NULL)
379         return;
380
381     brace->first_staff = score->num_staves;
382     brace->num_staves = staves;
383
384     score->num_braces++;
385     score->braces = talloc_realloc (score,
386                                     score->braces,
387                                     score_brace_t*,
388                                     score->num_braces);
389     if (score->braces == NULL) {
390         score->num_braces = 0;
391         return;
392     }
393
394     score->braces[score->num_braces - 1] = brace;
395
396 }
397
398 score_staff_t *
399 score_add_staff (score_t *score, score_clef_t clef)
400 {
401     score_staff_t *staff;
402
403     staff = talloc (score, score_staff_t);
404     if (staff == NULL)
405         return NULL;
406
407     staff->clef = clef;
408
409     staff->notes = NULL;
410     staff->num_notes = 0;
411
412     staff->upper_ledger_lines = 0;
413     staff->lower_ledger_lines = 0;
414
415     score->num_staves++;
416     score->staves = talloc_realloc (score,
417                                     score->staves,
418                                     score_staff_t*,
419                                     score->num_staves);
420     if (score->staves == NULL) {
421         score->num_staves = 0;
422         return NULL;
423     }
424
425     score->staves[score->num_staves - 1] = staff;
426
427     return staff;
428 }
429
430 score_note_t *
431 score_add_note (score_staff_t *staff,
432                 score_pitch_t pitch,
433                 int octave,
434                 score_duration_t duration)
435 {
436     score_note_t *note;
437     double line;
438
439     note = talloc (staff, score_note_t);
440     if (note == NULL)
441         return NULL;
442
443     note->staff = staff;
444     note->pitch = pitch;
445     note->octave = octave;
446     note->duration = duration;
447
448     note->color.r = 0.0;
449     note->color.g = 0.0;
450     note->color.b = 0.0;
451
452     line = _score_note_to_line (staff, note);
453     if (line < 0) {
454         int lines = (int) (- line);
455         if (lines > staff->upper_ledger_lines)
456             staff->upper_ledger_lines = lines;
457     } else {
458         int lines = (int) (line - 4);
459         if (lines > staff->lower_ledger_lines)
460             staff->lower_ledger_lines = lines;
461     }
462
463     staff->num_notes++;
464     staff->notes = talloc_realloc (staff,
465                                    staff->notes,
466                                    score_note_t*,
467                                    staff->num_notes);
468     if (staff->notes == NULL) {
469         staff->num_notes = 0;
470         return NULL;
471     }
472
473     staff->notes[staff->num_notes - 1] = note;
474
475     return note;
476 }
477
478 void
479 score_remove_note (score_note_t *note)
480 {
481     score_staff_t *staff = note->staff;
482     int i;
483
484     for (i = 0; i < staff->num_notes; i++)
485         if (staff->notes[i] == note)
486             break;
487
488     if (i == staff->num_notes)
489         return;
490
491     if (i < staff->num_notes - 1)
492     {
493         memmove (staff->notes + i,
494                  staff->notes + i + 1, 
495                  (staff->num_notes - 1 - i) * sizeof (score_note_t *));
496     }
497
498     staff->num_notes -= 1;
499
500     if (staff->num_notes == 0) {
501         staff->upper_ledger_lines = 0;
502         staff->lower_ledger_lines = 0;
503     }
504 }
505
506 void
507 score_set_note_color_rgb (score_note_t *note,
508                           double r,
509                           double g,
510                           double b)
511 {
512     note->color.r = r;
513     note->color.g = g;
514     note->color.b = b;
515 }
516
517 score_note_t *
518 score_staff_find_note (score_staff_t *staff,
519                        score_pitch_t pitch,
520                        int octave,
521                        score_duration_t duration)
522 {
523     int i;
524     score_note_t *note;
525
526     for (i = 0; i < staff->num_notes; i++) {
527         note = staff->notes[i];
528         if (note->pitch == pitch &&
529             note->octave == octave &&
530             note->duration == duration)
531         {
532             return note;
533         }
534     }
535
536     return NULL;
537 }
538