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_staff_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)
217 score_staff_t *staff;
223 staff = scherzo->challenge.staff;
225 _midi_to_score_pitch_and_octave (midi_note, &pitch, &octave);
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*));
236 scherzo->num_notes_pressed--;
243 _select_challenge (scherzo_t *scherzo)
245 category_t *category_unused;
246 bool_t introduced_unused;
248 challenge_t *challenge = &scherzo->challenge;
253 if (challenge->note) {
254 score_staff_remove_note (challenge->staff, challenge->note);
255 challenge->note = NULL;
258 mnemon_select_item (&scherzo->mnemon,
260 &challenge->item_index,
264 item = challenge->bin->items[challenge->item_index];
267 if (strncmp (s, "treble:", 7) == 0) {
269 challenge->staff = scherzo->treble;
270 } else if (strncmp (s, "bass:", 5) == 0) {
272 challenge->staff = scherzo->bass;
275 "Malformed staff name: %s (expected 'treble:' or 'bass:')\n",
282 pitch = SCORE_PITCH_VALUE(C, NATURAL);
285 pitch = SCORE_PITCH_VALUE(D, NATURAL);
288 pitch = SCORE_PITCH_VALUE(E, NATURAL);
291 pitch = SCORE_PITCH_VALUE(F, NATURAL);
294 pitch = SCORE_PITCH_VALUE(G, NATURAL);
297 pitch = SCORE_PITCH_VALUE(A, NATURAL);
300 pitch = SCORE_PITCH_VALUE(B, NATURAL);
303 fprintf (stderr, "Malformed pitch name: %s (expected 'A' - 'G')\n", s);
308 if (*s < '0' || *s > '9') {
309 fprintf (stderr, "Malformed octave number: %s (expected '0' - '9')\n", s);
315 challenge->note = score_staff_add_note (challenge->staff, pitch, octave,
316 SCORE_DURATION_WHOLE);
317 challenge->satisfied = 0;
318 challenge->mistaken = 0;
321 /* Determine whether the user hit the correct note. */
323 _judge_note (scherzo_t *scherzo, score_note_t *note)
325 challenge_t *challenge = &scherzo->challenge;
327 if (note->pitch == challenge->note->pitch &&
328 note->octave == challenge->note->octave)
330 challenge->satisfied = 1;
334 challenge->mistaken = 1;
338 /* If the user got the right note (eventually), then score it in
339 * mnemon and show the next note. */
341 _score_challenge (scherzo_t *scherzo)
343 challenge_t *challenge = &scherzo->challenge;
345 if (! challenge->satisfied)
348 mnemon_score_item (&scherzo->mnemon, challenge->bin, challenge->item_index,
349 ! challenge->mistaken);
351 _select_challenge (scherzo);
355 on_midi_input (unused (GIOChannel *channel),
356 unused (GIOCondition condition),
359 unsigned char buf[MIDI_BUF_SIZE], *next;
360 scherzo_t *scherzo = user_data;
362 snd_seq_event_t event;
365 remaining = read (scherzo->midi_fd, buf, MIDI_BUF_SIZE);
371 consumed = snd_midi_event_encode (scherzo->snd_midi_event,
372 next, remaining, &event);
374 remaining -= consumed;
376 switch (event.type) {
377 case SND_SEQ_EVENT_NONE:
378 /* Incomplete event. Nothing to do. */
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);
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);
390 case SND_SEQ_EVENT_CLOCK:
391 /* Ignore for now as my piano sends a constant stream of these. */
393 case SND_SEQ_EVENT_SENSING:
394 /* Ignore for now as my piano sends a constant stream of these. */
397 fprintf (stderr, "Fixme: Do not yet know how to handle MIDI event %d\n",
403 /* Return TRUE to continue to get called in the future. */
408 main (int argc, char *argv[])
410 GtkWidget *drawing_area;
416 gtk_init (&argc, &argv);
418 scherzo.ctx = talloc_new (NULL);
420 scherzo.score = score_create (scherzo.ctx);
421 scherzo.staff_height = 48;
422 score_set_staff_height (scherzo.score, scherzo.staff_height);
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);
428 scherzo.num_notes_pressed = 0;
429 scherzo.notes_pressed = NULL;
431 mnemon_init (&scherzo.mnemon);
432 /* XXX: Should create a default file if one cannot be loaded. */
433 mnemon_load_category (&scherzo.mnemon, "scherzo");
435 scherzo.challenge.note = NULL;
436 _select_challenge (&scherzo);
438 err = snd_midi_event_new (MIDI_BUF_SIZE, &scherzo.snd_midi_event);
440 fprintf (stderr, "Out of memory.\n");
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");
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);
456 scherzo.window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
458 gtk_window_set_default_size (GTK_WINDOW (scherzo.window), 600, 400);
460 g_signal_connect (scherzo.window, "delete-event",
461 G_CALLBACK (on_delete_event_quit), NULL);
463 drawing_area = gtk_drawing_area_new ();
465 gtk_container_add (GTK_CONTAINER (scherzo.window), drawing_area);
467 g_signal_connect (drawing_area, "expose-event",
468 G_CALLBACK (on_expose_event_draw),
471 g_signal_connect (scherzo.window, "key-press-event",
472 G_CALLBACK (on_key_press_event),
475 gtk_widget_show_all (scherzo.window);
479 mnemon_save (&scherzo.mnemon);
481 snd_midi_event_free (scherzo.snd_midi_event);
483 talloc_free (scherzo.ctx);