]> git.cworth.org Git - scherzo/blob - score.c
Add a few initializations to quiet compiler warnings.
[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         /* Initialize to keep the compiler quiet. */
429         int brace_width = 0;
430
431         for (i = 0; i < score->num_braces; i++)
432             _draw_brace (score, cr, score->braces[i], &brace_width);
433
434         /* Subtract space for brace itself */
435         cairo_translate (cr, brace_width, 0);
436         staff_width -= brace_width;
437
438         /* As well as some padding */
439         cairo_translate (cr, 2, 0);
440         staff_width -= 2;
441     }
442
443     /* Vertical lines at each end */
444     cairo_rectangle (cr,
445                      score->line_width / 2.0,
446                      score->staves[0]->y_pos + score->line_width / 2.0,
447                      staff_width - score->line_width,
448                      score->staves[score->num_staves-1]->y_pos + score->staff_height - score->staves[0]->y_pos);
449     cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */
450     cairo_set_line_width (cr, score->line_width);
451     cairo_stroke (cr);
452
453     for (i = 0; i < score->num_staves; i++) {
454         score_staff_t *staff = score->staves[i];
455         _draw_staff (score, cr, staff, staff_width);
456     }
457
458     cairo_restore (cr);
459 }
460
461 void
462 score_add_brace (score_t *score, int staves)
463 {
464     score_brace_t *brace;
465
466     brace = talloc (score, score_brace_t);
467     if (brace == NULL)
468         return;
469
470     brace->first_staff = score->num_staves;
471     brace->num_staves = staves;
472
473     score->num_braces++;
474     score->braces = talloc_realloc (score,
475                                     score->braces,
476                                     score_brace_t*,
477                                     score->num_braces);
478     if (score->braces == NULL) {
479         score->num_braces = 0;
480         return;
481     }
482
483     score->braces[score->num_braces - 1] = brace;
484
485 }
486
487 score_staff_t *
488 score_add_staff (score_t *score, score_clef_t clef)
489 {
490     score_staff_t *staff;
491
492     staff = talloc (score, score_staff_t);
493     if (staff == NULL)
494         return NULL;
495
496     staff->clef = clef;
497
498     staff->notes = NULL;
499     staff->num_notes = 0;
500
501     staff->chords = NULL;
502     staff->num_chords = 0;
503
504     staff->upper_ledger_lines = 0;
505     staff->lower_ledger_lines = 0;
506
507     score->num_staves++;
508     score->staves = talloc_realloc (score,
509                                     score->staves,
510                                     score_staff_t*,
511                                     score->num_staves);
512     if (score->staves == NULL) {
513         score->num_staves = 0;
514         return NULL;
515     }
516
517     score->staves[score->num_staves - 1] = staff;
518
519     return staff;
520 }
521
522 score_chord_t *
523 score_add_chord (score_staff_t *staff,
524                  const char *name)
525 {
526     score_chord_t *chord;
527
528     chord = talloc (staff, score_chord_t);
529     if (chord == NULL)
530         return NULL;
531
532     talloc_steal (chord, name);
533
534     chord->staff = staff;
535     chord->name = talloc_strdup (chord, name);
536
537     /* The width will get set correctly the first time _draw_chord is
538      * called. */
539     chord->width = 0.0;
540
541     staff->num_chords++;
542     staff->chords = talloc_realloc (staff,
543                                     staff->chords,
544                                     score_chord_t*,
545                                     staff->num_chords);
546     if (staff->chords == NULL) {
547         staff->num_chords = 0;
548         return NULL;
549     }
550
551     staff->chords[staff->num_chords - 1] = chord;
552
553     return chord;
554 }
555
556 void
557 score_remove_chord (score_chord_t *chord)
558 {
559     score_staff_t *staff = chord->staff;
560     int i;
561
562     for (i = 0; i < staff->num_chords; i++)
563         if (staff->chords[i] == chord)
564             break;
565
566     if (i == staff->num_chords)
567         return;
568
569     if (i < staff->num_chords - 1)
570     {
571         memmove (staff->chords + i,
572                  staff->chords + i + 1, 
573                  (staff->num_chords - 1 - i) * sizeof (score_chord_t *));
574     }
575
576     staff->num_chords -= 1;
577 }
578
579 score_note_t *
580 score_add_note (score_staff_t *staff,
581                 score_pitch_t pitch,
582                 int octave,
583                 score_duration_t duration)
584 {
585     score_note_t *note;
586     double line;
587     int i;
588
589     /* Return existing note if already present. */
590     for (i = 0; i < staff->num_notes; i++) {
591         note = staff->notes[i];
592         if (note->pitch == pitch &&
593             note->octave == octave &&
594             note->duration == duration)
595         {
596             return note;
597         }
598     }
599
600     note = talloc (staff, score_note_t);
601     if (note == NULL)
602         return NULL;
603
604     note->staff = staff;
605     note->pitch = pitch;
606     note->octave = octave;
607     note->duration = duration;
608
609     note->color.r = 0.0;
610     note->color.g = 0.0;
611     note->color.b = 0.0;
612
613     line = _score_note_to_line (staff, note);
614     if (line < 0) {
615         int lines = (int) (- line);
616         if (lines > staff->upper_ledger_lines)
617             staff->upper_ledger_lines = lines;
618     } else {
619         int lines = (int) (line - 4);
620         if (lines > staff->lower_ledger_lines)
621             staff->lower_ledger_lines = lines;
622     }
623
624     staff->num_notes++;
625     staff->notes = talloc_realloc (staff,
626                                    staff->notes,
627                                    score_note_t*,
628                                    staff->num_notes);
629     if (staff->notes == NULL) {
630         staff->num_notes = 0;
631         return NULL;
632     }
633
634     staff->notes[staff->num_notes - 1] = note;
635
636     return note;
637 }
638
639 void
640 score_remove_note (score_note_t *note)
641 {
642     score_staff_t *staff = note->staff;
643     int i;
644
645     for (i = 0; i < staff->num_notes; i++)
646         if (staff->notes[i] == note)
647             break;
648
649     if (i == staff->num_notes)
650         return;
651
652     if (i < staff->num_notes - 1)
653     {
654         memmove (staff->notes + i,
655                  staff->notes + i + 1, 
656                  (staff->num_notes - 1 - i) * sizeof (score_note_t *));
657     }
658
659     staff->num_notes -= 1;
660
661     if (staff->num_notes == 0) {
662         staff->upper_ledger_lines = 0;
663         staff->lower_ledger_lines = 0;
664     }
665 }
666
667 void
668 score_set_note_color_rgb (score_note_t *note,
669                           double r,
670                           double g,
671                           double b)
672 {
673     note->color.r = r;
674     note->color.g = g;
675     note->color.b = b;
676 }
677
678 score_note_t *
679 score_staff_find_note (score_staff_t *staff,
680                        score_pitch_t pitch,
681                        int octave,
682                        score_duration_t duration)
683 {
684     int i;
685     score_note_t *note;
686
687     for (i = 0; i < staff->num_notes; i++) {
688         note = staff->notes[i];
689         if (note->pitch == pitch &&
690             note->octave == octave &&
691             note->duration == duration)
692         {
693             return note;
694         }
695     }
696
697     return NULL;
698 }
699