};
/**
- * Stores an instance of an open mood (parser and statistics).
+ * Stores an instance of a loaded mood (parser and statistics).
*
- * A structure of this type is allocated and initialized at mood open time.
+ * A structure of this type is allocated and initialized when a mood is loaded.
*/
struct mood_instance {
/** NULL means that this is the "dummy" mood. */
};
/*
- * If current_mood is NULL then no mood is currently open. If
- * current_mood->name is NULL, the dummy mood is currently open.
+ * If current_mood is NULL then no mood is currently loaded. If
+ * current_mood->name is NULL, the current mood is the dummy mood.
*
* The statistics are adjusted dynamically through this pointer as files are
* added, removed or played.
return ret;
}
*m = alloc_new_mood(mood_name);
- PARA_INFO_LOG("opening mood %s\n", mood_name);
+ PARA_INFO_LOG("loading mood %s\n", mood_name);
ret = mp_init(mood_def.data, mood_def.size, &(*m)->parser_context, err);
osl_close_disk_object(&mood_def);
if (ret < 0)
static int check_mood(struct osl_row *mood_row, void *data)
{
- struct para_buffer *pb = data;
+ struct afs_callback_arg *aca = data;
char *mood_name, *errmsg;
struct osl_object mood_def;
struct mood_instance *m;
int ret = mood_get_name_and_def_by_row(mood_row, &mood_name, &mood_def);
if (ret < 0) {
- para_printf(pb, "cannot read mood\n");
+ afs_error(aca, "cannot read mood\n");
return ret;
}
if (!*mood_name) /* ignore dummy row */
ret = mp_init(mood_def.data, mood_def.size, &m->parser_context,
&errmsg);
if (ret < 0) {
- para_printf(pb, "%s: %s\n", mood_name, errmsg);
+ afs_error(aca, "%s: %s\n%s\n", mood_name, errmsg,
+ para_strerror(-ret));
free(errmsg);
- para_printf(pb, "%s\n", para_strerror(-ret));
} else
destroy_mood(m);
ret = 1; /* don't fail the loop on invalid mood definitions */
/**
* Check all moods for syntax errors.
*
- * \param aca Only ->pbout is used for diagnostics.
+ * \param aca Output goes to ->pbout, errors to ->fd on the error band.
*
* \return Negative on fatal errors. Inconsistent mood definitions are not
* considered an error.
int mood_check_callback(struct afs_callback_arg *aca)
{
para_printf(&aca->pbout, "checking moods...\n");
- return osl(osl_rbtree_loop(moods_table, BLOBCOL_ID, &aca->pbout,
- check_mood));
+ return osl(osl_rbtree_loop(moods_table, BLOBCOL_ID, aca, check_mood));
}
/*
* overflows and rounding errors we store the common divisor of the
* correction factors separately.
*/
-static int64_t normalized_value(int64_t x, int64_t n, int64_t sum, int64_t qd)
-{
- if (!n || !qd)
- return 0;
- return 100 * (n * x - sum) / (int64_t)int_sqrt(n) / (int64_t)int_sqrt(qd);
-}
-
static long compute_score(struct afs_info *afsi,
const struct afs_statistics *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,
- stats->last_played_sum, stats->last_played_qd);
- return score / 2;
+ int64_t mean_n, mean_l,score_n, score_l;
+
+ assert(stats->normalization_divisor > 0);
+ assert(stats->num > 0);
+ mean_n = stats->num_played_sum / stats->num;
+ mean_l = stats->last_played_sum / stats->num;
+
+ score_n = -((int64_t)afsi->num_played - mean_n)
+ * stats->num_played_correction
+ / stats->normalization_divisor;
+ score_l = -((int64_t)afsi->last_played - mean_l)
+ * stats->last_played_correction
+ / stats->normalization_divisor;
+ return (score_n + score_l) / 2;
}
static int add_afs_statistics(const struct osl_row *row,
}
/*
- * At mood open time we determine the set of admissible files for the given
+ * At mood load time we determine the set of admissible files for the given
* mood where each file is identified by a pointer to a row of the audio file
* table. In the first pass the pointers are added to a temporary array and
* statistics are computed. When all admissible files have been processed in
);
}
-/**
- * Close the current mood.
- *
- * Frees all resources of the current mood.
- */
-void close_current_mood(void)
+/** Free all resources of the current mood, if any. */
+void mood_unload(void)
{
destroy_mood(current_mood);
current_mood = NULL;
/**
* 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 mood_name The name of the mood to load.
* \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
*
* \sa struct \ref afs_info::last_played, \ref mp_eval_row().
*/
-int mood_switch(const char *mood_name, char **msg)
+int mood_load(const char *mood_name, char **msg)
{
int i, ret;
struct admissible_array aa = {.size = 0};
if (msg)
*msg = get_statistics(aa.m, rnow.tv_sec);
ret = aa.m->stats.num;
- close_current_mood();
+ mood_unload();
current_mood = aa.m;
out:
free(aa.array);
}
/*
- * Close and re-open the current mood.
+ * Empty the score table and start over.
*
* This function is called on events which render the current list of
* admissible files useless, for example if an attribute is removed from the
current_mood->name : "(dummy)");
if (current_mood->name)
mood_name = para_strdup(current_mood->name);
- close_current_mood();
- ret = mood_switch(mood_name, NULL);
+ mood_unload();
+ ret = mood_load(mood_name, NULL);
free(mood_name);
return ret;
}