Merge branch 'maint'
[paraslash.git] / mood.c
1 /* Copyright (C) 2007 Andre Noll <maan@tuebingen.mpg.de>, see file COPYING. */
2
3 /** \file mood.c Paraslash's mood handling functions. */
4
5 #include <regex.h>
6 #include <osl.h>
7 #include <lopsub.h>
8
9 #include "para.h"
10 #include "error.h"
11 #include "string.h"
12 #include "afh.h"
13 #include "afs.h"
14 #include "list.h"
15
16 /*
17 * Mood parser API. It's overkill to have an own header file for
18 * these declarations as they are only needed in this .c file.
19 */
20 struct mp_context;
21 int mp_init(const char *definition, int nbytes, struct mp_context **result,
22 char **errmsg);
23 bool mp_eval_row(const struct osl_row *aft_row, struct mp_context *ctx);
24 void mp_shutdown(struct mp_context *ctx);
25
26 /**
27 * Contains statistical data of the currently admissible audio files.
28 *
29 * It is used to assign normalized score values to each admissible audio file.
30 */
31 struct afs_statistics {
32 /** Sum of num played over all admissible files. */
33 int64_t num_played_sum;
34 /** Sum of last played times over all admissible files. */
35 int64_t last_played_sum;
36 /** Quadratic deviation of num played count. */
37 int64_t num_played_qd;
38 /** Quadratic deviation of last played time. */
39 int64_t last_played_qd;
40 /** Correction factor for the num played score. */
41 int64_t num_played_correction;
42 /** Correction factor for the last played score. */
43 int64_t last_played_correction;
44 /** Common divisor of the correction factors. */
45 int64_t normalization_divisor;
46 /** Number of admissible files */
47 unsigned num;
48 };
49
50 /**
51 * Stores an instance of a loaded mood (parser and statistics).
52 *
53 * A structure of this type is allocated and initialized when a mood is loaded.
54 */
55 struct mood_instance {
56 /** NULL means that this is the "dummy" mood. */
57 char *name;
58 /** Bison's abstract syntax tree, used to determine admissibility. */
59 struct mp_context *parser_context;
60 /** To compute the score. */
61 struct afs_statistics stats;
62 };
63
64 /*
65 * If current_mood is NULL then no mood is currently loaded. If
66 * current_mood->name is NULL, the current mood is the dummy mood.
67 *
68 * The statistics are adjusted dynamically through this pointer as files are
69 * added, removed or played.
70 */
71 static struct mood_instance *current_mood;
72
73 /*
74 * Find the position of the most-significant set bit.
75 *
76 * Copied and slightly adapted from the linux source tree, version 4.9.39
77 * (2017-07).
78 */
79 __a_const static uint32_t fls64(uint64_t v)
80 {
81 int n = 63;
82 const uint64_t ones = ~(uint64_t)0U;
83
84 if ((v & (ones << 32)) == 0) {
85 n -= 32;
86 v <<= 32;
87 }
88 if ((v & (ones << (64 - 16))) == 0) {
89 n -= 16;
90 v <<= 16;
91 }
92 if ((v & (ones << (64 - 8))) == 0) {
93 n -= 8;
94 v <<= 8;
95 }
96 if ((v & (ones << (64 - 4))) == 0) {
97 n -= 4;
98 v <<= 4;
99 }
100 if ((v & (ones << (64 - 2))) == 0) {
101 n -= 2;
102 v <<= 2;
103 }
104 if ((v & (ones << (64 - 1))) == 0)
105 n -= 1;
106 return n;
107 }
108
109 /*
110 * Compute the integer square root floor(sqrt(x)).
111 *
112 * Taken 2007 from the linux source tree.
113 */
114 __a_const static uint64_t int_sqrt(uint64_t x)
115 {
116 uint64_t op = x, res = 0, one = 1;
117
118 one = one << (fls64(x) & ~one);
119 while (one != 0) {
120 if (op >= res + one) {
121 op = op - (res + one);
122 res = res + 2 * one;
123 }
124 res /= 2;
125 one /= 4;
126 }
127 return res;
128 }
129
130 static void destroy_mood(struct mood_instance *m)
131 {
132 if (!m)
133 return;
134 mp_shutdown(m->parser_context);
135 free(m->name);
136 free(m);
137 }
138
139 static struct mood_instance *alloc_new_mood(const char *name)
140 {
141 struct mood_instance *m = zalloc(sizeof(*m));
142
143 if (name)
144 m->name = para_strdup(name);
145 m->stats.normalization_divisor = 1;
146 return m;
147 }
148
149 static int init_mood_parser(const char *mood_name, struct mood_instance **m,
150 char **err)
151 {
152 struct osl_object mood_def;
153 int ret;
154
155 if (!*mood_name) {
156 if (err)
157 *err = make_message("empty mood name\n");
158 return -ERRNO_TO_PARA_ERROR(EINVAL);
159 }
160 ret = mood_get_def_by_name(mood_name, &mood_def);
161 if (ret < 0) {
162 if (err)
163 *err = make_message("could not read mood definition\n");
164 return ret;
165 }
166 *m = alloc_new_mood(mood_name);
167 PARA_INFO_LOG("loading mood %s\n", mood_name);
168 ret = mp_init(mood_def.data, mood_def.size, &(*m)->parser_context, err);
169 osl_close_disk_object(&mood_def);
170 if (ret < 0)
171 destroy_mood(*m);
172 return ret;
173 }
174
175 static int check_mood(struct osl_row *mood_row, void *data)
176 {
177 struct afs_callback_arg *aca = data;
178 char *mood_name, *errmsg;
179 struct osl_object mood_def;
180 struct mood_instance *m;
181 int ret = mood_get_name_and_def_by_row(mood_row, &mood_name, &mood_def);
182
183 if (ret < 0) {
184 afs_error(aca, "cannot read mood\n");
185 return ret;
186 }
187 if (!*mood_name) /* ignore dummy row */
188 goto out;
189 m = alloc_new_mood("check");
190 ret = mp_init(mood_def.data, mood_def.size, &m->parser_context,
191 &errmsg);
192 if (ret < 0) {
193 afs_error(aca, "%s: %s\n%s\n", mood_name, errmsg,
194 para_strerror(-ret));
195 free(errmsg);
196 } else
197 destroy_mood(m);
198 ret = 1; /* don't fail the loop on invalid mood definitions */
199 out:
200 osl_close_disk_object(&mood_def);
201 return ret;
202 }
203
204 /**
205 * Check all moods for syntax errors.
206 *
207 * \param aca Output goes to ->pbout, errors to ->fd on the error band.
208 *
209 * \return Negative on fatal errors. Inconsistent mood definitions are not
210 * considered an error.
211 */
212 int mood_check_callback(struct afs_callback_arg *aca)
213 {
214 para_printf(&aca->pbout, "checking moods...\n");
215 return osl(osl_rbtree_loop(moods_table, BLOBCOL_ID, aca, check_mood));
216 }
217
218 /*
219 * The normalized num_played and last_played values are defined as
220 *
221 * nn := -(np - mean_n) / sigma_n and nl := -(lp - mean_l) / sigma_l
222 *
223 * For a (hypothetical) file with np = 0 and lp = now we thus have
224 *
225 * nn = mean_n / sigma_n =: hn > 0
226 * nl = -(now - mean_l) / sigma_l =: hl < 0
227 *
228 * We design the score function so that both contributions get the same
229 * weight. Define the np and lp score of an arbitrary file as
230 *
231 * sn := nn * -hl and sl := nl * hn
232 *
233 * Example:
234 * num_played mean/sigma: 87/14
235 * last_played mean/sigma: 45/32 days
236 *
237 * We have hn = 87 / 14 = 6.21 and hl = -45 / 32 = -1.41. Multiplying
238 * nn of every file with the correction factor 1.41 and nl with
239 * 6.21 makes the weight of the two contributions equal.
240 *
241 * The total score s := sn + sl has the representation
242 *
243 * s = -cn * (np - mean_n) - cl * (lp - mean_l)
244 *
245 * with positive correction factors
246 *
247 * cn = (now - mean_l) / (sqrt(ql) * sqrt(qn) / n)
248 * cl = mean_n / (sqrt(ql) * sqrt(qn) / n)
249 *
250 * where ql and qn are the quadratic deviations stored in the statistics
251 * structure and n is the number of admissible files. To avoid integer
252 * overflows and rounding errors we store the common divisor of the
253 * correction factors separately.
254 */
255 static long compute_score(struct afs_info *afsi,
256 const struct afs_statistics *stats)
257 {
258 int64_t mean_n, mean_l,score_n, score_l;
259
260 assert(stats->normalization_divisor > 0);
261 assert(stats->num > 0);
262 mean_n = stats->num_played_sum / stats->num;
263 mean_l = stats->last_played_sum / stats->num;
264
265 score_n = -((int64_t)afsi->num_played - mean_n)
266 * stats->num_played_correction
267 / stats->normalization_divisor;
268 score_l = -((int64_t)afsi->last_played - mean_l)
269 * stats->last_played_correction
270 / stats->normalization_divisor;
271 return (score_n + score_l) / 2;
272 }
273
274 static int add_afs_statistics(const struct osl_row *row,
275 struct afs_statistics *stats)
276 {
277 uint64_t n, x, s, q;
278 struct afs_info afsi;
279 int ret;
280
281 ret = get_afsi_of_row(row, &afsi);
282 if (ret < 0)
283 return ret;
284 n = stats->num;
285 x = afsi.last_played;
286 s = stats->last_played_sum;
287 if (n > 0) {
288 q = (x > s / n)? x - s / n : s / n - x;
289 stats->last_played_qd += q * q * n / (n + 1);
290 }
291 stats->last_played_sum += x;
292
293 x = afsi.num_played;
294 s = stats->num_played_sum;
295 if (n > 0) {
296 q = (x > s / n)? x - s / n : s / n - x;
297 stats->num_played_qd += q * q * n / (n + 1);
298 }
299 stats->num_played_sum += x;
300 stats->num++;
301 return 1;
302 }
303
304 static int del_afs_statistics(const struct osl_row *row)
305 {
306 struct afs_statistics *stats = &current_mood->stats;
307 uint64_t n, s, q, a, new_s;
308 struct afs_info afsi;
309 int ret;
310 ret = get_afsi_of_row(row, &afsi);
311 if (ret < 0)
312 return ret;
313 n = stats->num;
314 assert(n);
315 if (n == 1) {
316 memset(stats, 0, sizeof(*stats));
317 stats->normalization_divisor = 1;
318 return 1;
319 }
320
321 s = stats->last_played_sum;
322 q = stats->last_played_qd;
323 a = afsi.last_played;
324 new_s = s - a;
325 stats->last_played_sum = new_s;
326 stats->last_played_qd = q + s * s / n - a * a
327 - new_s * new_s / (n - 1);
328
329 s = stats->num_played_sum;
330 q = stats->num_played_qd;
331 a = afsi.num_played;
332 new_s = s - a;
333 stats->num_played_sum = new_s;
334 stats->num_played_qd = q + s * s / n - a * a
335 - new_s * new_s / (n - 1);
336
337 stats->num--;
338 return 1;
339 }
340
341 /*
342 * At mood load time we determine the set of admissible files for the given
343 * mood where each file is identified by a pointer to a row of the audio file
344 * table. In the first pass the pointers are added to a temporary array and
345 * statistics are computed. When all admissible files have been processed in
346 * this way, the score of each admissible file is computed and the (row, score)
347 * pair is added to the score table. This has to be done in a second pass
348 * since the score depends on the statistics. Finally, the array is freed.
349 */
350 struct admissible_array {
351 /** Files are admissible wrt. this mood. */
352 struct mood_instance *m;
353 /** The size of the array */
354 unsigned size;
355 /** Pointer to the array of admissible files. */
356 struct osl_row **array;
357 };
358
359 /*
360 * Check whether the given audio file is admissible. If it is, add it to array
361 * of admissible files.
362 */
363 static int add_if_admissible(struct osl_row *aft_row, void *data)
364 {
365 struct admissible_array *aa = data;
366 struct afs_statistics *stats = &aa->m->stats;
367
368 if (!mp_eval_row(aft_row, aa->m->parser_context))
369 return 0;
370 if (stats->num >= aa->size) {
371 aa->size *= 2;
372 aa->size += 100;
373 aa->array = arr_realloc(aa->array, aa->size,
374 sizeof(struct osl_row *));
375 }
376 aa->array[stats->num] = aft_row;
377 return add_afs_statistics(aft_row, stats);
378 }
379
380 /**
381 * Compute the new quadratic deviation in case one element changes.
382 *
383 * \param n Number of elements.
384 * \param old_qd The quadratic deviation before the change.
385 * \param old_val The value that was replaced.
386 * \param new_val The replacement value.
387 * \param old_sum The sum of all elements before the update.
388 *
389 * \return The new quadratic deviation resulting from replacing old_val
390 * by new_val.
391 *
392 * Given n real numbers a_1, ..., a_n, their sum S = a_1 + ... + a_n,
393 * their quadratic deviation
394 *
395 * q = (a_1 - S/n)^2 + ... + (a_n - S/n)^2,
396 *
397 * and a real number b, the quadratic deviation q' of a_1,...a_{n-1}, b (ie.
398 * the last number a_n was replaced by b) may be computed in O(1) time in terms
399 * of n, q, a_n, b, and S as
400 *
401 * q' = q + d * s - (2 * S + d) * d / n
402 * = q + d * (s - 2 * S / n - d /n),
403 *
404 * where d = b - a_n, and s = b + a_n.
405 *
406 * Example: n = 3, a_1 = 3, a_2 = 5, a_3 = 7, b = 10. Then S = 15, q = 8, d = 3,
407 * s = 17, so
408 *
409 * q + d * s - (2 * S + d) * d / n = 8 + 51 - 33 = 26,
410 *
411 * which equals q' = (3 - 6)^2 + (5 - 6)^2 + (10 - 6)^2.
412 *
413 */
414 _static_inline_ int64_t update_quadratic_deviation(int64_t n, int64_t old_qd,
415 int64_t old_val, int64_t new_val, int64_t old_sum)
416 {
417 int64_t delta = new_val - old_val;
418 int64_t sigma = new_val + old_val;
419 return old_qd + delta * (sigma - 2 * old_sum / n - delta / n);
420 }
421
422 static void update_afs_statistics(struct afs_info *old_afsi,
423 struct afs_info *new_afsi)
424 {
425 struct afs_statistics *stats = &current_mood->stats;
426
427 assert(stats->num > 0);
428 stats->last_played_qd = update_quadratic_deviation(stats->num,
429 stats->last_played_qd, old_afsi->last_played,
430 new_afsi->last_played, stats->last_played_sum);
431 stats->last_played_sum += new_afsi->last_played - old_afsi->last_played;
432
433 stats->num_played_qd = update_quadratic_deviation(stats->num,
434 stats->num_played_qd, old_afsi->num_played,
435 new_afsi->num_played, stats->num_played_sum);
436 stats->num_played_sum += new_afsi->num_played - old_afsi->num_played;
437 }
438
439 static int add_to_score_table(const struct osl_row *aft_row,
440 const struct afs_statistics *stats)
441 {
442 long score;
443 struct afs_info afsi;
444 int ret = get_afsi_of_row(aft_row, &afsi);
445
446 if (ret < 0)
447 return ret;
448 score = compute_score(&afsi, stats);
449 return score_add(aft_row, score);
450 }
451
452 static int delete_from_statistics_and_score_table(const struct osl_row *aft_row)
453 {
454 int ret = del_afs_statistics(aft_row);
455 if (ret < 0)
456 return ret;
457 return score_delete(aft_row);
458 }
459
460 /**
461 * Delete an audio file from the score table and update mood statistics.
462 *
463 * \param aft_row Identifies the row to delete.
464 *
465 * \return Standard.
466 *
467 * \sa \ref score_delete().
468 */
469 static int mood_delete_audio_file(const struct osl_row *aft_row)
470 {
471 if (!row_belongs_to_score_table(aft_row))
472 return 0;
473 return delete_from_statistics_and_score_table(aft_row);
474 }
475
476 /**
477 * Compute the new score of an audio file wrt. the current mood.
478 *
479 * \param aft_row Determines the audio file.
480 * \param old_afsi The audio file selector info before updating.
481 *
482 * The \a old_afsi argument may be \p NULL which indicates that no changes to
483 * the audio file info were made.
484 *
485 * \return Positive on success, negative on errors.
486 */
487 static int mood_update_audio_file(const struct osl_row *aft_row,
488 struct afs_info *old_afsi)
489 {
490 long score, percent;
491 int ret;
492 bool is_admissible, was_admissible;
493 struct afs_info afsi;
494
495 if (!current_mood)
496 return 1; /* nothing to do */
497 was_admissible = row_belongs_to_score_table(aft_row);
498 is_admissible = mp_eval_row(aft_row, current_mood->parser_context);
499 if (!was_admissible && !is_admissible)
500 return 1;
501 if (was_admissible && !is_admissible)
502 return delete_from_statistics_and_score_table(aft_row);
503 if (!was_admissible && is_admissible) {
504 ret = add_afs_statistics(aft_row, &current_mood->stats);
505 if (ret < 0)
506 return ret;
507 return add_to_score_table(aft_row, &current_mood->stats);
508 }
509 /* update score */
510 ret = get_afsi_of_row(aft_row, &afsi);
511 if (ret < 0)
512 return ret;
513 if (old_afsi)
514 update_afs_statistics(old_afsi, &afsi);
515 score = compute_score(&afsi, &current_mood->stats);
516 PARA_DEBUG_LOG("score: %li\n", score);
517 percent = (score + 100) / 3;
518 if (percent > 100)
519 percent = 100;
520 else if (percent < 0)
521 percent = 0;
522 PARA_DEBUG_LOG("moving to %li%%\n", percent);
523 return score_update(aft_row, percent);
524 }
525
526 /* sse: seconds since epoch. */
527 static char *get_statistics(struct mood_instance *m, int64_t sse)
528 {
529 unsigned n = m->stats.num;
530 int mean_days, sigma_days;
531
532 mean_days = (sse - m->stats.last_played_sum / n) / 3600 / 24;
533 sigma_days = int_sqrt(m->stats.last_played_qd / n) / 3600 / 24;
534 return make_message(
535 "loaded mood %s (%u files)\n"
536 "last_played mean/sigma: %d/%d days\n"
537 "num_played mean/sigma: %" PRId64 "/%" PRIu64 "\n"
538 ,
539 m->name? m->name : "(dummy)",
540 n,
541 mean_days, sigma_days,
542 m->stats.num_played_sum / n,
543 int_sqrt(m->stats.num_played_qd / n)
544 );
545 }
546
547 /** Free all resources of the current mood, if any. */
548 void mood_unload(void)
549 {
550 destroy_mood(current_mood);
551 current_mood = NULL;
552 }
553
554 static void compute_correction_factors(int64_t sse, struct afs_statistics *s)
555 {
556 if (s->num > 0) {
557 s->normalization_divisor = int_sqrt(s->last_played_qd)
558 * int_sqrt(s->num_played_qd) / s->num / 100;
559 s->num_played_correction = sse - s->last_played_sum / s->num;
560 s->last_played_correction = s->num_played_sum / s->num;
561 }
562 if (s->num_played_correction == 0)
563 s->num_played_correction = 1;
564 if (s->normalization_divisor == 0)
565 s->normalization_divisor = 1;
566 if (s->last_played_correction == 0)
567 s->last_played_correction = 1;
568 }
569
570 /**
571 * Change the current mood.
572 *
573 * \param mood_name The name of the mood to load.
574 * \param msg Error message or mood info is returned here.
575 *
576 * If \a mood_name is \a NULL, load the dummy mood that accepts every audio file
577 * and uses a scoring method based only on the \a last_played information.
578 *
579 * If the message pointer is not NULL, a suitable message is returned there in
580 * all cases. The caller must free this string.
581 *
582 * \return The number of admissible files on success, negative on errors. It is
583 * not considered an error if no files are admissible.
584 *
585 * \sa struct \ref afs_info::last_played, \ref mp_eval_row().
586 */
587 int mood_load(const char *mood_name, char **msg)
588 {
589 int i, ret;
590 struct admissible_array aa = {.size = 0};
591 /*
592 * We can not use the "now" pointer from sched.c here because we are
593 * called before schedule(), which initializes "now".
594 */
595 struct timeval rnow;
596
597 if (mood_name) {
598 ret = init_mood_parser(mood_name, &aa.m, msg);
599 if (ret < 0)
600 return ret;
601 } else /* load dummy mood */
602 aa.m = alloc_new_mood(NULL);
603 PARA_NOTICE_LOG("computing statistics of admissible files\n");
604 ret = audio_file_loop(&aa, add_if_admissible);
605 if (ret < 0) {
606 if (msg) /* false if we are called via the event handler */
607 *msg = make_message("audio file loop failed\n");
608 goto out;
609 }
610 clock_get_realtime(&rnow);
611 compute_correction_factors(rnow.tv_sec, &aa.m->stats);
612 if (aa.m->stats.num == 0) {
613 if (msg)
614 *msg = make_message("no admissible files\n");
615 ret = 0;
616 goto out;
617 }
618 for (i = 0; i < aa.m->stats.num; i++) {
619 ret = add_to_score_table(aa.array[i], &aa.m->stats);
620 if (ret < 0) {
621 if (msg)
622 *msg = make_message(
623 "could not add row to score table\n");
624 goto out;
625 }
626 }
627 /* success */
628 if (msg)
629 *msg = get_statistics(aa.m, rnow.tv_sec);
630 ret = aa.m->stats.num;
631 mood_unload();
632 current_mood = aa.m;
633 out:
634 free(aa.array);
635 if (ret < 0)
636 destroy_mood(aa.m);
637 return ret;
638 }
639
640 /*
641 * Empty the score table and start over.
642 *
643 * This function is called on events which render the current list of
644 * admissible files useless, for example if an attribute is removed from the
645 * attribute table.
646 */
647 static int reload_current_mood(void)
648 {
649 int ret;
650 char *mood_name = NULL;
651
652 assert(current_mood);
653 score_clear();
654 PARA_NOTICE_LOG("reloading %s\n", current_mood->name?
655 current_mood->name : "(dummy)");
656 if (current_mood->name)
657 mood_name = para_strdup(current_mood->name);
658 mood_unload();
659 ret = mood_load(mood_name, NULL);
660 free(mood_name);
661 return ret;
662 }
663
664 /**
665 * Notification callback for the moods table.
666 *
667 * \param event Type of the event just occurred.
668 * \param pb Unused.
669 * \param data Its type depends on the event.
670 *
671 * This function updates the score table according to the event that has
672 * occurred. Two actions are possible: (a) reload the current mood, or (b)
673 * add/remove/update the row of the score table which corresponds to the audio
674 * file that has been modified or whose afs info has been changed. It depends
675 * on the type of the event which action (if any) is performed.
676 *
677 * The callbacks of command handlers such as com_add() or com_touch() which
678 * modify the audio file table call this function. The virtual streaming system
679 * also calls this after it has updated the afs info of the file it is about to
680 * stream (the one with the highest score). If the file stays admissible, its
681 * score is recomputed so that a different file is picked next time.
682 *
683 * \return Standard.
684 */
685 int moods_event_handler(enum afs_events event, __a_unused struct para_buffer *pb,
686 void *data)
687 {
688 if (!current_mood)
689 return 0;
690 switch (event) {
691 /*
692 * The three blob events might change the set of admissible files,
693 * so we must reload the score list.
694 */
695 case BLOB_RENAME:
696 case BLOB_REMOVE:
697 case BLOB_ADD:
698 if (data == moods_table || data == playlists_table)
699 return 1; /* no reload necessary for these */
700 return reload_current_mood();
701 /* these also require reload of the score table */
702 case ATTRIBUTE_ADD:
703 case ATTRIBUTE_REMOVE:
704 case ATTRIBUTE_RENAME:
705 return reload_current_mood();
706 /* changes to the aft only require to re-examine the audio file */
707 case AFSI_CHANGE: {
708 struct afsi_change_event_data *aced = data;
709 return mood_update_audio_file(aced->aft_row, aced->old_afsi);
710 }
711 case AFHI_CHANGE:
712 case AUDIO_FILE_RENAME:
713 case AUDIO_FILE_ADD:
714 return mood_update_audio_file(data, NULL);
715 case AUDIO_FILE_REMOVE:
716 return mood_delete_audio_file(data);
717 default:
718 return 1;
719 }
720 }