From f6b8f7f23bb8a4a5b30bfde8242578cca90ddacf Mon Sep 17 00:00:00 2001 From: Carl Worth Date: Thu, 22 Sep 2011 23:04:53 -0700 Subject: [PATCH] Start drawing some very rudimentary notes. I'm picking the correct (but hard-coded by glyph index) noteheads out of the Gonville font. But I'm not drawing any stems nor flags yet. Also the spacing between the notes isn't interesting yet, nor do I have measure lines, nor ledger lines. Oh, and the notes I added here for testing aren't even musically interesting either. Anyway, we're still getting started obviously. --- scherzo.c | 17 +++++++- score.c | 107 ++++++++++++++++++++++++++++++++++++++++++------- score.h | 118 +++++++++++++++++++++++++++++++++++------------------- 3 files changed, 184 insertions(+), 58 deletions(-) diff --git a/scherzo.c b/scherzo.c index cccaa20..329884d 100644 --- a/scherzo.c +++ b/scherzo.c @@ -124,8 +124,23 @@ main (int argc, char *argv[]) treble = score_add_staff (scherzo.score, SCORE_CLEF_G); bass = score_add_staff (scherzo.score, SCORE_CLEF_F); + score_staff_add_note (treble, SCORE_BUILD_NOTE (D, 4, WHOLE)); + score_staff_add_note (treble, SCORE_BUILD_NOTE (E, 4, WHOLE)); score_staff_add_note (treble, SCORE_BUILD_NOTE (F, 4, WHOLE)); - score_staff_add_note (bass, SCORE_BUILD_NOTE (F, 3, HALF)); + score_staff_add_note (treble, SCORE_BUILD_NOTE (G, 4, WHOLE)); + score_staff_add_note (treble, SCORE_BUILD_NOTE (A, 4, WHOLE)); + score_staff_add_note (treble, SCORE_BUILD_NOTE (B, 4, WHOLE)); + score_staff_add_note (treble, SCORE_BUILD_NOTE (C, 5, WHOLE)); + score_staff_add_note (treble, SCORE_BUILD_NOTE (D, 5, WHOLE)); + + score_staff_add_note (bass, SCORE_BUILD_NOTE (B, 2, WHOLE)); + score_staff_add_note (bass, SCORE_BUILD_NOTE (C, 3, WHOLE)); + score_staff_add_note (bass, SCORE_BUILD_NOTE (D, 3, WHOLE)); + score_staff_add_note (bass, SCORE_BUILD_NOTE (E, 3, WHOLE)); + score_staff_add_note (bass, SCORE_BUILD_NOTE (F, 3, WHOLE)); + score_staff_add_note (bass, SCORE_BUILD_NOTE (G, 3, WHOLE)); + score_staff_add_note (bass, SCORE_BUILD_NOTE (A, 3, WHOLE)); + score_staff_add_note (bass, SCORE_BUILD_NOTE (B, 3, WHOLE)); window = gtk_window_new (GTK_WINDOW_TOPLEVEL); diff --git a/score.c b/score.c index fb54b95..7a1c231 100644 --- a/score.c +++ b/score.c @@ -146,12 +146,83 @@ _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_text_extents_t clef_extents; cairo_save (cr); @@ -164,27 +235,24 @@ _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++) { + cairo_glyph_extents (cr, &clef_glyph, 1, &clef_extents); + + /* 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 +262,15 @@ _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) (1.5 * clef_extents.width), 0); + + /* Draw notes */ + for (i = 0; i < staff->num_notes; i++) { + _draw_note (score, cr, staff, staff->notes[i]); + cairo_translate (cr, score->space_height * 2.0, 0); + } + cairo_restore (cr); } diff --git a/score.h b/score.h index 25d5583..98dd26f 100644 --- a/score.h +++ b/score.h @@ -28,49 +28,81 @@ typedef struct score score_t; typedef struct score_staff score_staff_t; typedef struct score_note score_note_t; +#define SCORE_PITCH_ACCIDENTAL_MASK 0x07 +#define SCORE_PITCH_ACCIDENTAL_SHIFT 0 + +typedef enum score_pitch_accidental +{ + SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT, + SCORE_PITCH_ACCIDENTAL_FLAT, + SCORE_PITCH_ACCIDENTAL_NATURAL, + SCORE_PITCH_ACCIDENTAL_SHARP, + SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP +} score_pitch_accidental_t; + +#define SCORE_PITCH_ACCIDENTAL(pitch) (((pitch) & SCORE_PITCH_ACCIDENTAL_MASK) >> SCORE_PITCH_ACCIDENTAL_SHIFT) + +#define SCORE_PITCH_NAME_MASK 0x38 +#define SCORE_PITCH_NAME_SHIFT 3 + +typedef enum score_pitch_name +{ + SCORE_PITCH_NAME_C = 0 << SCORE_PITCH_NAME_SHIFT, + SCORE_PITCH_NAME_D = 1 << SCORE_PITCH_NAME_SHIFT, + SCORE_PITCH_NAME_E = 2 << SCORE_PITCH_NAME_SHIFT, + SCORE_PITCH_NAME_F = 3 << SCORE_PITCH_NAME_SHIFT, + SCORE_PITCH_NAME_G = 4 << SCORE_PITCH_NAME_SHIFT, + SCORE_PITCH_NAME_A = 5 << SCORE_PITCH_NAME_SHIFT, + SCORE_PITCH_NAME_B = 6 << SCORE_PITCH_NAME_SHIFT +} score_pitch_name_t; + +#define SCORE_PITCH_NAME(pitch) (((pitch) & SCORE_PITCH_NAME_MASK) >> SCORE_PITCH_NAME_SHIFT) + +#define SCORE_PITCH_VALUE(name, accidental) ((SCORE_PITCH_NAME_##name) + (SCORE_PITCH_ACCIDENTAL_##accidental)) + typedef enum score_pitch { - SCORE_PITCH_Aff, - SCORE_PITCH_Af, - SCORE_PITCH_A, - SCORE_PITCH_As, - SCORE_PITCH_Ass, - - SCORE_PITCH_Bff, - SCORE_PITCH_Bf, - SCORE_PITCH_B, - SCORE_PITCH_Bs, - SCORE_PITCH_Bss, - - SCORE_PITCH_Cff, - SCORE_PITCH_Cf, - SCORE_PITCH_C, - SCORE_PITCH_Cs, - SCORE_PITCH_Css, - - SCORE_PITCH_Dff, - SCORE_PITCH_Df, - SCORE_PITCH_D, - SCORE_PITCH_Ds, - SCORE_PITCH_Dss, - - SCORE_PITCH_Eff, - SCORE_PITCH_Ef, - SCORE_PITCH_E, - SCORE_PITCH_Es, - SCORE_PITCH_Ess, - - SCORE_PITCH_Fff, - SCORE_PITCH_Ff, - SCORE_PITCH_F, - SCORE_PITCH_Fs, - SCORE_PITCH_Fss, - - SCORE_PITCH_Gff, - SCORE_PITCH_Gf, - SCORE_PITCH_G, - SCORE_PITCH_Gs, - SCORE_PITCH_Gss + SCORE_PITCH_Aff = SCORE_PITCH_VALUE (A, DOUBLE_FLAT), + SCORE_PITCH_Af = SCORE_PITCH_VALUE (A, FLAT), + SCORE_PITCH_A = SCORE_PITCH_VALUE (A, NATURAL), + SCORE_PITCH_As = SCORE_PITCH_VALUE (A, SHARP), + SCORE_PITCH_Ass = SCORE_PITCH_VALUE (A, DOUBLE_SHARP), + + SCORE_PITCH_Bff = SCORE_PITCH_VALUE (B, DOUBLE_FLAT), + SCORE_PITCH_Bf = SCORE_PITCH_VALUE (B, FLAT), + SCORE_PITCH_B = SCORE_PITCH_VALUE (B, NATURAL), + SCORE_PITCH_Bs = SCORE_PITCH_VALUE (B, SHARP), + SCORE_PITCH_Bss = SCORE_PITCH_VALUE (B, DOUBLE_SHARP), + + SCORE_PITCH_Cff = SCORE_PITCH_VALUE (C, DOUBLE_FLAT), + SCORE_PITCH_Cf = SCORE_PITCH_VALUE (C, FLAT), + SCORE_PITCH_C = SCORE_PITCH_VALUE (C, NATURAL), + SCORE_PITCH_Cs = SCORE_PITCH_VALUE (C, SHARP), + SCORE_PITCH_Css = SCORE_PITCH_VALUE (C, DOUBLE_SHARP), + + SCORE_PITCH_Dff = SCORE_PITCH_VALUE (D, DOUBLE_FLAT), + SCORE_PITCH_Df = SCORE_PITCH_VALUE (D, FLAT), + SCORE_PITCH_D = SCORE_PITCH_VALUE (D, NATURAL), + SCORE_PITCH_Ds = SCORE_PITCH_VALUE (D, SHARP), + SCORE_PITCH_Dss = SCORE_PITCH_VALUE (D, DOUBLE_SHARP), + + SCORE_PITCH_Eff = SCORE_PITCH_VALUE (E, DOUBLE_FLAT), + SCORE_PITCH_Ef = SCORE_PITCH_VALUE (E, FLAT), + SCORE_PITCH_E = SCORE_PITCH_VALUE (E, NATURAL), + SCORE_PITCH_Es = SCORE_PITCH_VALUE (E, SHARP), + SCORE_PITCH_Ess = SCORE_PITCH_VALUE (E, DOUBLE_SHARP), + + SCORE_PITCH_Fff = SCORE_PITCH_VALUE (F, DOUBLE_FLAT), + SCORE_PITCH_Ff = SCORE_PITCH_VALUE (F, FLAT), + SCORE_PITCH_F = SCORE_PITCH_VALUE (F, NATURAL), + SCORE_PITCH_Fs = SCORE_PITCH_VALUE (F, SHARP), + SCORE_PITCH_Fss = SCORE_PITCH_VALUE (F, DOUBLE_SHARP), + + SCORE_PITCH_Gff = SCORE_PITCH_VALUE (G, DOUBLE_FLAT), + SCORE_PITCH_Gf = SCORE_PITCH_VALUE (G, FLAT), + SCORE_PITCH_G = SCORE_PITCH_VALUE (G, NATURAL), + SCORE_PITCH_Gs = SCORE_PITCH_VALUE (G, SHARP), + SCORE_PITCH_Gss = SCORE_PITCH_VALUE (G, DOUBLE_SHARP), } score_pitch_t; typedef enum score_duration @@ -98,7 +130,9 @@ typedef enum score_duration typedef enum score_clef { SCORE_CLEF_G, - SCORE_CLEF_F + SCORE_CLEF_TREBLE = SCORE_CLEF_G, + SCORE_CLEF_F, + SCORE_CLEF_BASS = SCORE_CLEF_F } score_clef_t; /* Allocate a new, empty score object, (with optional ctx as talloc -- 2.43.0