Emeric Vigier | eebea67 | 2012-08-06 17:36:30 -0400 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (C) 2002-2011 Erik de Castro Lopo <erikd@mega-nerd.com> |
| 3 | ** |
| 4 | ** This program is free software; you can redistribute it and/or modify |
| 5 | ** it under the terms of the GNU General Public License as published by |
| 6 | ** the Free Software Foundation; either version 2 of the License, or |
| 7 | ** (at your option) any later version. |
| 8 | ** |
| 9 | ** This program 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 |
| 12 | ** GNU General Public License for more details. |
| 13 | ** |
| 14 | ** You should have received a copy of the GNU General Public License |
| 15 | ** along with this program; if not, write to the Free Software |
| 16 | ** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | /* |
| 20 | ** This code is part of Secret Rabbit Code aka libsamplerate. A commercial |
| 21 | ** use license for this code is available, please see: |
| 22 | ** http://www.mega-nerd.com/SRC/procedure.html |
| 23 | */ |
| 24 | |
| 25 | #ifndef COMMON_H_INCLUDED |
| 26 | #define COMMON_H_INCLUDED |
| 27 | |
| 28 | #ifdef HAVE_STDINT_H |
| 29 | #include <stdint.h> |
| 30 | #elif (SIZEOF_INT == 4) |
| 31 | typedef int int32_t ; |
| 32 | #elif (SIZEOF_LONG == 4) |
| 33 | typedef long int32_t ; |
| 34 | #endif |
| 35 | |
| 36 | #define SRC_MAX_RATIO 256 |
| 37 | #define SRC_MAX_RATIO_STR "256" |
| 38 | |
| 39 | #define SRC_MIN_RATIO_DIFF (1e-20) |
| 40 | |
| 41 | #define MAX(a,b) (((a) > (b)) ? (a) : (b)) |
| 42 | #define MIN(a,b) (((a) < (b)) ? (a) : (b)) |
| 43 | |
| 44 | #define ARRAY_LEN(x) ((int) (sizeof (x) / sizeof ((x) [0]))) |
| 45 | #define OFFSETOF(type,member) ((int) (&((type*) 0)->member)) |
| 46 | |
| 47 | #define MAKE_MAGIC(a,b,c,d,e,f) ((a) + ((b) << 4) + ((c) << 8) + ((d) << 12) + ((e) << 16) + ((f) << 20)) |
| 48 | |
| 49 | /* |
| 50 | ** Inspiration : http://sourcefrog.net/weblog/software/languages/C/unused.html |
| 51 | */ |
| 52 | #ifdef UNUSED |
| 53 | #elif defined (__GNUC__) |
| 54 | # define UNUSED(x) UNUSED_ ## x __attribute__ ((unused)) |
| 55 | #elif defined (__LCLINT__) |
| 56 | # define UNUSED(x) /*@unused@*/ x |
| 57 | #else |
| 58 | # define UNUSED(x) x |
| 59 | #endif |
| 60 | |
| 61 | #ifdef __GNUC__ |
| 62 | # define WARN_UNUSED __attribute__ ((warn_unused_result)) |
| 63 | #else |
| 64 | # define WARN_UNUSED |
| 65 | #endif |
| 66 | |
| 67 | |
| 68 | #include "samplerate.h" |
| 69 | |
| 70 | enum |
| 71 | { SRC_FALSE = 0, |
| 72 | SRC_TRUE = 1, |
| 73 | |
| 74 | SRC_MODE_PROCESS = 555, |
| 75 | SRC_MODE_CALLBACK = 556 |
| 76 | } ; |
| 77 | |
| 78 | enum |
| 79 | { SRC_ERR_NO_ERROR = 0, |
| 80 | |
| 81 | SRC_ERR_MALLOC_FAILED, |
| 82 | SRC_ERR_BAD_STATE, |
| 83 | SRC_ERR_BAD_DATA, |
| 84 | SRC_ERR_BAD_DATA_PTR, |
| 85 | SRC_ERR_NO_PRIVATE, |
| 86 | SRC_ERR_BAD_SRC_RATIO, |
| 87 | SRC_ERR_BAD_PROC_PTR, |
| 88 | SRC_ERR_SHIFT_BITS, |
| 89 | SRC_ERR_FILTER_LEN, |
| 90 | SRC_ERR_BAD_CONVERTER, |
| 91 | SRC_ERR_BAD_CHANNEL_COUNT, |
| 92 | SRC_ERR_SINC_BAD_BUFFER_LEN, |
| 93 | SRC_ERR_SIZE_INCOMPATIBILITY, |
| 94 | SRC_ERR_BAD_PRIV_PTR, |
| 95 | SRC_ERR_BAD_SINC_STATE, |
| 96 | SRC_ERR_DATA_OVERLAP, |
| 97 | SRC_ERR_BAD_CALLBACK, |
| 98 | SRC_ERR_BAD_MODE, |
| 99 | SRC_ERR_NULL_CALLBACK, |
| 100 | SRC_ERR_NO_VARIABLE_RATIO, |
| 101 | SRC_ERR_SINC_PREPARE_DATA_BAD_LEN, |
| 102 | |
| 103 | /* This must be the last error number. */ |
| 104 | SRC_ERR_MAX_ERROR |
| 105 | } ; |
| 106 | |
| 107 | typedef struct SRC_PRIVATE_tag |
| 108 | { double last_ratio, last_position ; |
| 109 | |
| 110 | int error ; |
| 111 | int channels ; |
| 112 | |
| 113 | /* SRC_MODE_PROCESS or SRC_MODE_CALLBACK */ |
| 114 | int mode ; |
| 115 | |
| 116 | /* Pointer to data to converter specific data. */ |
| 117 | void *private_data ; |
| 118 | |
| 119 | /* Varispeed process function. */ |
| 120 | int (*vari_process) (struct SRC_PRIVATE_tag *psrc, SRC_DATA *data) ; |
| 121 | |
| 122 | /* Constant speed process function. */ |
| 123 | int (*const_process) (struct SRC_PRIVATE_tag *psrc, SRC_DATA *data) ; |
| 124 | |
| 125 | /* State reset. */ |
| 126 | void (*reset) (struct SRC_PRIVATE_tag *psrc) ; |
| 127 | |
| 128 | /* Data specific to SRC_MODE_CALLBACK. */ |
| 129 | src_callback_t callback_func ; |
| 130 | void *user_callback_data ; |
| 131 | long saved_frames ; |
| 132 | float *saved_data ; |
| 133 | } SRC_PRIVATE ; |
| 134 | |
| 135 | /* In src_sinc.c */ |
| 136 | const char* sinc_get_name (int src_enum) ; |
| 137 | const char* sinc_get_description (int src_enum) ; |
| 138 | |
| 139 | int sinc_set_converter (SRC_PRIVATE *psrc, int src_enum) ; |
| 140 | |
| 141 | /* In src_linear.c */ |
| 142 | const char* linear_get_name (int src_enum) ; |
| 143 | const char* linear_get_description (int src_enum) ; |
| 144 | |
| 145 | int linear_set_converter (SRC_PRIVATE *psrc, int src_enum) ; |
| 146 | |
| 147 | /* In src_zoh.c */ |
| 148 | const char* zoh_get_name (int src_enum) ; |
| 149 | const char* zoh_get_description (int src_enum) ; |
| 150 | |
| 151 | int zoh_set_converter (SRC_PRIVATE *psrc, int src_enum) ; |
| 152 | |
| 153 | /*---------------------------------------------------------- |
| 154 | ** Common static inline functions. |
| 155 | */ |
| 156 | |
| 157 | static inline double |
| 158 | fmod_one (double x) |
| 159 | { double res ; |
| 160 | |
| 161 | res = x - lrint (x) ; |
| 162 | if (res < 0.0) |
| 163 | return res + 1.0 ; |
| 164 | |
| 165 | return res ; |
| 166 | } /* fmod_one */ |
| 167 | |
| 168 | #endif /* COMMON_H_INCLUDED */ |
| 169 | |