X-Git-Url: https://git.cworth.org/git?a=blobdiff_plain;f=scherzo.c;h=0192c0c1a4dee0ca60fdc68d2ead86336aeaad24;hb=e77d712bf9f99c3d95f909621ae177b9f8175163;hp=1f2fed753fae1aacefbd706b5cdfb7a5142dd9ad;hpb=41e993a65ab55db1f3297003fe65dd556a15f85f;p=scherzo diff --git a/scherzo.c b/scherzo.c index 1f2fed7..0192c0c 100644 --- a/scherzo.c +++ b/scherzo.c @@ -80,10 +80,10 @@ typedef struct scherzo /* Forward declarations. */ static score_note_t * -scherzo_press_note (scherzo_t *scherzo, pitch_t pitch, int octave); +scherzo_press_note (scherzo_t *scherzo, pitch_t pitch); static void -scherzo_release_note (scherzo_t *scherzo, pitch_t pitch, int octave); +scherzo_release_note (scherzo_t *scherzo, pitch_t pitch); static void scherzo_press_pedal (scherzo_t *scherzo); @@ -148,11 +148,11 @@ on_key_press_event (GtkWidget *widget, scherzo_t *scherzo = user_data; int octave; /* Initialize to keep the compiler quiet. */ - pitch_name_t pitch_name = PITCH_C; + pitch_name_t pitch_name = PITCH_NATURAL (C, 0); pitch_t pitch; if (scherzo->challenge.note) - octave = scherzo->challenge.note->octave; + octave = PITCH_OCTAVE (scherzo->challenge.note->pitch); else octave = scherzo->keyboard_octave; @@ -228,14 +228,14 @@ on_key_press_event (GtkWidget *widget, break; } - pitch = PITCH (pitch_name, scherzo->keyboard_accidental); + pitch = PITCH (pitch_name, scherzo->keyboard_accidental, octave); if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) || (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g)) { score_note_t *note; - note = scherzo_press_note (scherzo, pitch, octave); + note = scherzo_press_note (scherzo, pitch); _judge_note (scherzo, note); gtk_widget_queue_draw (scherzo->window); @@ -259,7 +259,7 @@ on_key_release_event (unused (GtkWidget *widget), pitch_t pitch; if (scherzo->challenge.note) - octave = scherzo->challenge.note->octave; + octave = PITCH_OCTAVE (scherzo->challenge.note->pitch); else octave = scherzo->keyboard_octave; @@ -297,12 +297,12 @@ on_key_release_event (unused (GtkWidget *widget), break; } - pitch = PITCH (pitch_name, scherzo->keyboard_accidental); + pitch = PITCH (pitch_name, scherzo->keyboard_accidental, octave); if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) || (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g)) { - scherzo_release_note (scherzo, pitch, octave); + scherzo_release_note (scherzo, pitch); _score_challenge (scherzo); gtk_widget_queue_draw (scherzo->window); @@ -315,9 +315,9 @@ on_key_release_event (unused (GtkWidget *widget), } static unsigned char -_pitch_and_octave_to_midi (pitch_t pitch, - int octave) +_pitch_to_midi (pitch_t pitch) { + int octave = PITCH_OCTAVE (pitch); unsigned char midi_note = 12 * (octave + 1); switch (PITCH_NAME (pitch)) { @@ -364,51 +364,49 @@ _pitch_and_octave_to_midi (pitch_t pitch, } /* octave can optionally by NULL */ -static void -_midi_to_pitch_and_octave (unsigned char midi_note, - pitch_t *pitch, - int *octave) +static pitch_t +_midi_to_pitch (unsigned char midi_note) { - if (octave) - *octave = midi_note / 12 - 1; + int octave = octave = midi_note / 12 - 1; switch (midi_note % 12) { + default: case 0: - *pitch = PITCH_C; + return PITCH_LITERAL (C, NATURAL, octave); break; case 1: - *pitch = PITCH_Cs; + return PITCH_LITERAL (C, SHARP, octave); break; case 2: - *pitch = PITCH_D; + return PITCH_LITERAL (D, NATURAL, octave); break; case 3: - *pitch = PITCH_Ds; + return PITCH_LITERAL (D, SHARP, octave); break; case 4: - *pitch = PITCH_E; + return PITCH_LITERAL (E, NATURAL, octave); break; case 5: - *pitch = PITCH_F; + return PITCH_LITERAL (F, NATURAL, octave); break; case 6: - *pitch = PITCH_Fs; + return PITCH_LITERAL (F, SHARP, octave); break; case 7: - *pitch = PITCH_G; + return PITCH_LITERAL (G, NATURAL, octave); break; case 8: - *pitch = PITCH_Gs; + return PITCH_LITERAL (G, SHARP, octave); break; case 9: - *pitch = PITCH_A; + return PITCH_LITERAL (A, NATURAL, octave); break; case 10: - *pitch = PITCH_As; + return PITCH_LITERAL (A, SHARP, octave); break; case 11: - *pitch = PITCH_B; + return PITCH_LITERAL (B, NATURAL, octave); break; } } @@ -492,21 +490,16 @@ _modified_degree_to_half_steps (const modified_degree_t *degree) } /* Number of half steps from 'root' up to 'pitch' (that is, counting - * the steps up a chromatic scale). */ + * the steps up a chromatic scale), within the same octave. */ static int _pitch_from_root_in_half_steps (pitch_t pitch, pitch_t root) { - int root_midi = _pitch_and_octave_to_midi (root, 0); - int pitch_midi = _pitch_and_octave_to_midi (pitch, 0); + int root_midi = _pitch_to_midi (root); + int pitch_midi = _pitch_to_midi (pitch); - if (pitch_midi < root_midi) + while (pitch_midi < root_midi) pitch_midi += 12; - if (pitch_midi < root_midi) { - fprintf (stderr, "Internal error: midi values differ by more than expected.\n"); - exit (1); - } - return (pitch_midi - root_midi) % 12; } @@ -560,81 +553,56 @@ _chord_signature_matches (analyzed_note_t *notes, static const char * _pitch_str (pitch_t pitch) { - switch (pitch) { - case PITCH_Cff: - return "C𝄫"; - case PITCH_Cf: - return "C♭"; - case PITCH_C: - return "C"; - case PITCH_Cs: - return "C♯"; - case PITCH_Css: - return "C𝄪"; - case PITCH_Dff: - return "D𝄫"; - case PITCH_Df: - return "D♭"; - case PITCH_D: - return "D"; - case PITCH_Ds: - return "D♯"; - case PITCH_Dss: - return "D𝄪"; - case PITCH_Eff: - return "E𝄫"; - case PITCH_Ef: - return "E♭"; - case PITCH_E: - return "E"; - case PITCH_Es: - return "E♯"; - case PITCH_Ess: - return "E𝄪"; - case PITCH_Fff: - return "F𝄫"; - case PITCH_Ff: - return "F♭"; - case PITCH_F: - return "F"; - case PITCH_Fs: - return "F♯"; - case PITCH_Fss: - return "F𝄪"; - case PITCH_Gff: - return "G𝄫"; - case PITCH_Gf: - return "G♭"; - case PITCH_G: - return "G"; - case PITCH_Gs: - return "G♯"; - case PITCH_Gss: - return "G𝄪"; - case PITCH_Aff: - return "A𝄫"; - case PITCH_Af: - return "A♭"; - case PITCH_A: - return "A"; - case PITCH_As: - return "A♯"; - case PITCH_Ass: - return "A𝄪"; - case PITCH_Bff: - return "B𝄫"; - case PITCH_Bf: - return "B♭"; - case PITCH_B: - return "B"; - case PITCH_Bs: - return "B♯"; - case PITCH_Bss: - return "B𝄪"; + static char double_flat[] = "X𝄫"; + static char flat[] = "X♭"; + static char natural[] = "X"; + static char sharp[] = "X♯"; + static char double_sharp[] = "X𝄪"; + char *ret; + + switch (PITCH_ACCIDENTAL (pitch)) { + case PITCH_ACCIDENTAL_DOUBLE_FLAT: + ret = double_flat; + break; + case PITCH_ACCIDENTAL_FLAT: + ret = flat; + break; + case PITCH_ACCIDENTAL_NATURAL: + ret = natural; + break; + case PITCH_ACCIDENTAL_SHARP: + ret = sharp; + break; + case PITCH_ACCIDENTAL_DOUBLE_SHARP: + ret = double_sharp; + break; } - fprintf (stderr, "Internal error: Unknown pitch %d\n", pitch); - exit (1); + switch (PITCH_NAME(pitch)) { + case PITCH_NAME_C: + ret[0] = 'C'; + break; + case PITCH_NAME_D: + ret[0] = 'D'; + break; + case PITCH_NAME_E: + ret[0] = 'E'; + break; + case PITCH_NAME_F: + ret[0] = 'F'; + break; + case PITCH_NAME_G: + ret[0] = 'G'; + break; + case PITCH_NAME_A: + ret[0] = 'A'; + break; + case PITCH_NAME_B: + ret[0] = 'B'; + break; + } + + return ret; } typedef struct chord_signature @@ -792,6 +760,7 @@ scherzo_adjust_note_to_match_modified_degree (score_note_t *note, { pitch_name_t name = PITCH_NAME (note->pitch); pitch_accidental_t accidental = PITCH_ACCIDENTAL (note->pitch); + int octave = PITCH_OCTAVE (note->pitch); int degree_zero_based = (degree->degree - 1) % 7; int degree_delta; @@ -816,7 +785,7 @@ scherzo_adjust_note_to_match_modified_degree (score_note_t *note, if (degree_delta == -1) { if (name == PITCH_NAME_B) { name = PITCH_NAME_C; - note->octave++; + octave++; } else { name++; } @@ -838,7 +807,7 @@ scherzo_adjust_note_to_match_modified_degree (score_note_t *note, if (degree_delta == +1) { if (name == PITCH_NAME_C) { name = PITCH_NAME_B; - note->octave--; + octave--; } else { name--; } @@ -861,7 +830,7 @@ scherzo_adjust_note_to_match_modified_degree (score_note_t *note, exit (1); } - note->pitch = PITCH (name, accidental); + note->pitch = PITCH (name, accidental, octave); } static void @@ -875,8 +844,8 @@ _spell_chord_by_signature (note_group_t *chord, /* Sanitize root to eliminate things like double-flats, * double-sharps, Cb, E#, etc. */ - root_midi = _pitch_and_octave_to_midi (root, 0); - _midi_to_pitch_and_octave (root_midi, &root, NULL); + root_midi = _pitch_to_midi (root); + root = _midi_to_pitch (root_midi); for (i = 0; i < num_notes; i++) { note_half_steps = _pitch_from_root_in_half_steps (chord->notes[i]->pitch, @@ -931,8 +900,7 @@ scherzo_analyze_chord (scherzo_t *scherzo) for (i = 0; i < num_notes; i++) { score_note_t *note = note_group->notes[i]; notes[i].note = note; - notes[i].midi_pitch = _pitch_and_octave_to_midi (note->pitch, - note->octave); + notes[i].midi_pitch = _pitch_to_midi (note->pitch); /* Relative pitch will be filled in after sorting. */ notes[i].relative_pitch = 0; } @@ -1032,13 +1000,9 @@ note_group_add_note (note_group_t *group, score_note_t *note) int i; /* Do nothing if note is already in group. */ - for (i = 0; i < group->num_notes; i++) { - if (group->notes[i]->pitch == note->pitch && - group->notes[i]->octave == note->octave) - { + for (i = 0; i < group->num_notes; i++) + if (group->notes[i]->pitch == note->pitch) return; - } - } group->num_notes++; @@ -1072,7 +1036,7 @@ note_group_remove_note_at (note_group_t *group, int i) } static score_note_t * -scherzo_press_note (scherzo_t *scherzo, pitch_t pitch, int octave) +scherzo_press_note (scherzo_t *scherzo, pitch_t pitch) { score_staff_t *staff; score_note_t *note; @@ -1080,22 +1044,18 @@ scherzo_press_note (scherzo_t *scherzo, pitch_t pitch, int octave) if (scherzo->challenge.note) { staff = scherzo->challenge.staff; - } else if (octave >= 4) { + } else if (PITCH_OCTAVE (pitch) >= 4) { staff = scherzo->treble; } else { staff = scherzo->bass; } /* Do nothing if this note is already pressed. */ - for (i = 0; i < scherzo->notes_pressed.num_notes; i++) { - if (scherzo->notes_pressed.notes[i]->pitch == pitch && - scherzo->notes_pressed.notes[i]->octave == octave) - { + for (i = 0; i < scherzo->notes_pressed.num_notes; i++) + if (scherzo->notes_pressed.notes[i]->pitch == pitch) return scherzo->notes_pressed.notes[i]; - } - } - note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE); + note = score_add_note (staff, pitch, SCORE_DURATION_WHOLE); note_group_add_note (&scherzo->notes_pressed, note); @@ -1108,17 +1068,17 @@ scherzo_press_note (scherzo_t *scherzo, pitch_t pitch, int octave) } static void -scherzo_release_note (scherzo_t *scherzo, pitch_t pitch, int octave) +scherzo_release_note (scherzo_t *scherzo, pitch_t pitch) { score_note_t *note; int i; int found = 0; - int target_midi = _pitch_and_octave_to_midi (pitch, octave); + int target_midi = _pitch_to_midi (pitch); int note_midi; for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) { note = scherzo->notes_pressed.notes[i]; - note_midi = _pitch_and_octave_to_midi (note->pitch, note->octave); + note_midi = _pitch_to_midi (note->pitch); if (note_midi == target_midi) { found = 1; if (! scherzo->pedal_pressed) @@ -1137,23 +1097,13 @@ scherzo_release_note (scherzo_t *scherzo, pitch_t pitch, int octave) static score_note_t * scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note) { - pitch_t pitch; - int octave; - - _midi_to_pitch_and_octave (midi_note, &pitch, &octave); - - return scherzo_press_note (scherzo, pitch, octave); + return scherzo_press_note (scherzo, _midi_to_pitch (midi_note)); } static void scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note) { - pitch_t pitch; - int octave; - - _midi_to_pitch_and_octave (midi_note, &pitch, &octave); - - scherzo_release_note (scherzo, pitch, octave); + scherzo_release_note (scherzo, _midi_to_pitch (midi_note)); } static void @@ -1177,7 +1127,7 @@ scherzo_release_pedal (scherzo_t *scherzo) /* Make new notes in score for all pressed notes. */ for (i = 0; i < scherzo->notes_pressed.num_notes; i++) { note = scherzo->notes_pressed.notes[i]; - new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration); + new_note = score_add_note (note->staff, note->pitch, note->duration); scherzo->notes_pressed.notes[i] = new_note; } @@ -1202,6 +1152,7 @@ _select_challenge (scherzo_t *scherzo) bool_t introduced_unused; item_t *item; challenge_t *challenge = &scherzo->challenge; + pitch_name_t pitch_name; pitch_t pitch; int octave; char *s; @@ -1235,25 +1186,25 @@ _select_challenge (scherzo_t *scherzo) switch (*s) { case 'C': - pitch = PITCH_C; + pitch_name = PITCH_NAME_C; break; case 'D': - pitch = PITCH_D; + pitch_name = PITCH_NAME_D; break; case 'E': - pitch = PITCH_E; + pitch_name = PITCH_NAME_E; break; case 'F': - pitch = PITCH_F; + pitch_name = PITCH_NAME_F; break; case 'G': - pitch = PITCH_G; + pitch_name = PITCH_NAME_G; break; case 'A': - pitch = PITCH_A; + pitch_name = PITCH_NAME_A; break; case 'B': - pitch = PITCH_B; + pitch_name = PITCH_NAME_B; break; default: fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s); @@ -1268,7 +1219,9 @@ _select_challenge (scherzo_t *scherzo) octave = *s - '0'; - challenge->note = score_add_note (challenge->staff, pitch, octave, + pitch = PITCH (pitch_name, PITCH_ACCIDENTAL_NATURAL, octave); + + challenge->note = score_add_note (challenge->staff, pitch, SCORE_DURATION_WHOLE); challenge->satisfied = 0; challenge->mistaken = 0; @@ -1285,8 +1238,7 @@ _judge_note (scherzo_t *scherzo, score_note_t *note) return; } - if (note->pitch == challenge->note->pitch && - note->octave == challenge->note->octave) + if (note->pitch == challenge->note->pitch) { challenge->satisfied = 1; score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */