]> git.cworth.org Git - scherzo/blob - score.c
Allow computer-keyboard input of accidentals, and draw accidentals.
[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[2];
222     static double extend_factor = 0.25;
223     cairo_text_extents_t extents;
224     int num_glyphs = 0;
225
226     void _draw_ledger_line (double line, double offset, double width) {
227         cairo_move_to (cr, offset - extend_factor * width / 2.0,
228                        score->space_height * line + score->line_width / 2.0);
229         cairo_rel_line_to (cr, (1 + extend_factor) * width, 0);
230         cairo_stroke (cr);
231     }
232
233     cairo_save (cr);
234
235     /* Move right so that X==0 is natural position for non-displaced
236      * noteheads.
237      */
238     cairo_translate (cr, score->space_height, 0);
239
240     /* Which line should the note appear on? Line 0 is the top line of
241      * the staff and increasing downwards. (Negative values indicate a
242      * note on a ledger line above the staff). Values half way between
243      * integers indicate notes appearing on a space between two staff
244      * lines (or ledger lines). */
245     line = _score_note_to_line (staff, note);
246
247     cairo_select_font_face (cr, "Gonville-26", 0, 0);
248     cairo_set_font_size (cr, score->staff_height);
249
250     /* XXX: The hard-coded glyph indices here are very ugly. We should
251      * figure out how to lookup glyphs by name from this font. */
252     switch (SCORE_PITCH_ACCIDENTAL (note->pitch)) {
253     case SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT:
254             note_glyph[num_glyphs].index = 77;
255             break;
256     case SCORE_PITCH_ACCIDENTAL_FLAT:
257             note_glyph[num_glyphs].index = 68;
258             break;
259     case SCORE_PITCH_ACCIDENTAL_NATURAL:
260             note_glyph[num_glyphs].index = 101;
261             break;
262     case SCORE_PITCH_ACCIDENTAL_SHARP:
263             note_glyph[num_glyphs].index = 134;
264             break;
265     case SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP:
266             note_glyph[num_glyphs].index = 142;
267             break;
268     }
269
270     if (SCORE_PITCH_ACCIDENTAL (note->pitch) != SCORE_PITCH_ACCIDENTAL_NATURAL)
271     {
272             note_glyph[num_glyphs].x = 0;
273
274             note_glyph[num_glyphs].y = score->space_height * line;
275
276             num_glyphs++;
277
278             cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
279
280 #define ACCIDENTAL_NOTE_SPACING (score->space_height * .15)
281
282             note_glyph[0].x = - (extents.width + ACCIDENTAL_NOTE_SPACING);
283     }
284
285     switch (note->duration) {
286     case SCORE_DURATION_1:
287         note_glyph[num_glyphs].index = 127;
288         break;
289     case SCORE_DURATION_2:
290         note_glyph[num_glyphs].index = 85;
291         break;
292     case SCORE_DURATION_4:
293     case SCORE_DURATION_8:
294     case SCORE_DURATION_16:
295     case SCORE_DURATION_32:
296     case SCORE_DURATION_64:
297     case SCORE_DURATION_128:
298     default:
299         note_glyph[num_glyphs].index = 84;
300     }
301
302     note_glyph[num_glyphs].x = 0;
303     note_glyph[num_glyphs].y = score->space_height * line;
304
305     num_glyphs++;
306
307     if (line < 0 || line > 4) {
308         double offset, width;
309         int i;
310
311         cairo_glyph_extents (cr, note_glyph, num_glyphs, &extents);
312         offset = note_glyph[0].x + extents.x_bearing;
313         width = extents.width;
314
315         if (line < 0) {
316             for (i = -1; i >= line; i--)
317                 _draw_ledger_line (i, offset, width);
318         } else {
319             for (i = 5; i <= line; i++)
320                 _draw_ledger_line (i, offset, width);
321         }
322     }
323
324     cairo_set_source_rgb (cr,
325                           note->color.r,
326                           note->color.g,
327                           note->color.b);
328     cairo_show_glyphs (cr, note_glyph, num_glyphs);
329
330     cairo_restore (cr);
331 }
332
333 static void
334 _draw_staff (score_t *score, cairo_t *cr,
335              score_staff_t *staff, int staff_width)
336 {
337     int i;
338     cairo_glyph_t clef_glyph;
339
340     cairo_save (cr);
341
342     cairo_translate (cr, 0, staff->y_pos);
343
344     cairo_select_font_face (cr, "Gonville-26", 0, 0);
345
346     cairo_set_font_size (cr, score->staff_height);
347
348     /* XXX: The hard-coded glyph indices here are very ugly. We should
349      * figure out how to lookup glyphs by name from this font. */
350     switch (staff->clef) {
351     case SCORE_CLEF_G:
352     default:
353         clef_glyph.index = 46;
354         clef_glyph.y = 3 * score->space_height;
355         break;
356     case SCORE_CLEF_F:
357         clef_glyph.index = 45;
358         clef_glyph.y = 1 * score->space_height;
359         break;
360     }
361     clef_glyph.x = 3 * score->line_width;
362     clef_glyph.y += score->line_width / 2.0;
363
364     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
365     cairo_show_glyphs (cr, &clef_glyph, 1);
366
367     /* Draw staff lines */
368     for (i = 0; i < 5; i++) {
369         cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0);
370         cairo_rel_line_to (cr, staff_width, 0);
371     }
372
373     cairo_set_line_width (cr, score->line_width);
374
375     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
376     cairo_stroke (cr);
377
378     /* Make space for clef before drawing notes */
379     cairo_translate (cr, (int) (4 * score->space_height), 0);
380
381     /* Draw chord symbols */
382     cairo_save (cr);
383     {
384         for (i = 0; i < staff->num_chords; i++) {
385             _draw_chord (score, cr, staff, staff->chords[i]);
386             cairo_translate (cr, staff->chords[i]->width, 0.0);
387         }
388     }
389     cairo_restore (cr);
390
391     /* Draw notes */
392     for (i = 0; i < staff->num_notes; i++) {
393         _draw_note (score, cr, staff, staff->notes[i]);
394         /* Draw all notes concurrent for now (as a chord)
395         cairo_translate (cr, score->space_height * 2.0, 0);
396         */
397     }
398
399     cairo_restore (cr);
400 }
401
402 void
403 score_draw (score_t *score, cairo_t *cr)
404 {
405     int i;
406     int staff_width = score->width;
407     int staff_y_pos;
408
409     if (score->num_staves == 0)
410         return;
411
412     cairo_save (cr);
413
414     /* Before drawing anything, position each staff based on the size
415      * of each (including ledger lines) */
416     staff_y_pos = 0;
417     for (i = 0; i < score->num_staves; i++) {
418         score_staff_t *staff = score->staves[i];
419         staff_y_pos += staff->upper_ledger_lines * score->space_height;
420         staff->y_pos = staff_y_pos;
421         staff_y_pos += (score->staff_height +
422                         staff->lower_ledger_lines * score->space_height +
423                         score->staff_height);
424     }
425
426     if (score->num_braces)
427     {
428         int brace_width;
429
430         for (i = 0; i < score->num_braces; i++)
431             _draw_brace (score, cr, score->braces[i], &brace_width);
432
433         /* Subtract space for brace itself */
434         cairo_translate (cr, brace_width, 0);
435         staff_width -= brace_width;
436
437         /* As well as some padding */
438         cairo_translate (cr, 2, 0);
439         staff_width -= 2;
440     }
441
442     /* Vertical lines at each end */
443     cairo_rectangle (cr,
444                      score->line_width / 2.0,
445                      score->staves[0]->y_pos + score->line_width / 2.0,
446                      staff_width - score->line_width,
447                      score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
448     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
449     cairo_set_line_width (cr, score->line_width);
450     cairo_stroke (cr);
451
452     for (i = 0; i < score->num_staves; i++) {
453         score_staff_t *staff = score->staves[i];
454         _draw_staff (score, cr, staff, staff_width);
455     }
456
457     cairo_restore (cr);
458 }
459
460 void
461 score_add_brace (score_t *score, int staves)
462 {
463     score_brace_t *brace;
464
465     brace = talloc (score, score_brace_t);
466     if (brace == NULL)
467         return;
468
469     brace->first_staff = score->num_staves;
470     brace->num_staves = staves;
471
472     score->num_braces++;
473     score->braces = talloc_realloc (score,
474                                     score->braces,
475                                     score_brace_t*,
476                                     score->num_braces);
477     if (score->braces == NULL) {
478         score->num_braces = 0;
479         return;
480     }
481
482     score->braces[score->num_braces - 1] = brace;
483
484 }
485
486 score_staff_t *
487 score_add_staff (score_t *score, score_clef_t clef)
488 {
489     score_staff_t *staff;
490
491     staff = talloc (score, score_staff_t);
492     if (staff == NULL)
493         return NULL;
494
495     staff->clef = clef;
496
497     staff->notes = NULL;
498     staff->num_notes = 0;
499
500     staff->chords = NULL;
501     staff->num_chords = 0;
502
503     staff->upper_ledger_lines = 0;
504     staff->lower_ledger_lines = 0;
505
506     score->num_staves++;
507     score->staves = talloc_realloc (score,
508                                     score->staves,
509                                     score_staff_t*,
510                                     score->num_staves);
511     if (score->staves == NULL) {
512         score->num_staves = 0;
513         return NULL;
514     }
515
516     score->staves[score->num_staves - 1] = staff;
517
518     return staff;
519 }
520
521 score_chord_t *
522 score_add_chord (score_staff_t *staff,
523                  const char *name)
524 {
525     score_chord_t *chord;
526
527     chord = talloc (staff, score_chord_t);
528     if (chord == NULL)
529         return NULL;
530
531     chord->staff = staff;
532     chord->name = talloc_strdup (chord, name);
533
534     /* The width will get set correctly the first time _draw_chord is
535      * called. */
536     chord->width = 0.0;
537
538     staff->num_chords++;
539     staff->chords = talloc_realloc (staff,
540                                     staff->chords,
541                                     score_chord_t*,
542                                     staff->num_chords);
543     if (staff->chords == NULL) {
544         staff->num_chords = 0;
545         return NULL;
546     }
547
548     staff->chords[staff->num_chords - 1] = chord;
549
550     return chord;
551 }
552
553 void
554 score_remove_chord (score_chord_t *chord)
555 {
556     score_staff_t *staff = chord->staff;
557     int i;
558
559     for (i = 0; i < staff->num_chords; i++)
560         if (staff->chords[i] == chord)
561             break;
562
563     if (i == staff->num_chords)
564         return;
565
566     if (i < staff->num_chords - 1)
567     {
568         memmove (staff->chords + i,
569                  staff->chords + i + 1, 
570                  (staff->num_chords - 1 - i) * sizeof (score_chord_t *));
571     }
572
573     staff->num_chords -= 1;
574 }
575
576 score_note_t *
577 score_add_note (score_staff_t *staff,
578                 score_pitch_t pitch,
579                 int octave,
580                 score_duration_t duration)
581 {
582     score_note_t *note;
583     double line;
584     int i;
585
586     /* Return existing note if already present. */
587     for (i = 0; i < staff->num_notes; i++) {
588         note = staff->notes[i];
589         if (note->pitch == pitch &&
590             note->octave == octave &&
591             note->duration == duration)
592         {
593             return note;
594         }
595     }
596
597     note = talloc (staff, score_note_t);
598     if (note == NULL)
599         return NULL;
600
601     note->staff = staff;
602     note->pitch = pitch;
603     note->octave = octave;
604     note->duration = duration;
605
606     note->color.r = 0.0;
607     note->color.g = 0.0;
608     note->color.b = 0.0;
609
610     line = _score_note_to_line (staff, note);
611     if (line < 0) {
612         int lines = (int) (- line);
613         if (lines > staff->upper_ledger_lines)
614             staff->upper_ledger_lines = lines;
615     } else {
616         int lines = (int) (line - 4);
617         if (lines > staff->lower_ledger_lines)
618             staff->lower_ledger_lines = lines;
619     }
620
621     staff->num_notes++;
622     staff->notes = talloc_realloc (staff,
623                                    staff->notes,
624                                    score_note_t*,
625                                    staff->num_notes);
626     if (staff->notes == NULL) {
627         staff->num_notes = 0;
628         return NULL;
629     }
630
631     staff->notes[staff->num_notes - 1] = note;
632
633     return note;
634 }
635
636 void
637 score_remove_note (score_note_t *note)
638 {
639     score_staff_t *staff = note->staff;
640     int i;
641
642     for (i = 0; i < staff->num_notes; i++)
643         if (staff->notes[i] == note)
644             break;
645
646     if (i == staff->num_notes)
647         return;
648
649     if (i < staff->num_notes - 1)
650     {
651         memmove (staff->notes + i,
652                  staff->notes + i + 1, 
653                  (staff->num_notes - 1 - i) * sizeof (score_note_t *));
654     }
655
656     staff->num_notes -= 1;
657
658     if (staff->num_notes == 0) {
659         staff->upper_ledger_lines = 0;
660         staff->lower_ledger_lines = 0;
661     }
662 }
663
664 void
665 score_set_note_color_rgb (score_note_t *note,
666                           double r,
667                           double g,
668                           double b)
669 {
670     note->color.r = r;
671     note->color.g = g;
672     note->color.b = b;
673 }
674
675 score_note_t *
676 score_staff_find_note (score_staff_t *staff,
677                        score_pitch_t pitch,
678                        int octave,
679                        score_duration_t duration)
680 {
681     int i;
682     score_note_t *note;
683
684     for (i = 0; i < staff->num_notes; i++) {
685         note = staff->notes[i];
686         if (note->pitch == pitch &&
687             note->octave == octave &&
688             note->duration == duration)
689         {
690             return note;
691         }
692     }
693
694     return NULL;
695 }
696