1 | //downmix.c: |
---|
2 | |
---|
3 | /* |
---|
4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - December 2008 |
---|
5 | * |
---|
6 | * This file is part of libroardsp a part of RoarAudio, |
---|
7 | * a cross-platform sound system for both, home and professional use. |
---|
8 | * See README for details. |
---|
9 | * |
---|
10 | * This file is free software; you can redistribute it and/or modify |
---|
11 | * it under the terms of the GNU General Public License version 3 |
---|
12 | * as published by the Free Software Foundation. |
---|
13 | * |
---|
14 | * libroardsp is distributed in the hope that it will be useful, |
---|
15 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
16 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
17 | * GNU General Public License for more details. |
---|
18 | * |
---|
19 | * You should have received a copy of the GNU General Public License |
---|
20 | * along with this software; see the file COPYING. If not, write to |
---|
21 | * the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
---|
22 | * |
---|
23 | */ |
---|
24 | |
---|
25 | #include "libroardsp.h" |
---|
26 | |
---|
27 | int roardsp_downmix_init (struct roardsp_filter * filter, struct roar_stream * stream, int id) { |
---|
28 | int mode = ROARDSP_DOWNMIX_ARITHMETIC; |
---|
29 | |
---|
30 | roardsp_downmix_ctl(filter, ROARDSP_FCTL_MODE, &mode); |
---|
31 | |
---|
32 | ROAR_DBG("roardsp_downmix_init(*) = 0"); |
---|
33 | return 0; |
---|
34 | } |
---|
35 | |
---|
36 | int roardsp_downmix_calc162 (struct roardsp_filter * filter, void * data, size_t samples) { |
---|
37 | int16_t * samp = (int16_t *) data; |
---|
38 | register int32_t mode = (ROAR_INSTINT)filter->inst; |
---|
39 | register union { |
---|
40 | int32_t i32; |
---|
41 | int64_t i64; |
---|
42 | } s; |
---|
43 | size_t i; |
---|
44 | |
---|
45 | ROAR_DBG("roardsp_downmix_calc162(*): mode=%i", mode); |
---|
46 | |
---|
47 | switch (mode) { |
---|
48 | case ROARDSP_DOWNMIX_ARITHMETIC: |
---|
49 | for (i = 0; i < samples; i += 2) { |
---|
50 | s.i32 = (samp[i] + samp[i+1])/2; |
---|
51 | samp[i ] = s.i32; |
---|
52 | samp[i+1] = s.i32; |
---|
53 | } |
---|
54 | break; |
---|
55 | case ROARDSP_DOWNMIX_RMS: |
---|
56 | for (i = 0; i < samples; i += 2) { |
---|
57 | s.i64 = (int64_t)samp[i]*samp[i] + (int64_t)samp[i+1]*samp[i+1]; |
---|
58 | s.i64 = sqrt(s.i64); |
---|
59 | s.i64 /= 2; |
---|
60 | |
---|
61 | if ( (samp[i] + samp[i+1]) < 0 ) |
---|
62 | s.i64 *= -1; |
---|
63 | |
---|
64 | samp[i ] = s.i64; |
---|
65 | samp[i+1] = s.i64; |
---|
66 | } |
---|
67 | break; |
---|
68 | default: |
---|
69 | ROAR_DBG("roardsp_downmix_calc162(*) = -1 // unknown mode"); |
---|
70 | return -1; |
---|
71 | break; |
---|
72 | } |
---|
73 | |
---|
74 | ROAR_DBG("roardsp_downmix_calc162(*) = 0"); |
---|
75 | return 0; |
---|
76 | } |
---|
77 | |
---|
78 | int roardsp_downmix_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
79 | int32_t old; |
---|
80 | |
---|
81 | if ( cmd == ROARDSP_FCTL_MODE ) { |
---|
82 | old = (ROAR_INSTINT)filter->inst; |
---|
83 | filter->inst = (void*)(ROAR_INSTINT)*(int32_t*)data; |
---|
84 | *(int32_t*)data = old; |
---|
85 | } else { |
---|
86 | return -1; |
---|
87 | } |
---|
88 | |
---|
89 | |
---|
90 | return 0; |
---|
91 | } |
---|
92 | |
---|
93 | //ll |
---|