]> git.cworth.org Git - scherzo/blob - scherzo.c
Add some color coding of input notes.
[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, 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_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_pitch_t pitch;
218     int octave;
219     score_note_t *note;
220     int i;
221
222     _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
223
224     for (i = 0; i < scherzo->num_notes_pressed; i++) {
225         note = scherzo->notes_pressed[i];
226         if (note->pitch == pitch && note->octave == octave) {
227             score_remove_note (note);
228             if (i < scherzo->num_notes_pressed - 1) {
229                 memmove (scherzo->notes_pressed + i,
230                          scherzo->notes_pressed + i + 1,
231                          (scherzo->num_notes_pressed - 1 - i) * sizeof (score_note_t*));
232             }
233             scherzo->num_notes_pressed--;
234             i--;
235         }
236     }
237 }
238
239 void
240 _select_challenge (scherzo_t *scherzo)
241 {
242     category_t *category_unused;
243     bool_t introduced_unused;
244     item_t *item;
245     challenge_t *challenge = &scherzo->challenge;
246     score_pitch_t pitch;
247     int octave;
248     char *s;
249
250     if (challenge->note) {
251         score_remove_note (challenge->note);
252         challenge->note = NULL;
253     }
254
255     mnemon_select_item (&scherzo->mnemon,
256                         &challenge->bin,
257                         &challenge->item_index,
258                         &category_unused,
259                         &introduced_unused);
260
261     item = challenge->bin->items[challenge->item_index];
262
263     s = item->challenge;
264     if (strncmp (s, "treble:", 7) == 0) {
265         s += 7;
266         challenge->staff = scherzo->treble;
267     } else if (strncmp (s, "bass:", 5) == 0) {
268         s += 5;
269         challenge->staff = scherzo->bass;
270     } else {
271         fprintf (stderr,
272                  "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
273                  s);
274         exit (1);
275     }
276
277     switch (*s) {
278     case 'C':
279         pitch = SCORE_PITCH_VALUE(C, NATURAL);
280         break;
281     case 'D':
282         pitch = SCORE_PITCH_VALUE(D, NATURAL);
283         break;
284     case 'E':
285         pitch = SCORE_PITCH_VALUE(E, NATURAL);
286         break;
287     case 'F':
288         pitch = SCORE_PITCH_VALUE(F, NATURAL);
289         break;
290     case 'G':
291         pitch = SCORE_PITCH_VALUE(G, NATURAL);
292         break;
293     case 'A':
294         pitch = SCORE_PITCH_VALUE(A, NATURAL);
295         break;
296     case 'B':
297         pitch = SCORE_PITCH_VALUE(B, NATURAL);
298         break;
299     default:
300         fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
301         exit (1);
302     }
303     s++;
304
305     if (*s < '0' || *s > '9') {
306         fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
307         exit (1);
308     }
309
310     octave = *s - '0';
311
312     challenge->note = score_add_note (challenge->staff, pitch, octave,
313                                       SCORE_DURATION_WHOLE);
314     challenge->satisfied = 0;
315     challenge->mistaken = 0;
316 }
317
318 /* Determine whether the user hit the correct note. */
319 static void
320 _judge_note (scherzo_t *scherzo, score_note_t *note)
321 {
322     challenge_t *challenge = &scherzo->challenge;
323
324     if (note->pitch == challenge->note->pitch &&
325         note->octave == challenge->note->octave)
326     {
327         challenge->satisfied = 1;
328         score_set_note_color_rgb (note, 18/256., 130/256., 28/256.); /* green */
329     }
330     else
331     {
332         challenge->mistaken = 1;
333         score_set_note_color_rgb (note, 184/256., 4/256., 22/256.); /* red */
334     }
335 }
336
337 /* If the user got the right note (eventually), then score it in
338  * mnemon and show the next note. */
339 static void
340 _score_challenge (scherzo_t *scherzo)
341 {
342     challenge_t *challenge = &scherzo->challenge;
343
344     if (! challenge->satisfied)
345         return;
346
347     mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
348                        ! challenge->mistaken);
349
350     _select_challenge (scherzo);
351 }
352
353 static int
354 on_midi_input (unused (GIOChannel *channel),
355                unused (GIOCondition condition),
356                void *user_data)
357 {
358     unsigned char buf[MIDI_BUF_SIZE], *next;
359     scherzo_t *scherzo = user_data;
360     ssize_t remaining;
361     snd_seq_event_t event;
362     score_note_t *note;
363     int need_redraw = FALSE;
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         next += consumed;
376
377         switch (event.type) {
378         case SND_SEQ_EVENT_NONE:
379             /* Incomplete event. Nothing to do. */
380             break;
381         case SND_SEQ_EVENT_NOTEON:
382             note = scherzo_add_note_midi (scherzo, event.data.note.note);
383             _judge_note (scherzo, note);
384             need_redraw = TRUE;
385             break;
386         case SND_SEQ_EVENT_NOTEOFF:
387             scherzo_remove_note_midi (scherzo, event.data.note.note);
388             _score_challenge (scherzo);
389             need_redraw = TRUE;
390             break;
391         case SND_SEQ_EVENT_CLOCK:
392             /* Ignore for now as my piano sends a constant stream of these. */
393             break;
394         case SND_SEQ_EVENT_SENSING:
395             /* Ignore for now as my piano sends a constant stream of these. */
396             break;
397         default:
398             fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
399                      event.type);
400             break;
401         }
402     }
403
404     if (need_redraw)
405         gtk_widget_queue_draw (scherzo->window);
406
407     /* Return TRUE to continue to get called in the future. */
408     return TRUE;
409 }
410
411 int
412 main (int argc, char *argv[])
413 {
414     GtkWidget *drawing_area;
415     scherzo_t scherzo;
416     int err;
417
418     srand (time (NULL));
419
420     gtk_init (&argc, &argv);
421
422     scherzo.ctx = talloc_new (NULL);
423
424     scherzo.score = score_create (scherzo.ctx);
425     scherzo.staff_height = 100;
426     score_set_staff_height (scherzo.score, scherzo.staff_height);
427
428     score_add_brace (scherzo.score, 2);
429     scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
430     scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
431
432     scherzo.num_notes_pressed = 0;
433     scherzo.notes_pressed = NULL;
434
435     mnemon_init (&scherzo.mnemon);
436     /* XXX: Should create a default file if one cannot be loaded. */
437     mnemon_load_category (&scherzo.mnemon, "scherzo");
438
439     scherzo.challenge.note = NULL;
440     _select_challenge (&scherzo);
441
442     err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
443     if (err) {
444         fprintf (stderr, "Out of memory.\n");
445         return 1;
446     }
447
448 #define MIDI_DEVICE "/dev/midi1"
449     scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
450     if (scherzo.midi_fd < 0) {
451         printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
452     } else {
453         GIOChannel *channel;
454
455         channel = g_io_channel_unix_new (scherzo.midi_fd);
456         g_io_channel_set_encoding (channel, NULL, NULL);
457         g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
458     }
459
460     scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
461
462     gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 1000, 600);
463
464     g_signal_connect (scherzo.window, "delete-event",
465                       G_CALLBACK (on_delete_event_quit), NULL);
466
467     drawing_area = gtk_drawing_area_new ();
468
469     gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
470
471     g_signal_connect (drawing_area, "expose-event",  
472                       G_CALLBACK (on_expose_event_draw),
473                       &scherzo);
474
475     g_signal_connect (scherzo.window, "key-press-event",  
476                       G_CALLBACK (on_key_press_event),
477                       &scherzo);
478     
479     gtk_widget_show_all (scherzo.window);
480     
481     gtk_main ();
482
483     mnemon_save (&scherzo.mnemon);
484
485     snd_midi_event_free (scherzo.snd_midi_event);
486
487     talloc_free (scherzo.ctx);
488
489     return 0;
490 }