2 * Copyright (C) 2007 Andre Noll <maan@systemlinux.org>
4 * Licensed under the GPL v2. For licencing details see COPYING.
7 /** \file mood.c Paraslash's mood handling functions. */
18 * Contains statistical data of the currently admissible audio files.
20 * It is used to assign normalized score values to each admissible audio file.
22 struct afs_statistics
{
23 /** Sum of num played over all admissible files. */
24 int64_t num_played_sum
;
25 /** Sum of last played times over all admissible files. */
26 int64_t last_played_sum
;
27 /** Quadratic deviation of num played time. */
28 int64_t num_played_qd
;
29 /** Quadratic deviation of last played time. */
30 int64_t last_played_qd
;
31 /** Number of admissible files */
34 struct afs_statistics statistics
;
37 * Assign scores according to a mood_method.
39 * Each mood_method has its own mood_score_function. The first parameter passed
40 * to that function is a pointer to a row of the audio file table. It
41 * determines the audio file for which a score is to be assigned. The second
42 * argument depends on the mood method this function is used for. It usually is
43 * the argument given at the end of a mood line.
45 * Mood score functions must return values between -100 and +100 inclusively.
46 * Boolean score functions should always return either -100 or +100.
48 * \sa struct mood_method, mood_parser.
50 typedef int mood_score_function(const char *path
, const struct afs_info
*afsi
,
51 const struct audio_format_info
*afhi
, const void *data
);
54 * Pre-process a mood line.
56 * The mood_parser of a mood_method is called once at mood open time for each
57 * line of the current mood definition that contains the mood_method's name as
58 * a keyword. The line is passed to the mood_parser as the first argument. The
59 * mood_parser must determine whether the line is syntactically correct and
60 * return a positive value if so and a negative value otherwise.
62 * Some mood parsers pre-process the data given in the mood line to compute a
63 * structure which depends of the particular mood_method and which is used
64 * later in the mood_score_function of the mood_method. The mood_parser may
65 * store a pointer to its structure via the second argument.
67 * \sa mood_open(), mood_cleanup_function, mood_score_function.
69 typedef int mood_parser(const char *, void **);
72 * Deallocate resources which were allocated by the mood_parser.
74 * This optional function of a mood_method is used to free any resources
75 * allocated in mood_open() by the mood_parser. The argument passed is a
76 * pointer to the mood_method specific data structure that was returned by the
81 typedef void mood_cleanup_function(void *);
84 * Used for scoring and to determine whether a file is admissible.
87 /* The name of the method. */
89 /** Pointer to the mood parser. */
91 /** Pointer to the score function */
92 mood_score_function
*score_function
;
93 /** Optional cleanup function. */
94 mood_cleanup_function
*cleanup
;
98 * Each line of the current mood corresponds to a mood_item.
101 /** The method this line is referring to. */
102 const struct mood_method
*method
;
103 /** The data structure computed by the mood parser. */
105 /** The given score value, or zero if none was given. */
107 /** Non-zero if random scoring was requested. */
109 /** Whether the "not" keyword was given in the mood line. */
111 /** The position in the list of items. */
112 struct list_head mood_item_node
;
116 * Created from the mood definition by mood_open().
118 * When a mood is opened, each line of its definition is investigated, and a
119 * corresponding mood item is produced. Each mood line starts with \p accept,
120 * \p deny, or \p score which determines the type of the mood line. For each
121 * such type a linked list is maintained whose entries are the mood items.
123 * \sa mood_item, mood_open().
126 /** The name of this mood. */
128 /** The list of mood items of type \p accept. */
129 struct list_head accept_list
;
130 /** The list of mood items of type \p deny. */
131 struct list_head deny_list
;
132 /** The list of mood items of type \p score. */
133 struct list_head score_list
;
136 static struct mood
*current_mood
;
139 * Rough approximation to sqrt.
141 * \param x Integer of which to calculate the sqrt.
143 * \return An integer res with res * res <= x.
145 static uint64_t int_sqrt(uint64_t x
)
147 uint64_t op
, res
, one
= 1;
156 if (op
>= res
+ one
) {
157 op
= op
- (res
+ one
);
163 // PARA_NOTICE_LOG("sqrt(%llu) = %llu\n", x, res);
167 static int mm_no_attributes_set_parser(const char *arg
, __a_unused
void **ignored
)
170 PARA_WARNING_LOG("ignored junk at eol: %s\n", arg
);
174 static int mm_no_attributes_set_score_function(__a_unused
const char *path
,
175 const struct afs_info
*afsi
,
176 __a_unused
const struct audio_format_info
*afhi
,
177 __a_unused
const void *data
)
179 if (!strcmp(path
, "/home/mp3/checked/dvd_08/cd_52/Sade__Paradise.mp3"))
180 PARA_NOTICE_LOG("%s: %llu\n", path
, afsi
->attributes
);
181 if (!afsi
->attributes
)
186 static int mm_played_rarely_score_function(__a_unused
const char *path
,
187 const struct afs_info
*afsi
,
188 __a_unused
const struct audio_format_info
*afhi
,
189 __a_unused
const void *data
)
192 int ret
= get_num_admissible_files(&num
);
196 if (statistics
.num_played_sum
- num
* afsi
->num_played
197 > int_sqrt(statistics
.num_played_qd
* num
))
202 static int mm_played_rarely_parser(const char *arg
, __a_unused
void **ignored
)
205 PARA_WARNING_LOG("ignored junk at eol: %s\n", arg
);
209 static int mm_name_like_score_function(const char *path
,
210 __a_unused
const struct afs_info
*afsi
,
211 __a_unused
const struct audio_format_info
*afhi
,
214 if (fnmatch(data
, path
, 0))
219 static int mm_name_like_parser(const char *arg
, void **data
)
221 *data
= para_strdup(arg
);
225 static void mm_name_like_cleanup(void *data
)
230 static int mm_is_set_parser(const char *arg
, void **bitnum
)
232 unsigned char *c
= para_malloc(1);
233 int ret
= get_attribute_bitnum_by_name(arg
, c
);
242 static int mm_is_set_score_function(__a_unused
const char *path
,
243 __a_unused
const struct afs_info
*afsi
,
244 __a_unused
const struct audio_format_info
*afhi
,
247 const unsigned char *bn
= data
;
248 if (afsi
->attributes
& (1ULL << *bn
))
253 /* returns 1 if row matches score item, negative otherwise */
254 static int add_item_score(const struct osl_row
*row
, struct mood_item
*item
, long *score
,
257 struct afs_info afsi
;
258 struct audio_format_info afhi
;
262 *score_arg_sum
+= item
->random_score
? 100 : PARA_ABS(item
->score_arg
);
265 ret
= get_afsi_of_row(row
, &afsi
);
268 ret
= get_afhi_of_row(row
, &afhi
);
271 ret
= get_audio_file_path_of_row(row
, &path
);
274 ret
= item
->method
->score_function(path
, &afsi
, &afhi
,
276 if ((ret
< 0 && !item
->logical_not
) || (ret
>= 0 && item
->logical_not
))
277 return -1; /* no match */
279 if (item
->random_score
)
280 *score
+= PARA_ABS(ret
) * para_random(100);
282 *score
+= PARA_ABS(ret
) * item
->score_arg
;
286 static int compute_mood_score(const struct osl_row
*aft_row
, struct mood
*m
,
289 struct mood_item
*item
;
291 long score_arg_sum
= 0, score
= 0;
295 /* reject audio file if it matches any entry in the deny list */
296 list_for_each_entry(item
, &m
->deny_list
, mood_item_node
)
297 if (add_item_score(aft_row
, item
, &score
, &score_arg_sum
) > 0)
298 return -E_NOT_ADMISSIBLE
;
299 list_for_each_entry(item
, &m
->accept_list
, mood_item_node
)
300 if (add_item_score(aft_row
, item
, &score
, &score_arg_sum
) > 0)
302 /* reject if there is no matching entry in the accept list */
303 if (!match
&& !list_empty(&m
->accept_list
))
304 return -E_NOT_ADMISSIBLE
;
305 list_for_each_entry(item
, &m
->score_list
, mood_item_node
)
306 add_item_score(aft_row
, item
, &score
, &score_arg_sum
);
308 score
/= score_arg_sum
;
313 #define DEFINE_MOOD_METHOD(_name) \
314 .parser = mm_ ## _name ## _parser, \
315 .score_function = mm_ ## _name ## _score_function, \
318 #define DEFINE_MOOD_METHOD_WITH_CLEANUP(_name) \
319 DEFINE_MOOD_METHOD(_name), \
320 .cleanup = mm_ ## _name ## _cleanup
322 static const struct mood_method mood_methods
[] = {
323 {DEFINE_MOOD_METHOD(no_attributes_set
)},
324 {DEFINE_MOOD_METHOD(played_rarely
)},
325 {DEFINE_MOOD_METHOD(is_set
)},
326 {DEFINE_MOOD_METHOD_WITH_CLEANUP(name_like
)},
330 static void cleanup_list_entry(struct mood_item
*item
)
332 if (item
->method
&& item
->method
->cleanup
)
333 item
->method
->cleanup(item
->parser_data
);
335 free(item
->parser_data
);
336 list_del(&item
->mood_item_node
);
340 static void destroy_mood(struct mood
*m
)
342 struct mood_item
*tmp
, *item
;
346 list_for_each_entry_safe(item
, tmp
, &m
->accept_list
, mood_item_node
)
347 cleanup_list_entry(item
);
348 list_for_each_entry_safe(item
, tmp
, &m
->deny_list
, mood_item_node
)
349 cleanup_list_entry(item
);
350 list_for_each_entry_safe(item
, tmp
, &m
->score_list
, mood_item_node
)
351 cleanup_list_entry(item
);
356 static struct mood
*alloc_new_mood(const char *name
)
358 struct mood
*m
= para_calloc(sizeof(struct mood
));
359 m
->name
= para_strdup(name
);
360 INIT_LIST_HEAD(&m
->accept_list
);
361 INIT_LIST_HEAD(&m
->deny_list
);
362 INIT_LIST_HEAD(&m
->score_list
);
366 /** The different types of a mood line. */
367 enum mood_line_type
{
378 struct mood_line_parser_data
{
384 * <accept [with score <score>] | deny [with score <score>] | score <score>>
385 * [if] [not] <mood_method> [options]
386 * <score> is either an integer or "random" which assigns a random score to
390 static int parse_mood_line(char *mood_line
, void *data
)
392 struct mood_line_parser_data
*mlpd
= data
;
398 enum mood_line_type mlt
= ML_INVALID
;
399 struct mood_item
*mi
= NULL
;
400 char *buf
= para_strdup(mood_line
);
403 num_words
= split_args(buf
, &argv
, delim
);
405 if (!num_words
) /* empty line */
408 if (**w
== '#') /* comment */
410 if (!strcmp(*w
, "accept"))
412 else if (!strcmp(*w
, "deny"))
414 else if (!strcmp(*w
, "score"))
416 ret
= -E_MOOD_SYNTAX
;
417 if (mlt
== ML_INVALID
)
419 mi
= para_calloc(sizeof(struct mood_item
));
420 if (mlt
!= ML_SCORE
) {
421 ret
= -E_MOOD_SYNTAX
;
425 if (!strcmp(*w
, "with")) {
431 if (mlt
== ML_SCORE
|| !strcmp(*w
, "score")) {
432 ret
= -E_MOOD_SYNTAX
;
436 if (strcmp(*w
, "random")) {
437 mi
->random_score
= 0;
438 ret
= para_atoi32(*w
, &mi
->score_arg
);
442 mi
->random_score
= 1;
444 goto success
; /* the line "score random" is valid */
448 ret
= -E_MOOD_SYNTAX
;
452 if (!strcmp(*w
, "if")) {
453 ret
= -E_MOOD_SYNTAX
;
458 if (!strcmp(*w
, "not")) {
459 ret
= -E_MOOD_SYNTAX
;
466 for (i
= 0; mood_methods
[i
].parser
; i
++) {
467 if (strcmp(*w
, mood_methods
[i
].name
))
471 ret
= -E_MOOD_SYNTAX
;
472 if (!mood_methods
[i
].parser
)
475 ret
= mood_methods
[i
].parser(*w
, &mi
->parser_data
);
478 mi
->method
= &mood_methods
[i
];
481 if (mlt
== ML_ACCEPT
)
482 para_list_add(&mi
->mood_item_node
, &mlpd
->m
->accept_list
);
483 else if (mlt
== ML_DENY
)
484 para_list_add(&mi
->mood_item_node
, &mlpd
->m
->deny_list
);
486 para_list_add(&mi
->mood_item_node
, &mlpd
->m
->score_list
);
488 PARA_DEBUG_LOG("%s entry added, method: %p\n", mlt
== ML_ACCEPT
? "accept" :
489 (mlt
== ML_DENY
? "deny" : "score"), mi
->method
);
497 free(mi
->parser_data
);
503 static int load_mood(const struct osl_row
*mood_row
, struct mood
**m
)
506 struct osl_object mood_def
;
507 struct mood_line_parser_data mlpd
= {.line_num
= 0};
508 int ret
= mood_get_name_and_def_by_row(mood_row
, &mood_name
, &mood_def
);
514 mlpd
.m
= alloc_new_mood(mood_name
);
515 ret
= for_each_line_ro(mood_def
.data
, mood_def
.size
,
516 parse_mood_line
, &mlpd
);
517 osl_close_disk_object(&mood_def
);
519 PARA_ERROR_LOG("unable to load mood %s: %s\n", mlpd
.m
->name
,
520 PARA_STRERROR(-ret
));
521 destroy_mood(mlpd
.m
);
524 PARA_INFO_LOG("loaded mood %s\n", mlpd
.m
->name
);
530 * Calls load_mood() and reverts its error value: It returns -E_MOOD_LOADED
531 * on _success_, and 1 on errors. This way the loop over all moods stops at the
534 static int load_mood_loop_func(struct osl_row
*mood_row
, void *data
)
536 struct mood
**m
= data
;
537 int ret
= load_mood(mood_row
, m
);
539 if (ret
!= -E_DUMMY_ROW
)
540 PARA_NOTICE_LOG("invalid mood (%d), trying next mood\n", ret
);
543 return -E_MOOD_LOADED
;
546 static int load_first_available_mood(struct mood
**m
)
548 int ret
= osl_rbtree_loop(moods_table
, BLOBCOL_NAME
, m
,
549 load_mood_loop_func
);
550 if (ret
== -E_MOOD_LOADED
) /* success */
553 return ret
; /* error */
554 PARA_NOTICE_LOG("no valid mood found\n");
558 static int check_mood(struct osl_row
*mood_row
, void *data
)
560 struct para_buffer
*pb
= data
;
562 struct osl_object mood_def
;
563 struct mood_line_parser_data mlpd
= {.line_num
= 0};
565 int ret
= mood_get_name_and_def_by_row(mood_row
, &mood_name
, &mood_def
);
568 para_printf(pb
, "failed to get mood definition\n");
571 if (!*mood_name
) /* ignore dummy row */
573 para_printf(pb
, "checking mood %s...\n", mood_name
);
574 ret
= for_each_line_ro(mood_def
.data
, mood_def
.size
,
575 parse_mood_line
, &mlpd
);
577 para_printf(pb
, "%s line %u: %s\n", mood_name
, mlpd
.line_num
,
578 PARA_STRERROR(-ret
));
580 osl_close_disk_object(&mood_def
);
585 * Check all moods for syntax errors.
587 * \param query Unused.
588 * \param result: Contains check messages.
590 int mood_check_callback(__a_unused
const struct osl_object
*query
,
591 struct osl_object
*result
)
593 struct para_buffer pb
= {.buf
= NULL
};
595 para_printf(&pb
, "checking moods...\n");
596 osl_rbtree_loop(moods_table
, BLOBCOL_ID
, &pb
,
598 result
->data
= pb
.buf
;
599 result
->size
= pb
.size
;
604 static unsigned int_log2(uint64_t x
)
616 static int64_t normalized_value(int64_t x
, int64_t n
, int64_t sum
, int64_t qd
)
620 return 100 * (n
* x
- sum
) / (int64_t)int_sqrt(n
* qd
);
623 static long compute_num_played_score(struct afs_info
*afsi
)
625 return -normalized_value(afsi
->num_played
, statistics
.num
,
626 statistics
.num_played_sum
, statistics
.num_played_qd
);
629 static long compute_last_played_score(struct afs_info
*afsi
)
631 return -normalized_value(afsi
->last_played
, statistics
.num
,
632 statistics
.last_played_sum
, statistics
.last_played_qd
);
635 static long compute_dynamic_score(const struct osl_row
*aft_row
)
637 struct afs_info afsi
;
638 int64_t score
, nscore
= 0, lscore
= 0;
641 ret
= get_afsi_of_row(aft_row
, &afsi
);
644 nscore
= compute_num_played_score(&afsi
);
645 lscore
= compute_last_played_score(&afsi
);
646 score
= nscore
+ lscore
;
650 static int add_afs_statistics(const struct osl_row
*row
)
653 struct afs_info afsi
;
656 ret
= get_afsi_of_row(row
, &afsi
);
660 x
= afsi
.last_played
;
661 s
= statistics
.last_played_sum
;
663 statistics
.last_played_qd
+= (x
- s
/ n
) * (x
- s
/ n
) * n
/ (n
+ 1);
664 statistics
.last_played_sum
+= x
;
667 s
= statistics
.num_played_sum
;
669 statistics
.num_played_qd
+= (x
- s
/ n
) * (x
- s
/ n
) * n
/ (n
+ 1);
670 statistics
.num_played_sum
+= x
;
675 static int del_afs_statistics(const struct osl_row
*row
)
677 uint64_t n
, s
, q
, a
, new_s
;
678 struct afs_info afsi
;
680 ret
= get_afsi_of_row(row
, &afsi
);
686 memset(&statistics
, 0, sizeof(statistics
));
690 s
= statistics
.last_played_sum
;
691 q
= statistics
.last_played_qd
;
692 a
= afsi
.last_played
;
694 statistics
.last_played_sum
= new_s
;
695 statistics
.last_played_qd
= q
+ s
* s
/ n
- a
* a
696 - new_s
* new_s
/ (n
- 1);
698 s
= statistics
.num_played_sum
;
699 q
= statistics
.num_played_qd
;
702 statistics
.num_played_sum
= new_s
;
703 statistics
.num_played_qd
= q
+ s
* s
/ n
- a
* a
704 - new_s
* new_s
/ (n
- 1);
711 * Structure used during mood_open().
713 * At mood open time, we look at each file in the audio file table in order to
714 * determine whether it is admissible. If a file happens to be admissible, its
715 * mood score is computed by calling each relevant mood_score_function. Next,
716 * we update the afs_statistics and add a struct admissible_file_info to a
719 * If all files have been processed that way, the final score of each
720 * admissible file is computed by adding the dynamic score (which depends on
721 * the afs_statistics) to the mood score. Finally, all audio files in the
722 * array are added to the score table and the admissible array is freed.
724 * \sa mood_method, admissible_array.
726 struct admissible_file_info
728 /** The admissible audio file. */
729 struct osl_row
*aft_row
;
734 /** The temporary array of admissible files. */
735 struct admissible_array
{
736 /** Files are admissible wrt. this mood. */
738 /** The size of the array */
740 /** Pointer to the array of admissible files. */
741 struct admissible_file_info
*array
;
745 * Add an entry to the array of admissible files.
747 * \param aft_row The audio file to be added.
748 * \param private_data Pointer to a struct admissible_file_info.
750 * \return Negative on errors, positive on success.
752 static int add_if_admissible(struct osl_row
*aft_row
, void *data
)
754 struct admissible_array
*aa
= data
;
758 ret
= compute_mood_score(aft_row
, aa
->m
, &score
);
760 return (ret
== -E_NOT_ADMISSIBLE
)? 1 : ret
;
761 if (statistics
.num
>= aa
->size
) {
764 aa
->array
= para_realloc(aa
->array
,
765 aa
->size
* sizeof(struct admissible_file_info
));
767 aa
->array
[statistics
.num
].aft_row
= aft_row
;
768 aa
->array
[statistics
.num
].score
= score
;
769 ret
= add_afs_statistics(aft_row
);
776 * Compute the new quadratic deviation in case one element changes.
778 * \param n Number of elements.
779 * \param old_qd The quadratic deviation before the change.
780 * \param old_val The value that was replaced.
781 * \param new_val The replacement value.
782 * \param old_sum The sum of all elements before the update.
784 * \return The new quadratic deviation resulting from replacing old_val
787 * Given n real numbers a_1, ..., a_n, their sum S = a_1 + ... + a_n,
788 * their quadratic deviation
790 * q = (a_1 - S/n)^2 + ... + (a_n - S/n)^2,
792 * and a real number b, the quadratic deviation q' of a_1,...a_{n-1}, b (ie.
793 * the last number a_n was replaced by b) may be computed in O(1) time in terms
794 * of n, q, a_n, b, and S as
796 * q' = q + d * s - (2 * S + d) * d / n,
798 * where d = b - a_n, and s = b + a_n.
800 * Example: n = 3, a_1 = 3, a_2 = 5, a_3 = 7, b = 10. Then S = 15, q = 8, d = 3,
803 * q + d * s - (2 * S + d) * d / n = 8 + 51 - 33 = 26,
805 * which equals q' = (3 - 6)^2 + (5 - 6)^2 + (10 - 6)^2.
808 _static_inline_
int64_t update_quadratic_deviation(int64_t n
, int64_t old_qd
,
809 int64_t old_val
, int64_t new_val
, int64_t old_sum
)
811 int64_t delta
= new_val
- old_val
;
812 int64_t sigma
= new_val
+ old_val
;
813 return old_qd
+ delta
* sigma
- (2 * old_sum
+ delta
) * delta
/ n
;
816 static int update_afs_statistics(struct afs_info
*old_afsi
, struct afs_info
*new_afsi
)
819 int ret
= get_num_admissible_files(&n
);
825 statistics
.last_played_qd
= update_quadratic_deviation(n
,
826 statistics
.last_played_qd
, old_afsi
->last_played
,
827 new_afsi
->last_played
, statistics
.last_played_sum
);
828 statistics
.last_played_sum
+= new_afsi
->last_played
- old_afsi
->last_played
;
830 statistics
.num_played_qd
= update_quadratic_deviation(n
,
831 statistics
.num_played_qd
, old_afsi
->num_played
,
832 new_afsi
->num_played
, statistics
.num_played_sum
);
833 statistics
.num_played_sum
+= new_afsi
->num_played
- old_afsi
->num_played
;
837 static int add_to_score_table(const struct osl_row
*aft_row
, long mood_score
)
839 long score
= (compute_dynamic_score(aft_row
) + mood_score
) / 3;
840 return score_add(aft_row
, score
);
843 static int delete_from_statistics_and_score_table(const struct osl_row
*aft_row
)
845 int ret
= del_afs_statistics(aft_row
);
848 return score_delete(aft_row
);
852 * Delete one entry from the statistics and from the score table.
854 * \param aft_row The audio file which is no longer admissible.
856 * \return Positive on success, negative on errors.
858 * \sa score_delete(), mood_update_audio_file().
860 int mood_delete_audio_file(const struct osl_row
*aft_row
)
864 ret
= row_belongs_to_score_table(aft_row
);
867 if (!ret
) /* not admissible, nothing to do */
869 return delete_from_statistics_and_score_table(aft_row
);
873 * Compute the new score of an audio file wrt. the current mood.
875 * \param aft_row Determines the audio file.
876 * \param old_afsi The audio file selector info before updating.
878 * The \a old_afsi argument may be \p NULL which indicates that no changes to
879 * the audio file info were made.
881 * \return Positive on success, negative on errors.
883 int mood_update_audio_file(const struct osl_row
*aft_row
, struct afs_info
*old_afsi
)
886 int ret
, is_admissible
, was_admissible
= 0;
887 struct afs_info afsi
;
890 return 1; /* nothing to do */
891 ret
= row_belongs_to_score_table(aft_row
);
894 was_admissible
= ret
;
895 ret
= compute_mood_score(aft_row
, current_mood
, &score
);
896 is_admissible
= (ret
> 0);
897 if (!was_admissible
&& !is_admissible
)
899 if (was_admissible
&& !is_admissible
)
900 return delete_from_statistics_and_score_table(aft_row
);
901 if (!was_admissible
&& is_admissible
) {
902 ret
= add_afs_statistics(aft_row
);
905 return add_to_score_table(aft_row
, score
);
908 ret
= get_afsi_of_row(aft_row
, &afsi
);
912 ret
= update_afs_statistics(old_afsi
, &afsi
);
916 score
+= compute_num_played_score(&afsi
);
917 score
+= compute_last_played_score(&afsi
);
919 PARA_DEBUG_LOG("score: %li\n", score
);
920 percent
= (score
+ 100) / 3;
923 else if (percent
< 0)
925 PARA_DEBUG_LOG("re-inserting at %lu%%\n", percent
);
926 return score_update(aft_row
, percent
);
929 static void log_statistics(void)
931 unsigned n
= statistics
.num
;
934 PARA_NOTICE_LOG("no admissible files\n");
937 PARA_NOTICE_LOG("last_played mean: %lli, last_played sigma: %llu\n",
938 (long long int)(statistics
.last_played_sum
/ n
),
939 (long long unsigned)int_sqrt(statistics
.last_played_qd
/ n
));
940 PARA_NOTICE_LOG("num_played mean: %lli, num_played sigma: %llu\n",
941 (long long int)statistics
.num_played_sum
/ n
,
942 (long long unsigned)int_sqrt(statistics
.num_played_qd
/ n
));
946 * Change the current mood.
948 * \param mood_name The name of the mood to open.
950 * There are two special cases: If \a mood_name is \a NULL, load the
951 * first available mood. If \a mood_name is the empty string "", load
952 * the dummy mood that accepts every audio file and uses a scoring method
953 * based only on the \a last_played information.
955 * If there is already an open mood, it will be closed first.
957 * \return Positive on success, negative on errors. Loading the dummy mood
960 * \sa struct admissible_file_info, struct admissible_array, struct
961 * afs_info::last_played, mood_close().
963 int change_current_mood(char *mood_name
)
966 struct admissible_array aa
= {
973 ret
= load_first_available_mood(&m
);
976 destroy_mood(current_mood
);
978 } else if (*mood_name
) {
981 struct osl_object obj
= {
983 .size
= strlen(mood_name
) + 1
985 ret
= osl_get_row(moods_table
, BLOBCOL_NAME
, &obj
, &row
);
987 PARA_NOTICE_LOG("no such mood: %s\n", mood_name
);
990 ret
= load_mood(row
, &m
);
993 destroy_mood(current_mood
);
996 destroy_mood(current_mood
);
997 current_mood
= alloc_new_mood("dummy");
1000 PARA_NOTICE_LOG("loaded mood %s\n", current_mood
->name
);
1001 PARA_INFO_LOG("%s\n", "computing statistics of admissible files");
1002 ret
= audio_file_loop(&aa
, add_if_admissible
);
1006 PARA_NOTICE_LOG("%d admissible files \n", statistics
.num
);
1007 for (i
= 0; i
< statistics
.num
; i
++) {
1008 struct admissible_file_info
*a
= aa
.array
+ i
;
1009 ret
= add_to_score_table(a
->aft_row
, a
->score
);
1013 PARA_NOTICE_LOG("score add complete\n");
1021 * Close the current mood.
1023 * Free all resources of the current mood which were allocated during
1026 void close_current_mood(void)
1028 destroy_mood(current_mood
);
1029 current_mood
= NULL
;
1030 memset(&statistics
, 0, sizeof(statistics
));
1034 * Close and re-open the current mood.
1036 * This function is used if changes to the audio file table or the
1037 * attribute table were made that render the current list of admissible
1038 * files useless. For example, if an attribute is removed from the
1039 * attribute table, this function is called.
1041 * \return Positive on success, negative on errors. If no mood is currently
1042 * open, the function returns success.
1044 * \sa mood_open(), mood_close().
1046 int reload_current_mood(void)
1054 mood_name
= para_strdup(current_mood
->name
);
1055 close_current_mood();
1056 ret
= change_current_mood(mood_name
);