]> git.cworth.org Git - scherzo/blob - scherzo.c
Add correct sizing/spacing of staves and braces for ledger lines.
[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     }
329     else
330     {
331         challenge->mistaken = 1;
332     }
333 }
334
335 /* If the user got the right note (eventually), then score it in
336  * mnemon and show the next note. */
337 static void
338 _score_challenge (scherzo_t *scherzo)
339 {
340     challenge_t *challenge = &scherzo->challenge;
341
342     if (! challenge->satisfied)
343         return;
344
345     mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
346                        ! challenge->mistaken);
347
348     _select_challenge (scherzo);
349 }
350
351 static int
352 on_midi_input (unused (GIOChannel *channel),
353                unused (GIOCondition condition),
354                void *user_data)
355 {
356     unsigned char buf[MIDI_BUF_SIZE], *next;
357     scherzo_t *scherzo = user_data;
358     ssize_t remaining;
359     snd_seq_event_t event;
360     score_note_t *note;
361     int need_redraw = FALSE;
362
363     remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
364
365     next = buf;
366     while (remaining) {
367         long consumed;
368
369         consumed = snd_midi_event_encode (scherzo->snd_midi_event,
370                                           next, remaining, &event);
371
372         remaining -= consumed;
373         next += consumed;
374
375         switch (event.type) {
376         case SND_SEQ_EVENT_NONE:
377             /* Incomplete event. Nothing to do. */
378             break;
379         case SND_SEQ_EVENT_NOTEON:
380             note = scherzo_add_note_midi (scherzo, event.data.note.note);
381             _judge_note (scherzo, note);
382             need_redraw = TRUE;
383             break;
384         case SND_SEQ_EVENT_NOTEOFF:
385             scherzo_remove_note_midi (scherzo, event.data.note.note);
386             _score_challenge (scherzo);
387             need_redraw = TRUE;
388             break;
389         case SND_SEQ_EVENT_CLOCK:
390             /* Ignore for now as my piano sends a constant stream of these. */
391             break;
392         case SND_SEQ_EVENT_SENSING:
393             /* Ignore for now as my piano sends a constant stream of these. */
394             break;
395         default:
396             fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
397                      event.type);
398             break;
399         }
400     }
401
402     if (need_redraw)
403         gtk_widget_queue_draw (scherzo->window);
404
405     /* Return TRUE to continue to get called in the future. */
406     return TRUE;
407 }
408
409 int
410 main (int argc, char *argv[])
411 {
412     GtkWidget *drawing_area;
413     scherzo_t scherzo;
414     int err;
415
416     srand (time (NULL));
417
418     gtk_init (&argc, &argv);
419
420     scherzo.ctx = talloc_new (NULL);
421
422     scherzo.score = score_create (scherzo.ctx);
423     scherzo.staff_height = 48;
424     score_set_staff_height (scherzo.score, scherzo.staff_height);
425
426     score_add_brace (scherzo.score, 2);
427     scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
428     scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
429
430     scherzo.num_notes_pressed = 0;
431     scherzo.notes_pressed = NULL;
432
433     mnemon_init (&scherzo.mnemon);
434     /* XXX: Should create a default file if one cannot be loaded. */
435     mnemon_load_category (&scherzo.mnemon, "scherzo");
436
437     scherzo.challenge.note = NULL;
438     _select_challenge (&scherzo);
439
440     err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
441     if (err) {
442         fprintf (stderr, "Out of memory.\n");
443         return 1;
444     }
445
446 #define MIDI_DEVICE "/dev/midi1"
447     scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
448     if (scherzo.midi_fd < 0) {
449         printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
450     } else {
451         GIOChannel *channel;
452
453         channel = g_io_channel_unix_new (scherzo.midi_fd);
454         g_io_channel_set_encoding (channel, NULL, NULL);
455         g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
456     }
457
458     scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
459
460     gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 600, 400);
461
462     g_signal_connect (scherzo.window, "delete-event",
463                       G_CALLBACK (on_delete_event_quit), NULL);
464
465     drawing_area = gtk_drawing_area_new ();
466
467     gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
468
469     g_signal_connect (drawing_area, "expose-event",  
470                       G_CALLBACK (on_expose_event_draw),
471                       &scherzo);
472
473     g_signal_connect (scherzo.window, "key-press-event",  
474                       G_CALLBACK (on_key_press_event),
475                       &scherzo);
476     
477     gtk_widget_show_all (scherzo.window);
478     
479     gtk_main ();
480
481     mnemon_save (&scherzo.mnemon);
482
483     snd_midi_event_free (scherzo.snd_midi_event);
484
485     talloc_free (scherzo.ctx);
486
487     return 0;
488 }