[4668] | 1 | //filter_downmix.c: |
---|
[1004] | 2 | |
---|
| 3 | /* |
---|
[4708] | 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2008-2011 |
---|
[1004] | 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 |
---|
[3517] | 21 | * the Free Software Foundation, 51 Franklin Street, Fifth Floor, |
---|
| 22 | * Boston, MA 02110-1301, USA. |
---|
[1004] | 23 | * |
---|
| 24 | */ |
---|
| 25 | |
---|
| 26 | #include "libroardsp.h" |
---|
| 27 | |
---|
| 28 | int roardsp_downmix_calc162 (struct roardsp_filter * filter, void * data, size_t samples) { |
---|
| 29 | int16_t * samp = (int16_t *) data; |
---|
| 30 | register int32_t mode = (ROAR_INSTINT)filter->inst; |
---|
| 31 | register union { |
---|
[1007] | 32 | // int16_t i16; |
---|
[1004] | 33 | int32_t i32; |
---|
| 34 | int64_t i64; |
---|
| 35 | } s; |
---|
| 36 | size_t i; |
---|
| 37 | |
---|
| 38 | ROAR_DBG("roardsp_downmix_calc162(*): mode=%i", mode); |
---|
| 39 | |
---|
| 40 | switch (mode) { |
---|
[1007] | 41 | case ROARDSP_DOWNMIX_LEFT: |
---|
| 42 | for (i = 0; i < samples; i += 2) |
---|
| 43 | samp[i+1] = samp[i]; |
---|
| 44 | break; |
---|
| 45 | case ROARDSP_DOWNMIX_RIGHT: |
---|
| 46 | for (i = 0; i < samples; i += 2) |
---|
| 47 | samp[i] = samp[i+1]; |
---|
| 48 | break; |
---|
[1004] | 49 | case ROARDSP_DOWNMIX_ARITHMETIC: |
---|
| 50 | for (i = 0; i < samples; i += 2) { |
---|
| 51 | s.i32 = (samp[i] + samp[i+1])/2; |
---|
| 52 | samp[i ] = s.i32; |
---|
| 53 | samp[i+1] = s.i32; |
---|
| 54 | } |
---|
| 55 | break; |
---|
[1104] | 56 | #ifdef ROAR_HAVE_LIBM |
---|
[1004] | 57 | case ROARDSP_DOWNMIX_RMS: |
---|
| 58 | for (i = 0; i < samples; i += 2) { |
---|
| 59 | s.i64 = (int64_t)samp[i]*samp[i] + (int64_t)samp[i+1]*samp[i+1]; |
---|
[1020] | 60 | #ifdef ROAR_HAVE_SQRTL |
---|
[1010] | 61 | s.i64 = sqrtl((long double)s.i64/2.0); |
---|
[1020] | 62 | #else |
---|
| 63 | s.i64 = sqrt((double)s.i64/2.0); |
---|
| 64 | #endif |
---|
[1010] | 65 | // s.i64 /= 2; |
---|
[1006] | 66 | |
---|
| 67 | if ( (samp[i] + samp[i+1]) < 0 ) |
---|
| 68 | s.i64 *= -1; |
---|
| 69 | |
---|
[1004] | 70 | samp[i ] = s.i64; |
---|
| 71 | samp[i+1] = s.i64; |
---|
| 72 | } |
---|
| 73 | break; |
---|
[1104] | 74 | #endif |
---|
[1004] | 75 | default: |
---|
| 76 | ROAR_DBG("roardsp_downmix_calc162(*) = -1 // unknown mode"); |
---|
| 77 | return -1; |
---|
| 78 | break; |
---|
| 79 | } |
---|
| 80 | |
---|
| 81 | ROAR_DBG("roardsp_downmix_calc162(*) = 0"); |
---|
| 82 | return 0; |
---|
| 83 | } |
---|
| 84 | |
---|
| 85 | int roardsp_downmix_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
| 86 | int32_t old; |
---|
| 87 | |
---|
| 88 | if ( cmd == ROARDSP_FCTL_MODE ) { |
---|
| 89 | old = (ROAR_INSTINT)filter->inst; |
---|
| 90 | filter->inst = (void*)(ROAR_INSTINT)*(int32_t*)data; |
---|
| 91 | *(int32_t*)data = old; |
---|
| 92 | } else { |
---|
| 93 | return -1; |
---|
| 94 | } |
---|
| 95 | |
---|
| 96 | |
---|
| 97 | return 0; |
---|
| 98 | } |
---|
| 99 | |
---|
[1141] | 100 | int roardsp_downmix_reset (struct roardsp_filter * filter, int what) { |
---|
[5181] | 101 | int32_t mode = ROARDSP_DOWNMIX_ARITHMETIC; |
---|
[1141] | 102 | |
---|
| 103 | if ( filter == NULL ) |
---|
| 104 | return -1; |
---|
| 105 | |
---|
| 106 | switch (what) { |
---|
| 107 | case ROARDSP_RESET_NONE: |
---|
| 108 | case ROARDSP_RESET_STATE: |
---|
| 109 | return 0; |
---|
| 110 | break; |
---|
| 111 | case ROARDSP_RESET_FULL: |
---|
| 112 | roardsp_downmix_ctl(filter, ROARDSP_FCTL_MODE, &mode); |
---|
| 113 | return 0; |
---|
| 114 | break; |
---|
| 115 | default: |
---|
| 116 | return -1; |
---|
| 117 | } |
---|
| 118 | |
---|
| 119 | return -1; |
---|
| 120 | } |
---|
| 121 | |
---|
[1004] | 122 | //ll |
---|