]> git.tuebingen.mpg.de Git - paraslash.git/commitdiff
Clean up and rename change_current_mood().
authorAndre Noll <maan@tuebingen.mpg.de>
Tue, 8 Mar 2022 17:46:07 +0000 (18:46 +0100)
committerAndre Noll <maan@tuebingen.mpg.de>
Mon, 17 Oct 2022 18:36:21 +0000 (20:36 +0200)
Move the code which destroys the current mood to the end of the
function so that we can still return to the old mood if something
goes awry.

To make this work, various functions need to be adjusted to no longer
refer to to afs statistics via the global current_mood pointer. Pass
a pointer to the statistics structure to those.

Also get rid of the local mood pointer variable in favor of ->m of
struct admissible_array.

Rename the function because it is public and deserves the mood_ prefix.

afs.c
mood.c
mood.h
mp.c

diff --git a/afs.c b/afs.c
index 72e7acccf3284fd4bb7d94697eb3d2ce70217d29..4d473dc1aa6d61f0d6786e97991ab0490c39f43d 100644 (file)
--- a/afs.c
+++ b/afs.c
@@ -430,7 +430,7 @@ static int activate_mood_or_playlist(const char *arg, int *num_admissible,
 
        if (!arg) {
                mode = PLAY_MODE_MOOD;
-               ret = change_current_mood(NULL, errmsg);
+               ret = mood_switch(NULL, errmsg);
                if (ret < 0) {
                        if (num_admissible)
                                *num_admissible = 0;
@@ -441,7 +441,7 @@ static int activate_mood_or_playlist(const char *arg, int *num_admissible,
                        ret = playlist_open(arg + 2, errmsg);
                        mode = PLAY_MODE_PLAYLIST;
                } else if (!strncmp(arg, "m/", 2)) {
-                       ret = change_current_mood(arg + 2, errmsg);
+                       ret = mood_switch(arg + 2, errmsg);
                        mode = PLAY_MODE_MOOD;
                } else {
                        if (errmsg)
diff --git a/mood.c b/mood.c
index 5b0ddfdd7449cd89e1e32c51565b5012f1597e69..db0dfaeec53602eb31fa3e58991fa078d451f73b 100644 (file)
--- a/mood.c
+++ b/mood.c
@@ -259,9 +259,9 @@ static int64_t normalized_value(int64_t x, int64_t n, int64_t sum, int64_t qd)
        return 100 * (n * x - sum) / (int64_t)int_sqrt(n) / (int64_t)int_sqrt(qd);
 }
 
-static long compute_score(struct afs_info *afsi)
+static long compute_score(struct afs_info *afsi,
+               const struct afs_statistics *stats)
 {
-       const struct afs_statistics *stats = &current_mood->stats;
        long score = -normalized_value(afsi->num_played, stats->num,
                stats->num_played_sum, stats->num_played_qd);
        score -= normalized_value(afsi->last_played, stats->num,
@@ -269,12 +269,12 @@ static long compute_score(struct afs_info *afsi)
        return score / 2;
 }
 
-static int add_afs_statistics(const struct osl_row *row)
+static int add_afs_statistics(const struct osl_row *row,
+               struct afs_statistics *stats)
 {
        uint64_t n, x, s, q;
        struct afs_info afsi;
        int ret;
-       struct afs_statistics *stats = &current_mood->stats;
 
        ret = get_afsi_of_row(row, &afsi);
        if (ret < 0)
@@ -360,8 +360,8 @@ struct admissible_array {
  */
 static int add_if_admissible(struct osl_row *aft_row, void *data)
 {
-       const struct afs_statistics *stats = &current_mood->stats;
        struct admissible_array *aa = data;
+       struct afs_statistics *stats = &aa->m->stats;
 
        if (!mp_eval_row(aft_row, aa->m->parser_context))
                return 0;
@@ -372,7 +372,7 @@ static int add_if_admissible(struct osl_row *aft_row, void *data)
                        sizeof(struct osl_row *));
        }
        aa->array[stats->num] = aft_row;
-       return add_afs_statistics(aft_row);
+       return add_afs_statistics(aft_row, stats);
 }
 
 /**
@@ -434,7 +434,8 @@ static void update_afs_statistics(struct afs_info *old_afsi,
        stats->num_played_sum += new_afsi->num_played - old_afsi->num_played;
 }
 
-static int add_to_score_table(const struct osl_row *aft_row)
+static int add_to_score_table(const struct osl_row *aft_row,
+               const struct afs_statistics *stats)
 {
        long score;
        struct afs_info afsi;
@@ -442,7 +443,7 @@ static int add_to_score_table(const struct osl_row *aft_row)
 
        if (ret < 0)
                return ret;
-       score = compute_score(&afsi);
+       score = compute_score(&afsi, stats);
        return score_add(aft_row, score);
 }
 
@@ -507,10 +508,10 @@ static int mood_update_audio_file(const struct osl_row *aft_row,
        if (was_admissible && !is_admissible)
                return delete_from_statistics_and_score_table(aft_row);
        if (!was_admissible && is_admissible) {
-               ret = add_afs_statistics(aft_row);
+               ret = add_afs_statistics(aft_row, &current_mood->stats);
                if (ret < 0)
                        return ret;
-               return add_to_score_table(aft_row);
+               return add_to_score_table(aft_row, &current_mood->stats);
        }
        /* update score */
        ret = get_afsi_of_row(aft_row, &afsi);
@@ -518,7 +519,7 @@ static int mood_update_audio_file(const struct osl_row *aft_row,
                return ret;
        if (old_afsi)
                update_afs_statistics(old_afsi, &afsi);
-       score = compute_score(&afsi);
+       score = compute_score(&afsi, &current_mood->stats);
        PARA_DEBUG_LOG("score: %li\n", score);
        percent = (score + 100) / 3;
        if (percent > 100)
@@ -530,15 +531,11 @@ static int mood_update_audio_file(const struct osl_row *aft_row,
 }
 
 /* sse: seconds since epoch. */
-static void log_statistics(int64_t sse)
+static void log_statistics(struct afs_statistics *stats, int64_t sse)
 {
-       const struct afs_statistics *stats = &current_mood->stats;
        unsigned n = stats->num;
        int mean_days, sigma_days;
 
-       assert(current_mood);
-       PARA_NOTICE_LOG("loaded mood %s\n", current_mood->name?
-               current_mood->name : "(dummy)");
        if (!n) {
                PARA_WARNING_LOG("no admissible files\n");
                return;
@@ -569,10 +566,8 @@ void close_current_mood(void)
        current_mood = NULL;
 }
 
-static void compute_correction_factors(int64_t sse)
+static void compute_correction_factors(int64_t sse, struct afs_statistics *s)
 {
-       struct afs_statistics *s = &current_mood->stats;
-
        if (s->num > 0) {
                s->normalization_divisor = int_sqrt(s->last_played_qd)
                        * int_sqrt(s->num_played_qd) / s->num / 100;
@@ -605,13 +600,10 @@ static void compute_correction_factors(int64_t sse)
  *
  * \sa struct \ref afs_info::last_played, \ref mp_eval_row().
  */
-int change_current_mood(const char *mood_name, char **errmsg)
+int mood_switch(const char *mood_name, char **errmsg)
 {
        int i, ret;
-       struct admissible_array aa = {
-               .size = 0,
-               .array = NULL
-       };
+       struct admissible_array aa = {.size = 0};
        /*
         * We can not use the "now" pointer from sched.c here because we are
         * called before schedule(), which initializes "now".
@@ -619,17 +611,11 @@ int change_current_mood(const char *mood_name, char **errmsg)
        struct timeval rnow;
 
        if (mood_name) {
-               struct mood *m;
-               ret = init_mood_parser(mood_name, &m, errmsg);
+               ret = init_mood_parser(mood_name, &aa.m, errmsg);
                if (ret < 0)
                        return ret;
-               close_current_mood();
-               current_mood = m;
-       } else { /* load dummy mood */
-               close_current_mood();
-               current_mood = alloc_new_mood(NULL);
-       }
-       aa.m = current_mood;
+       } else /* load dummy mood */
+               aa.m = alloc_new_mood(NULL);
        PARA_NOTICE_LOG("computing statistics of admissible files\n");
        ret = audio_file_loop(&aa, add_if_admissible);
        if (ret < 0) {
@@ -638,10 +624,10 @@ int change_current_mood(const char *mood_name, char **errmsg)
                goto out;
        }
        clock_get_realtime(&rnow);
-       compute_correction_factors(rnow.tv_sec);
-       log_statistics(rnow.tv_sec);
-       for (i = 0; i < current_mood->stats.num; i++) {
-               ret = add_to_score_table(aa.array[i]);
+       compute_correction_factors(rnow.tv_sec, &aa.m->stats);
+       log_statistics(&aa.m->stats, rnow.tv_sec);
+       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(
@@ -649,11 +635,15 @@ int change_current_mood(const char *mood_name, char **errmsg)
                        goto out;
                }
        }
-       ret = current_mood->stats.num;
+       /* success */
+       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)
-               close_current_mood();
+               destroy_mood(aa.m);
        return ret;
 }
 
@@ -678,7 +668,7 @@ static int reload_current_mood(void)
        if (current_mood->name)
                mood_name = para_strdup(current_mood->name);
        close_current_mood();
-       ret = change_current_mood(mood_name, NULL);
+       ret = mood_switch(mood_name, NULL);
        free(mood_name);
        return ret;
 }
diff --git a/mood.h b/mood.h
index fcfe1efc8bdbd1175b32766b0d75f8395f84cbd0..a4b262b4b8bf1a9b5164aaf984851049a4f312c9 100644 (file)
--- a/mood.h
+++ b/mood.h
@@ -2,6 +2,6 @@
 
 /** \file mood.h Public functions of mood.c. */
 
-int change_current_mood(const char *mood_name, char **errmsg);
+int mood_switch(const char *mood_name, char **errmsg);
 void close_current_mood(void);
 int mood_check_callback(struct afs_callback_arg *aca);
diff --git a/mp.c b/mp.c
index fb5a0c07472c46bb7d5ca42fcb4061d54755f96b..df030ead1998935f3553d7beca065bbe535ee1d1 100644 (file)
--- a/mp.c
+++ b/mp.c
@@ -557,7 +557,7 @@ int mp_init(const char *definition, int nbytes, struct mp_context **result,
  * function returns true (without looking at the audio file metadata) to
  * indicate that the given audio file should be considered admissible.
  *
- * \sa \ref change_current_mood(), \ref mp_eval_ast().
+ * \sa \ref mood_switch(), \ref mp_eval_ast().
  */
 bool mp_eval_row(const struct osl_row *aft_row, struct mp_context *ctx)
 {