2 * Copyright (C) 2007-2009 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. */
22 * Contains statistical data of the currently admissible audio files.
24 * It is used to assign normalized score values to each admissible audio file.
26 struct afs_statistics
{
27 /** Sum of num played over all admissible files. */
28 int64_t num_played_sum
;
29 /** Sum of last played times over all admissible files. */
30 int64_t last_played_sum
;
31 /** Quadratic deviation of num played time. */
32 int64_t num_played_qd
;
33 /** Quadratic deviation of last played time. */
34 int64_t last_played_qd
;
35 /** Number of admissible files */
38 struct afs_statistics statistics
;
41 * Assign scores according to a mood_method.
43 * Each mood_method has its own mood_score_function. The first three parameters
44 * passed to that function are informations about the audio file whose score is
45 * to be computed. The data argument depends on the mood method this function
46 * is used for. It usually is the argument given at the end of a mood line.
48 * Mood score functions must return values between -100 and +100 inclusively.
49 * Boolean score functions should always return either -100 or +100.
51 * \sa struct mood_method, mood_parser.
53 typedef int mood_score_function(const char *path
, const struct afs_info
*afsi
,
54 const struct afh_info
*afhi
, const void *data
);
57 * Pre-process a mood line.
59 * The mood_parser of a mood_method is called once at mood open time for each
60 * line of the current mood definition that contains the mood_method's name as
61 * a keyword. The line is passed to the mood_parser as the first argument. The
62 * mood_parser must determine whether the line is syntactically correct and
63 * return a positive value if so and a negative value otherwise.
65 * Some mood parsers pre-process the data given in the mood line to compute a
66 * structure which depends of the particular mood_method and which is used
67 * later in the mood_score_function of the mood_method. The mood_parser may
68 * store a pointer to its structure via the void** pointer.
70 * \sa mood_open(), mood_cleanup_function, mood_score_function.
72 typedef int mood_parser(int, char **, void **);
75 * Deallocate resources which were allocated by the mood_parser.
77 * This optional function of a mood_method is used to free any resources
78 * allocated in mood_open() by the mood_parser. The argument passed is a
79 * pointer to the mood_method specific data structure that was returned by the
84 typedef void mood_cleanup_function(void *);
87 * Used for scoring and to determine whether a file is admissible.
90 /** The name of the method. */
92 /** Pointer to the mood parser. */
94 /** Pointer to the score function */
95 mood_score_function
*score_function
;
96 /** Optional cleanup function. */
97 mood_cleanup_function
*cleanup
;
101 * Each line of the current mood corresponds to a mood_item.
104 /** The method this line is referring to. */
105 const struct mood_method
*method
;
106 /** The data structure computed by the mood parser. */
108 /** The given score value, or zero if none was given. */
110 /** Non-zero if random scoring was requested. */
112 /** Whether the "not" keyword was given in the mood line. */
114 /** The position in the list of items. */
115 struct list_head mood_item_node
;
119 * Created from the mood definition by mood_open().
121 * When a mood is opened, each line of its definition is investigated, and a
122 * corresponding mood item is produced. Each mood line starts with \p accept,
123 * \p deny, or \p score which determines the type of the mood line. For each
124 * such type a linked list is maintained whose entries are the mood items.
126 * \sa mood_item, mood_open().
129 /** The name of this mood. */
131 /** The list of mood items of type \p accept. */
132 struct list_head accept_list
;
133 /** The list of mood items of type \p deny. */
134 struct list_head deny_list
;
135 /** The list of mood items of type \p score. */
136 struct list_head score_list
;
139 static struct mood
*current_mood
;
142 * Rough approximation to sqrt.
144 * \param x Integer of which to calculate the sqrt.
146 * \return An integer res with res * res <= x.
148 static uint64_t int_sqrt(uint64_t x
)
150 uint64_t op
, res
, one
= 1;
159 if (op
>= res
+ one
) {
160 op
= op
- (res
+ one
);
166 // PARA_NOTICE_LOG("sqrt(%llu) = %llu\n", x, res);
170 static int mm_no_attributes_set_parser(int argc
, __a_unused
char **argv
,
171 __a_unused
void **ignored
)
173 return argc
? -E_MOOD_SYNTAX
: 1;
176 static int mm_no_attributes_set_score_function(__a_unused
const char *path
,
177 const struct afs_info
*afsi
,
178 __a_unused
const struct afh_info
*afhi
,
179 __a_unused
const void *data
)
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 afh_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(int argc
, __a_unused
char **argv
,
203 __a_unused
void **ignored
)
205 return argc
? -E_MOOD_SYNTAX
: 1;
208 static int mm_path_matches_score_function(const char *path
,
209 __a_unused
const struct afs_info
*afsi
,
210 __a_unused
const struct afh_info
*afhi
,
213 if (fnmatch(data
, path
, 0))
218 static int mm_path_matches_parser(int argc
, char **argv
, void **data
)
221 return -E_MOOD_SYNTAX
;
222 *data
= para_strdup(argv
[1]);
226 static void mm_path_matches_cleanup(void *data
)
231 static int mm_is_set_parser(int argc
, char **argv
, void **bitnum
)
234 unsigned char c
, *res
;
237 return -E_MOOD_SYNTAX
;
238 ret
= get_attribute_bitnum_by_name(argv
[1], &c
);
241 res
= para_malloc(1);
247 static int mm_is_set_score_function(__a_unused
const char *path
,
248 __a_unused
const struct afs_info
*afsi
,
249 __a_unused
const struct afh_info
*afhi
,
252 const unsigned char *bn
= data
;
253 if (afsi
->attributes
& (1ULL << *bn
))
258 /* returns 1 if row matches score item, negative otherwise */
259 static int add_item_score(const struct osl_row
*row
, struct mood_item
*item
, long *score
,
262 struct afs_info afsi
;
263 struct afh_info afhi
;
267 *score_arg_sum
+= item
->random_score
? 100 : PARA_ABS(item
->score_arg
);
270 ret
= get_afsi_of_row(row
, &afsi
);
273 ret
= get_afhi_of_row(row
, &afhi
);
276 ret
= get_audio_file_path_of_row(row
, &path
);
279 ret
= item
->method
->score_function(path
, &afsi
, &afhi
,
281 if ((ret
< 0 && !item
->logical_not
) || (ret
>= 0 && item
->logical_not
))
282 return -1; /* no match */
284 if (item
->random_score
)
285 *score
+= PARA_ABS(ret
) * para_random(100);
287 *score
+= PARA_ABS(ret
) * item
->score_arg
;
291 static int compute_mood_score(const struct osl_row
*aft_row
, struct mood
*m
,
294 struct mood_item
*item
;
296 long score_arg_sum
= 0, score
= 0;
300 /* reject audio file if it matches any entry in the deny list */
301 list_for_each_entry(item
, &m
->deny_list
, mood_item_node
)
302 if (add_item_score(aft_row
, item
, &score
, &score_arg_sum
) > 0)
303 return -E_NOT_ADMISSIBLE
;
304 list_for_each_entry(item
, &m
->accept_list
, mood_item_node
)
305 if (add_item_score(aft_row
, item
, &score
, &score_arg_sum
) > 0)
307 /* reject if there is no matching entry in the accept list */
308 if (!match
&& !list_empty(&m
->accept_list
))
309 return -E_NOT_ADMISSIBLE
;
310 list_for_each_entry(item
, &m
->score_list
, mood_item_node
)
311 add_item_score(aft_row
, item
, &score
, &score_arg_sum
);
313 score
/= score_arg_sum
;
318 #define DEFINE_MOOD_METHOD(_name) \
319 .parser = mm_ ## _name ## _parser, \
320 .score_function = mm_ ## _name ## _score_function, \
323 #define DEFINE_MOOD_METHOD_WITH_CLEANUP(_name) \
324 DEFINE_MOOD_METHOD(_name), \
325 .cleanup = mm_ ## _name ## _cleanup
327 static const struct mood_method mood_methods
[] = {
328 {DEFINE_MOOD_METHOD(no_attributes_set
)},
329 {DEFINE_MOOD_METHOD(played_rarely
)},
330 {DEFINE_MOOD_METHOD(is_set
)},
331 {DEFINE_MOOD_METHOD_WITH_CLEANUP(path_matches
)},
335 static void cleanup_list_entry(struct mood_item
*item
)
337 if (item
->method
&& item
->method
->cleanup
)
338 item
->method
->cleanup(item
->parser_data
);
340 free(item
->parser_data
);
341 list_del(&item
->mood_item_node
);
345 static void destroy_mood(struct mood
*m
)
347 struct mood_item
*tmp
, *item
;
351 list_for_each_entry_safe(item
, tmp
, &m
->accept_list
, mood_item_node
)
352 cleanup_list_entry(item
);
353 list_for_each_entry_safe(item
, tmp
, &m
->deny_list
, mood_item_node
)
354 cleanup_list_entry(item
);
355 list_for_each_entry_safe(item
, tmp
, &m
->score_list
, mood_item_node
)
356 cleanup_list_entry(item
);
361 static struct mood
*alloc_new_mood(const char *name
)
363 struct mood
*m
= para_calloc(sizeof(struct mood
));
364 m
->name
= para_strdup(name
);
365 INIT_LIST_HEAD(&m
->accept_list
);
366 INIT_LIST_HEAD(&m
->deny_list
);
367 INIT_LIST_HEAD(&m
->score_list
);
371 /** The different types of a mood line. */
372 enum mood_line_type
{
383 /** Data passed to the parser of a mood line. */
384 struct mood_line_parser_data
{
385 /** The mood this mood line belongs to. */
387 /** The line number in the mood definition. */
392 * <accept [with score <score>] | deny [with score <score>] | score <score>>
393 * [if] [not] <mood_method> [options]
394 * <score> is either an integer or "random" which assigns a random score to
398 static int parse_mood_line(char *mood_line
, void *data
)
400 struct mood_line_parser_data
*mlpd
= data
;
405 enum mood_line_type mlt
= ML_INVALID
;
406 struct mood_item
*mi
= NULL
;
409 ret
= create_argv(mood_line
, " \t", &argv
);
413 if (!num_words
) /* empty line */
416 if (**w
== '#') /* comment */
418 if (!strcmp(*w
, "accept"))
420 else if (!strcmp(*w
, "deny"))
422 else if (!strcmp(*w
, "score"))
424 ret
= -E_MOOD_SYNTAX
;
425 if (mlt
== ML_INVALID
)
427 mi
= para_calloc(sizeof(struct mood_item
));
428 if (mlt
!= ML_SCORE
) {
429 ret
= -E_MOOD_SYNTAX
;
433 if (strcmp(*w
, "with"))
438 if (strcmp(*w
, "score"))
441 if (mlt
== ML_SCORE
|| !strcmp(*w
, "score")) {
442 ret
= -E_MOOD_SYNTAX
;
446 if (strcmp(*w
, "random")) {
447 mi
->random_score
= 0;
448 ret
= para_atoi32(*w
, &mi
->score_arg
);
452 mi
->random_score
= 1;
454 goto success
; /* the line "score random" is valid */
458 ret
= -E_MOOD_SYNTAX
;
463 if (!strcmp(*w
, "if")) {
464 ret
= -E_MOOD_SYNTAX
;
469 if (!strcmp(*w
, "not")) {
470 ret
= -E_MOOD_SYNTAX
;
477 for (i
= 0; mood_methods
[i
].parser
; i
++) {
478 if (strcmp(*w
, mood_methods
[i
].name
))
482 ret
= -E_MOOD_SYNTAX
;
483 if (!mood_methods
[i
].parser
)
485 ret
= mood_methods
[i
].parser(num_words
- 1 - (w
- argv
), w
,
489 mi
->method
= &mood_methods
[i
];
492 if (mlt
== ML_ACCEPT
)
493 para_list_add(&mi
->mood_item_node
, &mlpd
->m
->accept_list
);
494 else if (mlt
== ML_DENY
)
495 para_list_add(&mi
->mood_item_node
, &mlpd
->m
->deny_list
);
497 para_list_add(&mi
->mood_item_node
, &mlpd
->m
->score_list
);
499 PARA_DEBUG_LOG("%s entry added, method: %p\n", mlt
== ML_ACCEPT
? "accept" :
500 (mlt
== ML_DENY
? "deny" : "score"), mi
->method
);
507 free(mi
->parser_data
);
513 static int load_mood(const struct osl_row
*mood_row
, struct mood
**m
)
516 struct osl_object mood_def
;
517 struct mood_line_parser_data mlpd
= {.line_num
= 0};
518 int ret
= mood_get_name_and_def_by_row(mood_row
, &mood_name
, &mood_def
);
524 mlpd
.m
= alloc_new_mood(mood_name
);
525 ret
= for_each_line_ro(mood_def
.data
, mood_def
.size
,
526 parse_mood_line
, &mlpd
);
527 osl_close_disk_object(&mood_def
);
529 PARA_ERROR_LOG("unable to load mood %s: %s\n", mlpd
.m
->name
,
530 para_strerror(-ret
));
531 destroy_mood(mlpd
.m
);
538 static int check_mood(struct osl_row
*mood_row
, void *data
)
540 struct para_buffer
*pb
= data
;
542 struct osl_object mood_def
;
543 struct mood_line_parser_data mlpd
= {.line_num
= 0};
545 int ret
= mood_get_name_and_def_by_row(mood_row
, &mood_name
, &mood_def
);
548 para_printf(pb
, "failed to get mood definition: %s\n",
549 para_strerror(-ret
));
552 if (!*mood_name
) /* ignore dummy row */
554 ret
= para_printf(pb
, "checking mood %s...\n", mood_name
);
557 ret
= for_each_line_ro(mood_def
.data
, mood_def
.size
,
558 parse_mood_line
, &mlpd
);
560 para_printf(pb
, "%s line %u: %s\n", mood_name
, mlpd
.line_num
,
561 para_strerror(-ret
));
563 osl_close_disk_object(&mood_def
);
568 * Check all moods for syntax errors.
570 * \param fd The afs socket.
571 * \param query Unused.
573 void mood_check_callback(int fd
, __a_unused
const struct osl_object
*query
)
575 struct para_buffer pb
= {
578 .max_size_handler
= pass_buffer_as_shm
581 int ret
= para_printf(&pb
, "checking moods...\n");
584 osl_rbtree_loop(moods_table
, BLOBCOL_ID
, &pb
,
587 pass_buffer_as_shm(pb
.buf
, pb
.offset
, &fd
);
592 static unsigned int_log2(uint64_t x
)
604 static int64_t normalized_value(int64_t x
, int64_t n
, int64_t sum
, int64_t qd
)
608 return 100 * (n
* x
- sum
) / (int64_t)int_sqrt(n
* qd
);
611 static long compute_num_played_score(struct afs_info
*afsi
)
613 return -normalized_value(afsi
->num_played
, statistics
.num
,
614 statistics
.num_played_sum
, statistics
.num_played_qd
);
617 static long compute_last_played_score(struct afs_info
*afsi
)
619 return -normalized_value(afsi
->last_played
, statistics
.num
,
620 statistics
.last_played_sum
, statistics
.last_played_qd
);
623 static long compute_dynamic_score(const struct osl_row
*aft_row
)
625 struct afs_info afsi
;
626 int64_t score
, nscore
= 0, lscore
= 0;
629 ret
= get_afsi_of_row(aft_row
, &afsi
);
632 nscore
= compute_num_played_score(&afsi
);
633 lscore
= compute_last_played_score(&afsi
);
634 score
= nscore
+ lscore
;
638 static int add_afs_statistics(const struct osl_row
*row
)
641 struct afs_info afsi
;
644 ret
= get_afsi_of_row(row
, &afsi
);
648 x
= afsi
.last_played
;
649 s
= statistics
.last_played_sum
;
651 statistics
.last_played_qd
+= (x
- s
/ n
) * (x
- s
/ n
) * n
/ (n
+ 1);
652 statistics
.last_played_sum
+= x
;
655 s
= statistics
.num_played_sum
;
657 statistics
.num_played_qd
+= (x
- s
/ n
) * (x
- s
/ n
) * n
/ (n
+ 1);
658 statistics
.num_played_sum
+= x
;
663 static int del_afs_statistics(const struct osl_row
*row
)
665 uint64_t n
, s
, q
, a
, new_s
;
666 struct afs_info afsi
;
668 ret
= get_afsi_of_row(row
, &afsi
);
674 memset(&statistics
, 0, sizeof(statistics
));
678 s
= statistics
.last_played_sum
;
679 q
= statistics
.last_played_qd
;
680 a
= afsi
.last_played
;
682 statistics
.last_played_sum
= new_s
;
683 statistics
.last_played_qd
= q
+ s
* s
/ n
- a
* a
684 - new_s
* new_s
/ (n
- 1);
686 s
= statistics
.num_played_sum
;
687 q
= statistics
.num_played_qd
;
690 statistics
.num_played_sum
= new_s
;
691 statistics
.num_played_qd
= q
+ s
* s
/ n
- a
* a
692 - new_s
* new_s
/ (n
- 1);
699 * Structure used during mood_open().
701 * At mood open time, we look at each file in the audio file table in order to
702 * determine whether it is admissible. If a file happens to be admissible, its
703 * mood score is computed by calling each relevant mood_score_function. Next,
704 * we update the afs_statistics and add a struct admissible_file_info to a
707 * If all files have been processed that way, the final score of each
708 * admissible file is computed by adding the dynamic score (which depends on
709 * the afs_statistics) to the mood score. Finally, all audio files in the
710 * array are added to the score table and the admissible array is freed.
712 * \sa mood_method, admissible_array.
714 struct admissible_file_info
716 /** The admissible audio file. */
717 struct osl_row
*aft_row
;
722 /** The temporary array of admissible files. */
723 struct admissible_array
{
724 /** Files are admissible wrt. this mood. */
726 /** The size of the array */
728 /** Pointer to the array of admissible files. */
729 struct admissible_file_info
*array
;
733 * Add an entry to the array of admissible files.
735 * \param aft_row The audio file to be added.
736 * \param private_data Pointer to a struct admissible_file_info.
738 * \return Negative on errors, positive on success.
740 static int add_if_admissible(struct osl_row
*aft_row
, void *data
)
742 struct admissible_array
*aa
= data
;
746 ret
= compute_mood_score(aft_row
, aa
->m
, &score
);
748 return (ret
== -E_NOT_ADMISSIBLE
)? 1 : ret
;
749 if (statistics
.num
>= aa
->size
) {
752 aa
->array
= para_realloc(aa
->array
,
753 aa
->size
* sizeof(struct admissible_file_info
));
755 aa
->array
[statistics
.num
].aft_row
= aft_row
;
756 aa
->array
[statistics
.num
].score
= score
;
757 ret
= add_afs_statistics(aft_row
);
764 * Compute the new quadratic deviation in case one element changes.
766 * \param n Number of elements.
767 * \param old_qd The quadratic deviation before the change.
768 * \param old_val The value that was replaced.
769 * \param new_val The replacement value.
770 * \param old_sum The sum of all elements before the update.
772 * \return The new quadratic deviation resulting from replacing old_val
775 * Given n real numbers a_1, ..., a_n, their sum S = a_1 + ... + a_n,
776 * their quadratic deviation
778 * q = (a_1 - S/n)^2 + ... + (a_n - S/n)^2,
780 * and a real number b, the quadratic deviation q' of a_1,...a_{n-1}, b (ie.
781 * the last number a_n was replaced by b) may be computed in O(1) time in terms
782 * of n, q, a_n, b, and S as
784 * q' = q + d * s - (2 * S + d) * d / n,
786 * where d = b - a_n, and s = b + a_n.
788 * Example: n = 3, a_1 = 3, a_2 = 5, a_3 = 7, b = 10. Then S = 15, q = 8, d = 3,
791 * q + d * s - (2 * S + d) * d / n = 8 + 51 - 33 = 26,
793 * which equals q' = (3 - 6)^2 + (5 - 6)^2 + (10 - 6)^2.
796 _static_inline_
int64_t update_quadratic_deviation(int64_t n
, int64_t old_qd
,
797 int64_t old_val
, int64_t new_val
, int64_t old_sum
)
799 int64_t delta
= new_val
- old_val
;
800 int64_t sigma
= new_val
+ old_val
;
801 return old_qd
+ delta
* sigma
- (2 * old_sum
+ delta
) * delta
/ n
;
804 static int update_afs_statistics(struct afs_info
*old_afsi
, struct afs_info
*new_afsi
)
807 int ret
= get_num_admissible_files(&n
);
813 statistics
.last_played_qd
= update_quadratic_deviation(n
,
814 statistics
.last_played_qd
, old_afsi
->last_played
,
815 new_afsi
->last_played
, statistics
.last_played_sum
);
816 statistics
.last_played_sum
+= new_afsi
->last_played
- old_afsi
->last_played
;
818 statistics
.num_played_qd
= update_quadratic_deviation(n
,
819 statistics
.num_played_qd
, old_afsi
->num_played
,
820 new_afsi
->num_played
, statistics
.num_played_sum
);
821 statistics
.num_played_sum
+= new_afsi
->num_played
- old_afsi
->num_played
;
825 static int add_to_score_table(const struct osl_row
*aft_row
, long mood_score
)
827 long score
= (compute_dynamic_score(aft_row
) + mood_score
) / 3;
828 return score_add(aft_row
, score
);
831 static int delete_from_statistics_and_score_table(const struct osl_row
*aft_row
)
833 int ret
= del_afs_statistics(aft_row
);
836 return score_delete(aft_row
);
840 * Delete one entry from the statistics and from the score table.
842 * \param aft_row The audio file which is no longer admissible.
844 * \return Positive on success, negative on errors.
846 * \sa score_delete().
848 static int mood_delete_audio_file(const struct osl_row
*aft_row
)
852 ret
= row_belongs_to_score_table(aft_row
, NULL
);
855 if (!ret
) /* not admissible, nothing to do */
857 return delete_from_statistics_and_score_table(aft_row
);
861 * Compute the new score of an audio file wrt. the current mood.
863 * \param aft_row Determines the audio file.
864 * \param old_afsi The audio file selector info before updating.
866 * The \a old_afsi argument may be \p NULL which indicates that no changes to
867 * the audio file info were made.
869 * \return Positive on success, negative on errors.
871 static int mood_update_audio_file(const struct osl_row
*aft_row
,
872 struct afs_info
*old_afsi
)
875 int ret
, is_admissible
, was_admissible
= 0;
876 struct afs_info afsi
;
880 return 1; /* nothing to do */
881 ret
= row_belongs_to_score_table(aft_row
, &rank
);
884 was_admissible
= ret
;
885 ret
= compute_mood_score(aft_row
, current_mood
, &score
);
886 is_admissible
= (ret
> 0);
887 if (!was_admissible
&& !is_admissible
)
889 if (was_admissible
&& !is_admissible
)
890 return delete_from_statistics_and_score_table(aft_row
);
891 if (!was_admissible
&& is_admissible
) {
892 ret
= add_afs_statistics(aft_row
);
895 return add_to_score_table(aft_row
, score
);
898 ret
= get_afsi_of_row(aft_row
, &afsi
);
902 ret
= update_afs_statistics(old_afsi
, &afsi
);
906 score
+= compute_num_played_score(&afsi
);
907 score
+= compute_last_played_score(&afsi
);
909 PARA_DEBUG_LOG("score: %li\n", score
);
910 percent
= (score
+ 100) / 3;
913 else if (percent
< 0)
915 PARA_DEBUG_LOG("moving from rank %u to %lu%%\n", rank
, percent
);
916 return score_update(aft_row
, percent
);
919 static void log_statistics(void)
921 unsigned n
= statistics
.num
;
924 PARA_NOTICE_LOG("no admissible files\n");
927 PARA_INFO_LOG("last_played mean: %lli, last_played sigma: %llu\n",
928 (long long int)(statistics
.last_played_sum
/ n
),
929 (long long unsigned)int_sqrt(statistics
.last_played_qd
/ n
));
930 PARA_INFO_LOG("num_played mean: %lli, num_played sigma: %llu\n",
931 (long long int)statistics
.num_played_sum
/ n
,
932 (long long unsigned)int_sqrt(statistics
.num_played_qd
/ n
));
936 * Close the current mood.
938 * Free all resources of the current mood which were allocated during
941 void close_current_mood(void)
943 destroy_mood(current_mood
);
945 memset(&statistics
, 0, sizeof(statistics
));
950 * Change the current mood.
952 * \param mood_name The name of the mood to open.
954 * If \a mood_name is \a NULL, load the dummy mood that accepts every audio file
955 * and uses a scoring method based only on the \a last_played information.
957 * If there is already an open mood, it will be closed first.
959 * \return Positive on success, negative on errors. Loading the dummy mood
962 * \sa struct admissible_file_info, struct admissible_array, struct
963 * afs_info::last_played, mood_close().
965 int change_current_mood(char *mood_name
)
968 struct admissible_array aa
= {
976 struct osl_object obj
= {
978 .size
= strlen(mood_name
) + 1
980 ret
= osl(osl_get_row(moods_table
, BLOBCOL_NAME
, &obj
, &row
));
982 PARA_NOTICE_LOG("no such mood: %s\n", mood_name
);
985 ret
= load_mood(row
, &m
);
988 close_current_mood();
991 close_current_mood();
992 current_mood
= alloc_new_mood("dummy");
995 PARA_NOTICE_LOG("computing statistics of admissible files\n");
996 ret
= audio_file_loop(&aa
, add_if_admissible
);
1000 PARA_INFO_LOG("%d admissible files \n", statistics
.num
);
1001 for (i
= 0; i
< statistics
.num
; i
++) {
1002 struct admissible_file_info
*a
= aa
.array
+ i
;
1003 ret
= add_to_score_table(a
->aft_row
, a
->score
);
1007 PARA_NOTICE_LOG("loaded mood %s\n", current_mood
->name
);
1008 ret
= statistics
.num
;
1014 * Close and re-open the current mood.
1016 * This function is used if changes to the audio file table or the
1017 * attribute table were made that render the current list of admissible
1018 * files useless. For example, if an attribute is removed from the
1019 * attribute table, this function is called.
1021 * \return Positive on success, negative on errors. If no mood is currently
1022 * open, the function returns success.
1024 * \sa mood_open(), mood_close().
1026 int reload_current_mood(void)
1029 char *mood_name
= NULL
;
1033 PARA_NOTICE_LOG("reloading %s\n", current_mood
->name
?
1034 current_mood
->name
: "(dummy)");
1035 if (current_mood
->name
)
1036 mood_name
= para_strdup(current_mood
->name
);
1037 close_current_mood();
1038 ret
= change_current_mood(mood_name
);
1043 int moods_event_handler(enum afs_events event
, __a_unused
struct para_buffer
*pb
,
1048 * The three blob events might change the set of admissible files,
1049 * so we must reload the score list.
1054 if (data
== moods_table
|| data
== playlists_table
)
1055 return 1; /* no reload necessary for these */
1056 return reload_current_mood();
1057 /* these also require reload of the score table */
1059 case ATTRIBUTE_REMOVE
:
1060 case ATTRIBUTE_RENAME
:
1061 return reload_current_mood();
1062 /* changes to the aft only require to re-examine the audio file */
1064 struct afsi_change_event_data
*aced
= data
;
1065 return mood_update_audio_file(aced
->aft_row
, aced
->old_afsi
);
1068 case AUDIO_FILE_RENAME
:
1069 case AUDIO_FILE_ADD
:
1070 return mood_update_audio_file(data
, NULL
);
1071 case AUDIO_FILE_REMOVE
:
1072 return mood_delete_audio_file(data
);