]> git.cworth.org Git - scherzo/blob - scherzo.c
Add preliminary chord analysis
[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 scherzo
45 {
46     void *ctx;
47
48     GtkWidget *window;
49     score_t *score;
50     int staff_height;
51     score_staff_t *treble;
52     score_staff_t *bass;
53     score_chord_t *chord;
54     int midi_fd;
55     snd_midi_event_t *snd_midi_event;
56
57     mnemon_t mnemon;
58     challenge_t challenge;
59
60     int num_notes_pressed;
61     score_note_t **notes_pressed;
62 } scherzo_t;
63
64 /* Forward declarations. */
65 static score_note_t *
66 scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
67
68 static void
69 scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
70
71 static void
72 _judge_note (scherzo_t *scherzo, score_note_t *note);
73
74 static void
75 _score_challenge (scherzo_t *scherzo);
76
77 static int
78 on_delete_event_quit (unused (GtkWidget *widget),
79                       unused (GdkEvent *event),
80                       unused (gpointer user_data))
81 {
82     gtk_main_quit ();
83
84     /* Returning FALSE allows the default handler for delete-event
85      * to proceed to cleanup the widget. */
86     return FALSE;
87 }
88
89 static int
90 on_expose_event_draw (GtkWidget *widget,
91                       unused (GdkEventExpose *expose),
92                       void * user_data)
93 {
94     scherzo_t *scherzo = user_data;
95     score_t *score = scherzo->score;
96     cairo_t *cr;
97     GtkAllocation allocation;
98     static const int pad = 10;
99     int widget_width;
100
101     gtk_widget_get_allocation (widget, &allocation);
102     widget_width = allocation.width;
103
104     cr = gdk_cairo_create (widget->window);
105
106     /* White background */
107     cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
108     cairo_paint (cr);
109
110     /* Add some padding on the sides and top */
111     cairo_translate (cr, pad, scherzo->staff_height);
112     score_set_staff_height (score, scherzo->staff_height);
113     score_set_width (score, widget_width - 2 * pad);
114
115     score_draw (score, cr);
116  
117     return TRUE;
118 }
119
120 static int
121 on_key_press_event (GtkWidget *widget,
122                     GdkEventKey *key,
123                     void *user_data)
124 {
125     scherzo_t *scherzo = user_data;
126     int octave;
127     score_pitch_t pitch;
128
129     if (scherzo->challenge.note)
130         octave = scherzo->challenge.note->octave;
131     else
132         octave = 4;
133
134     switch (key->keyval) {
135     case GDK_KEY_plus:
136     case GDK_KEY_KP_Add:
137     case GDK_KEY_equal:
138     case GDK_KEY_KP_Equal:
139         scherzo->staff_height += 4;
140         gtk_widget_queue_draw (widget);
141         return TRUE;
142         break;
143     case GDK_KEY_minus:
144     case GDK_KEY_KP_Subtract:
145         scherzo->staff_height -= 4;
146         gtk_widget_queue_draw (widget);
147         return TRUE;
148         break;
149     case GDK_KEY_q:
150     case GDK_KEY_Q:
151     case GDK_KEY_Escape:
152         gtk_main_quit ();
153         return FALSE;
154     case GDK_KEY_c:
155     case GDK_KEY_C:
156         pitch = SCORE_PITCH_C;
157         break;
158     case GDK_KEY_d:
159     case GDK_KEY_D:
160         pitch = SCORE_PITCH_D;
161         break;
162     case GDK_KEY_e:
163     case GDK_KEY_E:
164         pitch = SCORE_PITCH_E;
165         break;
166     case GDK_KEY_f:
167     case GDK_KEY_F:
168         pitch = SCORE_PITCH_F;
169         break;
170     case GDK_KEY_g:
171     case GDK_KEY_G:
172         pitch = SCORE_PITCH_G;
173         break;
174     case GDK_KEY_a:
175     case GDK_KEY_A:
176         pitch = SCORE_PITCH_A;
177         break;
178     case GDK_KEY_b:
179     case GDK_KEY_B:
180         pitch = SCORE_PITCH_B;
181         break;
182     }
183
184     if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
185         (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
186     {
187         score_note_t *note;
188
189         note = scherzo_add_note (scherzo, pitch, octave);
190         _judge_note (scherzo, note);
191         gtk_widget_queue_draw (scherzo->window);
192
193         return TRUE;
194     }
195
196
197     /* Allow an unhandled event to propagate to other handlers. */
198     return FALSE;
199 }
200
201 static int
202 on_key_release_event (unused (GtkWidget *widget),
203                       GdkEventKey *key,
204                       void *user_data)
205 {
206     scherzo_t *scherzo = user_data;
207     int octave;
208     score_pitch_t pitch;
209
210     if (scherzo->challenge.note)
211         octave = scherzo->challenge.note->octave;
212     else
213         octave = 4;
214
215     switch (key->keyval) {
216     case GDK_KEY_c:
217     case GDK_KEY_C:
218         pitch = SCORE_PITCH_C;
219         break;
220     case GDK_KEY_d:
221     case GDK_KEY_D:
222         pitch = SCORE_PITCH_D;
223         break;
224     case GDK_KEY_e:
225     case GDK_KEY_E:
226         pitch = SCORE_PITCH_E;
227         break;
228     case GDK_KEY_f:
229     case GDK_KEY_F:
230         pitch = SCORE_PITCH_F;
231         break;
232     case GDK_KEY_g:
233     case GDK_KEY_G:
234         pitch = SCORE_PITCH_G;
235         break;
236     case GDK_KEY_a:
237     case GDK_KEY_A:
238         pitch = SCORE_PITCH_A;
239         break;
240     case GDK_KEY_b:
241     case GDK_KEY_B:
242         pitch = SCORE_PITCH_B;
243         break;
244     }
245
246     if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
247         (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
248     {
249         scherzo_remove_note (scherzo, pitch, octave);
250         _score_challenge (scherzo);
251         gtk_widget_queue_draw (scherzo->window);
252
253         return TRUE;
254     }
255
256
257     /* Allow an unhandled event to propagate to other handlers. */
258     return FALSE;
259 }
260
261 static unsigned char
262 _score_pitch_and_octave_to_midi (score_pitch_t pitch,
263                                  int octave)
264 {
265     unsigned char midi_note = 12 * (octave + 1);
266
267     switch (SCORE_PITCH_NAME (pitch)) {
268     case SCORE_PITCH_NAME_C:
269         break;
270     case SCORE_PITCH_NAME_D:
271         midi_note += 2;
272         break;
273     case SCORE_PITCH_NAME_E:
274         midi_note += 4;
275         break;
276     case SCORE_PITCH_NAME_F:
277         midi_note += 5;
278         break;
279     case SCORE_PITCH_NAME_G:
280         midi_note += 7;
281         break;
282     case SCORE_PITCH_NAME_A:
283         midi_note += 9;
284         break;
285     case SCORE_PITCH_NAME_B:
286         midi_note += 11;
287         break;
288     }
289
290     switch (SCORE_PITCH_ACCIDENTAL (pitch)) {
291     case SCORE_PITCH_ACCIDENTAL_DOUBLE_FLAT:
292         midi_note -= 2;
293         break;
294     case SCORE_PITCH_ACCIDENTAL_FLAT:
295         midi_note -= 1;
296         break;
297     case SCORE_PITCH_ACCIDENTAL_NATURAL:
298         break;
299     case SCORE_PITCH_ACCIDENTAL_SHARP:
300         midi_note += 1;
301         break;
302     case SCORE_PITCH_ACCIDENTAL_DOUBLE_SHARP:
303         midi_note += 2;
304         break;
305     }
306
307     return midi_note;
308 }
309
310 static void
311 _midi_to_score_pitch_and_octave (unsigned char midi_note,
312                                  score_pitch_t *pitch,
313                                  int *octave)
314 {
315     *octave = midi_note / 12 - 1;
316
317     switch (midi_note % 12)
318     {
319     case 0:
320         *pitch = SCORE_PITCH_C;
321         break;
322     case 1:
323         *pitch = SCORE_PITCH_Cs;
324         break;
325     case 2:
326         *pitch = SCORE_PITCH_D;
327         break;
328     case 3:
329         *pitch = SCORE_PITCH_Ds;
330         break;
331     case 4:
332         *pitch = SCORE_PITCH_E;
333         break;
334     case 5:
335         *pitch = SCORE_PITCH_F;
336         break;
337     case 6:
338         *pitch = SCORE_PITCH_Fs;
339         break;
340     case 7:
341         *pitch = SCORE_PITCH_G;
342         break;
343     case 8:
344         *pitch = SCORE_PITCH_Gs;
345         break;
346     case 9:
347         *pitch = SCORE_PITCH_A;
348         break;
349     case 10:
350         *pitch = SCORE_PITCH_As;
351         break;
352     case 11:
353         *pitch = SCORE_PITCH_B;
354         break;
355     }
356 }
357
358 /* Determine a chord name (if any) from the current notes pressed */
359
360 typedef struct analyzed_note {
361     /* Original note being analzyed. */
362     score_note_t *note;
363
364     /* Absolute pitch (expressed as midi number). */
365     int midi_pitch;
366
367     /* Pitch relative to bass note. */
368     int relative_pitch;
369 } analyzed_note_t;
370
371 static int
372 _compare_analyzed_note (const void *va, const void *vb)
373 {
374     const analyzed_note_t *a = va, *b = vb;
375
376     return a->midi_pitch - b->midi_pitch;
377 }
378
379 static int
380 _chord_signature_matches (analyzed_note_t *notes,
381                           int num_notes,
382                           int *signature_pitches,
383                           int num_signature_pitches)
384 {
385     int n, s;
386
387     for (n = 0, s = 0; s < num_signature_pitches; s++) {
388         if (n >= num_notes)
389             return 0;
390         if (notes[n].relative_pitch != signature_pitches[s])
391             return 0;
392         n++;
393         /* Skip repeated notes in chord being tested. */
394         while (n < num_notes &&
395                notes[n].relative_pitch == signature_pitches[s])
396         {
397             n++;
398         }
399     }
400
401     return 1;
402 }
403
404 static void
405 scherzo_analyze_chord (scherzo_t *scherzo)
406 {
407     void *local = talloc_new (NULL);
408     analyzed_note_t *notes;
409     unsigned i, num_notes = scherzo->num_notes_pressed;
410     int bass_pitch;
411     const char *chord_name = NULL;
412
413     struct { int pitches[3]; const char *name; } triads[] = {
414         { {0, 4, 8}, "Augmented triad" },
415         { {0, 4, 7}, "Major triad" },
416         { {0, 3, 7}, "Minor triad" },
417         { {0, 3, 6}, "Diminished triad" }
418     };
419
420     if (scherzo->chord) {
421         score_remove_chord (scherzo->chord);
422         scherzo->chord = NULL;
423     }
424
425     if (num_notes == 0)
426         goto DONE;
427
428     notes = talloc_array (local, analyzed_note_t, num_notes);
429     if (notes == NULL)
430         goto DONE;
431
432     for (i = 0; i < num_notes; i++) {
433         score_note_t *note = scherzo->notes_pressed[i];
434         notes[i].note = note;
435         notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
436                                                                note->octave);
437         /* Relative pitch will be filled in after sorting. */
438         notes[i].relative_pitch = 0;
439     }
440
441     qsort (notes, num_notes, sizeof (analyzed_note_t), _compare_analyzed_note);
442     
443     bass_pitch = notes[0].midi_pitch;
444
445     for (i = 0; i < num_notes; i++) {
446         notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
447     }
448
449     for (i = 0; i < ARRAY_SIZE (triads); i++) {
450         if (_chord_signature_matches (notes, num_notes, triads[i].pitches, 3))
451             chord_name = triads[i].name;
452     }
453
454 /*
455     for (i = 0; i < num_sevenths; i++) {
456         if (_chord_signature_matches (notes, num_notes, seventh[i].pitches, 4))
457             chord_name = seventh[i].name;
458     }
459 */
460
461     if (chord_name)
462         scherzo->chord = score_add_chord (scherzo->treble, chord_name);
463     else
464         scherzo->chord = score_add_chord (scherzo->treble, "Unknown or not a chord");
465
466 DONE:
467     talloc_free (local);
468 }
469
470 static score_note_t *
471 scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
472 {
473     score_staff_t *staff;
474     score_note_t *note;
475     int i;
476
477     if (scherzo->challenge.note)
478         staff = scherzo->challenge.staff;
479     else
480         staff = scherzo->treble;
481
482     /* Do nothing if this note is already pressed. */
483     for (i = 0; i < scherzo->num_notes_pressed; i++) {
484         if (scherzo->notes_pressed[i]->pitch == pitch &&
485             scherzo->notes_pressed[i]->octave == octave)
486         {
487             return scherzo->notes_pressed[i];
488         }
489     }
490
491     note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
492
493     scherzo->num_notes_pressed++;
494     scherzo->notes_pressed = talloc_realloc (scherzo->ctx,
495                                              scherzo->notes_pressed,
496                                              score_note_t*,
497                                              scherzo->num_notes_pressed);
498     if (scherzo->notes_pressed == NULL) {
499         fprintf (stderr, "Out of memory.\n");
500         exit (1);
501     }
502
503     scherzo->notes_pressed[scherzo->num_notes_pressed - 1] = note;
504
505     scherzo_analyze_chord (scherzo);
506
507     return note;
508 }
509
510
511 static void
512 scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
513 {
514     score_note_t *note;
515     int i;
516
517     for (i = 0; i < scherzo->num_notes_pressed; i++) {
518         note = scherzo->notes_pressed[i];
519         if (note->pitch == pitch && note->octave == octave) {
520             score_remove_note (note);
521             if (i < scherzo->num_notes_pressed - 1) {
522                 memmove (scherzo->notes_pressed + i,
523                          scherzo->notes_pressed + i + 1,
524                          (scherzo->num_notes_pressed - 1 - i) * sizeof (score_note_t*));
525             }
526             scherzo->num_notes_pressed--;
527             i--;
528         }
529     }
530
531     scherzo_analyze_chord (scherzo);
532 }
533
534 static score_note_t *
535 scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note)
536 {
537     score_pitch_t pitch;
538     int octave;
539
540     _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
541
542     return scherzo_add_note (scherzo, pitch, octave);
543 }
544
545
546 static void
547 scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note)
548 {
549     score_pitch_t pitch;
550     int octave;
551  
552     _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
553
554     scherzo_remove_note (scherzo, pitch, octave);
555 }
556
557 void
558 _select_challenge (scherzo_t *scherzo)
559 {
560     category_t *category_unused;
561     bool_t introduced_unused;
562     item_t *item;
563     challenge_t *challenge = &scherzo->challenge;
564     score_pitch_t pitch;
565     int octave;
566     char *s;
567
568     if (challenge->note) {
569         score_remove_note (challenge->note);
570         challenge->note = NULL;
571     }
572
573     mnemon_select_item (&scherzo->mnemon,
574                         &challenge->bin,
575                         &challenge->item_index,
576                         &category_unused,
577                         &introduced_unused);
578
579     item = challenge->bin->items[challenge->item_index];
580
581     s = item->challenge;
582     if (strncmp (s, "treble:", 7) == 0) {
583         s += 7;
584         challenge->staff = scherzo->treble;
585     } else if (strncmp (s, "bass:", 5) == 0) {
586         s += 5;
587         challenge->staff = scherzo->bass;
588     } else {
589         fprintf (stderr,
590                  "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
591                  s);
592         exit (1);
593     }
594
595     switch (*s) {
596     case 'C':
597         pitch = SCORE_PITCH_VALUE(C, NATURAL);
598         break;
599     case 'D':
600         pitch = SCORE_PITCH_VALUE(D, NATURAL);
601         break;
602     case 'E':
603         pitch = SCORE_PITCH_VALUE(E, NATURAL);
604         break;
605     case 'F':
606         pitch = SCORE_PITCH_VALUE(F, NATURAL);
607         break;
608     case 'G':
609         pitch = SCORE_PITCH_VALUE(G, NATURAL);
610         break;
611     case 'A':
612         pitch = SCORE_PITCH_VALUE(A, NATURAL);
613         break;
614     case 'B':
615         pitch = SCORE_PITCH_VALUE(B, NATURAL);
616         break;
617     default:
618         fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
619         exit (1);
620     }
621     s++;
622
623     if (*s < '0' || *s > '9') {
624         fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
625         exit (1);
626     }
627
628     octave = *s - '0';
629
630     challenge->note = score_add_note (challenge->staff, pitch, octave,
631                                       SCORE_DURATION_WHOLE);
632     challenge->satisfied = 0;
633     challenge->mistaken = 0;
634 }
635
636 /* Determine whether the user hit the correct note. */
637 static void
638 _judge_note (scherzo_t *scherzo, score_note_t *note)
639 {
640     challenge_t *challenge = &scherzo->challenge;
641
642     if (! scherzo->challenge.note) {
643         score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
644         return;
645     }
646
647     if (note->pitch == challenge->note->pitch &&
648         note->octave == challenge->note->octave)
649     {
650         challenge->satisfied = 1;
651         score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
652     }
653     else
654     {
655         challenge->mistaken = 1;
656         score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
657     }
658 }
659
660 /* If the user got the right note (eventually), then score it in
661  * mnemon and show the next note. */
662 static void
663 _score_challenge (scherzo_t *scherzo)
664 {
665     challenge_t *challenge = &scherzo->challenge;
666
667     if (! challenge->note)
668         return;
669
670     if (! challenge->satisfied)
671         return;
672
673     mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
674                        ! challenge->mistaken);
675
676     _select_challenge (scherzo);
677 }
678
679 static int
680 on_midi_input (unused (GIOChannel *channel),
681                unused (GIOCondition condition),
682                void *user_data)
683 {
684     unsigned char buf[MIDI_BUF_SIZE], *next;
685     scherzo_t *scherzo = user_data;
686     ssize_t remaining;
687     snd_seq_event_t event;
688     score_note_t *note;
689     int need_redraw = FALSE;
690
691     remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
692
693     next = buf;
694     while (remaining) {
695         long consumed;
696
697         consumed = snd_midi_event_encode (scherzo->snd_midi_event,
698                                           next, remaining, &event);
699
700         remaining -= consumed;
701         next += consumed;
702
703         switch (event.type) {
704         case SND_SEQ_EVENT_NONE:
705             /* Incomplete event. Nothing to do. */
706             break;
707         case SND_SEQ_EVENT_NOTEON:
708             note = scherzo_add_note_midi (scherzo, event.data.note.note);
709             _judge_note (scherzo, note);
710             need_redraw = TRUE;
711             break;
712         case SND_SEQ_EVENT_NOTEOFF:
713             scherzo_remove_note_midi (scherzo, event.data.note.note);
714             _score_challenge (scherzo);
715             need_redraw = TRUE;
716             break;
717         case SND_SEQ_EVENT_CLOCK:
718             /* Ignore for now as my piano sends a constant stream of these. */
719             break;
720         case SND_SEQ_EVENT_SENSING:
721             /* Ignore for now as my piano sends a constant stream of these. */
722             break;
723         default:
724             fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
725                      event.type);
726             break;
727         }
728     }
729
730     if (need_redraw)
731         gtk_widget_queue_draw (scherzo->window);
732
733     /* Return TRUE to continue to get called in the future. */
734     return TRUE;
735 }
736
737 int
738 main (int argc, char *argv[])
739 {
740     GtkWidget *drawing_area;
741     scherzo_t scherzo;
742     int err;
743
744     srand (time (NULL));
745
746     gtk_init (&argc, &argv);
747
748     scherzo.ctx = talloc_new (NULL);
749
750     scherzo.score = score_create (scherzo.ctx);
751     scherzo.staff_height = 100;
752     score_set_staff_height (scherzo.score, scherzo.staff_height);
753
754     score_add_brace (scherzo.score, 2);
755     scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
756     scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
757
758     scherzo.chord = NULL;
759
760     scherzo.num_notes_pressed = 0;
761     scherzo.notes_pressed = NULL;
762
763     mnemon_init (&scherzo.mnemon);
764     /* XXX: Should create a default file if one cannot be loaded. */
765     mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
766
767     scherzo.challenge.note = NULL;
768 /*
769     _select_challenge (&scherzo);
770 */
771
772     err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
773     if (err) {
774         fprintf (stderr, "Out of memory.\n");
775         return 1;
776     }
777
778 #define MIDI_DEVICE "/dev/midi1"
779     scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
780     if (scherzo.midi_fd < 0) {
781         printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
782     } else {
783         GIOChannel *channel;
784
785         channel = g_io_channel_unix_new (scherzo.midi_fd);
786         g_io_channel_set_encoding (channel, NULL, NULL);
787         g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
788     }
789
790     scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
791
792     gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
793
794     g_signal_connect (scherzo.window, "delete-event",
795                       G_CALLBACK (on_delete_event_quit), NULL);
796
797     drawing_area = gtk_drawing_area_new ();
798
799     gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
800
801     g_signal_connect (drawing_area, "expose-event",  
802                       G_CALLBACK (on_expose_event_draw),
803                       &scherzo);
804
805     g_signal_connect (scherzo.window, "key-press-event",
806                       G_CALLBACK (on_key_press_event),
807                       &scherzo);
808
809     g_signal_connect (scherzo.window, "key-release-event",
810                       G_CALLBACK (on_key_release_event),
811                       &scherzo);
812     
813     gtk_widget_show_all (scherzo.window);
814     
815     gtk_main ();
816
817     mnemon_save (&scherzo.mnemon);
818
819     snd_midi_event_free (scherzo.snd_midi_event);
820
821     talloc_free (scherzo.ctx);
822
823     return 0;
824 }