]> git.cworth.org Git - scherzo/blob - score.c
ac0d346522715896fc3c2e478a3a215a2b32619e
[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_chord_t **chords;
30     int num_chords;
31
32     score_note_t **notes;
33     int num_notes;
34
35     /* How many ledger lines are needed for current notes */
36     int upper_ledger_lines;
37     int lower_ledger_lines;
38
39     /* Y position of top full line of staff */
40     int y_pos;
41 };
42
43 typedef struct score_brace
44 {
45     int first_staff;
46     int num_staves;
47 } score_brace_t;
48
49 struct score
50 {
51     /* Nominal height of a single staff (ledger lines may make it larger) */
52     int staff_height;
53
54     /* Height of one space within a staff */
55     int space_height;
56
57     /* Minimal line width for staff lines */
58     int line_width;
59
60     /* Full width of staff */
61     int width;
62
63     score_brace_t **braces;
64     int num_braces;
65     int brace_width;
66
67     score_staff_t **staves;
68     int num_staves;
69 };
70
71 score_t *
72 score_create (void *ctx)
73 {
74     score_t *score;
75
76     score = talloc (ctx, score_t);
77     if (score == NULL)
78         return NULL;
79
80     /* Also sets space_height and line_width */
81     score_set_staff_height (score, 76);
82
83     /* Just to have some nominal width. */
84     score->width = 1000;
85
86     score->braces = NULL;
87     score->num_braces = 0;
88
89     score->staves = NULL;
90     score->num_staves = 0;
91
92     return score;
93 }
94
95 int
96 score_set_staff_height (score_t *score, int height)
97 {
98     score->space_height = (int) height / 4;
99     score->staff_height = score->space_height * 4;
100
101     score->line_width = score->space_height / 10;
102     if (score->line_width == 0)
103         score->line_width = 1;
104
105     return score->staff_height;
106 }
107
108 void
109 score_set_width (score_t *score, int width)
110 {
111     score->width = width;
112 }
113
114 /* Returns in brace_width the width of the brace */
115 static void
116 _draw_brace (score_t *score, cairo_t *cr,
117              score_brace_t *brace, int *brace_width)
118 {
119     cairo_glyph_t brace_glyph;
120     cairo_text_extents_t brace_extents;
121     double top, bottom;
122
123     if (brace->num_staves == 0)
124         return;
125
126     cairo_save (cr);
127
128     top = score->staves[brace->first_staff]->y_pos;
129     bottom = score->staves[brace->first_staff + brace->num_staves - 1]->y_pos + score->staff_height;
130
131     cairo_select_font_face (cr, "Gonville-Brace", 0, 0);
132
133     /* XXX: This hard-coded glyph index is pretty ugly. We should
134      * figure out how to lookup the glyph we want, (though, as it
135      * turns out, this brace font pretty much just has numbered glyph
136      * names for different sizes, so it wouldn't be all that different
137      * than just the bare index here). */
138     brace_glyph.index = 300;
139     brace_glyph.x = 0;
140     brace_glyph.y = top + (bottom - top) / 2.0 + score->line_width / 2.0;
141
142     /* XXX: This font size (in conjunction with the glyph selection)
143      * is a rough guess at best. We should figure out how the brace
144      * font is intended to be used and actually measure to find the
145      * correctly-sized glyph. */
146     cairo_set_font_size (cr, (bottom - top) / 3.85);
147
148     cairo_glyph_extents (cr, &brace_glyph, 1, &brace_extents);
149
150     /* Subtract space for brace itself */
151     cairo_translate (cr, -brace_extents.x_bearing, 0);
152
153     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
154     cairo_show_glyphs (cr, &brace_glyph, 1);
155
156     cairo_restore (cr);
157
158     *brace_width = (int) -brace_extents.x_bearing;
159 }
160
161 /* Line containing middle C for the given clef. */
162 static int
163 _score_clef_c_line (score_clef_t clef)
164 {
165     switch (clef)
166     {
167     default:
168     case SCORE_CLEF_G:
169         return 5;
170     case SCORE_CLEF_F:
171         return -1;
172     }
173 }
174
175 static double
176 _score_note_to_line (score_staff_t *staff, score_note_t *note)
177 {
178     score_pitch_name_t name = SCORE_PITCH_NAME (note->pitch);
179     int c_line = _score_clef_c_line (staff->clef);
180
181     return c_line - (name - SCORE_PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4);
182 }
183
184 /* chord->width is updated as a side effect */
185 static void
186 _draw_chord (score_t *score, cairo_t *cr,
187              score_staff_t *staff, score_chord_t *chord)
188 {
189     cairo_text_extents_t chord_extents;
190     double total_staff_height;
191
192     /* XXX: The staff should manage this height itself. */
193     total_staff_height = (staff->upper_ledger_lines * score->space_height +
194                           score->staff_height +
195                           staff->lower_ledger_lines * score->space_height);
196
197     cairo_save (cr);
198
199     cairo_text_extents (cr, chord->name, &chord_extents);
200
201     cairo_select_font_face (cr, "serif", 0, 0);
202     cairo_set_font_size (cr, score->space_height * 3);
203
204     if (staff->clef == SCORE_CLEF_G)
205         cairo_move_to (cr, 0, - score->space_height * 0.5);
206     else
207         cairo_move_to (cr, 0, score->space_height * 0.5 + total_staff_height + chord_extents.y_bearing);
208
209     cairo_show_text (cr, chord->name);
210
211     chord->width = chord_extents.x_advance;
212
213     cairo_restore (cr);
214 }
215
216 static void
217 _draw_note (score_t *score, cairo_t *cr,
218             score_staff_t *staff, score_note_t *note)
219 {
220     double line;
221     cairo_glyph_t note_glyph;
222     static double extend_factor = 0.25;
223
224     void _draw_ledger_line (double line, double width) {
225         cairo_move_to (cr,
226                        - width * extend_factor / 2.0,
227                        score->space_height * line + score->line_width / 2.0);
228         cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
229         cairo_stroke (cr);
230     }
231
232     cairo_save (cr);
233
234     /* Which line should the note appear on? Line 0 is the top line of
235      * the staff and increasing downwards. (Negative values indicate a
236      * note on a ledger line above the staff). Values half way between
237      * integers indicate notes appearing on a space between two staff
238      * lines (or ledger lines). */
239     line = _score_note_to_line (staff, note);
240
241     cairo_select_font_face (cr, "Gonville-26", 0, 0);
242     cairo_set_font_size (cr, score->staff_height);
243
244     /* XXX: The hard-coded glyph indices here are very ugly. We should
245      * figure out how to lookup glyphs by name from this font. */
246     switch (note->duration) {
247     case SCORE_DURATION_1:
248         note_glyph.index = 127;
249         break;
250     case SCORE_DURATION_2:
251         note_glyph.index = 85;
252         break;
253     case SCORE_DURATION_4:
254     case SCORE_DURATION_8:
255     case SCORE_DURATION_16:
256     case SCORE_DURATION_32:
257     case SCORE_DURATION_64:
258     case SCORE_DURATION_128:
259     default:
260         note_glyph.index = 84;
261     }
262
263     note_glyph.x = 0;
264     note_glyph.y = score->space_height * line;
265
266     if (line < 0 || line > 4) {
267         int i;
268         cairo_text_extents_t note_extents;
269
270         cairo_glyph_extents (cr, &note_glyph, 1, &note_extents);
271
272         if (line < 0) {
273             for (i = -1; i >= line; i--)
274                 _draw_ledger_line (i, note_extents.width);
275         } else {
276             for (i = 5; i <= line; i++)
277                 _draw_ledger_line (i, note_extents.width);
278         }
279     }
280
281     cairo_set_source_rgb (cr,
282                           note->color.r,
283                           note->color.g,
284                           note->color.b);
285     cairo_show_glyphs (cr, &note_glyph, 1);
286
287     cairo_restore (cr);
288 }
289
290 static void
291 _draw_staff (score_t *score, cairo_t *cr,
292              score_staff_t *staff, int staff_width)
293 {
294     int i;
295     cairo_glyph_t clef_glyph;
296
297     cairo_save (cr);
298
299     cairo_translate (cr, 0, staff->y_pos);
300
301     cairo_select_font_face (cr, "Gonville-26", 0, 0);
302
303     cairo_set_font_size (cr, score->staff_height);
304
305     /* XXX: The hard-coded glyph indices here are very ugly. We should
306      * figure out how to lookup glyphs by name from this font. */
307     switch (staff->clef) {
308     case SCORE_CLEF_G:
309     default:
310         clef_glyph.index = 46;
311         clef_glyph.y = 3 * score->space_height;
312         break;
313     case SCORE_CLEF_F:
314         clef_glyph.index = 45;
315         clef_glyph.y = 1 * score->space_height;
316         break;
317     }
318     clef_glyph.x = 3 * score->line_width;
319     clef_glyph.y += score->line_width / 2.0;
320
321     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
322     cairo_show_glyphs (cr, &clef_glyph, 1);
323
324     /* Draw staff lines */
325     for (i = 0; i < 5; i++) {
326         cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
327         cairo_rel_line_to (cr, staff_width, 0);
328     }
329
330     cairo_set_line_width (cr, score->line_width);
331
332     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
333     cairo_stroke (cr);
334
335     /* Make space for clef before drawing notes */
336     cairo_translate (cr, (int) (4 * score->space_height), 0);
337
338     /* Draw chord symbols */
339     cairo_save (cr);
340     {
341         for (i = 0; i < staff->num_chords; i++) {
342             _draw_chord (score, cr, staff, staff->chords[i]);
343             cairo_translate (cr, staff->chords[i]->width, 0.0);
344         }
345     }
346     cairo_restore (cr);
347
348     /* Draw notes */
349     for (i = 0; i < staff->num_notes; i++) {
350         _draw_note (score, cr, staff, staff->notes[i]);
351         /* Draw all notes concurrent for now (as a chord)
352         cairo_translate (cr, score->space_height * 2.0, 0);
353         */
354     }
355
356     cairo_restore (cr);
357 }
358
359 void
360 score_draw (score_t *score, cairo_t *cr)
361 {
362     int i;
363     int staff_width = score->width;
364     int staff_y_pos;
365
366     if (score->num_staves == 0)
367         return;
368
369     cairo_save (cr);
370
371     /* Before drawing anything, position each staff based on the size
372      * of each (including ledger lines) */
373     staff_y_pos = 0;
374     for (i = 0; i < score->num_staves; i++) {
375         score_staff_t *staff = score->staves[i];
376         staff_y_pos += staff->upper_ledger_lines * score->space_height;
377         staff->y_pos = staff_y_pos;
378         staff_y_pos += (score->staff_height +
379                         staff->lower_ledger_lines * score->space_height +
380                         score->staff_height);
381     }
382
383     if (score->num_braces)
384     {
385         int brace_width;
386
387         for (i = 0; i < score->num_braces; i++)
388             _draw_brace (score, cr, score->braces[i], &brace_width);
389
390         /* Subtract space for brace itself */
391         cairo_translate (cr, brace_width, 0);
392         staff_width -= brace_width;
393
394         /* As well as some padding */
395         cairo_translate (cr, 2, 0);
396         staff_width -= 2;
397     }
398
399     /* Vertical lines at each end */
400     cairo_rectangle (cr,
401                      score->line_width / 2.0,
402                      score->staves[0]->y_pos + score->line_width / 2.0,
403                      staff_width - score->line_width,
404                      score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
405     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
406     cairo_set_line_width (cr, score->line_width);
407     cairo_stroke (cr);
408
409     for (i = 0; i < score->num_staves; i++) {
410         score_staff_t *staff = score->staves[i];
411         _draw_staff (score, cr, staff, staff_width);
412     }
413
414     cairo_restore (cr);
415 }
416
417 void
418 score_add_brace (score_t *score, int staves)
419 {
420     score_brace_t *brace;
421
422     brace = talloc (score, score_brace_t);
423     if (brace == NULL)
424         return;
425
426     brace->first_staff = score->num_staves;
427     brace->num_staves = staves;
428
429     score->num_braces++;
430     score->braces = talloc_realloc (score,
431                                     score->braces,
432                                     score_brace_t*,
433                                     score->num_braces);
434     if (score->braces == NULL) {
435         score->num_braces = 0;
436         return;
437     }
438
439     score->braces[score->num_braces - 1] = brace;
440
441 }
442
443 score_staff_t *
444 score_add_staff (score_t *score, score_clef_t clef)
445 {
446     score_staff_t *staff;
447
448     staff = talloc (score, score_staff_t);
449     if (staff == NULL)
450         return NULL;
451
452     staff->clef = clef;
453
454     staff->notes = NULL;
455     staff->num_notes = 0;
456
457     staff->chords = NULL;
458     staff->num_chords = 0;
459
460     staff->upper_ledger_lines = 0;
461     staff->lower_ledger_lines = 0;
462
463     score->num_staves++;
464     score->staves = talloc_realloc (score,
465                                     score->staves,
466                                     score_staff_t*,
467                                     score->num_staves);
468     if (score->staves == NULL) {
469         score->num_staves = 0;
470         return NULL;
471     }
472
473     score->staves[score->num_staves - 1] = staff;
474
475     return staff;
476 }
477
478 score_chord_t *
479 score_add_chord (score_staff_t *staff,
480                  const char *name)
481 {
482     score_chord_t *chord;
483
484     chord = talloc (staff, score_chord_t);
485     if (chord == NULL)
486         return NULL;
487
488     chord->staff = staff;
489     chord->name = talloc_strdup (chord, name);
490
491     /* The width will get set correctly the first time _draw_chord is
492      * called. */
493     chord->width = 0.0;
494
495     staff->num_chords++;
496     staff->chords = talloc_realloc (staff,
497                                     staff->chords,
498                                     score_chord_t*,
499                                     staff->num_chords);
500     if (staff->chords == NULL) {
501         staff->num_chords = 0;
502         return NULL;
503     }
504
505     staff->chords[staff->num_chords - 1] = chord;
506
507     return chord;
508 }
509
510 void
511 score_remove_chord (score_chord_t *chord)
512 {
513     score_staff_t *staff = chord->staff;
514     int i;
515
516     for (i = 0; i < staff->num_chords; i++)
517         if (staff->chords[i] == chord)
518             break;
519
520     if (i == staff->num_chords)
521         return;
522
523     if (i < staff->num_chords - 1)
524     {
525         memmove (staff->chords + i,
526                  staff->chords + i + 1, 
527                  (staff->num_chords - 1 - i) * sizeof (score_chord_t *));
528     }
529
530     staff->num_chords -= 1;
531 }
532
533 score_note_t *
534 score_add_note (score_staff_t *staff,
535                 score_pitch_t pitch,
536                 int octave,
537                 score_duration_t duration)
538 {
539     score_note_t *note;
540     double line;
541     int i;
542
543     /* Return existing note if already present. */
544     for (i = 0; i < staff->num_notes; i++) {
545         note = staff->notes[i];
546         if (note->pitch == pitch &&
547             note->octave == octave &&
548             note->duration == duration)
549         {
550             return note;
551         }
552     }
553
554     note = talloc (staff, score_note_t);
555     if (note == NULL)
556         return NULL;
557
558     note->staff = staff;
559     note->pitch = pitch;
560     note->octave = octave;
561     note->duration = duration;
562
563     note->color.r = 0.0;
564     note->color.g = 0.0;
565     note->color.b = 0.0;
566
567     line = _score_note_to_line (staff, note);
568     if (line < 0) {
569         int lines = (int) (- line);
570         if (lines > staff->upper_ledger_lines)
571             staff->upper_ledger_lines = lines;
572     } else {
573         int lines = (int) (line - 4);
574         if (lines > staff->lower_ledger_lines)
575             staff->lower_ledger_lines = lines;
576     }
577
578     staff->num_notes++;
579     staff->notes = talloc_realloc (staff,
580                                    staff->notes,
581                                    score_note_t*,
582                                    staff->num_notes);
583     if (staff->notes == NULL) {
584         staff->num_notes = 0;
585         return NULL;
586     }
587
588     staff->notes[staff->num_notes - 1] = note;
589
590     return note;
591 }
592
593 void
594 score_remove_note (score_note_t *note)
595 {
596     score_staff_t *staff = note->staff;
597     int i;
598
599     for (i = 0; i < staff->num_notes; i++)
600         if (staff->notes[i] == note)
601             break;
602
603     if (i == staff->num_notes)
604         return;
605
606     if (i < staff->num_notes - 1)
607     {
608         memmove (staff->notes + i,
609                  staff->notes + i + 1, 
610                  (staff->num_notes - 1 - i) * sizeof (score_note_t *));
611     }
612
613     staff->num_notes -= 1;
614
615     if (staff->num_notes == 0) {
616         staff->upper_ledger_lines = 0;
617         staff->lower_ledger_lines = 0;
618     }
619 }
620
621 void
622 score_set_note_color_rgb (score_note_t *note,
623                           double r,
624                           double g,
625                           double b)
626 {
627     note->color.r = r;
628     note->color.g = g;
629     note->color.b = b;
630 }
631
632 score_note_t *
633 score_staff_find_note (score_staff_t *staff,
634                        score_pitch_t pitch,
635                        int octave,
636                        score_duration_t duration)
637 {
638     int i;
639     score_note_t *note;
640
641     for (i = 0; i < staff->num_notes; i++) {
642         note = staff->notes[i];
643         if (note->pitch == pitch &&
644             note->octave == octave &&
645             note->duration == duration)
646         {
647             return note;
648         }
649     }
650
651     return NULL;
652 }
653