/* 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);
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;
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);
pitch_t pitch;
if (scherzo->challenge.note)
- octave = scherzo->challenge.note->octave;
+ octave = PITCH_OCTAVE (scherzo->challenge.note->pitch);
else
octave = scherzo->keyboard_octave;
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);
}
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)) {
}
/* 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;
}
}
}
/* 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;
}
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
{
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;
if (degree_delta == -1) {
if (name == PITCH_NAME_B) {
name = PITCH_NAME_C;
- note->octave++;
+ octave++;
} else {
name++;
}
if (degree_delta == +1) {
if (name == PITCH_NAME_C) {
name = PITCH_NAME_B;
- note->octave--;
+ octave--;
} else {
name--;
}
exit (1);
}
- note->pitch = PITCH (name, accidental);
+ note->pitch = PITCH (name, accidental, octave);
}
static void
/* 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,
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;
}
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++;
}
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;
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);
}
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)
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
/* 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;
}
bool_t introduced_unused;
item_t *item;
challenge_t *challenge = &scherzo->challenge;
+ pitch_name_t pitch_name;
pitch_t pitch;
int octave;
char *s;
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);
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;
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 */