]> git.cworth.org Git - scherzo/blob - scherzo.c
Don't print root pitch for octaves and intervals
[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     score_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, score_pitch_t pitch, int octave);
84
85 static void
86 scherzo_release_note (scherzo_t *scherzo, score_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     score_pitch_name_t pitch_name = SCORE_PITCH_C;
152     score_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 = SCORE_PITCH_NAME_C;
182         break;
183     case GDK_KEY_d:
184     case GDK_KEY_D:
185         pitch_name = SCORE_PITCH_NAME_D;
186         break;
187     case GDK_KEY_e:
188     case GDK_KEY_E:
189         pitch_name = SCORE_PITCH_NAME_E;
190         break;
191     case GDK_KEY_f:
192     case GDK_KEY_F:
193         pitch_name = SCORE_PITCH_NAME_F;
194         break;
195     case GDK_KEY_g:
196     case GDK_KEY_G:
197         pitch_name = SCORE_PITCH_NAME_G;
198         break;
199     case GDK_KEY_a:
200     case GDK_KEY_A:
201         pitch_name = SCORE_PITCH_NAME_A;
202         break;
203     case GDK_KEY_b:
204     case GDK_KEY_B:
205         pitch_name = SCORE_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 < SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP)
223             scherzo->keyboard_accidental++;
224         break;
225     case GDK_KEY_Down:
226         if (scherzo->keyboard_accidental > SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT)
227             scherzo->keyboard_accidental--;
228         break;
229     }
230
231     pitch = SCORE_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     score_pitch_name_t pitch_name = SCORE_PITCH_NAME_C;
259     score_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 = SCORE_PITCH_NAME_C;
270         break;
271     case GDK_KEY_d:
272     case GDK_KEY_D:
273         pitch_name = SCORE_PITCH_NAME_D;
274         break;
275     case GDK_KEY_e:
276     case GDK_KEY_E:
277         pitch_name = SCORE_PITCH_NAME_E;
278         break;
279     case GDK_KEY_f:
280     case GDK_KEY_F:
281         pitch_name = SCORE_PITCH_NAME_F;
282         break;
283     case GDK_KEY_g:
284     case GDK_KEY_G:
285         pitch_name = SCORE_PITCH_NAME_G;
286         break;
287     case GDK_KEY_a:
288     case GDK_KEY_A:
289         pitch_name = SCORE_PITCH_NAME_A;
290         break;
291     case GDK_KEY_b:
292     case GDK_KEY_B:
293         pitch_name = SCORE_PITCH_NAME_B;
294         break;
295     case GDK_KEY_space:
296         scherzo_release_pedal (scherzo);
297         break;
298     }
299
300     pitch = SCORE_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 _score_pitch_and_octave_to_midi (score_pitch_t pitch,
319                                  int octave)
320 {
321     unsigned char midi_note = 12 * (octave + 1);
322
323     switch (SCORE_PITCH_NAME (pitch)) {
324     case SCORE_PITCH_NAME_C:
325         break;
326     case SCORE_PITCH_NAME_D:
327         midi_note += 2;
328         break;
329     case SCORE_PITCH_NAME_E:
330         midi_note += 4;
331         break;
332     case SCORE_PITCH_NAME_F:
333         midi_note += 5;
334         break;
335     case SCORE_PITCH_NAME_G:
336         midi_note += 7;
337         break;
338     case SCORE_PITCH_NAME_A:
339         midi_note += 9;
340         break;
341     case SCORE_PITCH_NAME_B:
342         midi_note += 11;
343         break;
344     }
345
346     switch (SCORE_PITCH_ACCIDENTAL (pitch)) {
347     case SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT:
348         midi_note -= 2;
349         break;
350     case SCORE_PITCH_ACCIDENTAL_FLAT:
351         midi_note -= 1;
352         break;
353     case SCORE_PITCH_ACCIDENTAL_NATURAL:
354         break;
355     case SCORE_PITCH_ACCIDENTAL_SHARP:
356         midi_note += 1;
357         break;
358     case SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP:
359         midi_note += 2;
360         break;
361     }
362
363     return midi_note;
364 }
365
366 static void
367 _midi_to_score_pitch_and_octave (unsigned char midi_note,
368                                  score_pitch_t *pitch,
369                                  int *octave)
370 {
371     *octave = midi_note / 12 - 1;
372
373     switch (midi_note % 12)
374     {
375     case 0:
376         *pitch = SCORE_PITCH_C;
377         break;
378     case 1:
379         *pitch = SCORE_PITCH_Cs;
380         break;
381     case 2:
382         *pitch = SCORE_PITCH_D;
383         break;
384     case 3:
385         *pitch = SCORE_PITCH_Ds;
386         break;
387     case 4:
388         *pitch = SCORE_PITCH_E;
389         break;
390     case 5:
391         *pitch = SCORE_PITCH_F;
392         break;
393     case 6:
394         *pitch = SCORE_PITCH_Fs;
395         break;
396     case 7:
397         *pitch = SCORE_PITCH_G;
398         break;
399     case 8:
400         *pitch = SCORE_PITCH_Gs;
401         break;
402     case 9:
403         *pitch = SCORE_PITCH_A;
404         break;
405     case 10:
406         *pitch = SCORE_PITCH_As;
407         break;
408     case 11:
409         *pitch = SCORE_PITCH_B;
410         break;
411     }
412 }
413
414 /* Determine a chord name (if any) from the current notes pressed */
415
416 typedef struct analyzed_note {
417     /* Original note being analzyed. */
418     score_note_t *note;
419
420     /* Absolute pitch (expressed as midi number). */
421     int midi_pitch;
422
423     /* Pitch relative to bass note. */
424     int relative_pitch;
425 } analyzed_note_t;
426
427 static int
428 _compare_analyzed_note_by_midi_pitch (const void *va, const void *vb)
429 {
430     const analyzed_note_t *a = va, *b = vb;
431
432     return a->midi_pitch - b->midi_pitch;
433 }
434
435 static int
436 _compare_analyzed_note_by_relative_pitch (const void *va, const void *vb)
437 {
438     const analyzed_note_t *a = va, *b = vb;
439
440     return a->relative_pitch - b->relative_pitch;
441 }
442
443 typedef struct modified_degree
444 {
445     int degree;
446     int modification;
447 } modified_degree_t;
448
449 static int
450 _modified_degree_to_half_steps (const modified_degree_t *degree)
451 {
452     int half_steps;
453     int scale_degree = degree->degree;
454
455     /* Restrict to actual degrees within a scale. */
456     if (scale_degree > 7)
457         scale_degree = (scale_degree % 8) + 1;
458
459     /* Number of half steps from root to specified degree within a
460      * diatonic scaled. */
461     switch (scale_degree) {
462     case 1:
463         half_steps = 0;
464         break;
465     case 2:
466         half_steps = 2;
467         break;
468     case 3:
469         half_steps = 4;
470         break;
471     case 4:
472         half_steps = 5;
473         break;
474     case 5:
475         half_steps = 7;
476         break;
477     case 6:
478         half_steps = 9;
479         break;
480     case 7:
481         half_steps = 11;
482         break;
483     default:
484         fprintf (stderr, "Internal: Invalid degree %d\n", degree->degree);
485         exit (1);
486         break;
487     }
488
489     return half_steps + degree->modification;
490 }
491
492 static int
493 _chord_signature_matches (analyzed_note_t *notes,
494                           int num_notes,
495                           modified_degree_t *degrees,
496                           int num_degrees,
497                           int inversion,
498                           score_pitch_t *root)
499 {
500 #define MAX_DEGREES 6
501     int relative_pitches[MAX_DEGREES];
502     int i, root_index;
503
504     assert (num_degrees <= MAX_DEGREES);
505
506     if (num_notes != num_degrees)
507         return 0;
508
509     if (inversion >= num_degrees)
510         return 0;
511
512     /* We never spell simple intervals as inversions. */
513     if (num_degrees == 2 && inversion > 0)
514         return 0;
515
516     for (i = 0; i < num_degrees; i++) {
517         /* The num_degrees is in the addition just to ensure all
518          * inputs to the modulus operator remain positive. */
519         int index = (i + num_degrees - inversion) % num_degrees;
520
521         /* Again, adding a 12 to keep things positive. */
522         relative_pitches[index] =
523             (12 +
524              _modified_degree_to_half_steps (&degrees[i]) -
525              _modified_degree_to_half_steps (&degrees[inversion])) % 12;
526                 
527     }
528
529     for (i = 0; i < num_notes; i++)
530         if (notes[i].relative_pitch != relative_pitches[i])
531             return 0;
532
533     root_index = (num_notes - inversion) % num_notes;
534     *root = notes[root_index].note->pitch;
535
536     return 1;
537 }
538
539 static const char *
540 _pitch_str (score_pitch_t pitch)
541 {
542     switch (pitch) {
543     case SCORE_PITCH_Cff:
544         return "C𝄫";
545     case SCORE_PITCH_Cf:
546         return "C♭";
547     case SCORE_PITCH_C:
548         return "C";
549     case SCORE_PITCH_Cs:
550         return "C♯";
551     case SCORE_PITCH_Css:
552         return "C𝄪";
553     case SCORE_PITCH_Dff:
554         return "D𝄫";
555     case SCORE_PITCH_Df:
556         return "D♭";
557     case SCORE_PITCH_D:
558         return "D";
559     case SCORE_PITCH_Ds:
560         return "D♯";
561     case SCORE_PITCH_Dss:
562         return "D𝄪";
563     case SCORE_PITCH_Eff:
564         return "E𝄫";
565     case SCORE_PITCH_Ef:
566         return "E♭";
567     case SCORE_PITCH_E:
568         return "E";
569     case SCORE_PITCH_Es:
570         return "E♯";
571     case SCORE_PITCH_Ess:
572         return "E𝄪";
573     case SCORE_PITCH_Fff:
574         return "F𝄫";
575     case SCORE_PITCH_Ff:
576         return "F♭";
577     case SCORE_PITCH_F:
578         return "F";
579     case SCORE_PITCH_Fs:
580         return "F♯";
581     case SCORE_PITCH_Fss:
582         return "F𝄪";
583     case SCORE_PITCH_Gff:
584         return "G𝄫";
585     case SCORE_PITCH_Gf:
586         return "G♭";
587     case SCORE_PITCH_G:
588         return "G";
589     case SCORE_PITCH_Gs:
590         return "G♯";
591     case SCORE_PITCH_Gss:
592         return "G𝄪";
593     case SCORE_PITCH_Aff:
594         return "A𝄫";
595     case SCORE_PITCH_Af:
596         return "A♭";
597     case SCORE_PITCH_A:
598         return "A";
599     case SCORE_PITCH_As:
600         return "A♯";
601     case SCORE_PITCH_Ass:
602         return "A𝄪";
603     case SCORE_PITCH_Bff:
604         return "B𝄫";
605     case SCORE_PITCH_Bf:
606         return "B♭";
607     case SCORE_PITCH_B:
608         return "B";
609     case SCORE_PITCH_Bs:
610         return "B♯";
611     case SCORE_PITCH_Bss:
612         return "B𝄪";
613     }
614
615     fprintf (stderr, "Internal error: Unknown pitch %d\n", pitch);
616     exit (1);
617 }
618
619 static void
620 scherzo_analyze_chord (scherzo_t *scherzo)
621 {
622     void *local = talloc_new (NULL);
623     analyzed_note_t *notes;
624     note_group_t *note_group;
625     unsigned i, j, num_notes;
626     int bass_pitch, inversion;
627     score_pitch_t root;
628     const char *chord_name = NULL;
629
630     if (scherzo->pedal_pressed)
631         note_group = &scherzo->notes_pedaled;
632     else
633         note_group = &scherzo->notes_pressed;
634
635     num_notes = note_group->num_notes;
636
637     struct { modified_degree_t degrees[1]; const char *name; } octaves[] = {
638         { {{1, 0}}, " Octave"}
639     };
640
641     struct { modified_degree_t degrees[2]; const char *name; } intervals[] = {
642         { {{1, 0}, {2, -1}}, " Minor 2nd"},
643         { {{1, 0}, {2,  0}}, " Major 2nd"},
644         { {{1, 0}, {3, -1}}, " Minor 3rd"},
645         { {{1, 0}, {3,  0}}, " Major 3rd"},
646         { {{1, 0}, {4,  0}}, " Perfect 4th"},
647         { {{1, 0}, {5, -1}}, " Diminished 5th"},
648         { {{1, 0}, {5,  0}}, " Perfect 5th"},
649         { {{1, 0}, {6, -1}}, " Minor 6th"},
650         { {{1, 0}, {6,  0}}, " Major 6th"},
651         { {{1, 0}, {7, -1}}, " Minor 7th"},
652         { {{1, 0}, {7,  0}}, " Major 7th"}
653     };
654
655 /* XXX: The superscript rise value should be relative to the current
656  * font size. Best would be for pango to support this. See:
657  *
658  * https://bugzilla.gnome.org/show_bug.cgi?id=708780
659  *
660  * For now, these are emipirically-derived values that look reasonable
661  * at the default size that score.c is using to draw the staff. If the
662  * user scales the staff up or down then these will look wrong.
663  */
664
665 #define SUP "<span font='33' rise='30000'>"
666 #define PUS "</span>"
667
668     struct { modified_degree_t degrees[3]; const char *name; } trichords[] = {
669         /* Triads */
670         { {{1, 0}, {3, +1}, {5,  0}}, "sus" },
671         { {{1, 0}, {3,  0}, {5, +1}}, SUP "+" PUS },
672         { {{1, 0}, {3,  0}, {5,  0}}, "" },
673         { {{1, 0}, {3, -1}, {5,  0}}, "m" },
674         { {{1, 0}, {3, -1}, {5, -1}}, "°" },
675         { {{1, 0}, {2,  0}, {5,  0}}, "msus2" },
676         /* Seventh chords with no 3rd */
677         { {{1, 0}, {5, +1}, {7,  0}}, SUP "+M7" PUS },
678         { {{1, 0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
679         { {{1, 0}, {5,  0}, {7,  0}}, "M7" },
680         { {{1, 0}, {5,  0}, {7, -1}}, "7" },
681         { {{1, 0}, {5, -1}, {7, -1}}, "7♭5" },
682         /* Seventh chords with no 5th */
683         { {{1, 0}, {4,  0}, {7,  0}}, "M7sus" },
684         { {{1, 0}, {4,  0}, {7, -1}}, "7sus" },
685         { {{1, 0}, {3,  0}, {7,  0}}, "M7" },
686         { {{1, 0}, {3,  0}, {7, -1}}, "7" },
687         { {{1, 0}, {3, -1}, {7,  0}}, "m" SUP "M7" PUS },
688         { {{1, 0}, {3, -1}, {7, -1}}, "m7" },
689         { {{1, 0}, {2,  0}, {7,  0}}, "m" SUP "M7" PUS "sus2" },
690         { {{1, 0}, {2,  0}, {7, -1}}, "m7sus2" },
691     };
692
693     struct { modified_degree_t degrees[4]; const char *name; } tetrachords[] = {
694         /* Sixth chords */
695         { {{1, 0}, {4,  0}, {5,  0}, {6,  0}}, "6sus" },
696         { {{1, 0}, {3,  0}, {5,  0}, {6,  0}}, "6" }, /* Ambiguous with m7 */
697         { {{1, 0}, {3, -1}, {5,  0}, {6,  0}}, "m6" },
698         { {{1, 0}, {2,  0}, {5,  0}, {6,  0}}, "m6sus2" },
699         /* Seventh chords */
700         { {{1, 0}, {4,  0}, {5, +1}, {7,  0}}, SUP "+M7" PUS "sus" },
701         { {{1, 0}, {4,  0}, {5, +1}, {7, -1}}, SUP "+7" PUS "sus" },
702         { {{1, 0}, {4,  0}, {5,  0}, {7,  0}}, "M7sus" },
703         { {{1, 0}, {4,  0}, {5,  0}, {7, -1}}, "7sus" },
704         { {{1, 0}, {4,  0}, {5, -1}, {7, -1}}, "7♭5sus" },
705         { {{1, 0}, {3,  0}, {5, +1}, {7,  0}}, SUP "+M7" PUS },
706         { {{1, 0}, {3,  0}, {5, +1}, {7, -1}}, SUP "+7" PUS },
707         { {{1, 0}, {3,  0}, {5,  0}, {7,  0}}, "M7" },
708         { {{1, 0}, {3,  0}, {5,  0}, {7, -1}}, "7" },
709         { {{1, 0}, {3,  0}, {5, -1}, {7, -1}}, "7♭5" },
710         { {{1, 0}, {3, -1}, {5,  0}, {7,  0}}, "m" SUP "M7" PUS },
711         { {{1, 0}, {3, -1}, {5,  0}, {7, -1}}, "m7" },
712         { {{1, 0}, {3, -1}, {5, -1}, {7,  0}}, "°" SUP "M7" PUS },
713         { {{1, 0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS },
714         { {{1, 0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "7" PUS },
715         { {{1, 0}, {2,  0}, {5,  0}, {7,  0}}, "m" SUP "M7" PUS "sus2" },
716         { {{1, 0}, {2,  0}, {5,  0}, {7, -1}}, "m7sus2" },
717         { {{1, 0}, {2,  0}, {5, -1}, {7,  0}}, "°" SUP "M7" PUS "sus2" },
718         { {{1, 0}, {2,  0}, {5, -1}, {7, -1}}, "𝆩" SUP "7" PUS "sus2" },
719         { {{1, 0}, {2,  0}, {5, -1}, {7, -2}}, "°" SUP "7" PUS "sus2" }, /* Ambiguous with 7 */
720         /* Ninth chords voiced with no 5th */
721         { {{1, 0}, {9,  0}, {4,  0}, {7,  0}}, "M9sus" },
722         { {{1, 0}, {9,  0}, {4,  0}, {7, -1}}, "9sus" },
723         { {{1, 0}, {9,  0}, {3,  0}, {7,  0}}, "M9" },
724         { {{1, 0}, {9,  0}, {3,  0}, {7, -1}}, "9" },
725         { {{1, 0}, {9,  0}, {3, -1}, {7,  0}}, "m" SUP "M9" PUS },
726         { {{1, 0}, {9,  0}, {3, -1}, {7, -1}}, "m9" },
727         { {{1, 0}, {9, -1}, {3, -1}, {7, -1}}, "m♭9" },
728         { {{1, 0}, {9,  0}, {3, -1}, {6,  0}}, "m6/9" },
729         { {{1, 0}, {9, -1}, {3, -1}, {6,  0}}, "m6/♭9" },
730     };
731
732     /* The sorting here is funny to keep the array in degree order
733      * after reducing each degree to an actual scale degree, (9 -> 2,
734      * 11 -> 4, 13 -> 6) */
735     struct { modified_degree_t degrees[5]; const char *name; } pentachords[] = {
736         /* Sixth plus 9 */
737         { {{1, 0}, {9,  0}, {3,  0}, {5,  0}, {6,  0}}, "6/9" },
738         { {{1, 0}, {9,  0}, {3, -1}, {5,  0}, {6,  0}}, "m6/9" },
739         { {{1, 0}, {9, -1}, {3, -1}, {5,  0}, {6,  0}}, "m6/♭9" },
740         /* Seventh plus altered 9 */
741         { {{1, 0}, {9, +1}, {3,  0}, {5,  0}, {7, -1}}, "7♯9" },
742         { {{1, 0}, {9, -1}, {3,  0}, {5,  0}, {7, -1}}, "7♭9" },
743         { {{1, 0}, {9, +1}, {3,  0}, {5, -1}, {7, -1}}, "7♭5♯9" },
744         { {{1, 0}, {9, -1}, {3,  0}, {5, -1}, {7, -1}}, "7♭5♭9" },
745         /* Ninth chords */
746         { {{1, 0}, {9,  0}, {4,  0}, {5, +1}, {7,  0}}, SUP "+M9" PUS "sus" },
747         { {{1, 0}, {9,  0}, {4,  0}, {5, +1}, {7, -1}}, SUP "+9" PUS "sus" },
748         { {{1, 0}, {9,  0}, {4,  0}, {5,  0}, {7,  0}}, "M9sus" },
749         { {{1, 0}, {9,  0}, {4,  0}, {5,  0}, {7, -1}}, "9sus" },
750
751         { {{1, 0}, {9,  0}, {3,  0}, {5, +1}, {7,  0}}, SUP "+M9" PUS },
752         { {{1, 0}, {9,  0}, {3,  0}, {5, +1}, {7, -1}}, SUP "+9" PUS },
753
754         { {{1, 0}, {9,  0}, {3,  0}, {5,  0}, {7,  0}}, "M9" },
755         { {{1, 0}, {9,  0}, {3,  0}, {5,  0}, {7, -1}}, "9" },
756         { {{1, 0}, {9,  0}, {3, -1}, {5,  0}, {7,  0}}, "m" SUP "M9" PUS },
757         { {{1, 0}, {9,  0}, {3, -1}, {5,  0}, {7, -1}}, "m9" },
758         { {{1, 0}, {9, -1}, {3, -1}, {5,  0}, {7, -1}}, "m♭9" },
759         { {{1, 0}, {9,  0}, {3,  0}, {5, -1}, {7,  0}}, "M9" SUP "♭5" PUS },
760         { {{1, 0}, {9,  0}, {3,  0}, {5, -1}, {7, -1}}, "9" SUP "♭5" PUS },
761         { {{1, 0}, {9,  0}, {3, -1}, {5, -1}, {7,  0}}, "m" SUP "M9♭5" PUS },
762         { {{1, 0}, {9,  0}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "9" PUS },
763         { {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -1}}, "𝆩" SUP "♭9" PUS },
764         { {{1, 0}, {9,  0}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "9" PUS },
765         { {{1, 0}, {9, -1}, {3, -1}, {5, -1}, {7, -2}}, "°" SUP "♭9" PUS },
766     };
767
768     struct { modified_degree_t degrees[6]; const char *name; } hexachords[] = {
769         { {{1, 0}, {9,  0}, {3,  0}, {11,  0}, {5, +1}, {7,  0}}, SUP "+M11" PUS },
770         { {{1, 0}, {9,  0}, {3,  0}, {11,  0}, {5, +1}, {7, -1}}, SUP "+11" PUS },
771         { {{1, 0}, {9,  0}, {3,  0}, {11,  0}, {5,  0}, {7,  0}}, "M11" },
772         { {{1, 0}, {9,  0}, {3,  0}, {11,  0}, {5,  0}, {7, -1}}, "11" },
773         { {{1, 0}, {9,  0}, {3, -1}, {11,  0}, {5,  0}, {7,  0}}, "m" SUP "M11" PUS },
774         { {{1, 0}, {9,  0}, {3, -1}, {11,  0}, {5,  0}, {7, -1}}, "m11" },
775         { {{1, 0}, {9, -1}, {3, -1}, {11,  0}, {5, -1}, {7, -1}}, "𝆩" SUP "11" PUS },
776         { {{1, 0}, {9, -1}, {3, -1}, {11, -1}, {5, -1}, {7, -2}}, "°" SUP "11" PUS }
777     };
778
779     if (scherzo->chord) {
780         score_remove_chord (scherzo->chord);
781         scherzo->chord = NULL;
782     }
783
784     if (num_notes <= 1)
785         goto DONE;
786
787     notes = talloc_array (local, analyzed_note_t, num_notes);
788     if (notes == NULL)
789         goto DONE;
790
791     for (i = 0; i < num_notes; i++) {
792         score_note_t *note = note_group->notes[i];
793         notes[i].note = note;
794         notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
795                                                                note->octave);
796         /* Relative pitch will be filled in after sorting. */
797         notes[i].relative_pitch = 0;
798     }
799
800     /* First, sort by midi pitch to find the bass note. */
801     qsort (notes, num_notes, sizeof (analyzed_note_t),
802            _compare_analyzed_note_by_midi_pitch);
803     
804     bass_pitch = notes[0].midi_pitch;
805
806     /* With the bass note identified, we can find all relative pitches. */
807     for (i = 0; i < num_notes; i++) {
808         notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
809         while (notes[i].relative_pitch >= 12)
810             notes[i].relative_pitch -= 12;
811     }
812
813     /* Now, sort again by relative pitch. */
814     qsort (notes, num_notes, sizeof (analyzed_note_t),
815            _compare_analyzed_note_by_relative_pitch);
816
817     /* Finally, eliminate all duplicate notes. */
818     for (i = 0; i < num_notes - 1; i++) {
819             if (notes[i+1].relative_pitch == notes[i].relative_pitch) {
820                     j = i+1;
821                     while (j < num_notes &&
822                            notes[j].relative_pitch == notes[i].relative_pitch)
823                     {
824                             j++;
825                     }
826                     /* The loop incremented j one past the last
827                      * duplicate. Decrement so that it points to the
828                      * last duplicate (and is guaranteed to not exceed
829                      * the array bounds).*/
830                     j--;
831
832                     if (j < num_notes - 1) {
833                             memmove (&notes[i+1], &notes[j+1],
834                                      (num_notes - j) * sizeof (analyzed_note_t));
835                     }
836
837                     num_notes -= (j - i);
838             }
839     }
840
841     for (inversion = 0; inversion < 4; inversion++) {
842         switch (num_notes) {
843         case 1:
844             for (i = 0; i < ARRAY_SIZE (octaves); i++) {
845                 if (_chord_signature_matches (notes, num_notes,
846                                               octaves[i].degrees, 1,
847                                               inversion, &root))
848                 {
849                     chord_name = octaves[i].name;
850                     goto CHORD_NAME_KNOWN;
851                 }
852             }
853             break;
854         case 2:
855             for (i = 0; i < ARRAY_SIZE (intervals); i++) {
856                 if (_chord_signature_matches (notes, num_notes,
857                                               intervals[i].degrees, 2,
858                                               inversion, &root))
859                 {
860                     chord_name = intervals[i].name;
861                     goto CHORD_NAME_KNOWN;
862                 }
863             }
864             break;
865         case 3:
866             for (i = 0; i < ARRAY_SIZE (trichords); i++) {
867                 if (_chord_signature_matches (notes, num_notes,
868                                               trichords[i].degrees, 3,
869                                               inversion, &root))
870                 {
871                     chord_name = trichords[i].name;
872                     goto CHORD_NAME_KNOWN;
873                 }
874             }
875             break;
876         case 4:
877             for (i = 0; i < ARRAY_SIZE (tetrachords); i++) {
878                 if (_chord_signature_matches (notes, num_notes,
879                                               tetrachords[i].degrees, 4,
880                                               inversion, &root))
881                 {
882                     chord_name = tetrachords[i].name;
883                     goto CHORD_NAME_KNOWN;
884                 }
885             }
886             break;
887         case 5:
888             for (i = 0; i < ARRAY_SIZE (pentachords); i++) {
889                 if (_chord_signature_matches (notes, num_notes,
890                                               pentachords[i].degrees, 5,
891                                               inversion, &root))
892                 {
893                     chord_name = pentachords[i].name;
894                     goto CHORD_NAME_KNOWN;
895                 }
896             }
897             break;
898         case 6:
899             for (i = 0; i < ARRAY_SIZE (hexachords); i++) {
900                 if (_chord_signature_matches (notes, num_notes,
901                                               hexachords[i].degrees, 6,
902                                               inversion, &root))
903                 {
904                     chord_name = hexachords[i].name;
905                     goto CHORD_NAME_KNOWN;
906                 }
907             }
908             break;
909         }
910     }
911     CHORD_NAME_KNOWN:
912
913     if (chord_name) {
914         if (inversion) {
915             const char *inversion_str;
916             switch (inversion) {
917             case 1:
918                 inversion_str = "1st inversion";
919                 break;
920             case 2:
921                 inversion_str = "2nd inversion";
922                 break;
923             case 3:
924                 inversion_str = "3rd inversion";
925                 break;
926             default:
927                 fprintf (stderr, "Internal error: Unexpected inversion: %d\n",
928                          inversion);
929                 exit(1);
930             }
931             chord_name = talloc_asprintf (local, "%s%s %s",
932                                           _pitch_str (root),
933                                           chord_name, inversion_str);
934         } else {
935             /* Don't print root pitch for octaves and inversions,
936              * (since a pitch name alone looks like a major triad) */
937             if (num_notes < 3) {
938                 chord_name = talloc_strdup (local, chord_name);
939             } else {
940                 chord_name = talloc_asprintf (local, "%s%s",
941                                               _pitch_str (root),
942                                               chord_name);
943             }
944         }
945     } else {
946         chord_name = talloc_strdup (local, "Unknown chord");
947     }
948
949     scherzo->chord = score_add_chord (scherzo->treble, chord_name);
950
951 DONE:
952     talloc_free (local);
953 }
954
955 static void
956 note_group_init (void *ctx, note_group_t *group)
957 {
958     group->ctx = ctx;
959     group->notes = NULL;
960     group->size = 0;
961     group->num_notes = 0;
962 }
963
964 static void
965 note_group_add_note (note_group_t *group, score_note_t *note)
966 {
967     int i;
968
969     /* Do nothing if note is already in group. */
970     for (i = 0; i < group->num_notes; i++) {
971         if (group->notes[i]->pitch == note->pitch &&
972             group->notes[i]->octave == note->octave)
973         {
974             return;
975         }
976     }
977
978     group->num_notes++;
979
980     if (group->num_notes > group->size) {
981         group->size++;
982         group->notes = talloc_realloc (group->ctx, group->notes,
983                                        score_note_t*, group->size);
984
985         if (group->notes == NULL) {
986             fprintf (stderr, "Out of memory.\n");
987             exit (1);
988         }
989     }
990
991     group->notes[group->num_notes - 1] = note;
992 }
993
994 static void
995 note_group_remove_note_at (note_group_t *group, int i)
996 {
997     if (i >= group->num_notes) {
998         fprintf (stderr, "Internal error: No note to remove at index %d\n", i);
999         exit (1);
1000     }
1001
1002     if (i < group->num_notes - 1) {
1003         memmove (group->notes + i, group->notes + i + 1,
1004                  (group->num_notes - 1 - i) * sizeof (score_note_t*));
1005     }
1006     group->num_notes--;
1007 }
1008
1009 static score_note_t *
1010 scherzo_press_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
1011 {
1012     score_staff_t *staff;
1013     score_note_t *note;
1014     int i;
1015
1016     if (scherzo->challenge.note) {
1017         staff = scherzo->challenge.staff;
1018     } else if (octave >= 4) {
1019         staff = scherzo->treble;
1020     } else {
1021         staff = scherzo->bass;
1022     }
1023
1024     /* Do nothing if this note is already pressed. */
1025     for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
1026         if (scherzo->notes_pressed.notes[i]->pitch == pitch &&
1027             scherzo->notes_pressed.notes[i]->octave == octave)
1028         {
1029             return scherzo->notes_pressed.notes[i];
1030         }
1031     }
1032
1033     note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
1034
1035     note_group_add_note (&scherzo->notes_pressed, note);
1036
1037     if (scherzo->pedal_pressed)
1038         note_group_add_note (&scherzo->notes_pedaled, note);
1039
1040     scherzo_analyze_chord (scherzo);
1041
1042     return note;
1043 }
1044
1045 static void
1046 scherzo_release_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
1047 {
1048     score_note_t *note;
1049     int i;
1050     int found = 0;
1051
1052     for (i = scherzo->notes_pressed.num_notes - 1; i >=0; i--) {
1053         note = scherzo->notes_pressed.notes[i];
1054         if (note->pitch == pitch && note->octave == octave) {
1055             found = 1;
1056             if (! scherzo->pedal_pressed)
1057                 score_remove_note (note);
1058             note_group_remove_note_at (&scherzo->notes_pressed, i);
1059         }
1060     }
1061
1062     if (found == 0) {
1063         fprintf (stderr, "Internal error: Failed to find note to release.\n");
1064     }
1065
1066     scherzo_analyze_chord (scherzo);
1067 }
1068
1069 static score_note_t *
1070 scherzo_press_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1071 {
1072     score_pitch_t pitch;
1073     int octave;
1074
1075     _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
1076
1077     return scherzo_press_note (scherzo, pitch, octave);
1078 }
1079
1080 static void
1081 scherzo_release_note_midi (scherzo_t *scherzo, unsigned char midi_note)
1082 {
1083     score_pitch_t pitch;
1084     int octave;
1085  
1086     _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
1087
1088     scherzo_release_note (scherzo, pitch, octave);
1089 }
1090
1091 static void
1092 scherzo_press_pedal (scherzo_t *scherzo)
1093 {
1094     int i;
1095
1096     scherzo->pedal_pressed = 1;
1097
1098     /* Copy all pressed notes to pedaled notes */
1099     for (i = 0; i < scherzo->notes_pressed.num_notes; i++)
1100         note_group_add_note (&scherzo->notes_pedaled, scherzo->notes_pressed.notes[i]);
1101 }
1102
1103 static void
1104 scherzo_release_pedal (scherzo_t *scherzo)
1105 {
1106     score_note_t *note, *new_note;
1107     int i;
1108
1109     /* Make new notes in score for all pressed notes. */
1110     for (i = 0; i < scherzo->notes_pressed.num_notes; i++) {
1111         note = scherzo->notes_pressed.notes[i];
1112         new_note = score_add_note (note->staff, note->pitch, note->octave, note->duration);
1113         scherzo->notes_pressed.notes[i] = new_note;
1114     }
1115
1116     /* Then remove all previously pedaled notes from the score. */
1117     for (i = scherzo->notes_pedaled.num_notes - 1; i >=0; i--) {
1118         note = scherzo->notes_pedaled.notes[i];
1119         score_remove_note (note);
1120         note_group_remove_note_at (&scherzo->notes_pedaled, i);
1121     }
1122
1123     scherzo->pedal_pressed = 0;
1124
1125     scherzo_analyze_chord (scherzo);
1126
1127     gtk_widget_queue_draw (scherzo->window);
1128 }
1129
1130 void
1131 _select_challenge (scherzo_t *scherzo)
1132 {
1133     category_t *category_unused;
1134     bool_t introduced_unused;
1135     item_t *item;
1136     challenge_t *challenge = &scherzo->challenge;
1137     score_pitch_t pitch;
1138     int octave;
1139     char *s;
1140
1141     if (challenge->note) {
1142         score_remove_note (challenge->note);
1143         challenge->note = NULL;
1144     }
1145
1146     mnemon_select_item (&scherzo->mnemon,
1147                         &challenge->bin,
1148                         &challenge->item_index,
1149                         &category_unused,
1150                         &introduced_unused);
1151
1152     item = challenge->bin->items[challenge->item_index];
1153
1154     s = item->challenge;
1155     if (strncmp (s, "treble:", 7) == 0) {
1156         s += 7;
1157         challenge->staff = scherzo->treble;
1158     } else if (strncmp (s, "bass:", 5) == 0) {
1159         s += 5;
1160         challenge->staff = scherzo->bass;
1161     } else {
1162         fprintf (stderr,
1163                  "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
1164                  s);
1165         exit (1);
1166     }
1167
1168     switch (*s) {
1169     case 'C':
1170         pitch = SCORE_PITCH_C;
1171         break;
1172     case 'D':
1173         pitch = SCORE_PITCH_D;
1174         break;
1175     case 'E':
1176         pitch = SCORE_PITCH_E;
1177         break;
1178     case 'F':
1179         pitch = SCORE_PITCH_F;
1180         break;
1181     case 'G':
1182         pitch = SCORE_PITCH_G;
1183         break;
1184     case 'A':
1185         pitch = SCORE_PITCH_A;
1186         break;
1187     case 'B':
1188         pitch = SCORE_PITCH_B;
1189         break;
1190     default:
1191         fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
1192         exit (1);
1193     }
1194     s++;
1195
1196     if (*s < '0' || *s > '9') {
1197         fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
1198         exit (1);
1199     }
1200
1201     octave = *s - '0';
1202
1203     challenge->note = score_add_note (challenge->staff, pitch, octave,
1204                                       SCORE_DURATION_WHOLE);
1205     challenge->satisfied = 0;
1206     challenge->mistaken = 0;
1207 }
1208
1209 /* Determine whether the user hit the correct note. */
1210 static void
1211 _judge_note (scherzo_t *scherzo, score_note_t *note)
1212 {
1213     challenge_t *challenge = &scherzo->challenge;
1214
1215     if (! scherzo->challenge.note) {
1216         score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
1217         return;
1218     }
1219
1220     if (note->pitch == challenge->note->pitch &&
1221         note->octave == challenge->note->octave)
1222     {
1223         challenge->satisfied = 1;
1224         score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
1225     }
1226     else
1227     {
1228         challenge->mistaken = 1;
1229         score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
1230     }
1231 }
1232
1233 /* If the user got the right note (eventually), then score it in
1234  * mnemon and show the next note. */
1235 static void
1236 _score_challenge (scherzo_t *scherzo)
1237 {
1238     challenge_t *challenge = &scherzo->challenge;
1239
1240     if (! challenge->note)
1241         return;
1242
1243     if (! challenge->satisfied)
1244         return;
1245
1246     mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
1247                        ! challenge->mistaken);
1248
1249     _select_challenge (scherzo);
1250 }
1251
1252 static int
1253 on_midi_input (unused (GIOChannel *channel),
1254                unused (GIOCondition condition),
1255                void *user_data)
1256 {
1257     unsigned char buf[MIDI_BUF_SIZE], *next;
1258     scherzo_t *scherzo = user_data;
1259     ssize_t remaining;
1260     snd_seq_event_t event;
1261     score_note_t *note;
1262     int need_redraw = FALSE;
1263
1264     remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
1265
1266     next = buf;
1267     while (remaining) {
1268         long consumed;
1269
1270         consumed = snd_midi_event_encode (scherzo->snd_midi_event,
1271                                           next, remaining, &event);
1272
1273         remaining -= consumed;
1274         next += consumed;
1275
1276         switch (event.type) {
1277         case SND_SEQ_EVENT_NONE:
1278             /* Incomplete event. Nothing to do. */
1279             break;
1280         case SND_SEQ_EVENT_NOTEON:
1281             note = scherzo_press_note_midi (scherzo, event.data.note.note);
1282             _judge_note (scherzo, note);
1283             need_redraw = TRUE;
1284             break;
1285         case SND_SEQ_EVENT_NOTEOFF:
1286             scherzo_release_note_midi (scherzo, event.data.note.note);
1287             _score_challenge (scherzo);
1288             need_redraw = TRUE;
1289             break;
1290         case SND_SEQ_EVENT_CLOCK:
1291             /* Ignore for now as my piano sends a constant stream of these. */
1292             break;
1293         case SND_SEQ_EVENT_SENSING:
1294             /* Ignore for now as my piano sends a constant stream of these. */
1295             break;
1296         case SND_SEQ_EVENT_CONTROLLER:
1297             /* XXX: My piano gives me 64 for the sustain pedal, is
1298              * that universal? */
1299             if (event.data.control.param == 64) {
1300                 if (event.data.control.value == 0)
1301                     scherzo_release_pedal (scherzo);
1302                 else
1303                     scherzo_press_pedal (scherzo);
1304             } else {
1305                 fprintf (stderr, "Fixme: Unhandled MIDI Control event (param=%d, value=%d)\n",
1306                          event.data.control.param, event.data.control.value);
1307             }
1308             break;
1309         default:
1310             fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
1311                      event.type);
1312             break;
1313         }
1314     }
1315
1316     if (need_redraw)
1317         gtk_widget_queue_draw (scherzo->window);
1318
1319     /* Return TRUE to continue to get called in the future. */
1320     return TRUE;
1321 }
1322
1323 int
1324 main (int argc, char *argv[])
1325 {
1326     GtkWidget *drawing_area;
1327     scherzo_t scherzo;
1328     int err;
1329
1330     srand (time (NULL));
1331
1332     gtk_init (&argc, &argv);
1333
1334     scherzo.ctx = talloc_new (NULL);
1335
1336     scherzo.score = score_create (scherzo.ctx);
1337     scherzo.staff_height = 100;
1338     score_set_staff_height (scherzo.score, scherzo.staff_height);
1339
1340     score_add_brace (scherzo.score, 2);
1341     scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
1342     scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
1343
1344     scherzo.chord = NULL;
1345
1346     /* Default to octave 4 and natural for computer keyboard keypresses. */
1347     scherzo.keyboard_octave = 4;
1348     scherzo.keyboard_accidental = SCORE_PITCH_ACCIDENTAL_NATURAL;
1349
1350     note_group_init (scherzo.ctx, &scherzo.notes_pressed);
1351     note_group_init (scherzo.ctx, &scherzo.notes_pedaled);
1352
1353     scherzo.pedal_pressed = 0;
1354
1355     mnemon_init (&scherzo.mnemon);
1356     /* XXX: Should create a default file if one cannot be loaded. */
1357     mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
1358
1359     scherzo.challenge.note = NULL;
1360 /*
1361     _select_challenge (&scherzo);
1362 */
1363
1364     err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
1365     if (err) {
1366         fprintf (stderr, "Out of memory.\n");
1367         return 1;
1368     }
1369
1370 #define MIDI_DEVICE "/dev/midi1"
1371     scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
1372     if (scherzo.midi_fd < 0) {
1373         printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
1374     } else {
1375         GIOChannel *channel;
1376
1377         channel = g_io_channel_unix_new (scherzo.midi_fd);
1378         g_io_channel_set_encoding (channel, NULL, NULL);
1379         g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
1380     }
1381
1382     scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
1383
1384     gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
1385
1386     g_signal_connect (scherzo.window, "delete-event",
1387                       G_CALLBACK (on_delete_event_quit), NULL);
1388
1389     drawing_area = gtk_drawing_area_new ();
1390
1391     gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
1392
1393     g_signal_connect (drawing_area, "expose-event",  
1394                       G_CALLBACK (on_expose_event_draw),
1395                       &scherzo);
1396
1397     g_signal_connect (scherzo.window, "key-press-event",
1398                       G_CALLBACK (on_key_press_event),
1399                       &scherzo);
1400
1401     g_signal_connect (scherzo.window, "key-release-event",
1402                       G_CALLBACK (on_key_release_event),
1403                       &scherzo);
1404     
1405     gtk_widget_show_all (scherzo.window);
1406     
1407     gtk_main ();
1408
1409     mnemon_save (&scherzo.mnemon);
1410
1411     snd_midi_event_free (scherzo.snd_midi_event);
1412
1413     talloc_free (scherzo.ctx);
1414
1415     return 0;
1416 }