]> git.cworth.org Git - scherzo/blob - scherzo.c
Integrate some simple mnemon quizzing into scherzo.
[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 unused(foo) foo __attribute__((unused))
28
29 #define MIDI_BUF_SIZE 4096
30
31 typedef struct challenge
32 {
33     bin_t *bin;
34     int item_index;
35     score_staff_t *staff;
36     score_note_t *note;
37
38     int satisfied;
39     int mistaken;
40 } challenge_t;
41
42 typedef struct scherzo
43 {
44     void *ctx;
45
46     GtkWidget *window;
47     score_t *score;
48     int staff_height;
49     score_staff_t *treble;
50     score_staff_t *bass;
51     int midi_fd;
52     snd_midi_event_t *snd_midi_event;
53
54     mnemon_t mnemon;
55     challenge_t challenge;
56
57     int num_notes_pressed;
58     score_note_t **notes_pressed;
59 } scherzo_t;
60
61 static int
62 on_delete_event_quit (unused (GtkWidget *widget),
63                       unused (GdkEvent *event),
64                       unused (gpointer user_data))
65 {
66     gtk_main_quit ();
67
68     /* Returning FALSE allows the default handler for delete-event
69      * to proceed to cleanup the widget. */
70     return FALSE;
71 }
72
73 static int
74 on_expose_event_draw (GtkWidget *widget,
75                       unused (GdkEventExpose *expose),
76                       void * user_data)
77 {
78     scherzo_t *scherzo = user_data;
79     score_t *score = scherzo->score;
80     cairo_t *cr;
81     GtkAllocation allocation;
82     static const int pad = 10;
83     int widget_width;
84
85     gtk_widget_get_allocation (widget, &allocation);
86     widget_width = allocation.width;
87
88     cr = gdk_cairo_create (widget->window);
89
90     /* White background */
91     cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
92     cairo_paint (cr);
93
94     /* Add some padding on the sides and top */
95     cairo_translate (cr, pad, 2 * scherzo->staff_height);
96     score_set_staff_height (score, scherzo->staff_height);
97     score_set_width (score, widget_width - 2 * pad);
98
99     score_draw (score, cr);
100  
101     return TRUE;
102 }
103
104 static int
105 on_key_press_event (GtkWidget *widget,
106                     GdkEventKey *key,
107                     void *user_data)
108 {
109     scherzo_t *scherzo = user_data;
110
111     switch (key->keyval) {
112     case GDK_KEY_plus:
113     case GDK_KEY_KP_Add:
114     case GDK_KEY_equal:
115     case GDK_KEY_KP_Equal:
116         scherzo->staff_height += 4;
117         gtk_widget_queue_draw (widget);
118         return TRUE;
119         break;
120     case GDK_KEY_minus:
121     case GDK_KEY_KP_Subtract:
122         scherzo->staff_height -= 4;
123         gtk_widget_queue_draw (widget);
124         return TRUE;
125         break;
126     case GDK_KEY_q:
127     case GDK_KEY_Q:
128     case GDK_KEY_Escape:
129         gtk_main_quit ();
130         return FALSE;
131     }
132
133     /* Allow the event to propagate to other handlers. */
134     return FALSE;
135 }
136
137 static void
138 _midi_to_score_pitch_and_octave (unsigned char midi_note,
139                                  score_pitch_t *pitch,
140                                  int *octave)
141 {
142     *octave = midi_note / 12 - 1;
143
144     switch (midi_note % 12)
145     {
146     case 0:
147         *pitch = SCORE_PITCH_C;
148         break;
149     case 1:
150         *pitch = SCORE_PITCH_Cs;
151         break;
152     case 2:
153         *pitch = SCORE_PITCH_D;
154         break;
155     case 3:
156         *pitch = SCORE_PITCH_Ds;
157         break;
158     case 4:
159         *pitch = SCORE_PITCH_E;
160         break;
161     case 5:
162         *pitch = SCORE_PITCH_F;
163         break;
164     case 6:
165         *pitch = SCORE_PITCH_Fs;
166         break;
167     case 7:
168         *pitch = SCORE_PITCH_G;
169         break;
170     case 8:
171         *pitch = SCORE_PITCH_Gs;
172         break;
173     case 9:
174         *pitch = SCORE_PITCH_A;
175         break;
176     case 10:
177         *pitch = SCORE_PITCH_As;
178         break;
179     case 11:
180         *pitch = SCORE_PITCH_B;
181         break;
182     }
183 }
184
185 static score_note_t *
186 scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note)
187 {
188     score_staff_t *staff;
189     score_pitch_t pitch;
190     int octave;
191     score_note_t *note;
192
193     staff = scherzo->challenge.staff;
194
195     _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
196
197     note = score_staff_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
198
199     scherzo->num_notes_pressed++;
200     scherzo->notes_pressed = talloc_realloc (scherzo->ctx,
201                                              scherzo->notes_pressed,
202                                              score_note_t*,
203                                              scherzo->num_notes_pressed);
204     if (scherzo->notes_pressed == NULL) {
205         fprintf (stderr, "Out of memory.\n");
206         exit (1);
207     }
208
209     scherzo->notes_pressed[scherzo->num_notes_pressed - 1] = note;
210
211     return note;
212 }
213
214 static void
215 scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note)
216 {
217     score_staff_t *staff;
218     score_pitch_t pitch;
219     int octave;
220     score_note_t *note;
221     int i;
222
223     staff = scherzo->challenge.staff;
224
225     _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
226
227     for (i = 0; i < scherzo->num_notes_pressed; i++) {
228         note = scherzo->notes_pressed[i];
229         if (note->pitch == pitch && note->octave == octave) {
230             score_staff_remove_note (staff, note);
231             if (i < scherzo->num_notes_pressed - 1) {
232                 memmove (scherzo->notes_pressed + i,
233                          scherzo->notes_pressed + i + 1,
234                          (scherzo->num_notes_pressed - 1 - i) * sizeof (score_note_t*));
235             }
236             scherzo->num_notes_pressed--;
237             i--;
238         }
239     }
240 }
241
242 void
243 _select_challenge (scherzo_t *scherzo)
244 {
245     category_t *category_unused;
246     bool_t introduced_unused;
247     item_t *item;
248     challenge_t *challenge = &scherzo->challenge;
249     score_pitch_t pitch;
250     int octave;
251     char *s;
252
253     if (challenge->note) {
254         score_staff_remove_note (challenge->staff, challenge->note);
255         challenge->note = NULL;
256     }
257
258     mnemon_select_item (&scherzo->mnemon,
259                         &challenge->bin,
260                         &challenge->item_index,
261                         &category_unused,
262                         &introduced_unused);
263
264     item = challenge->bin->items[challenge->item_index];
265
266     s = item->challenge;
267     if (strncmp (s, "treble:", 7) == 0) {
268         s += 7;
269         challenge->staff = scherzo->treble;
270     } else if (strncmp (s, "bass:", 5) == 0) {
271         s += 5;
272         challenge->staff = scherzo->bass;
273     } else {
274         fprintf (stderr,
275                  "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
276                  s);
277         exit (1);
278     }
279
280     switch (*s) {
281     case 'C':
282         pitch = SCORE_PITCH_VALUE(C, NATURAL);
283         break;
284     case 'D':
285         pitch = SCORE_PITCH_VALUE(D, NATURAL);
286         break;
287     case 'E':
288         pitch = SCORE_PITCH_VALUE(E, NATURAL);
289         break;
290     case 'F':
291         pitch = SCORE_PITCH_VALUE(F, NATURAL);
292         break;
293     case 'G':
294         pitch = SCORE_PITCH_VALUE(G, NATURAL);
295         break;
296     case 'A':
297         pitch = SCORE_PITCH_VALUE(A, NATURAL);
298         break;
299     case 'B':
300         pitch = SCORE_PITCH_VALUE(B, NATURAL);
301         break;
302     default:
303         fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
304         exit (1);
305     }
306     s++;
307
308     if (*s < '0' || *s > '9') {
309         fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
310         exit (1);
311     }
312
313     octave = *s - '0';
314
315     challenge->note = score_staff_add_note (challenge->staff, pitch, octave,
316                                             SCORE_DURATION_WHOLE);
317     challenge->satisfied = 0;
318     challenge->mistaken = 0;
319 }
320
321 /* Determine whether the user hit the correct note. */
322 static void
323 _judge_note (scherzo_t *scherzo, score_note_t *note)
324 {
325     challenge_t *challenge = &scherzo->challenge;
326
327     if (note->pitch == challenge->note->pitch &&
328         note->octave == challenge->note->octave)
329     {
330         challenge->satisfied = 1;
331     }
332     else
333     {
334         challenge->mistaken = 1;
335     }
336 }
337
338 /* If the user got the right note (eventually), then score it in
339  * mnemon and show the next note. */
340 static void
341 _score_challenge (scherzo_t *scherzo)
342 {
343     challenge_t *challenge = &scherzo->challenge;
344
345     if (! challenge->satisfied)
346         return;
347
348     mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
349                        ! challenge->mistaken);
350
351     _select_challenge (scherzo);
352 }
353
354 static int
355 on_midi_input (unused (GIOChannel *channel),
356                unused (GIOCondition condition),
357                void *user_data)
358 {
359     unsigned char buf[MIDI_BUF_SIZE], *next;
360     scherzo_t *scherzo = user_data;
361     ssize_t remaining;
362     snd_seq_event_t event;
363     score_note_t *note;
364
365     remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
366
367     next = buf;
368     while (remaining) {
369         long consumed;
370
371         consumed = snd_midi_event_encode (scherzo->snd_midi_event,
372                                           next, remaining, &event);
373
374         remaining -= consumed;
375
376         switch (event.type) {
377         case SND_SEQ_EVENT_NONE:
378             /* Incomplete event. Nothing to do. */
379             break;
380         case SND_SEQ_EVENT_NOTEON:
381             note = scherzo_add_note_midi (scherzo, event.data.note.note);
382             _judge_note (scherzo, note);
383             gtk_widget_queue_draw (scherzo->window);
384             break;
385         case SND_SEQ_EVENT_NOTEOFF:
386             scherzo_remove_note_midi (scherzo, event.data.note.note);
387             _score_challenge (scherzo);
388             gtk_widget_queue_draw (scherzo->window);
389             break;
390         case SND_SEQ_EVENT_CLOCK:
391             /* Ignore for now as my piano sends a constant stream of these. */
392             break;
393         case SND_SEQ_EVENT_SENSING:
394             /* Ignore for now as my piano sends a constant stream of these. */
395             break;
396         default:
397             fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
398                      event.type);
399             break;
400         }
401     }
402     
403     /* Return TRUE to continue to get called in the future. */
404     return TRUE;
405 }
406
407 int
408 main (int argc, char *argv[])
409 {
410     GtkWidget *drawing_area;
411     scherzo_t scherzo;
412     int err;
413
414     srand (time (NULL));
415
416     gtk_init (&argc, &argv);
417
418     scherzo.ctx = talloc_new (NULL);
419
420     scherzo.score = score_create (scherzo.ctx);
421     scherzo.staff_height = 48;
422     score_set_staff_height (scherzo.score, scherzo.staff_height);
423
424     score_add_brace (scherzo.score, 2);
425     scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
426     scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
427
428     scherzo.num_notes_pressed = 0;
429     scherzo.notes_pressed = NULL;
430
431     mnemon_init (&scherzo.mnemon);
432     /* XXX: Should create a default file if one cannot be loaded. */
433     mnemon_load_category (&scherzo.mnemon, "scherzo");
434
435     scherzo.challenge.note = NULL;
436     _select_challenge (&scherzo);
437
438     err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
439     if (err) {
440         fprintf (stderr, "Out of memory.\n");
441         return 1;
442     }
443
444 #define MIDI_DEVICE "/dev/midi1"
445     scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
446     if (scherzo.midi_fd < 0) {
447         printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
448     } else {
449         GIOChannel *channel;
450
451         channel = g_io_channel_unix_new (scherzo.midi_fd);
452         g_io_channel_set_encoding (channel, NULL, NULL);
453         g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
454     }
455
456     scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
457
458     gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 600, 400);
459
460     g_signal_connect (scherzo.window, "delete-event",
461                       G_CALLBACK (on_delete_event_quit), NULL);
462
463     drawing_area = gtk_drawing_area_new ();
464
465     gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
466
467     g_signal_connect (drawing_area, "expose-event",  
468                       G_CALLBACK (on_expose_event_draw),
469                       &scherzo);
470
471     g_signal_connect (scherzo.window, "key-press-event",  
472                       G_CALLBACK (on_key_press_event),
473                       &scherzo);
474     
475     gtk_widget_show_all (scherzo.window);
476     
477     gtk_main ();
478
479     mnemon_save (&scherzo.mnemon);
480
481     snd_midi_event_free (scherzo.snd_midi_event);
482
483     talloc_free (scherzo.ctx);
484
485     return 0;
486 }