add blend mode tests
[swfdec.git] / swfdec / swfdec_sound_matrix.c
blob5531a43a206743d8424e37b3742174656058b763
1 /* Swfdec
2 * Copyright (C) 2008 Benjamin Otte <otte@gnome.org>
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
20 #ifndef HAVE_CONFIG_H
21 #include "config.h"
22 #endif
24 #include <swfdec_sound_matrix.h>
25 #include <liboil/liboil.h>
26 #include <swfdec_debug.h>
28 void
29 swfdec_sound_matrix_init_identity (SwfdecSoundMatrix *sound)
31 sound->ll = sound->rr = sound->volume = 100;
32 sound->lr = sound->rl = 0;
35 int
36 swfdec_sound_matrix_get_pan (const SwfdecSoundMatrix *sound)
38 g_return_val_if_fail (sound != NULL, 0);
40 if (sound->ll == 100)
41 return ABS (sound->rr) - 100;
42 else
43 return 100 - ABS (sound->ll);
46 void
47 swfdec_sound_matrix_set_pan (SwfdecSoundMatrix *sound, int pan)
49 g_return_if_fail (sound != NULL);
51 if (pan > 0) {
52 sound->rr = 100;
53 sound->ll = 100 - pan;
54 } else {
55 sound->ll = 100;
56 sound->rr = 100 + pan;
58 sound->lr = 0;
59 sound->rl = 0;
62 gboolean
63 swfdec_sound_matrix_is_identity (const SwfdecSoundMatrix *sound)
65 g_return_val_if_fail (sound != NULL, FALSE);
67 return sound->ll == 100 && sound->rr == 100 &&
68 sound->lr == 0 && sound->rl == 0 && sound->volume == 100;
71 gboolean
72 swfdec_sound_matrix_is_equal (const SwfdecSoundMatrix *a, const SwfdecSoundMatrix *b)
74 g_return_val_if_fail (a != NULL, FALSE);
75 g_return_val_if_fail (b != NULL, FALSE);
77 return a->ll == b->ll && a->rr == b->rr &&
78 a->lr == b->lr && a->rl == b->rl && a->volume == b->volume;
81 void
82 swfdec_sound_matrix_apply (const SwfdecSoundMatrix *sound,
83 gint16 *dest, guint n_samples)
85 guint i;
86 int left, right;
88 if (swfdec_sound_matrix_is_identity (sound))
89 return;
90 for (i = 0; i < n_samples; i++) {
91 left = (sound->ll * dest[0] + sound->lr * dest[1]) / 100;
92 left *= sound->volume / 100;
93 right = (sound->rl * dest[0] + sound->rr * dest[1]) / 100;
94 right *= sound->volume / 100;
95 dest[0] = left;
96 dest[1] = right;
97 dest += 2;
102 void
103 swfdec_sound_matrix_multiply (SwfdecSoundMatrix *dest,
104 const SwfdecSoundMatrix *a, const SwfdecSoundMatrix *b)
106 int ll, lr, rl, rr;
108 g_return_if_fail (dest != NULL);
109 g_return_if_fail (a != NULL);
110 g_return_if_fail (b != NULL);
112 ll = (b->ll * a->ll + b->lr * a->rl) / 100;
113 rl = (b->rl * a->ll + b->rr * a->rl) / 100;
114 lr = (b->ll * a->lr + b->lr * a->rr) / 100;
115 rr = (b->rl * a->lr + b->rr * a->rr) / 100;
117 dest->volume = b->volume * a->volume / 100;
118 dest->ll = ll;
119 dest->rl = rl;
120 dest->lr = lr;
121 dest->rr = rr;