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