]> git.cworth.org Git - scherzo/blob - scherzo.c
Fix the broken SCORE_PITCH_NAME macro to work correctly
[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 /* Forward declarations. */
62 static score_note_t *
63 scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
64
65 static void
66 scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave);
67
68 static void
69 _judge_note (scherzo_t *scherzo, score_note_t *note);
70
71 static void
72 _score_challenge (scherzo_t *scherzo);
73
74 static int
75 on_delete_event_quit (unused (GtkWidget *widget),
76                       unused (GdkEvent *event),
77                       unused (gpointer user_data))
78 {
79     gtk_main_quit ();
80
81     /* Returning FALSE allows the default handler for delete-event
82      * to proceed to cleanup the widget. */
83     return FALSE;
84 }
85
86 static int
87 on_expose_event_draw (GtkWidget *widget,
88                       unused (GdkEventExpose *expose),
89                       void * user_data)
90 {
91     scherzo_t *scherzo = user_data;
92     score_t *score = scherzo->score;
93     cairo_t *cr;
94     GtkAllocation allocation;
95     static const int pad = 10;
96     int widget_width;
97
98     gtk_widget_get_allocation (widget, &allocation);
99     widget_width = allocation.width;
100
101     cr = gdk_cairo_create (widget->window);
102
103     /* White background */
104     cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
105     cairo_paint (cr);
106
107     /* Add some padding on the sides and top */
108     cairo_translate (cr, pad, scherzo->staff_height);
109     score_set_staff_height (score, scherzo->staff_height);
110     score_set_width (score, widget_width - 2 * pad);
111
112     score_draw (score, cr);
113  
114     return TRUE;
115 }
116
117 static int
118 on_key_press_event (GtkWidget *widget,
119                     GdkEventKey *key,
120                     void *user_data)
121 {
122     scherzo_t *scherzo = user_data;
123     int octave = scherzo->challenge.note->octave;
124     score_pitch_t pitch;
125
126     switch (key->keyval) {
127     case GDK_KEY_plus:
128     case GDK_KEY_KP_Add:
129     case GDK_KEY_equal:
130     case GDK_KEY_KP_Equal:
131         scherzo->staff_height += 4;
132         gtk_widget_queue_draw (widget);
133         return TRUE;
134         break;
135     case GDK_KEY_minus:
136     case GDK_KEY_KP_Subtract:
137         scherzo->staff_height -= 4;
138         gtk_widget_queue_draw (widget);
139         return TRUE;
140         break;
141     case GDK_KEY_q:
142     case GDK_KEY_Q:
143     case GDK_KEY_Escape:
144         gtk_main_quit ();
145         return FALSE;
146     case GDK_KEY_c:
147     case GDK_KEY_C:
148         pitch = SCORE_PITCH_C;
149         break;
150     case GDK_KEY_d:
151     case GDK_KEY_D:
152         pitch = SCORE_PITCH_D;
153         break;
154     case GDK_KEY_e:
155     case GDK_KEY_E:
156         pitch = SCORE_PITCH_E;
157         break;
158     case GDK_KEY_f:
159     case GDK_KEY_F:
160         pitch = SCORE_PITCH_F;
161         break;
162     case GDK_KEY_g:
163     case GDK_KEY_G:
164         pitch = SCORE_PITCH_G;
165         break;
166     case GDK_KEY_a:
167     case GDK_KEY_A:
168         pitch = SCORE_PITCH_A;
169         break;
170     case GDK_KEY_b:
171     case GDK_KEY_B:
172         pitch = SCORE_PITCH_B;
173         break;
174     }
175
176     if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
177         (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
178     {
179         score_note_t *note;
180
181         note = scherzo_add_note (scherzo, pitch, octave);
182         _judge_note (scherzo, note);
183         gtk_widget_queue_draw (scherzo->window);
184
185         return TRUE;
186     }
187
188
189     /* Allow an unhandled event to propagate to other handlers. */
190     return FALSE;
191 }
192
193 static int
194 on_key_release_event (unused (GtkWidget *widget),
195                       GdkEventKey *key,
196                       void *user_data)
197 {
198     scherzo_t *scherzo = user_data;
199     int octave = scherzo->challenge.note->octave;
200     score_pitch_t pitch;
201
202     switch (key->keyval) {
203     case GDK_KEY_c:
204     case GDK_KEY_C:
205         pitch = SCORE_PITCH_C;
206         break;
207     case GDK_KEY_d:
208     case GDK_KEY_D:
209         pitch = SCORE_PITCH_D;
210         break;
211     case GDK_KEY_e:
212     case GDK_KEY_E:
213         pitch = SCORE_PITCH_E;
214         break;
215     case GDK_KEY_f:
216     case GDK_KEY_F:
217         pitch = SCORE_PITCH_F;
218         break;
219     case GDK_KEY_g:
220     case GDK_KEY_G:
221         pitch = SCORE_PITCH_G;
222         break;
223     case GDK_KEY_a:
224     case GDK_KEY_A:
225         pitch = SCORE_PITCH_A;
226         break;
227     case GDK_KEY_b:
228     case GDK_KEY_B:
229         pitch = SCORE_PITCH_B;
230         break;
231     }
232
233     if ((key->keyval >= GDK_KEY_A && key->keyval <= GDK_KEY_G) ||
234         (key->keyval >= GDK_KEY_a && key->keyval <= GDK_KEY_g))
235     {
236         scherzo_remove_note (scherzo, pitch, octave);
237         _score_challenge (scherzo);
238         gtk_widget_queue_draw (scherzo->window);
239
240         return TRUE;
241     }
242
243
244     /* Allow an unhandled event to propagate to other handlers. */
245     return FALSE;
246 }
247
248 static void
249 _midi_to_score_pitch_and_octave (unsigned char midi_note,
250                                  score_pitch_t *pitch,
251                                  int *octave)
252 {
253     *octave = midi_note / 12 - 1;
254
255     switch (midi_note % 12)
256     {
257     case 0:
258         *pitch = SCORE_PITCH_C;
259         break;
260     case 1:
261         *pitch = SCORE_PITCH_Cs;
262         break;
263     case 2:
264         *pitch = SCORE_PITCH_D;
265         break;
266     case 3:
267         *pitch = SCORE_PITCH_Ds;
268         break;
269     case 4:
270         *pitch = SCORE_PITCH_E;
271         break;
272     case 5:
273         *pitch = SCORE_PITCH_F;
274         break;
275     case 6:
276         *pitch = SCORE_PITCH_Fs;
277         break;
278     case 7:
279         *pitch = SCORE_PITCH_G;
280         break;
281     case 8:
282         *pitch = SCORE_PITCH_Gs;
283         break;
284     case 9:
285         *pitch = SCORE_PITCH_A;
286         break;
287     case 10:
288         *pitch = SCORE_PITCH_As;
289         break;
290     case 11:
291         *pitch = SCORE_PITCH_B;
292         break;
293     }
294 }
295
296 static score_note_t *
297 scherzo_add_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
298 {
299     score_staff_t *staff;
300     score_note_t *note;
301
302     staff = scherzo->challenge.staff;
303
304     note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
305
306     scherzo->num_notes_pressed++;
307     scherzo->notes_pressed = talloc_realloc (scherzo->ctx,
308                                              scherzo->notes_pressed,
309                                              score_note_t*,
310                                              scherzo->num_notes_pressed);
311     if (scherzo->notes_pressed == NULL) {
312         fprintf (stderr, "Out of memory.\n");
313         exit (1);
314     }
315
316     scherzo->notes_pressed[scherzo->num_notes_pressed - 1] = note;
317
318     return note;
319 }
320
321
322 static void
323 scherzo_remove_note (scherzo_t *scherzo, score_pitch_t pitch, int octave)
324 {
325     score_note_t *note;
326     int i;
327
328     for (i = 0; i < scherzo->num_notes_pressed; i++) {
329         note = scherzo->notes_pressed[i];
330         if (note->pitch == pitch && note->octave == octave) {
331             score_remove_note (note);
332             if (i < scherzo->num_notes_pressed - 1) {
333                 memmove (scherzo->notes_pressed + i,
334                          scherzo->notes_pressed + i + 1,
335                          (scherzo->num_notes_pressed - 1 - i) * sizeof (score_note_t*));
336             }
337             scherzo->num_notes_pressed--;
338             i--;
339         }
340     }
341 }
342
343 static score_note_t *
344 scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note)
345 {
346     score_pitch_t pitch;
347     int octave;
348
349     _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
350
351     return scherzo_add_note (scherzo, pitch, octave);
352 }
353
354
355 static void
356 scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note)
357 {
358     score_pitch_t pitch;
359     int octave;
360  
361     _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
362
363     scherzo_remove_note (scherzo, pitch, octave);
364 }
365
366 void
367 _select_challenge (scherzo_t *scherzo)
368 {
369     category_t *category_unused;
370     bool_t introduced_unused;
371     item_t *item;
372     challenge_t *challenge = &scherzo->challenge;
373     score_pitch_t pitch;
374     int octave;
375     char *s;
376
377     if (challenge->note) {
378         score_remove_note (challenge->note);
379         challenge->note = NULL;
380     }
381
382     mnemon_select_item (&scherzo->mnemon,
383                         &challenge->bin,
384                         &challenge->item_index,
385                         &category_unused,
386                         &introduced_unused);
387
388     item = challenge->bin->items[challenge->item_index];
389
390     s = item->challenge;
391     if (strncmp (s, "treble:", 7) == 0) {
392         s += 7;
393         challenge->staff = scherzo->treble;
394     } else if (strncmp (s, "bass:", 5) == 0) {
395         s += 5;
396         challenge->staff = scherzo->bass;
397     } else {
398         fprintf (stderr,
399                  "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
400                  s);
401         exit (1);
402     }
403
404     switch (*s) {
405     case 'C':
406         pitch = SCORE_PITCH_VALUE(C, NATURAL);
407         break;
408     case 'D':
409         pitch = SCORE_PITCH_VALUE(D, NATURAL);
410         break;
411     case 'E':
412         pitch = SCORE_PITCH_VALUE(E, NATURAL);
413         break;
414     case 'F':
415         pitch = SCORE_PITCH_VALUE(F, NATURAL);
416         break;
417     case 'G':
418         pitch = SCORE_PITCH_VALUE(G, NATURAL);
419         break;
420     case 'A':
421         pitch = SCORE_PITCH_VALUE(A, NATURAL);
422         break;
423     case 'B':
424         pitch = SCORE_PITCH_VALUE(B, NATURAL);
425         break;
426     default:
427         fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
428         exit (1);
429     }
430     s++;
431
432     if (*s < '0' || *s > '9') {
433         fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
434         exit (1);
435     }
436
437     octave = *s - '0';
438
439     challenge->note = score_add_note (challenge->staff, pitch, octave,
440                                       SCORE_DURATION_WHOLE);
441     challenge->satisfied = 0;
442     challenge->mistaken = 0;
443 }
444
445 /* Determine whether the user hit the correct note. */
446 static void
447 _judge_note (scherzo_t *scherzo, score_note_t *note)
448 {
449     challenge_t *challenge = &scherzo->challenge;
450
451     if (note->pitch == challenge->note->pitch &&
452         note->octave == challenge->note->octave)
453     {
454         challenge->satisfied = 1;
455         score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
456     }
457     else
458     {
459         challenge->mistaken = 1;
460         score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
461     }
462 }
463
464 /* If the user got the right note (eventually), then score it in
465  * mnemon and show the next note. */
466 static void
467 _score_challenge (scherzo_t *scherzo)
468 {
469     challenge_t *challenge = &scherzo->challenge;
470
471     if (! challenge->satisfied)
472         return;
473
474     mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
475                        ! challenge->mistaken);
476
477     _select_challenge (scherzo);
478 }
479
480 static int
481 on_midi_input (unused (GIOChannel *channel),
482                unused (GIOCondition condition),
483                void *user_data)
484 {
485     unsigned char buf[MIDI_BUF_SIZE], *next;
486     scherzo_t *scherzo = user_data;
487     ssize_t remaining;
488     snd_seq_event_t event;
489     score_note_t *note;
490     int need_redraw = FALSE;
491
492     remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
493
494     next = buf;
495     while (remaining) {
496         long consumed;
497
498         consumed = snd_midi_event_encode (scherzo->snd_midi_event,
499                                           next, remaining, &event);
500
501         remaining -= consumed;
502         next += consumed;
503
504         switch (event.type) {
505         case SND_SEQ_EVENT_NONE:
506             /* Incomplete event. Nothing to do. */
507             break;
508         case SND_SEQ_EVENT_NOTEON:
509             note = scherzo_add_note_midi (scherzo, event.data.note.note);
510             _judge_note (scherzo, note);
511             need_redraw = TRUE;
512             break;
513         case SND_SEQ_EVENT_NOTEOFF:
514             scherzo_remove_note_midi (scherzo, event.data.note.note);
515             _score_challenge (scherzo);
516             need_redraw = TRUE;
517             break;
518         case SND_SEQ_EVENT_CLOCK:
519             /* Ignore for now as my piano sends a constant stream of these. */
520             break;
521         case SND_SEQ_EVENT_SENSING:
522             /* Ignore for now as my piano sends a constant stream of these. */
523             break;
524         default:
525             fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
526                      event.type);
527             break;
528         }
529     }
530
531     if (need_redraw)
532         gtk_widget_queue_draw (scherzo->window);
533
534     /* Return TRUE to continue to get called in the future. */
535     return TRUE;
536 }
537
538 int
539 main (int argc, char *argv[])
540 {
541     GtkWidget *drawing_area;
542     scherzo_t scherzo;
543     int err;
544
545     srand (time (NULL));
546
547     gtk_init (&argc, &argv);
548
549     scherzo.ctx = talloc_new (NULL);
550
551     scherzo.score = score_create (scherzo.ctx);
552     scherzo.staff_height = 100;
553     score_set_staff_height (scherzo.score, scherzo.staff_height);
554
555     score_add_brace (scherzo.score, 2);
556     scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
557     scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
558
559     scherzo.num_notes_pressed = 0;
560     scherzo.notes_pressed = NULL;
561
562     mnemon_init (&scherzo.mnemon);
563     /* XXX: Should create a default file if one cannot be loaded. */
564     mnemon_load_category (&scherzo.mnemon, "scherzo-notes");
565
566     scherzo.challenge.note = NULL;
567     _select_challenge (&scherzo);
568
569     err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
570     if (err) {
571         fprintf (stderr, "Out of memory.\n");
572         return 1;
573     }
574
575 #define MIDI_DEVICE "/dev/midi1"
576     scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
577     if (scherzo.midi_fd < 0) {
578         printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
579     } else {
580         GIOChannel *channel;
581
582         channel = g_io_channel_unix_new (scherzo.midi_fd);
583         g_io_channel_set_encoding (channel, NULL, NULL);
584         g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
585     }
586
587     scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
588
589     gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
590
591     g_signal_connect (scherzo.window, "delete-event",
592                       G_CALLBACK (on_delete_event_quit), NULL);
593
594     drawing_area = gtk_drawing_area_new ();
595
596     gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
597
598     g_signal_connect (drawing_area, "expose-event",  
599                       G_CALLBACK (on_expose_event_draw),
600                       &scherzo);
601
602     g_signal_connect (scherzo.window, "key-press-event",
603                       G_CALLBACK (on_key_press_event),
604                       &scherzo);
605
606     g_signal_connect (scherzo.window, "key-release-event",
607                       G_CALLBACK (on_key_release_event),
608                       &scherzo);
609     
610     gtk_widget_show_all (scherzo.window);
611     
612     gtk_main ();
613
614     mnemon_save (&scherzo.mnemon);
615
616     snd_midi_event_free (scherzo.snd_midi_event);
617
618     talloc_free (scherzo.ctx);
619
620     return 0;
621 }