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