]> git.cworth.org Git - scherzo/blob - scherzo.c
Add recognition of 7th chords
[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     struct { int pitches[4]; const char *name; } sevenths[] = {
421         { {0, 4, 8, 11}, "Augmented/major 7" },
422         { {0, 4, 7, 11}, "Major 7" },
423         { {0, 4, 7, 10}, "Dominant 7" },
424         { {0, 3, 7, 11}, "Minor/major 7" },
425         { {0, 3, 7, 10}, "Minor 7" },
426         { {0, 3, 6, 10}, "Half-diminished 7" },
427         { {0, 3, 6, 9},  "Diminished 7" },
428         { {0, 4, 8, 10}, "Augmented 7" },
429         { {0, 3, 6, 11}, "Diminished/major 7" },
430     };
431
432     if (scherzo->chord) {
433         score_remove_chord (scherzo->chord);
434         scherzo->chord = NULL;
435     }
436
437     if (num_notes == 0)
438         goto DONE;
439
440     notes = talloc_array (local, analyzed_note_t, num_notes);
441     if (notes == NULL)
442         goto DONE;
443
444     for (i = 0; i < num_notes; i++) {
445         score_note_t *note = scherzo->notes_pressed[i];
446         notes[i].note = note;
447         notes[i].midi_pitch = _score_pitch_and_octave_to_midi (note->pitch,
448                                                                note->octave);
449         /* Relative pitch will be filled in after sorting. */
450         notes[i].relative_pitch = 0;
451     }
452
453     qsort (notes, num_notes, sizeof (analyzed_note_t), _compare_analyzed_note);
454     
455     bass_pitch = notes[0].midi_pitch;
456
457     for (i = 0; i < num_notes; i++) {
458         notes[i].relative_pitch = notes[i].midi_pitch - bass_pitch;
459     }
460
461     for (i = 0; i < ARRAY_SIZE (triads); i++) {
462         if (_chord_signature_matches (notes, num_notes, triads[i].pitches, 3))
463             chord_name = triads[i].name;
464     }
465
466     for (i = 0; i < ARRAY_SIZE(sevenths); i++) {
467         if (_chord_signature_matches (notes, num_notes, sevenths[i].pitches, 4))
468             chord_name = sevenths[i].name;
469     }
470
471     if (chord_name)
472         scherzo->chord = score_add_chord (scherzo->treble, chord_name);
473     else
474         scherzo->chord = score_add_chord (scherzo->treble, "Unknown or not a chord");
475
476 DONE:
477     talloc_free (local);
478 }
479
480 static score_note_t *
481 scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
482 {
483     score_staff_t *staff;
484     score_note_t *note;
485     int i;
486
487     if (scherzo->challenge.note)
488         staff = scherzo->challenge.staff;
489     else
490         staff = scherzo->treble;
491
492     /* Do nothing if this note is already pressed. */
493     for (i = 0; i < scherzo->num_notes_pressed; i++) {
494         if (scherzo->notes_pressed[i]->pitch == pitch &&
495             scherzo->notes_pressed[i]->octave == octave)
496         {
497             return scherzo->notes_pressed[i];
498         }
499     }
500
501     note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
502
503     scherzo->num_notes_pressed++;
504     scherzo->notes_pressed = talloc_realloc (scherzo->ctx,
505                                              scherzo->notes_pressed,
506                                              score_note_t*,
507                                              scherzo->num_notes_pressed);
508     if (scherzo->notes_pressed == NULL) {
509         fprintf (stderr, "Out of memory.\n");
510         exit (1);
511     }
512
513     scherzo->notes_pressed[scherzo->num_notes_pressed - 1] = note;
514
515     scherzo_analyze_chord (scherzo);
516
517     return note;
518 }
519
520
521 static void
522 scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
523 {
524     score_note_t *note;
525     int i;
526
527     for (i = 0; i < scherzo->num_notes_pressed; i++) {
528         note = scherzo->notes_pressed[i];
529         if (note->pitch == pitch && note->octave == octave) {
530             score_remove_note (note);
531             if (i < scherzo->num_notes_pressed - 1) {
532                 memmove (scherzo->notes_pressed + i,
533                          scherzo->notes_pressed + i + 1,
534                          (scherzo->num_notes_pressed - 1 - i) * sizeof (score_note_t*));
535             }
536             scherzo->num_notes_pressed--;
537             i--;
538         }
539     }
540
541     scherzo_analyze_chord (scherzo);
542 }
543
544 static score_note_t *
545 scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note)
546 {
547     score_pitch_t pitch;
548     int octave;
549
550     _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
551
552     return scherzo_add_note (scherzo, pitch, octave);
553 }
554
555
556 static void
557 scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note)
558 {
559     score_pitch_t pitch;
560     int octave;
561  
562     _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
563
564     scherzo_remove_note (scherzo, pitch, octave);
565 }
566
567 void
568 _select_challenge (scherzo_t *scherzo)
569 {
570     category_t *category_unused;
571     bool_t introduced_unused;
572     item_t *item;
573     challenge_t *challenge = &scherzo->challenge;
574     score_pitch_t pitch;
575     int octave;
576     char *s;
577
578     if (challenge->note) {
579         score_remove_note (challenge->note);
580         challenge->note = NULL;
581     }
582
583     mnemon_select_item (&scherzo->mnemon,
584                         &challenge->bin,
585                         &challenge->item_index,
586                         &category_unused,
587                         &introduced_unused);
588
589     item = challenge->bin->items[challenge->item_index];
590
591     s = item->challenge;
592     if (strncmp (s, "treble:", 7) == 0) {
593         s += 7;
594         challenge->staff = scherzo->treble;
595     } else if (strncmp (s, "bass:", 5) == 0) {
596         s += 5;
597         challenge->staff = scherzo->bass;
598     } else {
599         fprintf (stderr,
600                  "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
601                  s);
602         exit (1);
603     }
604
605     switch (*s) {
606     case 'C':
607         pitch = SCORE_PITCH_VALUE(C, NATURAL);
608         break;
609     case 'D':
610         pitch = SCORE_PITCH_VALUE(D, NATURAL);
611         break;
612     case 'E':
613         pitch = SCORE_PITCH_VALUE(E, NATURAL);
614         break;
615     case 'F':
616         pitch = SCORE_PITCH_VALUE(F, NATURAL);
617         break;
618     case 'G':
619         pitch = SCORE_PITCH_VALUE(G, NATURAL);
620         break;
621     case 'A':
622         pitch = SCORE_PITCH_VALUE(A, NATURAL);
623         break;
624     case 'B':
625         pitch = SCORE_PITCH_VALUE(B, NATURAL);
626         break;
627     default:
628         fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
629         exit (1);
630     }
631     s++;
632
633     if (*s < '0' || *s > '9') {
634         fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
635         exit (1);
636     }
637
638     octave = *s - '0';
639
640     challenge->note = score_add_note (challenge->staff, pitch, octave,
641                                       SCORE_DURATION_WHOLE);
642     challenge->satisfied = 0;
643     challenge->mistaken = 0;
644 }
645
646 /* Determine whether the user hit the correct note. */
647 static void
648 _judge_note (scherzo_t *scherzo, score_note_t *note)
649 {
650     challenge_t *challenge = &scherzo->challenge;
651
652     if (! scherzo->challenge.note) {
653         score_set_note_color_rgb (note, 0.0, 0.0, 0.0); /* black */
654         return;
655     }
656
657     if (note->pitch == challenge->note->pitch &&
658         note->octave == challenge->note->octave)
659     {
660         challenge->satisfied = 1;
661         score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
662     }
663     else
664     {
665         challenge->mistaken = 1;
666         score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
667     }
668 }
669
670 /* If the user got the right note (eventually), then score it in
671  * mnemon and show the next note. */
672 static void
673 _score_challenge (scherzo_t *scherzo)
674 {
675     challenge_t *challenge = &scherzo->challenge;
676
677     if (! challenge->note)
678         return;
679
680     if (! challenge->satisfied)
681         return;
682
683     mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
684                        ! challenge->mistaken);
685
686     _select_challenge (scherzo);
687 }
688
689 static int
690 on_midi_input (unused (GIOChannel *channel),
691                unused (GIOCondition condition),
692                void *user_data)
693 {
694     unsigned char buf[MIDI_BUF_SIZE], *next;
695     scherzo_t *scherzo = user_data;
696     ssize_t remaining;
697     snd_seq_event_t event;
698     score_note_t *note;
699     int need_redraw = FALSE;
700
701     remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
702
703     next = buf;
704     while (remaining) {
705         long consumed;
706
707         consumed = snd_midi_event_encode (scherzo->snd_midi_event,
708                                           next, remaining, &event);
709
710         remaining -= consumed;
711         next += consumed;
712
713         switch (event.type) {
714         case SND_SEQ_EVENT_NONE:
715             /* Incomplete event. Nothing to do. */
716             break;
717         case SND_SEQ_EVENT_NOTEON:
718             note = scherzo_add_note_midi (scherzo, event.data.note.note);
719             _judge_note (scherzo, note);
720             need_redraw = TRUE;
721             break;
722         case SND_SEQ_EVENT_NOTEOFF:
723             scherzo_remove_note_midi (scherzo, event.data.note.note);
724             _score_challenge (scherzo);
725             need_redraw = TRUE;
726             break;
727         case SND_SEQ_EVENT_CLOCK:
728             /* Ignore for now as my piano sends a constant stream of these. */
729             break;
730         case SND_SEQ_EVENT_SENSING:
731             /* Ignore for now as my piano sends a constant stream of these. */
732             break;
733         default:
734             fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
735                      event.type);
736             break;
737         }
738     }
739
740     if (need_redraw)
741         gtk_widget_queue_draw (scherzo->window);
742
743     /* Return TRUE to continue to get called in the future. */
744     return TRUE;
745 }
746
747 int
748 main (int argc, char *argv[])
749 {
750     GtkWidget *drawing_area;
751     scherzo_t scherzo;
752     int err;
753
754     srand (time (NULL));
755
756     gtk_init (&argc, &argv);
757
758     scherzo.ctx = talloc_new (NULL);
759
760     scherzo.score = score_create (scherzo.ctx);
761     scherzo.staff_height = 100;
762     score_set_staff_height (scherzo.score, scherzo.staff_height);
763
764     score_add_brace (scherzo.score, 2);
765     scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
766     scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
767
768     scherzo.chord = NULL;
769
770     scherzo.num_notes_pressed = 0;
771     scherzo.notes_pressed = NULL;
772
773     mnemon_init (&scherzo.mnemon);
774     /* XXX: Should create a default file if one cannot be loaded. */
775     mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
776
777     scherzo.challenge.note = NULL;
778 /*
779     _select_challenge (&scherzo);
780 */
781
782     err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
783     if (err) {
784         fprintf (stderr, "Out of memory.\n");
785         return 1;
786     }
787
788 #define MIDI_DEVICE "/dev/midi1"
789     scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
790     if (scherzo.midi_fd < 0) {
791         printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
792     } else {
793         GIOChannel *channel;
794
795         channel = g_io_channel_unix_new (scherzo.midi_fd);
796         g_io_channel_set_encoding (channel, NULL, NULL);
797         g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
798     }
799
800     scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
801
802     gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
803
804     g_signal_connect (scherzo.window, "delete-event",
805                       G_CALLBACK (on_delete_event_quit), NULL);
806
807     drawing_area = gtk_drawing_area_new ();
808
809     gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
810
811     g_signal_connect (drawing_area, "expose-event",  
812                       G_CALLBACK (on_expose_event_draw),
813                       &scherzo);
814
815     g_signal_connect (scherzo.window, "key-press-event",
816                       G_CALLBACK (on_key_press_event),
817                       &scherzo);
818
819     g_signal_connect (scherzo.window, "key-release-event",
820                       G_CALLBACK (on_key_release_event),
821                       &scherzo);
822     
823     gtk_widget_show_all (scherzo.window);
824     
825     gtk_main ();
826
827     mnemon_save (&scherzo.mnemon);
828
829     snd_midi_event_free (scherzo.snd_midi_event);
830
831     talloc_free (scherzo.ctx);
832
833     return 0;
834 }