X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=score.c;h=0c9d5f37cb37e20d9913e0601ef4b66246ccf777;hb=4000561216687a173abf597a0fd26d2c336954fc;hp=fb54b95ea4c6c72575ee30f971dde29252324c12;hpb=7e2ab4eb3ab1fc670d9a0150996d682e7f55f183;p=scherzo diff --git a/score.c b/score.c index fb54b95..0c9d5f3 100644 --- a/score.c +++ b/score.c @@ -18,6 +18,8 @@ * along with this program. If not, see http://www.gnu.org/licenses/ . */ +#include + #include "score.h" struct score_staff @@ -56,13 +58,6 @@ struct score int num_staves; }; -typedef struct score_note -{ - score_pitch_t pitch; - int octave; - score_duration_t duration; -} score_note_t; - score_t * score_create (void *ctx) { @@ -146,12 +141,82 @@ _draw_brace (score_t *score, cairo_t *cr, *brace_width = (int) -brace_extents.x_bearing; } +/* Line containing middle C for the given clef. */ +static int +_score_clef_c_line (score_clef_t clef) +{ + switch (clef) + { + default: + case SCORE_CLEF_G: + return 5; + case SCORE_CLEF_F: + return -1; + } +} + +static double +_score_note_to_line (score_staff_t *staff, score_note_t *note) +{ + score_pitch_name_t name = SCORE_PITCH_NAME (note->pitch); + int c_line = _score_clef_c_line (staff->clef); + + return c_line - (name - SCORE_PITCH_NAME_C) / 2.0 - 3.5 * (note->octave - 4); +} + +static void +_draw_note (score_t *score, cairo_t *cr, + score_staff_t *staff, score_note_t *note) +{ + double line; + cairo_glyph_t note_glyph; + + cairo_save (cr); + + /* Which line should the note appear on? Line 0 is the top line of + * the staff and increasing downwards. (Negative values indicate a + * note on a ledger line above the staff). Values half way between + * integers indicate notes appearing on a space between two staff + * lines (or ledger lines). */ + line = _score_note_to_line (staff, note); + + cairo_select_font_face (cr, "Gonville-26", 0, 0); + cairo_set_font_size (cr, score->staff_height); + + /* XXX: The hard-coded glyph indices here are very ugly. We should + * figure out how to lookup glyphs by name from this font. */ + switch (note->duration) { + case SCORE_DURATION_1: + note_glyph.index = 127; + break; + case SCORE_DURATION_2: + note_glyph.index = 85; + break; + case SCORE_DURATION_4: + case SCORE_DURATION_8: + case SCORE_DURATION_16: + case SCORE_DURATION_32: + case SCORE_DURATION_64: + case SCORE_DURATION_128: + default: + note_glyph.index = 84; + } + + note_glyph.x = 0; + note_glyph.y = score->space_height * line + score->line_width / 2.0; + + cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */ + cairo_show_glyphs (cr, ¬e_glyph, 1); + + cairo_restore (cr); +} + static void _draw_staff (score_t *score, cairo_t *cr, score_staff_t *staff, int staff_width) { int i; - cairo_glyph_t glyph; + cairo_glyph_t clef_glyph; cairo_save (cr); @@ -164,27 +229,22 @@ _draw_staff (score_t *score, cairo_t *cr, switch (staff->clef) { case SCORE_CLEF_G: default: - glyph.index = 46; - glyph.y = 3 * score->space_height; + clef_glyph.index = 46; + clef_glyph.y = 3 * score->space_height; break; case SCORE_CLEF_F: - glyph.index = 45; - glyph.y = 1 * score->space_height; + clef_glyph.index = 45; + clef_glyph.y = 1 * score->space_height; break; } - glyph.x = 3 * score->line_width; - glyph.y += 1; + clef_glyph.x = 3 * score->line_width; + clef_glyph.y += 1; cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */ - cairo_show_glyphs (cr, &glyph, 1); + cairo_show_glyphs (cr, &clef_glyph, 1); - cairo_rectangle (cr, - score->line_width / 2.0, - score->line_width / 2.0, - staff_width - score->line_width, - score->space_height * 4); - - for (i = 1; i < 4; i++) { + /* Draw staff lines */ + for (i = 0; i < 5; i++) { cairo_move_to (cr, 0, i * score->space_height + score->line_width / 2.0); cairo_rel_line_to (cr, staff_width, 0); } @@ -194,6 +254,17 @@ _draw_staff (score_t *score, cairo_t *cr, cairo_set_source_rgb (cr, 0.0, 0.0, 0.0); /* black */ cairo_stroke (cr); + /* Make space for clef before drawing notes */ + cairo_translate (cr, (int) (4 * score->space_height), 0); + + /* Draw notes */ + for (i = 0; i < staff->num_notes; i++) { + _draw_note (score, cr, staff, staff->notes[i]); + /* Draw all notes concurrent for now (as a chord) + cairo_translate (cr, score->space_height * 2.0, 0); + */ + } + cairo_restore (cr); } @@ -295,10 +366,10 @@ score_add_staff (score_t *score, score_clef_t clef) } score_note_t * -score_staff_add_note (score_staff_t *staff, - score_pitch_t pitch, - int octave, - score_duration_t duration) +score_add_note (score_staff_t *staff, + score_pitch_t pitch, + int octave, + score_duration_t duration) { score_note_t *note; @@ -306,6 +377,7 @@ score_staff_add_note (score_staff_t *staff, if (note == NULL) return NULL; + note->staff = staff; note->pitch = pitch; note->octave = octave; note->duration = duration; @@ -325,3 +397,48 @@ score_staff_add_note (score_staff_t *staff, return note; } +void +score_remove_note (score_note_t *note) +{ + score_staff_t *staff = note->staff; + int i; + + for (i = 0; i < staff->num_notes; i++) + if (staff->notes[i] == note) + break; + + if (i == staff->num_notes) + return; + + if (i < staff->num_notes - 1) + { + memmove (staff->notes + i, + staff->notes + i + 1, + (staff->num_notes - 1 - i) * sizeof (score_note_t *)); + } + + staff->num_notes -= 1; +} + +score_note_t * +score_staff_find_note (score_staff_t *staff, + score_pitch_t pitch, + int octave, + score_duration_t duration) +{ + int i; + score_note_t *note; + + for (i = 0; i < staff->num_notes; i++) { + note = staff->notes[i]; + if (note->pitch == pitch && + note->octave == octave && + note->duration == duration) + { + return note; + } + } + + return NULL; +} +