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