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