X-Git-Url: https://git.cworth.org/git?p=scherzo;a=blobdiff_plain;f=scherzo.c;h=0192c0c1a4dee0ca60fdc68d2ead86336aeaad24;hp=d3ca62843fa1e0beb4921b3a2c3c887e66af4119;hb=e77d712bf9f99c3d95f909621ae177b9f8175163;hpb=659845dcf5781c10a2b292f0b7f6fe372874ff8a diff --git a/scherzo.c b/scherzo.c index d3ca628..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_NATURAL (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_LITERAL (C, NATURAL); + return PITCH_LITERAL (C, NATURAL, octave); break; case 1: - *pitch = PITCH_LITERAL (C, SHARP); + return PITCH_LITERAL (C, SHARP, octave); break; case 2: - *pitch = PITCH_LITERAL (D, NATURAL); + return PITCH_LITERAL (D, NATURAL, octave); break; case 3: - *pitch = PITCH_LITERAL (D, SHARP); + return PITCH_LITERAL (D, SHARP, octave); break; case 4: - *pitch = PITCH_LITERAL (E, NATURAL); + return PITCH_LITERAL (E, NATURAL, octave); break; case 5: - *pitch = PITCH_LITERAL (F, NATURAL); + return PITCH_LITERAL (F, NATURAL, octave); break; case 6: - *pitch = PITCH_LITERAL (F, SHARP); + return PITCH_LITERAL (F, SHARP, octave); break; case 7: - *pitch = PITCH_LITERAL (G, NATURAL); + return PITCH_LITERAL (G, NATURAL, octave); break; case 8: - *pitch = PITCH_LITERAL (G, SHARP); + return PITCH_LITERAL (G, SHARP, octave); break; case 9: - *pitch = PITCH_LITERAL (A, NATURAL); + return PITCH_LITERAL (A, NATURAL, octave); break; case 10: - *pitch = PITCH_LITERAL (A, SHARP); + return PITCH_LITERAL (A, SHARP, octave); break; case 11: - *pitch = PITCH_LITERAL (B, NATURAL); + 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; } @@ -767,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; @@ -791,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++; } @@ -813,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--; } @@ -836,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 @@ -850,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, @@ -906,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; } @@ -1007,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++; @@ -1047,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; @@ -1055,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); @@ -1083,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) @@ -1112,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 @@ -1152,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; } @@ -1177,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; @@ -1210,25 +1186,25 @@ _select_challenge (scherzo_t *scherzo) switch (*s) { case 'C': - pitch = PITCH_NATURAL (C); + pitch_name = PITCH_NAME_C; break; case 'D': - pitch = PITCH_NATURAL (D); + pitch_name = PITCH_NAME_D; break; case 'E': - pitch = PITCH_NATURAL (E); + pitch_name = PITCH_NAME_E; break; case 'F': - pitch = PITCH_NATURAL (F); + pitch_name = PITCH_NAME_F; break; case 'G': - pitch = PITCH_NATURAL (G); + pitch_name = PITCH_NAME_G; break; case 'A': - pitch = PITCH_NATURAL (A); + pitch_name = PITCH_NAME_A; break; case 'B': - pitch = PITCH_NATURAL (B); + pitch_name = PITCH_NAME_B; break; default: fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s); @@ -1243,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; @@ -1260,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 */