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.
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
24 #include <swfdec_sound_matrix.h>
25 #include <liboil/liboil.h>
26 #include <swfdec_debug.h>
29 swfdec_sound_matrix_init_identity (SwfdecSoundMatrix
*sound
)
31 sound
->ll
= sound
->rr
= sound
->volume
= 100;
32 sound
->lr
= sound
->rl
= 0;
36 swfdec_sound_matrix_get_pan (const SwfdecSoundMatrix
*sound
)
38 g_return_val_if_fail (sound
!= NULL
, 0);
41 return ABS (sound
->rr
) - 100;
43 return 100 - ABS (sound
->ll
);
47 swfdec_sound_matrix_set_pan (SwfdecSoundMatrix
*sound
, int pan
)
49 g_return_if_fail (sound
!= NULL
);
53 sound
->ll
= 100 - pan
;
56 sound
->rr
= 100 + pan
;
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;
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
;
82 swfdec_sound_matrix_apply (const SwfdecSoundMatrix
*sound
,
83 gint16
*dest
, guint n_samples
)
88 if (swfdec_sound_matrix_is_identity (sound
))
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;
103 swfdec_sound_matrix_multiply (SwfdecSoundMatrix
*dest
,
104 const SwfdecSoundMatrix
*a
, const SwfdecSoundMatrix
*b
)
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;