[4513] | 1 | //float.c: |
---|
| 2 | |
---|
| 3 | /* |
---|
| 4 | * Copyright (C) Philipp 'ph3-der-loewe' Schafft - 2009 |
---|
| 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 | |
---|
[4514] | 27 | static inline float _si32_to_float (int32_t src) { |
---|
| 28 | if ( src > 0 ) { |
---|
| 29 | return (float)src / 2147483647.f; |
---|
| 30 | } else { |
---|
| 31 | return (float)src / 2147483648.f; |
---|
| 32 | } |
---|
| 33 | } |
---|
| 34 | |
---|
| 35 | static inline int32_t _float_to_si32 (float src) { |
---|
| 36 | if ( src > 0 ) { |
---|
| 37 | return (src * 2147483647.f); |
---|
| 38 | } else { |
---|
| 39 | return (src * 2147483648.f); |
---|
| 40 | } |
---|
| 41 | } |
---|
| 42 | |
---|
| 43 | int roar_conv_int32_float(float * dst, const int32_t * src, size_t len) { |
---|
| 44 | size_t i; |
---|
| 45 | |
---|
| 46 | if ( len == 0 ) |
---|
| 47 | return 0; |
---|
| 48 | |
---|
| 49 | if ( dst == NULL || src == NULL ) |
---|
| 50 | return -1; |
---|
| 51 | |
---|
| 52 | for (i = 0; i < len; i++) |
---|
| 53 | dst[i] = _si32_to_float(src[i]); |
---|
| 54 | |
---|
| 55 | return 0; |
---|
| 56 | } |
---|
| 57 | |
---|
| 58 | int roar_conv_float_int32(int32_t * dst, const float * src, size_t len) { |
---|
| 59 | size_t i; |
---|
| 60 | |
---|
| 61 | if ( len == 0 ) |
---|
| 62 | return 0; |
---|
| 63 | |
---|
| 64 | if ( dst == NULL || src == NULL ) |
---|
| 65 | return -1; |
---|
| 66 | |
---|
| 67 | for (i = 0; i < len; i++) |
---|
| 68 | dst[i] = _float_to_si32(src[i]); |
---|
| 69 | |
---|
| 70 | return 0; |
---|
| 71 | } |
---|
| 72 | |
---|
| 73 | int roar_conv_int32_float_deint(float ** dst, const int32_t * src, size_t len, size_t channels) { |
---|
| 74 | register size_t i, c, o; |
---|
| 75 | |
---|
| 76 | ROAR_DBG("roar_conv_int32_float_deint(*) = ?"); |
---|
| 77 | |
---|
| 78 | if ( len == 0 ) |
---|
| 79 | return 0; |
---|
| 80 | |
---|
| 81 | ROAR_DBG("roar_conv_int32_float_deint(*) = ?"); |
---|
| 82 | |
---|
| 83 | if ( dst == NULL || src == NULL ) |
---|
| 84 | return -1; |
---|
| 85 | |
---|
| 86 | ROAR_DBG("roar_conv_int32_float_deint(*) = ?"); |
---|
| 87 | |
---|
| 88 | // check if len is a multiple of channels: |
---|
| 89 | if ( (len % channels) != 0 ) |
---|
| 90 | return -1; |
---|
[4513] | 91 | |
---|
[4514] | 92 | ROAR_DBG("roar_conv_int32_float_deint(*) = ?"); |
---|
| 93 | |
---|
| 94 | for (c = 0; c < channels; c++) |
---|
| 95 | if ( dst[c] == NULL ) |
---|
| 96 | return -1; |
---|
| 97 | |
---|
| 98 | ROAR_DBG("roar_conv_int32_float_deint(*) = ?"); |
---|
| 99 | |
---|
| 100 | switch (channels) { |
---|
| 101 | case 1: |
---|
| 102 | return roar_conv_int32_float(dst[0], src, len); |
---|
| 103 | break; |
---|
| 104 | case 2: |
---|
| 105 | for (i = o = 0; i < len; i += 2, o += 1) { |
---|
| 106 | dst[0][o] = _si32_to_float(src[i+0]); |
---|
| 107 | dst[1][o] = _si32_to_float(src[i+1]); |
---|
| 108 | } |
---|
| 109 | break; |
---|
| 110 | default: |
---|
| 111 | for (i = c = o = 0; i < len; i++) { |
---|
| 112 | dst[c][o] = _si32_to_float(src[i]); |
---|
| 113 | c++; |
---|
| 114 | if ( c == channels ) { |
---|
| 115 | c = 0; |
---|
| 116 | o++; |
---|
| 117 | } |
---|
| 118 | } |
---|
| 119 | break; |
---|
| 120 | } |
---|
| 121 | |
---|
| 122 | return 0; |
---|
| 123 | } |
---|
| 124 | |
---|
| 125 | int roar_conv_float_int32_enint(int32_t * dst, const float ** src, size_t len, size_t channels) { |
---|
| 126 | size_t i, c, o; |
---|
| 127 | |
---|
| 128 | if ( len == 0 ) |
---|
| 129 | return 0; |
---|
| 130 | |
---|
| 131 | if ( dst == NULL || src == NULL ) |
---|
| 132 | return -1; |
---|
| 133 | |
---|
| 134 | // check if len is a multiple of channels: |
---|
| 135 | if ( (len % channels) != 0 ) |
---|
| 136 | return -1; |
---|
| 137 | |
---|
| 138 | for (c = 0; c < channels; c++) |
---|
| 139 | if ( src[c] == NULL ) |
---|
| 140 | return -1; |
---|
| 141 | |
---|
| 142 | switch (channels) { |
---|
| 143 | default: |
---|
| 144 | for (i = c = o = 0; o < len; o++) { |
---|
| 145 | dst[o] = _float_to_si32(src[c][i]); |
---|
| 146 | c++; |
---|
| 147 | if ( c == channels ) { |
---|
| 148 | c = 0; |
---|
| 149 | i++; |
---|
| 150 | } |
---|
| 151 | } |
---|
| 152 | break; |
---|
| 153 | } |
---|
| 154 | |
---|
| 155 | return 0; |
---|
| 156 | } |
---|
[4513] | 157 | |
---|
| 158 | //ll |
---|