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