[1004] | 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_calc162 (struct roardsp_filter * filter, void * data, size_t samples) { |
---|
| 28 | int16_t * samp = (int16_t *) data; |
---|
| 29 | register int32_t mode = (ROAR_INSTINT)filter->inst; |
---|
| 30 | register union { |
---|
[1007] | 31 | // int16_t i16; |
---|
[1004] | 32 | int32_t i32; |
---|
| 33 | int64_t i64; |
---|
| 34 | } s; |
---|
| 35 | size_t i; |
---|
| 36 | |
---|
| 37 | ROAR_DBG("roardsp_downmix_calc162(*): mode=%i", mode); |
---|
| 38 | |
---|
| 39 | switch (mode) { |
---|
[1007] | 40 | case ROARDSP_DOWNMIX_LEFT: |
---|
| 41 | for (i = 0; i < samples; i += 2) |
---|
| 42 | samp[i+1] = samp[i]; |
---|
| 43 | break; |
---|
| 44 | case ROARDSP_DOWNMIX_RIGHT: |
---|
| 45 | for (i = 0; i < samples; i += 2) |
---|
| 46 | samp[i] = samp[i+1]; |
---|
| 47 | break; |
---|
[1004] | 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; |
---|
[1104] | 55 | #ifdef ROAR_HAVE_LIBM |
---|
[1004] | 56 | case ROARDSP_DOWNMIX_RMS: |
---|
| 57 | for (i = 0; i < samples; i += 2) { |
---|
| 58 | s.i64 = (int64_t)samp[i]*samp[i] + (int64_t)samp[i+1]*samp[i+1]; |
---|
[1020] | 59 | #ifdef ROAR_HAVE_SQRTL |
---|
[1010] | 60 | s.i64 = sqrtl((long double)s.i64/2.0); |
---|
[1020] | 61 | #else |
---|
| 62 | s.i64 = sqrt((double)s.i64/2.0); |
---|
| 63 | #endif |
---|
[1010] | 64 | // s.i64 /= 2; |
---|
[1006] | 65 | |
---|
| 66 | if ( (samp[i] + samp[i+1]) < 0 ) |
---|
| 67 | s.i64 *= -1; |
---|
| 68 | |
---|
[1004] | 69 | samp[i ] = s.i64; |
---|
| 70 | samp[i+1] = s.i64; |
---|
| 71 | } |
---|
| 72 | break; |
---|
[1104] | 73 | #endif |
---|
[1004] | 74 | default: |
---|
| 75 | ROAR_DBG("roardsp_downmix_calc162(*) = -1 // unknown mode"); |
---|
| 76 | return -1; |
---|
| 77 | break; |
---|
| 78 | } |
---|
| 79 | |
---|
| 80 | ROAR_DBG("roardsp_downmix_calc162(*) = 0"); |
---|
| 81 | return 0; |
---|
| 82 | } |
---|
| 83 | |
---|
| 84 | int roardsp_downmix_ctl (struct roardsp_filter * filter, int cmd, void * data) { |
---|
| 85 | int32_t old; |
---|
| 86 | |
---|
| 87 | if ( cmd == ROARDSP_FCTL_MODE ) { |
---|
| 88 | old = (ROAR_INSTINT)filter->inst; |
---|
| 89 | filter->inst = (void*)(ROAR_INSTINT)*(int32_t*)data; |
---|
| 90 | *(int32_t*)data = old; |
---|
| 91 | } else { |
---|
| 92 | return -1; |
---|
| 93 | } |
---|
| 94 | |
---|
| 95 | |
---|
| 96 | return 0; |
---|
| 97 | } |
---|
| 98 | |
---|
[1141] | 99 | int roardsp_downmix_reset (struct roardsp_filter * filter, int what) { |
---|
| 100 | int mode = ROARDSP_DOWNMIX_ARITHMETIC; |
---|
| 101 | |
---|
| 102 | if ( filter == NULL ) |
---|
| 103 | return -1; |
---|
| 104 | |
---|
| 105 | switch (what) { |
---|
| 106 | case ROARDSP_RESET_NONE: |
---|
| 107 | case ROARDSP_RESET_STATE: |
---|
| 108 | return 0; |
---|
| 109 | break; |
---|
| 110 | case ROARDSP_RESET_FULL: |
---|
| 111 | roardsp_downmix_ctl(filter, ROARDSP_FCTL_MODE, &mode); |
---|
| 112 | return 0; |
---|
| 113 | break; |
---|
| 114 | default: |
---|
| 115 | return -1; |
---|
| 116 | } |
---|
| 117 | |
---|
| 118 | return -1; |
---|
| 119 | } |
---|
| 120 | |
---|
[1004] | 121 | //ll |
---|