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