From: Carl Worth Date: Mon, 26 Sep 2011 16:42:20 +0000 (-0700) Subject: Fix bug with stuck notes when challenge would switch staves. X-Git-Url: https://git.cworth.org/git?p=scherzo;a=commitdiff_plain;h=3f6a9cdbf572c83c323f40cc66e301bdf128f076 Fix bug with stuck notes when challenge would switch staves. In this case we were failing to remove a note beacuse we were looking at the new staff (where the new challenge is) rather than the old staff where the note was originally created. Fix this by having each note remember the staff it's created on. This also impacts the API a bit (no longer need a staff argument to remove_note, so drop the "_staff" portion of the name from both add_note and remove_note). --- diff --git a/scherzo.c b/scherzo.c index c285891..955f5b7 100644 --- a/scherzo.c +++ b/scherzo.c @@ -194,7 +194,7 @@ scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note) _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave); - note = score_staff_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE); + note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE); scherzo->num_notes_pressed++; scherzo->notes_pressed = talloc_realloc (scherzo->ctx, @@ -214,20 +214,17 @@ scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note) static void scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note) { - score_staff_t *staff; score_pitch_t pitch; int octave; score_note_t *note; int i; - staff = scherzo->challenge.staff; - _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave); for (i = 0; i < scherzo->num_notes_pressed; i++) { note = scherzo->notes_pressed[i]; if (note->pitch == pitch && note->octave == octave) { - score_staff_remove_note (staff, note); + score_remove_note (note); if (i < scherzo->num_notes_pressed - 1) { memmove (scherzo->notes_pressed + i, scherzo->notes_pressed + i + 1, @@ -251,7 +248,7 @@ _select_challenge (scherzo_t *scherzo) char *s; if (challenge->note) { - score_staff_remove_note (challenge->staff, challenge->note); + score_remove_note (challenge->note); challenge->note = NULL; } @@ -312,8 +309,8 @@ _select_challenge (scherzo_t *scherzo) octave = *s - '0'; - challenge->note = score_staff_add_note (challenge->staff, pitch, octave, - SCORE_DURATION_WHOLE); + challenge->note = score_add_note (challenge->staff, pitch, octave, + SCORE_DURATION_WHOLE); challenge->satisfied = 0; challenge->mistaken = 0; } diff --git a/score.c b/score.c index 965d127..0c9d5f3 100644 --- a/score.c +++ b/score.c @@ -366,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; @@ -377,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; @@ -397,8 +398,9 @@ score_staff_add_note (score_staff_t *staff, } void -score_staff_remove_note (score_staff_t *staff, score_note_t *note) +score_remove_note (score_note_t *note) { + score_staff_t *staff = note->staff; int i; for (i = 0; i < staff->num_notes; i++) diff --git a/score.h b/score.h index 1e74b1a..5317103 100644 --- a/score.h +++ b/score.h @@ -128,6 +128,7 @@ typedef enum score_duration typedef struct score_note { + score_staff_t *staff; score_pitch_t pitch; int octave; score_duration_t duration; @@ -179,14 +180,14 @@ score_add_staff (score_t *score, score_clef_t clef); * QUARTER=4, EIGHTH=8, etc.) */ score_note_t * -score_staff_add_note (score_staff_t *staff, - score_pitch_t pitch, - int octave, - score_duration_t); +score_add_note (score_staff_t *staff, + score_pitch_t pitch, + int octave, + score_duration_t); /* Remove the given note from the given staff. */ void -score_staff_remove_note (score_staff_t *staff, score_note_t *note); +score_remove_note (score_note_t *note); /* Return the first note on the given staff with the given pitch, * octave, and durations. Returns NULL if no match is found. */