]> git.tuebingen.mpg.de Git - paraslash.git/blobdiff - mood.c
Simplify and improve activate_mood_or_playlist().
[paraslash.git] / mood.c
diff --git a/mood.c b/mood.c
index 66024db6add623716b428edd0263847705bc80e9..7b22f72d003673454f56d759952424891e297307 100644 (file)
--- a/mood.c
+++ b/mood.c
@@ -521,28 +521,24 @@ static int mood_update_audio_file(const struct osl_row *aft_row,
 }
 
 /* sse: seconds since epoch. */
-static void log_statistics(struct afs_statistics *stats, int64_t sse)
+static char *get_statistics(struct mood *m, int64_t sse)
 {
-       unsigned n = stats->num;
+       unsigned n = m->stats.num;
        int mean_days, sigma_days;
 
-       if (!n) {
-               PARA_WARNING_LOG("no admissible files\n");
-               return;
-       }
-       PARA_NOTICE_LOG("%u admissible files\n", stats->num);
-       mean_days = (sse - stats->last_played_sum / n) / 3600 / 24;
-       sigma_days = int_sqrt(stats->last_played_qd / n) / 3600 / 24;
-       PARA_NOTICE_LOG("last_played mean/sigma: %d/%d days\n", mean_days, sigma_days);
-       PARA_NOTICE_LOG("num_played mean/sigma: %" PRId64 "/%" PRIu64 "\n",
-               stats->num_played_sum / n,
-               int_sqrt(stats->num_played_qd / n));
-       PARA_NOTICE_LOG("num_played correction factor: %" PRId64 "\n",
-               stats->num_played_correction);
-       PARA_NOTICE_LOG("last_played correction factor: %" PRId64 "\n",
-               stats->last_played_correction);
-       PARA_NOTICE_LOG("normalization divisor: %" PRId64 "\n",
-               stats->normalization_divisor);
+       mean_days = (sse - m->stats.last_played_sum / n) / 3600 / 24;
+       sigma_days = int_sqrt(m->stats.last_played_qd / n) / 3600 / 24;
+       return make_message(
+               "loaded mood %s (%u files)\n"
+               "last_played mean/sigma: %d/%d days\n"
+               "num_played mean/sigma: %" PRId64 "/%" PRIu64 "\n"
+       ,
+               m->name? m->name : "(dummy)",
+               n,
+               mean_days, sigma_days,
+               m->stats.num_played_sum / n,
+                       int_sqrt(m->stats.num_played_qd / n)
+       );
 }
 
 /**
@@ -575,22 +571,23 @@ static void compute_correction_factors(int64_t sse, struct afs_statistics *s)
 /**
  * Change the current mood.
  *
+ * If there is already an open mood, it will be closed first.
+ *
  * \param mood_name The name of the mood to open.
- * \param errmsg Error description is returned here.
+ * \param msg Error message or mood info is returned here.
  *
  * If \a mood_name is \a NULL, load the dummy mood that accepts every audio file
  * and uses a scoring method based only on the \a last_played information.
  *
- * The errmsg pointer may be NULL, in which case no error message will be
- * returned. If a non-NULL pointer is given, the caller must free *errmsg.
+ * If the message pointer is not NULL, a suitable message is returned there in
+ * all cases. The caller must free this string.
  *
- * If there is already an open mood, it will be closed first.
- *
- * \return Positive on success, negative on errors.
+ * \return The number of admissible files on success, negative on errors. It is
+ * not considered an error if no files are admissible.
  *
  * \sa struct \ref afs_info::last_played, \ref mp_eval_row().
  */
-int mood_switch(const char *mood_name, char **errmsg)
+int mood_switch(const char *mood_name, char **msg)
 {
        int i, ret;
        struct admissible_array aa = {.size = 0};
@@ -601,7 +598,7 @@ int mood_switch(const char *mood_name, char **errmsg)
        struct timeval rnow;
 
        if (mood_name) {
-               ret = init_mood_parser(mood_name, &aa.m, errmsg);
+               ret = init_mood_parser(mood_name, &aa.m, msg);
                if (ret < 0)
                        return ret;
        } else /* load dummy mood */
@@ -609,27 +606,33 @@ int mood_switch(const char *mood_name, char **errmsg)
        PARA_NOTICE_LOG("computing statistics of admissible files\n");
        ret = audio_file_loop(&aa, add_if_admissible);
        if (ret < 0) {
-               if (errmsg)
-                       *errmsg = make_message("audio file loop failed");
+               if (msg) /* false if we are called via the event handler */
+                       *msg = make_message("audio file loop failed\n");
                goto out;
        }
        clock_get_realtime(&rnow);
        compute_correction_factors(rnow.tv_sec, &aa.m->stats);
-       log_statistics(&aa.m->stats, rnow.tv_sec);
+       if (aa.m->stats.num == 0) {
+               if (msg)
+                       *msg = make_message("no admissible files\n");
+               ret = 0;
+               goto out;
+       }
        for (i = 0; i < aa.m->stats.num; i++) {
                ret = add_to_score_table(aa.array[i], &aa.m->stats);
                if (ret < 0) {
-                       if (errmsg)
-                               *errmsg = make_message(
-                                       "could not add row to score table");
+                       if (msg)
+                               *msg = make_message(
+                                       "could not add row to score table\n");
                        goto out;
                }
        }
        /* success */
+       if (msg)
+               *msg = get_statistics(aa.m, rnow.tv_sec);
+       ret = aa.m->stats.num;
        close_current_mood();
        current_mood = aa.m;
-       PARA_NOTICE_LOG("loaded mood %s\n", mood_name? mood_name : "(dummy)");
-       ret = aa.m->stats.num;
 out:
        free(aa.array);
        if (ret < 0)