]> git.cworth.org Git - scherzo/blob - scherzo.c
Drop the enumerated pitch values.
[scherzo] / scherzo.c
1 /* scherzo - Music notation training
2  *
3  * Copyright © 2010 Carl Worth
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program.  If not, see http://www.gnu.org/licenses/ .
17  */
18
19 #include <gtk/gtk.h>
20 #include <gdk/gdkkeysyms.h>
21
22 #include <asoundlib.h>
23
24 #include "score.h"
25 #include "mnemon.h"
26
27 #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0]))
28
29 #define unused(foo) foo __attribute__((unused))
30
31 #define MIDI_BUF_SIZE 4096
32
33 typedef struct challenge
34 {
35     bin_t *bin;
36     int item_index;
37     score_staff_t *staff;
38     score_note_t *note;
39
40     int satisfied;
41     int mistaken;
42 } challenge_t;
43
44 typedef struct note_group
45 {
46     void *ctx;
47     score_note_t **notes;
48     int size;
49     int num_notes;
50 } note_group_t;
51
52 typedef struct scherzo
53 {
54     void *ctx;
55
56     GtkWidget *window;
57     score_t *score;
58     int staff_height;
59     score_staff_t *treble;
60     score_staff_t *bass;
61     score_chord_t *chord;
62
63     /* The word "keyboard" here is referring to a "computer
64      * keyboard". Any "piano keyboard" key knows its own octave and
65      * accidental already. */
66     int keyboard_octave;
67     pitch_accidental_t keyboard_accidental;
68
69     int midi_fd;
70     snd_midi_event_t *snd_midi_event;
71
72     mnemon_t mnemon;
73     challenge_t challenge;
74
75     note_group_t notes_pressed;
76     note_group_t notes_pedaled;
77
78     int pedal_pressed;
79 } scherzo_t;
80
81 /* Forward declarations. */
82 static score_note_t *
83 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch, int octave);
84
85 static void
86 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch, int octave);
87
88 static void
89 scherzo_press_pedal (scherzo_t *scherzo);
90
91 static void
92 scherzo_release_pedal (scherzo_t *scherzo);
93
94 static void
95 _judge_note (scherzo_t *scherzo, score_note_t *note);
96
97 static void
98 _score_challenge (scherzo_t *scherzo);
99
100 static int
101 on_delete_event_quit (unused (GtkWidget *widget),
102                       unused (GdkEvent *event),
103                       unused (gpointer user_data))
104 {
105     gtk_main_quit ();
106
107     /* Returning FALSE allows the default handler for delete-event
108      * to proceed to cleanup the widget. */
109     return FALSE;
110 }
111
112 static int
113 on_expose_event_draw (GtkWidget *widget,
114                       unused (GdkEventExpose *expose),
115                       void * user_data)
116 {
117     scherzo_t *scherzo = user_data;
118     score_t *score = scherzo->score;
119     cairo_t *cr;
120     GtkAllocation allocation;
121     static const int pad = 10;
122     int widget_width;
123
124     gtk_widget_get_allocation (widget, &allocation);
125     widget_width = allocation.width;
126
127     cr = gdk_cairo_create (widget->window);
128
129     /* White background */
130     cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
131     cairo_paint (cr);
132
133     /* Add some padding on the sides and top */
134     cairo_translate (cr, pad, scherzo->staff_height);
135     score_set_staff_height (score, scherzo->staff_height);
136     score_set_width (score, widget_width - 2 * pad);
137
138     score_draw (score, cr);
139  
140     return TRUE;
141 }
142
143 static int
144 on_key_press_event (GtkWidget *widget,
145                     GdkEventKey *key,
146                     void *user_data)
147 {
148     scherzo_t *scherzo = user_data;
149     int octave;
150     /* Initialize to keep the compiler quiet. */
151     pitch_name_t pitch_name = PITCH_NATURAL (C);
152     pitch_t pitch;
153
154     if (scherzo->challenge.note)
155         octave = scherzo->challenge.note->octave;
156     else
157         octave = scherzo->keyboard_octave;
158
159     switch (key->keyval) {
160     case GDK_KEY_plus:
161     case GDK_KEY_KP_Add:
162     case GDK_KEY_equal:
163     case GDK_KEY_KP_Equal:
164         scherzo->staff_height += 4;
165         gtk_widget_queue_draw (widget);
166         return TRUE;
167         break;
168     case GDK_KEY_minus:
169     case GDK_KEY_KP_Subtract:
170         scherzo->staff_height -= 4;
171         gtk_widget_queue_draw (widget);
172         return TRUE;
173         break;
174     case GDK_KEY_q:
175     case GDK_KEY_Q:
176     case GDK_KEY_Escape:
177         gtk_main_quit ();
178         return FALSE;
179     case GDK_KEY_c:
180     case GDK_KEY_C:
181         pitch_name = PITCH_NAME_C;
182         break;
183     case GDK_KEY_d:
184     case GDK_KEY_D:
185         pitch_name = PITCH_NAME_D;
186         break;
187     case GDK_KEY_e:
188     case GDK_KEY_E:
189         pitch_name = PITCH_NAME_E;
190         break;
191     case GDK_KEY_f:
192     case GDK_KEY_F:
193         pitch_name = PITCH_NAME_F;
194         break;
195     case GDK_KEY_g:
196     case GDK_KEY_G:
197         pitch_name = PITCH_NAME_G;
198         break;
199     case GDK_KEY_a:
200     case GDK_KEY_A:
201         pitch_name = PITCH_NAME_A;
202         break;
203     case GDK_KEY_b:
204     case GDK_KEY_B:
205         pitch_name = PITCH_NAME_B;
206         break;
207     case GDK_KEY_0:
208     case GDK_KEY_1:
209     case GDK_KEY_2:
210     case GDK_KEY_3:
211     case GDK_KEY_4:
212     case GDK_KEY_5:
213     case GDK_KEY_6:
214     case GDK_KEY_7:
215     case GDK_KEY_8:
216         scherzo->keyboard_octave = key->keyval - GDK_KEY_0;
217         break;
218     case GDK_KEY_space:
219         scherzo_press_pedal (scherzo);
220         break;
221     case GDK_KEY_Up:
222         if (scherzo->keyboard_accidental < PITCH_ACCIDENTAL_DOUBLE_SHARP)
223             scherzo->keyboard_accidental++;
224         break;
225     case GDK_KEY_Down:
226         if (scherzo->keyboard_accidental > PITCH_ACCIDENTAL_DOUBLE_FLAT)
227             scherzo->keyboard_accidental--;
228         break;
229     }
230
231     pitch = PITCH (pitch_name, scherzo->keyboard_accidental);
232
233     if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
234         (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
235     {
236         score_note_t *note;
237
238         note = scherzo_press_note (scherzo, pitch, octave);
239         _judge_note (scherzo, note);
240         gtk_widget_queue_draw (scherzo->window);
241
242         return TRUE;
243     }
244
245
246     /* Allow an unhandled event to propagate to other handlers. */
247     return FALSE;
248 }
249
250 static int
251 on_key_release_event (unused (GtkWidget *widget),
252                       GdkEventKey *key,
253                       void *user_data)
254 {
255     scherzo_t *scherzo = user_data;
256     int octave;
257     /* Initialize to keep the compiler quiet. */
258     pitch_name_t pitch_name = PITCH_NAME_C;
259     pitch_t pitch;
260
261     if (scherzo->challenge.note)
262         octave = scherzo->challenge.note->octave;
263     else
264         octave = scherzo->keyboard_octave;
265
266     switch (key->keyval) {
267     case GDK_KEY_c:
268     case GDK_KEY_C:
269         pitch_name = PITCH_NAME_C;
270         break;
271     case GDK_KEY_d:
272     case GDK_KEY_D:
273         pitch_name = PITCH_NAME_D;
274         break;
275     case GDK_KEY_e:
276     case GDK_KEY_E:
277         pitch_name = PITCH_NAME_E;
278         break;
279     case GDK_KEY_f:
280     case GDK_KEY_F:
281         pitch_name = PITCH_NAME_F;
282         break;
283     case GDK_KEY_g:
284     case GDK_KEY_G:
285         pitch_name = PITCH_NAME_G;
286         break;
287     case GDK_KEY_a:
288     case GDK_KEY_A:
289         pitch_name = PITCH_NAME_A;
290         break;
291     case GDK_KEY_b:
292     case GDK_KEY_B:
293         pitch_name = PITCH_NAME_B;
294         break;
295     case GDK_KEY_space:
296         scherzo_release_pedal (scherzo);
297         break;
298     }
299
300     pitch = PITCH (pitch_name, scherzo->keyboard_accidental);
301
302     if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
303         (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
304     {
305         scherzo_release_note (scherzo, pitch, octave);
306         _score_challenge (scherzo);
307         gtk_widget_queue_draw (scherzo->window);
308
309         return TRUE;
310     }
311
312
313     /* Allow an unhandled event to propagate to other handlers. */
314     return FALSE;
315 }
316
317 static unsigned char
318 _pitch_and_octave_to_midi (pitch_t pitch,
319                                  int octave)
320 {
321     unsigned char midi_note = 12 * (octave + 1);
322
323     switch (PITCH_NAME (pitch)) {
324     case PITCH_NAME_C:
325         break;
326     case PITCH_NAME_D:
327         midi_note += 2;
328         break;
329     case PITCH_NAME_E:
330         midi_note += 4;
331         break;
332     case PITCH_NAME_F:
333         midi_note += 5;
334         break;
335     case PITCH_NAME_G:
336         midi_note += 7;
337         break;
338     case PITCH_NAME_A:
339         midi_note += 9;
340         break;
341     case PITCH_NAME_B:
342         midi_note += 11;
343         break;
344     }
345
346     switch (PITCH_ACCIDENTAL (pitch)) {
347     case PITCH_ACCIDENTAL_DOUBLE_FLAT:
348         midi_note -= 2;
349         break;
350     case PITCH_ACCIDENTAL_FLAT:
351         midi_note -= 1;
352         break;
353     case PITCH_ACCIDENTAL_NATURAL:
354         break;
355     case PITCH_ACCIDENTAL_SHARP:
356         midi_note += 1;
357         break;
358     case PITCH_ACCIDENTAL_DOUBLE_SHARP:
359         midi_note += 2;
360         break;
361     }
362
363     return midi_note;
364 }
365
366 /* octave can optionally by NULL */
367 static void
368 _midi_to_pitch_and_octave (unsigned char midi_note,
369                                  pitch_t *pitch,
370                                  int *octave)
371 {
372     if (octave)
373         *octave = midi_note / 12 - 1;
374
375     switch (midi_note % 12)
376     {
377     case 0:
378         *pitch = PITCH_LITERAL (C, NATURAL);
379         break;
380     case 1:
381         *pitch = PITCH_LITERAL (C, SHARP);
382         break;
383     case 2:
384         *pitch = PITCH_LITERAL (D, NATURAL);
385         break;
386     case 3:
387         *pitch = PITCH_LITERAL (D, SHARP);
388         break;
389     case 4:
390         *pitch = PITCH_LITERAL (E, NATURAL);
391         break;
392     case 5:
393         *pitch = PITCH_LITERAL (F, NATURAL);
394         break;
395     case 6:
396         *pitch = PITCH_LITERAL (F, SHARP);
397         break;
398     case 7:
399         *pitch = PITCH_LITERAL (G, NATURAL);
400         break;
401     case 8:
402         *pitch = PITCH_LITERAL (G, SHARP);
403         break;
404     case 9:
405         *pitch = PITCH_LITERAL (A, NATURAL);
406         break;
407     case 10:
408         *pitch = PITCH_LITERAL (A, SHARP);
409         break;
410     case 11:
411         *pitch = PITCH_LITERAL (B, NATURAL);
412         break;
413     }
414 }
415
416 /* Determine a chord name (if any) from the current notes pressed */
417
418 typedef struct analyzed_note {
419     /* Original note being analzyed. */
420     score_note_t *note;
421
422     /* Absolute pitch (expressed as midi number). */
423     int midi_pitch;
424
425     /* Pitch relative to bass note. */
426     int relative_pitch;
427 } analyzed_note_t;
428
429 static int
430 _compare_analyzed_note_by_midi_pitch (const void *va, const void *vb)
431 {
432     const analyzed_note_t *a = va, *b = vb;
433
434     return a->midi_pitch - b->midi_pitch;
435 }
436
437 static int
438 _compare_analyzed_note_by_relative_pitch (const void *va, const void *vb)
439 {
440     const analyzed_note_t *a = va, *b = vb;
441
442     return a->relative_pitch - b->relative_pitch;
443 }
444
445 typedef struct modified_degree
446 {
447     int degree;
448     int modification;
449 } modified_degree_t;
450
451 static int
452 _modified_degree_to_half_steps (const modified_degree_t *degree)
453 {
454     int half_steps;
455     int scale_degree = degree->degree;
456
457     /* Restrict to actual degrees within a scale. */
458     if (scale_degree > 7)
459         scale_degree = (scale_degree % 8) + 1;
460
461     /* Number of half steps from root to specified degree within a
462      * diatonic scale. */
463     switch (scale_degree) {
464     case 1:
465         half_steps = 0;
466         break;
467     case 2:
468         half_steps = 2;
469         break;
470     case 3:
471         half_steps = 4;
472         break;
473     case 4:
474         half_steps = 5;
475         break;
476     case 5:
477         half_steps = 7;
478         break;
479     case 6:
480         half_steps = 9;
481         break;
482     case 7:
483         half_steps = 11;
484         break;
485     default:
486         fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
487         exit (1);
488         break;
489     }
490
491     return half_steps + degree->modification;
492 }
493
494 /* Number of half steps from 'root' up to 'pitch' (that is, counting
495  * the steps up a chromatic scale). */
496 static int
497 _pitch_from_root_in_half_steps (pitch_t pitch, pitch_t root)
498 {
499     int root_midi = _pitch_and_octave_to_midi (root, 0);
500     int pitch_midi = _pitch_and_octave_to_midi (pitch, 0);
501
502     if (pitch_midi < root_midi)
503         pitch_midi += 12;
504
505     if (pitch_midi < root_midi) {
506         fprintf (stderr, "Internal error: midi values differ by more than expected.\n");
507         exit (1);
508     }
509
510     return (pitch_midi - root_midi) % 12;
511 }
512
513 static int
514 _chord_signature_matches (analyzed_note_t *notes,
515                           int num_notes,
516                           modified_degree_t *degrees,
517                           int num_degrees,
518                           int inversion,
519                           pitch_t *root)
520 {
521 #define MAX_DEGREES 6
522     int relative_pitches[MAX_DEGREES];
523     int i, root_index;
524
525     assert (num_degrees <= MAX_DEGREES);
526
527     if (num_notes != num_degrees)
528         return 0;
529
530     if (inversion >= num_degrees)
531         return 0;
532
533     /* We never spell simple intervals as inversions. */
534     if (num_degrees == 2 && inversion > 0)
535         return 0;
536
537     for (i = 0; i < num_degrees; i++) {
538         /* The num_degrees is in the addition just to ensure all
539          * inputs to the modulus operator remain positive. */
540         int index = (i + num_degrees - inversion) % num_degrees;
541
542         /* Again, adding a 12 to keep things positive. */
543         relative_pitches[index] =
544             (12 +
545              _modified_degree_to_half_steps (&degrees[i]) -
546              _modified_degree_to_half_steps (&degrees[inversion])) % 12;
547                 
548     }
549
550     for (i = 0; i < num_notes; i++)
551         if (notes[i].relative_pitch != relative_pitches[i])
552             return 0;
553
554     root_index = (num_notes - inversion) % num_notes;
555     *root = notes[root_index].note->pitch;
556
557     return 1;
558 }
559
560 static const char *
561 _pitch_str (pitch_t pitch)
562 {
563     static char double_flat[] = "X𝄫";
564     static char flat[] = "X♭";
565     static char natural[] = "X";
566     static char sharp[] = "X♯";
567     static char double_sharp[] = "X𝄪";
568     char *ret;
569
570     switch (PITCH_ACCIDENTAL (pitch)) {
571     case PITCH_ACCIDENTAL_DOUBLE_FLAT:
572         ret = double_flat;
573         break;
574     case PITCH_ACCIDENTAL_FLAT:
575         ret = flat;
576         break;
577     case PITCH_ACCIDENTAL_NATURAL:
578         ret = natural;
579         break;
580     case PITCH_ACCIDENTAL_SHARP:
581         ret = sharp;
582         break;
583     case PITCH_ACCIDENTAL_DOUBLE_SHARP:
584         ret = double_sharp;
585         break;
586     }
587
588     switch (PITCH_NAME(pitch)) {
589     case PITCH_NAME_C:
590         ret[0] = 'C';
591         break;
592     case PITCH_NAME_D:
593         ret[0] = 'D';
594         break;
595     case PITCH_NAME_E:
596         ret[0] = 'E';
597         break;
598     case PITCH_NAME_F:
599         ret[0] = 'F';
600         break;
601     case PITCH_NAME_G:
602         ret[0] = 'G';
603         break;
604     case PITCH_NAME_A:
605         ret[0] = 'A';
606         break;
607     case PITCH_NAME_B:
608         ret[0] = 'B';
609         break;
610     }
611
612     return ret;
613 }
614
615 typedef struct chord_signature
616 {
617     int num_degrees;
618     modified_degree_t degrees[MAX_DEGREES];
619     const char *name;
620 } chord_signature_t;
621
622 /* XXX: The superscript rise value should be relative to the current
623  * font size. Best would be for pango to support this. See:
624  *
625  * https://bugzilla.gnome.org/show_bug.cgi?id=708780
626  *
627  * For now, these are emipirically-derived values that look reasonable
628  * at the default size that score.c is using to draw the staff. If the
629  * user scales the staff up or down then these will look wrong.
630  */
631
632 #define SUP "<span font='33' rise='30000'>"
633 #define PUS "</span>"
634
635 chord_signature_t signatures[] = {
636     /* Octave */
637     { 1, {{1, 0}}, "P8" },
638
639     /* Intervals */
640     { 2, {{1, 0}, {2, -1}}, "m2" },
641     { 2, {{1, 0}, {2,  0}}, "M2" },
642     { 2, {{1, 0}, {3, -1}}, "m3" },
643     { 2, {{1, 0}, {3,  0}}, "M3" },
644     { 2, {{1, 0}, {4,  0}}, "P4" },
645     { 2, {{1, 0}, {5, -1}}, "Tri-tone" },
646     { 2, {{1, 0}, {5,  0}}, "P5" },
647     { 2, {{1, 0}, {6, -1}}, "m6" },
648     { 2, {{1, 0}, {6,  0}}, "M6" },
649     { 2, {{1, 0}, {7, -1}}, "m7" },
650     { 2, {{1, 0}, {7,  0}}, "M7" },
651
652     /* Triads */
653     { 3, {{1, 0}, {4,  0}, {5,  0}}, "sus" },
654     { 3, {{1, 0}, {3,  0}, {5, +1}}, SUP "+" PUS },
655     { 3, {{1, 0}, {3,  0}, {5,  0}}, "" },
656     { 3, {{1, 0}, {3, -1}, {5,  0}}, "m" },
657     { 3, {{1, 0}, {3, -1}, {5, -1}}, "°" },
658     { 3, {{1, 0}, {2,  0}, {5,  0}}, "msus2" },
659
660     /* Seventh chords with no 3rd */
661     { 3, {{1, 0}, {5, +1}, {7,  0}}, SUP "+M7" PUS },
662     { 3, {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
663     { 3, {{1, 0}, {5,  0}, {7,  0}}, "M7" },
664     { 3, {{1, 0}, {5,  0}, {7, -1}}, "7" },
665     { 3, {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
666
667     /* Seventh chords with no 5th */
668     { 3, {{1, 0}, {4,  0}, {7,  0}}, "M7sus" },
669     { 3, {{1, 0}, {4,  0}, {7, -1}}, "7sus" },
670     { 3, {{1, 0}, {3,  0}, {7,  0}}, "M7" },
671     { 3, {{1, 0}, {3,  0}, {7, -1}}, "7" },
672     { 3, {{1, 0}, {3, -1}, {7,  0}}, "m" SUP "M7" PUS },
673     { 3, {{1, 0}, {3, -1}, {7, -1}}, "m7" },
674     { 3, {{1, 0}, {2,  0}, {7,  0}}, "m" SUP "M7" PUS "sus2" },
675     { 3, {{1, 0}, {2,  0}, {7, -1}}, "m7sus2" },
676
677     /* Sixth chords */
678     { 4, {{1, 0}, {4,  0}, {5,  0}, {6,  0}}, "6sus" },
679     { 4, {{1, 0}, {3,  0}, {5,  0}, {6,  0}}, "6" }, /* Ambiguous with m7 */
680     { 4, {{1, 0}, {3, -1}, {5,  0}, {6,  0}}, "m6" },
681     { 4, {{1, 0}, {2,  0}, {5,  0}, {6,  0}}, "m6sus2" },
682
683     /* Seventh chords */
684     { 4, {{1, 0}, {4,  0}, {5, +1}, {7,  0}}, SUP "+M7" PUS "sus" },
685     { 4, {{1, 0}, {4,  0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
686     { 4, {{1, 0}, {4,  0}, {5,  0}, {7,  0}}, "M7sus" },
687     { 4, {{1, 0}, {4,  0}, {5,  0}, {7, -1}}, "7sus" },
688     { 4, {{1, 0}, {4,  0}, {5, -1}, {7, -1}}, "7♭5sus" },
689     { 4, {{1, 0}, {3,  0}, {5, +1}, {7,  0}}, SUP "+M7" PUS },
690     { 4, {{1, 0}, {3,  0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
691     { 4, {{1, 0}, {3,  0}, {5,  0}, {7,  0}}, "M7" },
692     { 4, {{1, 0}, {3,  0}, {5,  0}, {7, -1}}, "7" },
693     { 4, {{1, 0}, {3,  0}, {5, -1}, {7, -1}}, "7♭5" },
694     { 4, {{1, 0}, {3, -1}, {5,  0}, {7,  0}}, "m" SUP "M7" PUS },
695     { 4, {{1, 0}, {3, -1}, {5,  0}, {7, -1}}, "m7" },
696     { 4, {{1, 0}, {3, -1}, {5, -1}, {7,  0}}, "°" SUP "M7" PUS },
697     { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
698     { 4, {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
699     { 4, {{1, 0}, {2,  0}, {5,  0}, {7,  0}}, "m" SUP "M7" PUS "sus2" },
700     { 4, {{1, 0}, {2,  0}, {5,  0}, {7, -1}}, "m7sus2" },
701     { 4, {{1, 0}, {2,  0}, {5, -1}, {7,  0}}, "°" SUP "M7" PUS "sus2" },
702     { 4, {{1, 0}, {2,  0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
703     { 4, {{1, 0}, {2,  0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
704
705     /* The sorting for chords with degree higher than 9 is funny
706      * to keep the array in degree order after reducing each
707      * degree to an actual scale degree, (9 -> 2, 11 -> 4, 13 ->
708      * 6) */
709
710     /* Ninth chords voiced with no 5th */
711     { 4, {{1, 0}, {9,  0}, {4,  0}, {7,  0}}, "M9sus" },
712     { 4, {{1, 0}, {9,  0}, {4,  0}, {7, -1}}, "9sus" },
713     { 4, {{1, 0}, {9,  0}, {3,  0}, {7,  0}}, "M9" },
714     { 4, {{1, 0}, {9,  0}, {3,  0}, {7, -1}}, "9" },
715     { 4, {{1, 0}, {9,  0}, {3, -1}, {7,  0}}, "m" SUP "M9" PUS },
716     { 4, {{1, 0}, {9,  0}, {3, -1}, {7, -1}}, "m9" },
717     { 4, {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
718     { 4, {{1, 0}, {9,  0}, {3, -1}, {6,  0}}, "m6/9" },
719     { 4, {{1, 0}, {9, -1}, {3, -1}, {6,  0}}, "m6/♭9" },
720
721     /* Sixth plus 9 */
722     { 5, {{1, 0}, {9,  0}, {3,  0}, {5,  0}, {6,  0}}, "6/9" },
723     { 5, {{1, 0}, {9,  0}, {3, -1}, {5,  0}, {6,  0}}, "m6/9" },
724     { 5, {{1, 0}, {9, -1}, {3, -1}, {5,  0}, {6,  0}}, "m6/♭9" },
725
726     /* Seventh plus altered 9 */
727     { 5, {{1, 0}, {9, +1}, {3,  0}, {5,  0}, {7, -1}}, "7♯9" },
728     { 5, {{1, 0}, {9, -1}, {3,  0}, {5,  0}, {7, -1}}, "7♭9" },
729     { 5, {{1, 0}, {9, +1}, {3,  0}, {5, -1}, {7, -1}}, "7♭5♯9" },
730     { 5, {{1, 0}, {9, -1}, {3,  0}, {5, -1}, {7, -1}}, "7♭5♭9" },
731
732     /* Ninth chords */
733     { 5, {{1, 0}, {9,  0}, {4,  0}, {5, +1}, {7,  0}}, SUP "+M9" PUS "sus" },
734     { 5, {{1, 0}, {9,  0}, {4,  0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
735     { 5, {{1, 0}, {9,  0}, {4,  0}, {5,  0}, {7,  0}}, "M9sus" },
736     { 5, {{1, 0}, {9,  0}, {4,  0}, {5,  0}, {7, -1}}, "9sus" },
737     { 5, {{1, 0}, {9,  0}, {3,  0}, {5, +1}, {7,  0}}, SUP "+M9" PUS },
738     { 5, {{1, 0}, {9,  0}, {3,  0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
739     { 5, {{1, 0}, {9,  0}, {3,  0}, {5,  0}, {7,  0}}, "M9" },
740     { 5, {{1, 0}, {9,  0}, {3,  0}, {5,  0}, {7, -1}}, "9" },
741     { 5, {{1, 0}, {9,  0}, {3, -1}, {5,  0}, {7,  0}}, "m" SUP "M9" PUS },
742     { 5, {{1, 0}, {9,  0}, {3, -1}, {5,  0}, {7, -1}}, "m9" },
743     { 5, {{1, 0}, {9, -1}, {3, -1}, {5,  0}, {7, -1}}, "m♭9" },
744     { 5, {{1, 0}, {9,  0}, {3,  0}, {5, -1}, {7,  0}}, "M9" SUP "♭5" PUS },
745     { 5, {{1, 0}, {9,  0}, {3,  0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
746     { 5, {{1, 0}, {9,  0}, {3, -1}, {5, -1}, {7,  0}}, "m" SUP "M9♭5" PUS },
747     { 5, {{1, 0}, {9,  0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
748     { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
749     { 5, {{1, 0}, {9,  0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
750     { 5, {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
751
752     /* Eleventh chords */
753     { 6, {{1, 0}, {9,  0}, {3,  0}, {11,  0}, {5, +1}, {7,  0}}, SUP "+M11" PUS },
754     { 6, {{1, 0}, {9,  0}, {3,  0}, {11,  0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
755     { 6, {{1, 0}, {9,  0}, {3,  0}, {11,  0}, {5,  0}, {7,  0}}, "M11" },
756     { 6, {{1, 0}, {9,  0}, {3,  0}, {11,  0}, {5,  0}, {7, -1}}, "11" },
757     { 6, {{1, 0}, {9,  0}, {3, -1}, {11,  0}, {5,  0}, {7,  0}}, "m" SUP "M11" PUS },
758     { 6, {{1, 0}, {9,  0}, {3, -1}, {11,  0}, {5,  0}, {7, -1}}, "m11" },
759     { 6, {{1, 0}, {9, -1}, {3, -1}, {11,  0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
760     { 6, {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
761 };
762
763 static void
764 scherzo_adjust_note_to_match_modified_degree (score_note_t *note,
765                                               pitch_t root,
766                                               modified_degree_t *degree)
767 {
768     pitch_name_t name = PITCH_NAME (note->pitch);
769     pitch_accidental_t accidental = PITCH_ACCIDENTAL (note->pitch);
770     int degree_zero_based = (degree->degree - 1) % 7;
771     int degree_delta;
772
773     int note_degree_zero_based = name - PITCH_NAME (root);
774     if (note_degree_zero_based < 0)
775         note_degree_zero_based += 7;
776
777     if (note_degree_zero_based == degree_zero_based)
778         return;
779
780     degree_delta = note_degree_zero_based - degree_zero_based;
781     if (degree_delta > 3)
782         degree_delta = - (7 - degree_delta);
783     if (degree_delta < -3)
784         degree_delta = - (-7 - degree_delta);
785
786     if (abs (degree_delta) != 1) {
787         fprintf (stderr, "Internal error: Cannot adjust a note more than one degree (%d vs. %d).\n", note_degree_zero_based + 1, degree->degree);
788         exit (1);
789     }
790
791     if (degree_delta == -1) {
792         if (name == PITCH_NAME_B) {
793             name = PITCH_NAME_C;
794             note->octave++;
795         } else {
796             name++;
797         }
798         switch (name) {
799         case PITCH_NAME_D:
800         case PITCH_NAME_E:
801         case PITCH_NAME_G:
802         case PITCH_NAME_A:
803         case PITCH_NAME_B:
804             accidental -= 2;
805             break;
806         case PITCH_NAME_C:
807         case PITCH_NAME_F:
808             accidental -= 1;
809             break;
810         }
811     }
812
813     if (degree_delta == +1) {
814         if (name == PITCH_NAME_C) {
815             name = PITCH_NAME_B;
816             note->octave--;
817         } else {
818             name--;
819         }
820         switch (name) {
821         case PITCH_NAME_C:
822         case PITCH_NAME_D:
823         case PITCH_NAME_F:
824         case PITCH_NAME_G:
825         case PITCH_NAME_A:
826             accidental += 2;
827             break;
828         case PITCH_NAME_E:
829         case PITCH_NAME_B:
830             accidental += 1;
831         }
832     }
833
834     if (accidental < 0 || accidental > PITCH_ACCIDENTAL_DOUBLE_SHARP) {
835         fprintf (stderr, "Internal error: Trying to adjust an accidental out of range (degree_delta == %d (%d - %d), new accidental == %d).\n", degree_delta, note_degree_zero_based, degree_zero_based, accidental);
836         exit (1);
837     }
838
839     note->pitch = PITCH (name, accidental);
840 }
841
842 static void
843 _spell_chord_by_signature (note_group_t *chord,
844                            int num_notes,
845                            chord_signature_t *signature,
846                            pitch_t root)
847 {
848     int i, degree, note_half_steps, degree_half_steps;
849     int root_midi;
850
851     /* Sanitize root to eliminate things like double-flats,
852      * double-sharps, Cb, E#, etc. */
853     root_midi = _pitch_and_octave_to_midi (root, 0);
854     _midi_to_pitch_and_octave (root_midi, &root, NULL);
855     
856     for (i = 0; i < num_notes; i++) {
857         note_half_steps = _pitch_from_root_in_half_steps (chord->notes[i]->pitch,
858                                                           root);
859         for (degree = 0; degree < signature->num_degrees; degree++) {
860             degree_half_steps = _modified_degree_to_half_steps (&signature->degrees[degree]);
861             if (note_half_steps == degree_half_steps) {
862                 scherzo_adjust_note_to_match_modified_degree (chord->notes[i],
863                                                               root,
864                                                               &signature->degrees[degree]);
865                 break;
866             }
867         }
868         if (note_half_steps != degree_half_steps) {
869             fprintf (stderr, "Internal error: Chord and degree mis-match\n");
870             exit (1);
871         }
872     }
873 }
874
875 static void
876 scherzo_analyze_chord (scherzo_t *scherzo)
877 {
878     void *local = talloc_new (NULL);
879     analyzed_note_t *notes;
880     note_group_t *note_group;
881     unsigned i, j, num_notes;
882     int bass_pitch, inversion;
883     pitch_t root;
884     chord_signature_t *match = NULL;
885     char *chord_name;
886
887     if (scherzo->pedal_pressed)
888         note_group = &scherzo->notes_pedaled;
889     else
890         note_group = &scherzo->notes_pressed;
891
892     num_notes = note_group->num_notes;
893
894     if (scherzo->chord) {
895         score_remove_chord (scherzo->chord);
896         scherzo->chord = NULL;
897     }
898
899     if (num_notes <= 1)
900         goto DONE;
901
902     notes = talloc_array (local, analyzed_note_t, num_notes);
903     if (notes == NULL)
904         goto DONE;
905
906     for (i = 0; i < num_notes; i++) {
907         score_note_t *note = note_group->notes[i];
908         notes[i].note = note;
909         notes[i].midi_pitch = _pitch_and_octave_to_midi (note->pitch,
910                                                                note->octave);
911         /* Relative pitch will be filled in after sorting. */
912         notes[i].relative_pitch = 0;
913     }
914
915     /* First, sort by midi pitch to find the bass note. */
916     qsort (notes, num_notes, sizeof (analyzed_note_t),
917            _compare_analyzed_note_by_midi_pitch);
918     
919     bass_pitch = notes[0].midi_pitch;
920
921     /* With the bass note identified, we can find all relative pitches. */
922     for (i = 0; i < num_notes; i++) {
923         notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
924         while (notes[i].relative_pitch >= 12)
925             notes[i].relative_pitch -= 12;
926     }
927
928     /* Now, sort again by relative pitch. */
929     qsort (notes, num_notes, sizeof (analyzed_note_t),
930            _compare_analyzed_note_by_relative_pitch);
931
932     /* Finally, eliminate all duplicate notes. */
933     for (i = 0; i < num_notes - 1; i++) {
934             if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
935                     j = i+1;
936                     while (j < num_notes &&
937                            notes[j].relative_pitch == notes[i].relative_pitch)
938                     {
939                             j++;
940                     }
941                     /* The loop incremented j one past the last
942                      * duplicate. Decrement so that it points to the
943                      * last duplicate (and is guaranteed to not exceed
944                      * the array bounds).*/
945                     j--;
946
947                     if (j < num_notes - 1) {
948                             memmove (&notes[i+1], &notes[j+1],
949                                      (num_notes - j) * sizeof (analyzed_note_t));
950                     }
951
952                     num_notes -= (j - i);
953             }
954     }
955
956     for (inversion = 0; inversion < 4; inversion++) {
957         for (i = 0; i < ARRAY_SIZE (signatures); i++) {
958             if (_chord_signature_matches (notes, num_notes,
959                                           signatures[i].degrees,
960                                           signatures[i].num_degrees,
961                                           inversion, &root))
962             {
963                 match = &signatures[i];
964                 goto HAVE_MATCH;
965             }
966         }
967     }
968 HAVE_MATCH:
969
970     chord_name = (char *) signatures[i].name;
971
972     if (match) {
973         /* Don't print root pitch for octaves and inversions,
974          * (since a pitch name alone looks like a major triad) */
975         if (num_notes < 3) {
976             chord_name = talloc_strdup (local, chord_name);
977         } else {
978             chord_name = talloc_asprintf (local, "%s%s",
979                                           _pitch_str (root),
980                                           chord_name);
981         }
982
983         _spell_chord_by_signature (note_group, num_notes,
984                                    match, root);
985     } else {
986         chord_name = talloc_strdup (local, "?");
987     }
988
989     scherzo->chord = score_add_chord (scherzo->treble, chord_name);
990
991 DONE:
992     talloc_free (local);
993 }
994
995 static void
996 note_group_init (void *ctx, note_group_t *group)
997 {
998     group->ctx = ctx;
999     group->notes = NULL;
1000     group->size = 0;
1001     group->num_notes = 0;
1002 }
1003
1004 static void
1005 note_group_add_note (note_group_t *group, score_note_t *note)
1006 {
1007     int i;
1008
1009     /* Do nothing if note is already in group. */
1010     for (i = 0; i < group->num_notes; i++) {
1011         if (group->notes[i]->pitch == note->pitch &&
1012             group->notes[i]->octave == note->octave)
1013         {
1014             return;
1015         }
1016     }
1017
1018     group->num_notes++;
1019
1020     if (group->num_notes > group->size) {
1021         group->size++;
1022         group->notes = talloc_realloc (group->ctx, group->notes,
1023                                        score_note_t*, group->size);
1024
1025         if (group->notes == NULL) {
1026             fprintf (stderr, "Out of memory.\n");
1027             exit (1);
1028         }
1029     }
1030
1031     group->notes[group->num_notes - 1] = note;
1032 }
1033
1034 static void
1035 note_group_remove_note_at (note_group_t *group, int i)
1036 {
1037     if (i >= group->num_notes) {
1038         fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
1039         exit (1);
1040     }
1041
1042     if (i < group->num_notes - 1) {
1043         memmove (group->notes + i, group->notes + i + 1,
1044                  (group->num_notes - 1 - i) * sizeof (score_note_t*));
1045     }
1046     group->num_notes--;
1047 }
1048
1049 static score_note_t *
1050 scherzo_press_note (scherzo_t *scherzo, pitch_t pitch, int octave)
1051 {
1052     score_staff_t *staff;
1053     score_note_t *note;
1054     int i;
1055
1056     if (scherzo->challenge.note) {
1057         staff = scherzo->challenge.staff;
1058     } else if (octave >= 4) {
1059         staff = scherzo->treble;
1060     } else {
1061         staff = scherzo->bass;
1062     }
1063
1064     /* Do nothing if this note is already pressed. */
1065     for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
1066         if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
1067             scherzo->notes_pressed.notes[i]->octave == octave)
1068         {
1069             return scherzo->notes_pressed.notes[i];
1070         }
1071     }
1072
1073     note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
1074
1075     note_group_add_note (&scherzo->notes_pressed, note);
1076
1077     if (scherzo->pedal_pressed)
1078         note_group_add_note (&scherzo->notes_pedaled, note);
1079
1080     scherzo_analyze_chord (scherzo);
1081
1082     return note;
1083 }
1084
1085 static void
1086 scherzo_release_note (scherzo_t *scherzo, pitch_t pitch, int octave)
1087 {
1088     score_note_t *note;
1089     int i;
1090     int found = 0;
1091     int target_midi = _pitch_and_octave_to_midi (pitch, octave);
1092     int note_midi;
1093
1094     for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
1095         note = scherzo->notes_pressed.notes[i];
1096         note_midi = _pitch_and_octave_to_midi (note->pitch, note->octave);
1097         if (note_midi == target_midi) {
1098             found = 1;
1099             if (! scherzo->pedal_pressed)
1100                 score_remove_note (note);
1101             note_group_remove_note_at (&scherzo->notes_pressed, i);
1102         }
1103     }
1104
1105     if (found == 0) {
1106         fprintf (stderr, "Internal error: Failed to find note to release.\n");
1107     }
1108
1109     scherzo_analyze_chord (scherzo);
1110 }
1111
1112 static score_note_t *
1113 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1114 {
1115     pitch_t pitch;
1116     int octave;
1117
1118     _midi_to_pitch_and_octave (midi_note, &pitch, &octave);
1119
1120     return scherzo_press_note (scherzo, pitch, octave);
1121 }
1122
1123 static void
1124 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1125 {
1126     pitch_t pitch;
1127     int octave;
1128  
1129     _midi_to_pitch_and_octave (midi_note, &pitch, &octave);
1130
1131     scherzo_release_note (scherzo, pitch, octave);
1132 }
1133
1134 static void
1135 scherzo_press_pedal (scherzo_t *scherzo)
1136 {
1137     int i;
1138
1139     scherzo->pedal_pressed = 1;
1140
1141     /* Copy all pressed notes to pedaled notes */
1142     for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
1143         note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
1144 }
1145
1146 static void
1147 scherzo_release_pedal (scherzo_t *scherzo)
1148 {
1149     score_note_t *note, *new_note;
1150     int i;
1151
1152     /* Make new notes in score for all pressed notes. */
1153     for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
1154         note = scherzo->notes_pressed.notes[i];
1155         new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
1156         scherzo->notes_pressed.notes[i] = new_note;
1157     }
1158
1159     /* Then remove all previously pedaled notes from the score. */
1160     for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
1161         note = scherzo->notes_pedaled.notes[i];
1162         score_remove_note (note);
1163         note_group_remove_note_at (&scherzo->notes_pedaled, i);
1164     }
1165
1166     scherzo->pedal_pressed = 0;
1167
1168     scherzo_analyze_chord (scherzo);
1169
1170     gtk_widget_queue_draw (scherzo->window);
1171 }
1172
1173 void
1174 _select_challenge (scherzo_t *scherzo)
1175 {
1176     category_t *category_unused;
1177     bool_t introduced_unused;
1178     item_t *item;
1179     challenge_t *challenge = &scherzo->challenge;
1180     pitch_t pitch;
1181     int octave;
1182     char *s;
1183
1184     if (challenge->note) {
1185         score_remove_note (challenge->note);
1186         challenge->note = NULL;
1187     }
1188
1189     mnemon_select_item (&scherzo->mnemon,
1190                         &challenge->bin,
1191                         &challenge->item_index,
1192                         &category_unused,
1193                         &introduced_unused);
1194
1195     item = challenge->bin->items[challenge->item_index];
1196
1197     s = item->challenge;
1198     if (strncmp (s, "treble:", 7) == 0) {
1199         s += 7;
1200         challenge->staff = scherzo->treble;
1201     } else if (strncmp (s, "bass:", 5) == 0) {
1202         s += 5;
1203         challenge->staff = scherzo->bass;
1204     } else {
1205         fprintf (stderr,
1206                  "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1207                  s);
1208         exit (1);
1209     }
1210
1211     switch (*s) {
1212     case 'C':
1213         pitch = PITCH_NATURAL (C);
1214         break;
1215     case 'D':
1216         pitch = PITCH_NATURAL (D);
1217         break;
1218     case 'E':
1219         pitch = PITCH_NATURAL (E);
1220         break;
1221     case 'F':
1222         pitch = PITCH_NATURAL (F);
1223         break;
1224     case 'G':
1225         pitch = PITCH_NATURAL (G);
1226         break;
1227     case 'A':
1228         pitch = PITCH_NATURAL (A);
1229         break;
1230     case 'B':
1231         pitch = PITCH_NATURAL (B);
1232         break;
1233     default:
1234         fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1235         exit (1);
1236     }
1237     s++;
1238
1239     if (*s < '0' || *s > '9') {
1240         fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1241         exit (1);
1242     }
1243
1244     octave = *s - '0';
1245
1246     challenge->note = score_add_note (challenge->staff, pitch, octave,
1247                                       SCORE_DURATION_WHOLE);
1248     challenge->satisfied = 0;
1249     challenge->mistaken = 0;
1250 }
1251
1252 /* Determine whether the user hit the correct note. */
1253 static void
1254 _judge_note (scherzo_t *scherzo, score_note_t *note)
1255 {
1256     challenge_t *challenge = &scherzo->challenge;
1257
1258     if (! scherzo->challenge.note) {
1259         score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
1260         return;
1261     }
1262
1263     if (note->pitch == challenge->note->pitch &&
1264         note->octave == challenge->note->octave)
1265     {
1266         challenge->satisfied = 1;
1267         score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
1268     }
1269     else
1270     {
1271         challenge->mistaken = 1;
1272         score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
1273     }
1274 }
1275
1276 /* If the user got the right note (eventually), then score it in
1277  * mnemon and show the next note. */
1278 static void
1279 _score_challenge (scherzo_t *scherzo)
1280 {
1281     challenge_t *challenge = &scherzo->challenge;
1282
1283     if (! challenge->note)
1284         return;
1285
1286     if (! challenge->satisfied)
1287         return;
1288
1289     mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1290                        ! challenge->mistaken);
1291
1292     _select_challenge (scherzo);
1293 }
1294
1295 static int
1296 on_midi_input (unused (GIOChannel *channel),
1297                unused (GIOCondition condition),
1298                void *user_data)
1299 {
1300     unsigned char buf[MIDI_BUF_SIZE], *next;
1301     scherzo_t *scherzo = user_data;
1302     ssize_t remaining;
1303     snd_seq_event_t event;
1304     score_note_t *note;
1305     int need_redraw = FALSE;
1306
1307     remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1308
1309     next = buf;
1310     while (remaining) {
1311         long consumed;
1312
1313         consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1314                                           next, remaining, &event);
1315
1316         remaining -= consumed;
1317         next += consumed;
1318
1319         switch (event.type) {
1320         case SND_SEQ_EVENT_NONE:
1321             /* Incomplete event. Nothing to do. */
1322             break;
1323         case SND_SEQ_EVENT_NOTEON:
1324             note = scherzo_press_note_midi (scherzo, event.data.note.note);
1325             _judge_note (scherzo, note);
1326             need_redraw = TRUE;
1327             break;
1328         case SND_SEQ_EVENT_NOTEOFF:
1329             scherzo_release_note_midi (scherzo, event.data.note.note);
1330             _score_challenge (scherzo);
1331             need_redraw = TRUE;
1332             break;
1333         case SND_SEQ_EVENT_CLOCK:
1334             /* Ignore for now as my piano sends a constant stream of these. */
1335             break;
1336         case SND_SEQ_EVENT_SENSING:
1337             /* Ignore for now as my piano sends a constant stream of these. */
1338             break;
1339         case SND_SEQ_EVENT_CONTROLLER:
1340             /* XXX: My piano gives me 64 for the sustain pedal, is
1341              * that universal? */
1342             if (event.data.control.param == 64) {
1343                 if (event.data.control.value == 0)
1344                     scherzo_release_pedal (scherzo);
1345                 else
1346                     scherzo_press_pedal (scherzo);
1347             } else {
1348                 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1349                          event.data.control.param, event.data.control.value);
1350             }
1351             break;
1352         default:
1353             fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1354                      event.type);
1355             break;
1356         }
1357     }
1358
1359     if (need_redraw)
1360         gtk_widget_queue_draw (scherzo->window);
1361
1362     /* Return TRUE to continue to get called in the future. */
1363     return TRUE;
1364 }
1365
1366 int
1367 main (int argc, char *argv[])
1368 {
1369     GtkWidget *drawing_area;
1370     scherzo_t scherzo;
1371     int err;
1372
1373     srand (time (NULL));
1374
1375     gtk_init (&argc, &argv);
1376
1377     scherzo.ctx = talloc_new (NULL);
1378
1379     scherzo.score = score_create (scherzo.ctx);
1380     scherzo.staff_height = 100;
1381     score_set_staff_height (scherzo.score, scherzo.staff_height);
1382
1383     score_add_brace (scherzo.score, 2);
1384     scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1385     scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1386
1387     scherzo.chord = NULL;
1388
1389     /* Default to octave 4 and natural for computer keyboard keypresses. */
1390     scherzo.keyboard_octave = 4;
1391     scherzo.keyboard_accidental = PITCH_ACCIDENTAL_NATURAL;
1392
1393     note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1394     note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1395
1396     scherzo.pedal_pressed = 0;
1397
1398     mnemon_init (&scherzo.mnemon);
1399     /* XXX: Should create a default file if one cannot be loaded. */
1400     mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1401
1402     scherzo.challenge.note = NULL;
1403 /*
1404     _select_challenge (&scherzo);
1405 */
1406
1407     err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1408     if (err) {
1409         fprintf (stderr, "Out of memory.\n");
1410         return 1;
1411     }
1412
1413 #define MIDI_DEVICE "/dev/midi1"
1414     scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1415     if (scherzo.midi_fd < 0) {
1416         printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1417     } else {
1418         GIOChannel *channel;
1419
1420         channel = g_io_channel_unix_new (scherzo.midi_fd);
1421         g_io_channel_set_encoding (channel, NULL, NULL);
1422         g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1423     }
1424
1425     scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1426
1427     gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1428
1429     g_signal_connect (scherzo.window, "delete-event",
1430                       G_CALLBACK (on_delete_event_quit), NULL);
1431
1432     drawing_area = gtk_drawing_area_new ();
1433
1434     gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1435
1436     g_signal_connect (drawing_area, "expose-event",  
1437                       G_CALLBACK (on_expose_event_draw),
1438                       &scherzo);
1439
1440     g_signal_connect (scherzo.window, "key-press-event",
1441                       G_CALLBACK (on_key_press_event),
1442                       &scherzo);
1443
1444     g_signal_connect (scherzo.window, "key-release-event",
1445                       G_CALLBACK (on_key_release_event),
1446                       &scherzo);
1447     
1448     gtk_widget_show_all (scherzo.window);
1449     
1450     gtk_main ();
1451
1452     mnemon_save (&scherzo.mnemon);
1453
1454     snd_midi_event_free (scherzo.snd_midi_event);
1455
1456     talloc_free (scherzo.ctx);
1457
1458     return 0;
1459 }