2 * Copyright © 2006 Carl Worth
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2, or (at your option)
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software Foundation,
16 * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA."
27 #include <sys/types.h>
35 #include <readline/readline.h>
36 #include <readline/history.h>
38 #define ASSERT_NOT_REACHED \
40 static const int NOT_REACHED = 0; \
41 assert (NOT_REACHED); \
46 typedef struct _item {
60 CATEGORY_ORDER_RANDOM,
61 CATEGORY_ORDER_SEQUENTIAL
71 typedef struct _category {
77 /* Support sequential introduction of items from bin 0 */
78 category_order_t order;
79 /* Support categories where responses are timed (0.0 == disable). */
82 /* Support challenges of non-text types (image, audio, etc.) */
83 challenge_type_t challenge_type;
86 typedef struct _mnemon {
91 category_t *categories;
104 xmalloc (size_t size)
110 fprintf (stderr, "Error: out of memory\n");
118 xrealloc (void *ptr, size_t size)
122 ret = realloc (ptr, size);
124 fprintf (stderr, "Error: out of memory\n");
132 xstrdup (const char *s)
138 fprintf (stderr, "Error: out of memory\n");
146 xstrndup (const char *s, size_t n)
150 ret = strndup (s, n);
152 fprintf (stderr, "Error: out of memory\n");
160 xasprintf (char **strp, const char *fmt, ...)
166 ret = vasprintf (strp, fmt, ap);
170 fprintf (stderr, "Error: out of memory\n");
176 item_init (item_t *item,
178 const char *challenge,
179 const char *response)
183 item->challenge = xmalloc (strlen (challenge) + 1 +
184 strlen (response) + 1);
185 item->response = item->challenge + strlen (challenge) + 1;
187 strcpy (item->challenge, challenge);
188 strcpy (item->response, response);
192 item_fini (item_t *item)
194 /* item->response shares allocation with item->challenge, so
195 * doesn't require a separate call to free */
196 free (item->challenge);
200 category_init (category_t *category,
203 category->name = xstrdup (name);
205 category->items_size = 0;
206 category->num_items = 0;
207 category->items = NULL;
208 category->order = CATEGORY_ORDER_RANDOM;
209 category->time_limit = 0.0;
210 category->bin_zero_head = 0;
211 category->challenge_type = CHALLENGE_TYPE_TEXT;
215 category_fini (category_t *category)
219 for (i = 0; i < category->num_items; i++)
220 item_fini (&category->items[i]);
222 free (category->items);
224 free (category->name);
228 category_grow (category_t *category)
230 if (category->items_size)
231 category->items_size *= 2;
233 category->items_size = 1;
235 category->items = xrealloc (category->items,
236 category->items_size * sizeof (item_t));
240 category_add_item (category_t *category,
242 const char *challenge,
243 const char *response)
247 if (category->num_items == category->items_size)
248 category_grow (category);
250 item = &category->items[category->num_items++];
252 item_init (item, score, challenge, response);
258 category_next_bin_zero_item (category_t *category)
260 int *i = &category->bin_zero_head;
262 for ( ; *i < category->num_items; *i = *i + 1)
263 if (category->items[*i].score == 0)
264 return &category->items[*i];
270 category_print (category_t *category,
276 fprintf (file, "order = %s\n\n",
277 category->order == CATEGORY_ORDER_RANDOM ? "random" : "sequential");
278 fprintf (file, "time = %f\n\n",
279 category->time_limit);
281 fprintf (file, "challenge = ");
282 switch (category->challenge_type) {
283 case CHALLENGE_TYPE_TEXT:
284 fprintf (file, "text");
286 case CHALLENGE_TYPE_IMAGE:
287 fprintf (file, "image");
289 case CHALLENGE_TYPE_AUDIO:
290 fprintf (file, "audio");
292 case CHALLENGE_TYPE_MIDI:
293 fprintf (file, "midi");
296 fprintf (file, "\n\n");
298 for (i = 0; i < category->num_items; i++) {
299 item = &category->items[i];
301 fprintf (file, "\n");
302 fprintf (file, "%d\n%s\n%s\n",
310 bin_init (bin_t *bin,
321 bin_fini (bin_t *bin)
327 bin_grow (bin_t *bin)
330 bin->items_size *= 2;
334 bin->items = xrealloc (bin->items,
335 bin->items_size * sizeof (item_t*));
339 bin_add_item (bin_t *bin,
342 assert (item->score == bin->score);
344 if (bin->num_items == bin->items_size)
347 bin->items[bin->num_items++] = item;
351 bin_remove_item (bin_t *bin,
354 /* Replace the current item with the last item, (no need to shift
355 * any more than that since we don't care about the order of the
356 * items within a bin). */
359 bin->items[item_index] = bin->items[bin->num_items];
362 /* Find the index for an item within a bin.
364 * XXX: This is currently a linear search, so is a potential
365 * performance problem.
368 bin_item_index (bin_t *bin,
373 for (i = 0; i < bin->num_items; i++)
374 if (bin->items[i] == item)
380 typedef int (item_match_predicate_t) (void *closure, item_t *item);
382 /* Return the number of items in the bin from the given category (or
383 * from all categories if category == NULL) */
385 bin_num_items_matching (bin_t *bin,
386 item_match_predicate_t *predicate,
389 int i, num_items = 0;
391 if (predicate == NULL)
392 return bin->num_items;
394 for (i = 0; i < bin->num_items; i++)
395 if ((predicate) (closure, bin->items[i]))
402 mnemon_init (mnemon_t *mnemon)
406 home = getenv ("HOME");
410 xasprintf (&mnemon->dir_name, "%s/.mnemon", getenv ("HOME"));
412 mnemon->categories_size = 0;
413 mnemon->num_categories = 0;
414 mnemon->categories = NULL;
416 mnemon->bins_size = 0;
417 mnemon->num_bins = 0;
420 mnemon->to_introduce = 10;
421 mnemon->to_master = 10;
422 mnemon->unlearned = 0;
423 mnemon->mastered = -1;
427 mnemon_fini (mnemon_t *mnemon)
431 for (i = 0; i < mnemon->num_bins; i++)
432 bin_fini (&mnemon->bins[i]);
435 for (i = 0; i < mnemon->num_categories; i++)
436 category_fini (&mnemon->categories[i]);
437 free (mnemon->categories);
439 free (mnemon->dir_name);
443 mnemon_categories_grow (mnemon_t *mnemon)
445 if (mnemon->categories_size)
446 mnemon->categories_size *= 2;
448 mnemon->categories_size = 1;
450 mnemon->categories = xrealloc (mnemon->categories,
451 mnemon->categories_size * sizeof (category_t));
454 /* Get a category by name if it exists */
456 mnemon_get_category_if_exists (mnemon_t *mnemon,
461 for (i = 0; i < mnemon->num_categories; i++)
462 if (strcmp (mnemon->categories[i].name, name) == 0)
463 return &mnemon->categories[i];
468 /* Get a category by name, creating new one if necessary. */
470 mnemon_get_category (mnemon_t *mnemon,
473 category_t *category;
475 category = mnemon_get_category_if_exists (mnemon, name);
479 mnemon_categories_grow (mnemon);
481 category = &mnemon->categories[mnemon->num_categories++];
483 category_init (category, name);
489 mnemon_bins_grow (mnemon_t *mnemon)
491 if (mnemon->bins_size)
492 mnemon->bins_size *= 2;
494 mnemon->bins_size = 1;
496 mnemon->bins = xrealloc (mnemon->bins,
497 mnemon->bins_size * sizeof (bin_t));
501 mnemon_get_bin (mnemon_t *mnemon,
507 for (i = 0; i < mnemon->num_bins; i++)
508 if (mnemon->bins[i].score == score)
509 return &mnemon->bins[i];
510 else if (mnemon->bins[i].score > score)
513 if (mnemon->num_bins == mnemon->bins_size)
514 mnemon_bins_grow (mnemon);
516 bin = &mnemon->bins[i];
518 /* Make room to insert new bin at its sorted location. */
519 if (i < mnemon->num_bins)
520 memmove (bin + 1, bin, (mnemon->num_bins - i) * sizeof (bin_t));
523 bin_init (bin, score);
529 mnemon_remove_bin (mnemon_t *mnemon,
532 int i = bin - mnemon->bins;
536 memmove (bin, bin + 1, (mnemon->num_bins - i) * sizeof (bin_t));
543 int len = strlen (s);
546 if (s[len - 1] == '\n')
551 trim_space (char *string)
556 while (*s && isspace (*s))
561 s = string + strlen (string) - 1;
562 while (s > string && isspace (*s)) {
571 mnemon_load_category (mnemon_t *mnemon,
575 char *line = NULL, *end;
576 size_t line_size = 0;
580 category_t *category;
583 path = xmalloc (strlen (mnemon->dir_name) + 1 + strlen (name) + 1);
584 sprintf (path, "%s/%s", mnemon->dir_name, name);
586 file = fopen (path, "r");
588 fprintf (stderr, "Error: Failed to open %s: %s\n",
589 path, strerror (errno));
593 category = mnemon_get_category (mnemon, name);
595 #define READ_LINE do { \
596 bytes_read = getline (&line, &line_size, file); \
597 if (bytes_read == -1) \
605 char *name, *equal, *value;
607 /* Ignore blank lines */
612 /* An initial digit means we hit an item. Trigger the
613 * spaghetti machine. */
614 if ((*line >= '0' && *line <= '9') || *line == '-')
617 equal = strchr (line, '=');
619 fprintf (stderr, "Malformed option, (expected name=value): \"%s\" at %s:%d\n",
620 line, path, line_count);
628 name = trim_space (name);
629 value = trim_space (value);
631 if (strcmp (name, "order") == 0) {
632 if (strcmp (value, "sequential") == 0) {
633 category->order = CATEGORY_ORDER_SEQUENTIAL;
634 } else if (strcmp (value, "random") == 0) {
635 category->order = CATEGORY_ORDER_RANDOM;
637 fprintf (stderr, "Unknown value for \"order\" option \"%s\" at %s:%d\n",
638 value, path, line_count);
641 } else if (strcmp (name, "time") == 0) {
644 limit = strtod (value, &end);
645 while (isspace (*end))
648 category->time_limit = limit;
650 fprintf (stderr, "Failed to parse time value: %s at %s:%d\n",
651 value, path, line_count);
654 } else if (strcmp (name, "challenge") == 0) {
655 if (strcmp (value, "text") == 0) {
656 category->challenge_type = CHALLENGE_TYPE_TEXT;
657 } else if (strcmp (value, "image") == 0) {
658 category->challenge_type = CHALLENGE_TYPE_IMAGE;
659 } else if (strcmp (value, "audio") == 0) {
660 category->challenge_type = CHALLENGE_TYPE_AUDIO;
661 } else if (strcmp (value, "midi") == 0) {
662 category->challenge_type = CHALLENGE_TYPE_MIDI;
664 fprintf (stderr, "Unknown value for \"challenge\" option \"%s\" at %s:%d\n",
665 value, path, line_count);
669 fprintf (stderr, "Unknown option %s at %s:%d\n",
670 name, path, line_count);
678 char *challenge, *response;
680 /* Ignore blank lines */
685 /* Read bin number */
687 score = strtol (line, &end, 10);
689 fprintf (stderr, "Failed to parse bin number from \"%s\" at %s:%d\n",
690 line, path, line_count);
696 challenge = strdup (line);
702 category_add_item (category, score, challenge, response);
712 /* Resize category items to fit exactly. */
713 category->items_size = category->num_items;
714 category->items = xrealloc (category->items, category->items_size * sizeof (item_t));
716 /* Now that the category is completely loaded, with stable
717 * pointers to every item, we can add each item to its appropriate
719 for (i = 0; i < category->num_items; i++) {
720 item_t *item = &category->items[i];
721 bin_t *bin = mnemon_get_bin (mnemon, item->score);
723 bin_add_item (bin, item);
728 mnemon_load (mnemon_t *mnemon)
731 struct dirent *dirent;
733 dir = opendir (mnemon->dir_name);
735 fprintf (stderr, "Error: Failed to open directory %s: %s\n",
736 mnemon->dir_name, strerror (errno));
741 dirent = readdir (dir);
745 if (dirent->d_type == DT_REG) {
746 /* Ignore files matching *~, (yes, this shouldn't be
747 * hard-coded in such an ad-hoc way, but there you go. */
748 if (dirent->d_name[strlen(dirent->d_name)-1] != '~')
749 mnemon_load_category (mnemon, dirent->d_name);
757 mnemon_save (mnemon_t *mnemon)
760 char *filename, *lock_filename;
762 category_t *category;
764 for (i = 0; i < mnemon->num_categories; i++) {
765 category = &mnemon->categories[i];
767 xasprintf (&filename, "%s/%s",
768 mnemon->dir_name, category->name);
769 xasprintf (&lock_filename, "%s/.#%s",
770 mnemon->dir_name, category->name);
772 file = fopen (lock_filename, "w");
774 fprintf (stderr, "Error: Failed to open %s for writing: %s\n",
775 lock_filename, strerror (errno));
779 category_print (category, file);
781 fsync (fileno (file));
784 err = rename (lock_filename, filename);
786 fprintf (stderr, "Error: Failed to rename %s to %s: %s\n",
787 lock_filename, filename, strerror (errno));
792 free (lock_filename);
796 /* Return a uniformly-distributed pseudo-random integer within the
799 * 0 <= result < num_values
802 rand_within (int num_values)
804 return (int) (num_values * (rand() / (RAND_MAX + 1.0)));
807 /* Return an exponentially-distributed pseudo-random integer within
810 * 0 <= result < num_values
812 * The distribution is such that each successively larger value will
813 * occur with a probability of half of the previous value.
816 rand_within_exponential (int num_values)
819 static uint32_t mask = 0;
823 /* Optimize the constant case. */
833 while (mask > RAND_MAX)
840 if (ones == num_values)
848 /* Find the category to which an item belongs. */
850 mnemon_item_category (mnemon_t *mnemon,
853 category_t *category;
856 for (i = 0; i < mnemon->num_categories; i++) {
857 category = &mnemon->categories[i];
858 item_index = item - category->items;
859 if (item_index >= 0 && item_index < category->num_items)
866 typedef struct _item_in_category_closure
869 category_t *category;
870 } item_in_category_closure_t;
873 mnemon_item_in_category (void *closure, item_t *item)
875 item_in_category_closure_t *iicc = closure;
876 mnemon_t *mnemon = iicc->mnemon;
877 category_t *category = iicc->category;
879 return (mnemon_item_category (mnemon, item) == category);
882 typedef struct _item_in_category_of_length_closure
885 category_t *category;
887 } item_in_category_of_length_closure_t;
890 mnemon_item_in_category_of_length (void *closure, item_t *item)
892 item_in_category_of_length_closure_t *iicolc = closure;
893 mnemon_t *mnemon = iicolc->mnemon;
894 category_t *category = iicolc->category;
895 int length = iicolc->length;
897 if (mnemon_item_category (mnemon, item) != category)
900 return strlen (item->challenge) == length;
904 mnemon_select_item (mnemon_t *mnemon,
907 category_t **category_ret)
909 int bin_index, item_index;
912 category_t *category;
914 bin_index = rand_within_exponential (mnemon->num_bins);
915 bin = &mnemon->bins[bin_index];
917 /* The most intuitive understanding of the to_introduce counter is
918 * that it's tracking never-before-learned items as they are
919 * pulled from the bin with score 0. But that bin can become
920 * empty. So the refined rule is that we decrement to_introduce
921 * whenever we pull from the lowest-indexed bin with a
922 * non-negative score. */
923 if (mnemon->to_introduce && bin->score >=0 &&
924 (bin_index == 0 || mnemon->bins[bin_index-1].score < 0))
926 mnemon->to_introduce--;
929 item_index = rand_within (bin->num_items);
931 item = bin->items[item_index];
932 category = mnemon_item_category (mnemon, item);
934 if (bin->score == 0) {
935 if (category->order == CATEGORY_ORDER_SEQUENTIAL) {
936 item = category_next_bin_zero_item (category);
938 item_index = bin_item_index (bin, item);
943 *item_index_ret = item_index;
944 *category_ret = category;
948 #define HISTOGRAM_ROW_FORMAT "%3d: %3d"
949 #define HISTOGRAM_BAR_WIDTH 63
952 print_histogram_bar (double size,
955 int units_per_cell = (int) ceil (max / HISTOGRAM_BAR_WIDTH);
956 static char const *boxes[8] = {
961 while (size > units_per_cell) {
963 size -= units_per_cell;
966 size /= units_per_cell;
970 else if (size > 6.5/8.0)
972 else if (size > 5.5/8.0)
974 else if (size > 4.5/8.0)
976 else if (size > 3.5/8.0)
978 else if (size > 2.5/8.0)
980 else if (size > 1.5/8.0)
982 else if (size > 0.5/8.0)
989 mnemon_print_histogram (mnemon_t *mnemon,
990 const char *category_name,
993 int i, last_score, max;
994 category_t *category = NULL;
997 item_match_predicate_t *predicate = NULL;
998 void *closure = NULL;
999 item_in_category_closure_t item_in_category;
1000 item_in_category_of_length_closure_t item_in_category_of_length;
1002 if (mnemon->num_bins == 0)
1005 if (category_name) {
1006 category = mnemon_get_category_if_exists (mnemon, category_name);
1009 predicate = mnemon_item_in_category_of_length;
1010 item_in_category_of_length.mnemon = mnemon;
1011 item_in_category_of_length.category = category;
1012 item_in_category_of_length.length = length;
1013 closure = &item_in_category_of_length;
1015 predicate = mnemon_item_in_category;
1016 item_in_category.mnemon = mnemon;
1017 item_in_category.category = category;
1018 closure = &item_in_category;
1023 for (i = 0; i < mnemon->num_bins; i++) {
1024 num_items = bin_num_items_matching (&mnemon->bins[i],
1025 predicate, closure);
1026 if (i == 0 || num_items > max)
1030 for (i = 0; i < mnemon->num_bins; i++) {
1031 bin = &mnemon->bins[i];
1033 while (bin->score - last_score > 1)
1034 printf (HISTOGRAM_ROW_FORMAT "\n", ++last_score, 0);
1035 num_items = bin_num_items_matching (bin,
1036 predicate, closure);
1037 printf (HISTOGRAM_ROW_FORMAT " ", bin->score, num_items);
1038 print_histogram_bar (num_items, max);
1039 last_score = bin->score;
1044 mnemon_handle_command (mnemon_t *mnemon,
1045 const char *command)
1049 switch (command[0]) {
1050 /* 'h' for histogram */
1053 char *category = NULL;
1057 arg += strspn (arg, " \t");
1058 len = strcspn (arg, " \t");
1060 category = xstrndup (arg, len);
1062 arg += strspn (arg, " \t");
1064 length = atoi (arg);
1066 mnemon_print_histogram (mnemon, category, length);
1069 /* 'r' for repeat */
1072 /* Nothing necessary for repeating. */
1076 printf ("Unknown command: %s\n", command);
1082 mnemon_handle_response (mnemon_t *mnemon,
1086 const char *response,
1087 double response_time,
1092 correct = (strcmp (response, item->response) == 0);
1094 bin_remove_item (bin, item_index);
1096 /* If the bin is now empty, we must remove it. Also if we just
1097 * picked the last word we'll ever pick from the bin with
1098 * score 0, then we can remove that as well. */
1099 if (bin->num_items == 0 ||
1100 (bin->score == 0 && mnemon->to_introduce == 0))
1102 mnemon_remove_bin (mnemon, bin);
1106 (time_limit == 0.0 || response_time < time_limit))
1109 mnemon->to_master--;
1110 /* We reserve an item score of 0 for an item that has
1111 * never been asked. */
1112 if (item->score == 0) {
1114 mnemon->unlearned--;
1115 mnemon->to_master--;
1116 printf ("You got it!");
1117 } else if (item->score < 0) {
1118 printf ("Yes---just give me %d more.",
1120 } else if (item->score == 1) {
1121 printf ("On your first try, no less!");
1123 printf ("Masterful (%dx).", item->score);
1127 printf (" %s is the correct answer.",
1130 printf ("Correct, but not quite quick enough (%0.2f seconds---needed %0.2f seconds)\n",
1131 response_time, time_limit);
1132 /* Penalize an incorrect response by forcing the score
1134 if (item->score >= 0) {
1135 if (item->score > 0)
1136 printf (" Oops, you knew that, right? (%dx)\n ",
1138 mnemon->unlearned++;
1139 /* We add three here, (rather than just 2 to track the
1140 * change in the item's score below), as an extra
1141 * penalty. If the user is forgetting stuff learned
1142 * previously, then more time should be spent on mastering
1143 * than learning new items. */
1144 mnemon->to_master += item->score + 3;
1145 /* We go to -2 to force a little extra reinforcement
1146 * when re-learning an item, (otherwise, it will often
1147 * get asked again immediately where it is easy to get
1148 * a correct response without any learning). */
1152 mnemon->to_master++;
1157 if (mnemon->to_introduce)
1158 printf ("%d to come. ", mnemon->to_introduce);
1159 if (mnemon->unlearned)
1160 printf ("%d still unlearned. ", mnemon->unlearned);
1161 if (mnemon->to_introduce == 0 && mnemon->to_master > 0)
1162 printf ("%d items to master", mnemon->to_master);
1165 bin = mnemon_get_bin (mnemon, item->score);
1167 bin_add_item (bin, item);
1171 mnemon_show_challenge (mnemon_t *mnemon,
1172 challenge_type_t challenge_type,
1173 const char *challenge)
1175 const char *program;
1178 if (challenge_type == CHALLENGE_TYPE_TEXT) {
1179 printf ("%s\n", challenge);
1183 /* XXX: Yes, shelling out to system is total cheese. The planned
1184 * fix here is to bring graphical display in process, (or at least
1185 * have a custom external program that accepts image filenames on
1188 switch (challenge_type) {
1189 case CHALLENGE_TYPE_TEXT:
1192 case CHALLENGE_TYPE_IMAGE:
1193 program = "xli -gamma 2.2";
1195 case CHALLENGE_TYPE_AUDIO:
1198 case CHALLENGE_TYPE_MIDI:
1199 program = "timidity -Os";
1203 xasprintf (&command, "%s %s/%s >/dev/null 2>&1 &",
1212 mnemon_hide_challenge (mnemon_t *mnemon, challenge_type_t challenge_type)
1216 if (challenge_type != CHALLENGE_TYPE_IMAGE)
1219 /* XXX: And this is just embarrassing (obviously wrong in several
1220 * ways). Hopefully I'll amend away any commit that includes this.
1222 xasprintf (&command, "killall xli");
1228 mnemon_do_challenges (mnemon_t *mnemon)
1233 category_t *category;
1237 /* Count the number of items with negative scores. */
1238 mnemon->unlearned = 0;
1239 for (i = 0; i < mnemon->num_bins; i++) {
1240 bin = &mnemon->bins[i];
1241 if (bin->score >= 0)
1243 mnemon->unlearned += bin->num_items;
1246 mnemon->to_introduce -= mnemon->unlearned;
1247 if (mnemon->to_introduce < 0)
1248 mnemon->to_introduce = 0;
1250 /* Get rid of bin with score of 0 if we aren't going to be
1251 * introducing anything from it. */
1252 if (mnemon->to_introduce == 0) {
1253 bin = mnemon_get_bin (mnemon, 0);
1254 mnemon_remove_bin (mnemon, bin);
1257 if (mnemon->unlearned) {
1258 printf ("You've got %d items to learn already. ", mnemon->unlearned);
1259 if (mnemon->to_introduce)
1260 printf ("I'll introduce %d more as we go.", mnemon->to_introduce);
1263 printf ("Introducing %d new items.\n", mnemon->to_introduce);
1268 struct timeval start, end;
1270 mnemon_select_item (mnemon, &bin, &item_index, &category);
1271 item = bin->items[item_index];
1274 if (category->time_limit > 0.0) {
1275 response = readline ("The next one is timed. Press enter when ready:");
1279 mnemon_show_challenge (mnemon, category->challenge_type,
1282 gettimeofday (&start, NULL);
1283 response = readline ("> ");
1284 gettimeofday (&end, NULL);
1286 mnemon_hide_challenge (mnemon, category->challenge_type);
1288 /* Terminate on EOF */
1289 if (response == NULL) {
1294 if (response[0] == '/') {
1295 mnemon_handle_command (mnemon, response + 1);
1302 mnemon_handle_response (mnemon, bin, item_index,
1304 (end.tv_sec + end.tv_usec / 1e6) -
1305 (start.tv_sec + start.tv_usec / 1e6),
1306 category->time_limit);
1309 /* Replay audio challenges for reinforcement. */
1310 if (category->challenge_type == CHALLENGE_TYPE_AUDIO ||
1311 category->challenge_type == CHALLENGE_TYPE_MIDI)
1313 mnemon_show_challenge (mnemon, category->challenge_type,
1315 printf ("%s\n", item->challenge);
1318 } while (mnemon->to_introduce ||
1319 mnemon->unlearned ||
1320 mnemon->to_master > 0);
1324 main (int argc, char *argv[])
1329 srand (time (NULL));
1331 mnemon_init (&mnemon);
1333 mnemon_load (&mnemon);
1335 mnemon_do_challenges (&mnemon);
1337 mnemon_save (&mnemon);
1339 mnemon_fini (&mnemon);
1341 mnemon_init (&mnemon);
1342 mnemon_load (&mnemon);
1344 printf ("Great job.\nHere are your current results:\n");
1345 mnemon_print_histogram (&mnemon, NULL, 0);
1346 response = readline ("Press enter to quit.\n");
1349 mnemon_fini (&mnemon);