1 /* scherzo - Music notation training
3 * Copyright © 2010 Carl Worth
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.
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.
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/ .
20 #include <gdk/gdkkeysyms.h>
22 #include <asoundlib.h>
27 #define unused(foo) foo __attribute__((unused))
29 #define MIDI_BUF_SIZE 4096
31 typedef struct challenge
42 typedef struct scherzo
49 score_staff_t *treble;
52 snd_midi_event_t *snd_midi_event;
55 challenge_t challenge;
57 int num_notes_pressed;
58 score_note_t **notes_pressed;
62 on_delete_event_quit (unused (GtkWidget *widget),
63 unused (GdkEvent *event),
64 unused (gpointer user_data))
68 /* Returning FALSE allows the default handler for delete-event
69 * to proceed to cleanup the widget. */
74 on_expose_event_draw (GtkWidget *widget,
75 unused (GdkEventExpose *expose),
78 scherzo_t *scherzo = user_data;
79 score_t *score = scherzo->score;
81 GtkAllocation allocation;
82 static const int pad = 10;
85 gtk_widget_get_allocation (widget, &allocation);
86 widget_width = allocation.width;
88 cr = gdk_cairo_create (widget->window);
90 /* White background */
91 cairo_set_source_rgb (cr, 1.0, 1.0, 1.0);
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);
99 score_draw (score, cr);
105 on_key_press_event (GtkWidget *widget,
109 scherzo_t *scherzo = user_data;
111 switch (key->keyval) {
115 case GDK_KEY_KP_Equal:
116 scherzo->staff_height += 4;
117 gtk_widget_queue_draw (widget);
121 case GDK_KEY_KP_Subtract:
122 scherzo->staff_height -= 4;
123 gtk_widget_queue_draw (widget);
133 /* Allow the event to propagate to other handlers. */
138 _midi_to_score_pitch_and_octave (unsigned char midi_note,
139 score_pitch_t *pitch,
142 *octave = midi_note / 12 - 1;
144 switch (midi_note % 12)
147 *pitch = SCORE_PITCH_C;
150 *pitch = SCORE_PITCH_Cs;
153 *pitch = SCORE_PITCH_D;
156 *pitch = SCORE_PITCH_Ds;
159 *pitch = SCORE_PITCH_E;
162 *pitch = SCORE_PITCH_F;
165 *pitch = SCORE_PITCH_Fs;
168 *pitch = SCORE_PITCH_G;
171 *pitch = SCORE_PITCH_Gs;
174 *pitch = SCORE_PITCH_A;
177 *pitch = SCORE_PITCH_As;
180 *pitch = SCORE_PITCH_B;
185 static score_note_t *
186 scherzo_add_note_midi (scherzo_t *scherzo, unsigned char midi_note)
188 score_staff_t *staff;
193 staff = scherzo->challenge.staff;
195 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
197 note = score_add_note (staff, pitch, octave, SCORE_DURATION_WHOLE);
199 scherzo->num_notes_pressed++;
200 scherzo->notes_pressed = talloc_realloc (scherzo->ctx,
201 scherzo->notes_pressed,
203 scherzo->num_notes_pressed);
204 if (scherzo->notes_pressed == NULL) {
205 fprintf (stderr, "Out of memory.\n");
209 scherzo->notes_pressed[scherzo->num_notes_pressed - 1] = note;
215 scherzo_remove_note_midi (scherzo_t *scherzo, unsigned char midi_note)
222 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
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*));
233 scherzo->num_notes_pressed--;
240 _select_challenge (scherzo_t *scherzo)
242 category_t *category_unused;
243 bool_t introduced_unused;
245 challenge_t *challenge = &scherzo->challenge;
250 if (challenge->note) {
251 score_remove_note (challenge->note);
252 challenge->note = NULL;
255 mnemon_select_item (&scherzo->mnemon,
257 &challenge->item_index,
261 item = challenge->bin->items[challenge->item_index];
264 if (strncmp (s, "treble:", 7) == 0) {
266 challenge->staff = scherzo->treble;
267 } else if (strncmp (s, "bass:", 5) == 0) {
269 challenge->staff = scherzo->bass;
272 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
279 pitch = SCORE_PITCH_VALUE(C, NATURAL);
282 pitch = SCORE_PITCH_VALUE(D, NATURAL);
285 pitch = SCORE_PITCH_VALUE(E, NATURAL);
288 pitch = SCORE_PITCH_VALUE(F, NATURAL);
291 pitch = SCORE_PITCH_VALUE(G, NATURAL);
294 pitch = SCORE_PITCH_VALUE(A, NATURAL);
297 pitch = SCORE_PITCH_VALUE(B, NATURAL);
300 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
305 if (*s < '0' || *s > '9') {
306 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
312 challenge->note = score_add_note (challenge->staff, pitch, octave,
313 SCORE_DURATION_WHOLE);
314 challenge->satisfied = 0;
315 challenge->mistaken = 0;
318 /* Determine whether the user hit the correct note. */
320 _judge_note (scherzo_t *scherzo, score_note_t *note)
322 challenge_t *challenge = &scherzo->challenge;
324 if (note->pitch == challenge->note->pitch &&
325 note->octave == challenge->note->octave)
327 challenge->satisfied = 1;
331 challenge->mistaken = 1;
335 /* If the user got the right note (eventually), then score it in
336 * mnemon and show the next note. */
338 _score_challenge (scherzo_t *scherzo)
340 challenge_t *challenge = &scherzo->challenge;
342 if (! challenge->satisfied)
345 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
346 ! challenge->mistaken);
348 _select_challenge (scherzo);
352 on_midi_input (unused (GIOChannel *channel),
353 unused (GIOCondition condition),
356 unsigned char buf[MIDI_BUF_SIZE], *next;
357 scherzo_t *scherzo = user_data;
359 snd_seq_event_t event;
362 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
368 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
369 next, remaining, &event);
371 remaining -= consumed;
373 switch (event.type) {
374 case SND_SEQ_EVENT_NONE:
375 /* Incomplete event. Nothing to do. */
377 case SND_SEQ_EVENT_NOTEON:
378 note = scherzo_add_note_midi (scherzo, event.data.note.note);
379 _judge_note (scherzo, note);
380 gtk_widget_queue_draw (scherzo->window);
382 case SND_SEQ_EVENT_NOTEOFF:
383 scherzo_remove_note_midi (scherzo, event.data.note.note);
384 _score_challenge (scherzo);
385 gtk_widget_queue_draw (scherzo->window);
387 case SND_SEQ_EVENT_CLOCK:
388 /* Ignore for now as my piano sends a constant stream of these. */
390 case SND_SEQ_EVENT_SENSING:
391 /* Ignore for now as my piano sends a constant stream of these. */
394 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
400 /* Return TRUE to continue to get called in the future. */
405 main (int argc, char *argv[])
407 GtkWidget *drawing_area;
413 gtk_init (&argc, &argv);
415 scherzo.ctx = talloc_new (NULL);
417 scherzo.score = score_create (scherzo.ctx);
418 scherzo.staff_height = 48;
419 score_set_staff_height (scherzo.score, scherzo.staff_height);
421 score_add_brace (scherzo.score, 2);
422 scherzo.treble = score_add_staff (scherzo.score, SCORE_CLEF_G);
423 scherzo.bass = score_add_staff (scherzo.score, SCORE_CLEF_F);
425 scherzo.num_notes_pressed = 0;
426 scherzo.notes_pressed = NULL;
428 mnemon_init (&scherzo.mnemon);
429 /* XXX: Should create a default file if one cannot be loaded. */
430 mnemon_load_category (&scherzo.mnemon, "scherzo");
432 scherzo.challenge.note = NULL;
433 _select_challenge (&scherzo);
435 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
437 fprintf (stderr, "Out of memory.\n");
441 #define MIDI_DEVICE "/dev/midi1"
442 scherzo.midi_fd = open (MIDI_DEVICE, O_RDONLY);
443 if (scherzo.midi_fd < 0) {
444 printf ("failed to open " MIDI_DEVICE ". Midi input will not be available.\n");
448 channel = g_io_channel_unix_new (scherzo.midi_fd);
449 g_io_channel_set_encoding (channel, NULL, NULL);
450 g_io_add_watch (channel, G_IO_IN, on_midi_input, &scherzo);
453 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
455 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 600, 400);
457 g_signal_connect (scherzo.window, "delete-event",
458 G_CALLBACK (on_delete_event_quit), NULL);
460 drawing_area = gtk_drawing_area_new ();
462 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
464 g_signal_connect (drawing_area, "expose-event",
465 G_CALLBACK (on_expose_event_draw),
468 g_signal_connect (scherzo.window, "key-press-event",
469 G_CALLBACK (on_key_press_event),
472 gtk_widget_show_all (scherzo.window);
476 mnemon_save (&scherzo.mnemon);
478 snd_midi_event_free (scherzo.snd_midi_event);
480 talloc_free (scherzo.ctx);