You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

161 lines
3.6KB

  1. /*
  2. ** Copyright (c) 2002-2016, Erik de Castro Lopo <erikd@mega-nerd.com>
  3. ** All rights reserved.
  4. **
  5. ** This code is released under 2-clause BSD license. Please see the
  6. ** file at : https://github.com/erikd/libsamplerate/blob/master/COPYING
  7. */
  8. #ifndef COMMON_H_INCLUDED
  9. #define COMMON_H_INCLUDED
  10. #ifdef HAVE_STDINT_H
  11. #include <stdint.h>
  12. #elif (SIZEOF_INT == 4)
  13. typedef int int32_t ;
  14. #elif (SIZEOF_LONG == 4)
  15. typedef long int32_t ;
  16. #endif
  17. #define SRC_MAX_RATIO 256
  18. #define SRC_MAX_RATIO_STR "256"
  19. #define SRC_MIN_RATIO_DIFF (1e-20)
  20. #define MAX(a,b) (((a) > (b)) ? (a) : (b))
  21. #define MIN(a,b) (((a) < (b)) ? (a) : (b))
  22. #define ARRAY_LEN(x) ((int) (sizeof (x) / sizeof ((x) [0])))
  23. #define OFFSETOF(type,member) ((int) (&((type*) 0)->member))
  24. #define MAKE_MAGIC(a,b,c,d,e,f) ((a) + ((b) << 4) + ((c) << 8) + ((d) << 12) + ((e) << 16) + ((f) << 20))
  25. /*
  26. ** Inspiration : http://sourcefrog.net/weblog/software/languages/C/unused.html
  27. */
  28. #ifdef UNUSED
  29. #elif defined (__GNUC__)
  30. # define UNUSED(x) UNUSED_ ## x __attribute__ ((unused))
  31. #elif defined (__LCLINT__)
  32. # define UNUSED(x) /*@unused@*/ x
  33. #else
  34. # define UNUSED(x) x
  35. #endif
  36. #ifdef __GNUC__
  37. # define WARN_UNUSED __attribute__ ((warn_unused_result))
  38. #else
  39. # define WARN_UNUSED
  40. #endif
  41. #include "samplerate.h"
  42. enum
  43. { SRC_FALSE = 0,
  44. SRC_TRUE = 1,
  45. SRC_MODE_PROCESS = 555,
  46. SRC_MODE_CALLBACK = 556
  47. } ;
  48. enum
  49. { SRC_ERR_NO_ERROR = 0,
  50. SRC_ERR_MALLOC_FAILED,
  51. SRC_ERR_BAD_STATE,
  52. SRC_ERR_BAD_DATA,
  53. SRC_ERR_BAD_DATA_PTR,
  54. SRC_ERR_NO_PRIVATE,
  55. SRC_ERR_BAD_SRC_RATIO,
  56. SRC_ERR_BAD_PROC_PTR,
  57. SRC_ERR_SHIFT_BITS,
  58. SRC_ERR_FILTER_LEN,
  59. SRC_ERR_BAD_CONVERTER,
  60. SRC_ERR_BAD_CHANNEL_COUNT,
  61. SRC_ERR_SINC_BAD_BUFFER_LEN,
  62. SRC_ERR_SIZE_INCOMPATIBILITY,
  63. SRC_ERR_BAD_PRIV_PTR,
  64. SRC_ERR_BAD_SINC_STATE,
  65. SRC_ERR_DATA_OVERLAP,
  66. SRC_ERR_BAD_CALLBACK,
  67. SRC_ERR_BAD_MODE,
  68. SRC_ERR_NULL_CALLBACK,
  69. SRC_ERR_NO_VARIABLE_RATIO,
  70. SRC_ERR_SINC_PREPARE_DATA_BAD_LEN,
  71. SRC_ERR_BAD_INTERNAL_STATE,
  72. /* This must be the last error number. */
  73. SRC_ERR_MAX_ERROR
  74. } ;
  75. typedef struct SRC_PRIVATE_tag
  76. { double last_ratio, last_position ;
  77. int error ;
  78. int channels ;
  79. /* SRC_MODE_PROCESS or SRC_MODE_CALLBACK */
  80. int mode ;
  81. /* Pointer to data to converter specific data. */
  82. void *private_data ;
  83. /* Varispeed process function. */
  84. int (*vari_process) (struct SRC_PRIVATE_tag *psrc, SRC_DATA *data) ;
  85. /* Constant speed process function. */
  86. int (*const_process) (struct SRC_PRIVATE_tag *psrc, SRC_DATA *data) ;
  87. /* State reset. */
  88. void (*reset) (struct SRC_PRIVATE_tag *psrc) ;
  89. /* Data specific to SRC_MODE_CALLBACK. */
  90. src_callback_t callback_func ;
  91. void *user_callback_data ;
  92. long saved_frames ;
  93. const float *saved_data ;
  94. } SRC_PRIVATE ;
  95. /* In src_sinc.c */
  96. const char* sinc_get_name (int src_enum) ;
  97. const char* sinc_get_description (int src_enum) ;
  98. int sinc_set_converter (SRC_PRIVATE *psrc, int src_enum) ;
  99. /* In src_linear.c */
  100. const char* linear_get_name (int src_enum) ;
  101. const char* linear_get_description (int src_enum) ;
  102. int linear_set_converter (SRC_PRIVATE *psrc, int src_enum) ;
  103. /* In src_zoh.c */
  104. const char* zoh_get_name (int src_enum) ;
  105. const char* zoh_get_description (int src_enum) ;
  106. int zoh_set_converter (SRC_PRIVATE *psrc, int src_enum) ;
  107. /*----------------------------------------------------------
  108. ** Common static inline functions.
  109. */
  110. static inline double
  111. fmod_one (double x)
  112. { double res ;
  113. res = x - lrint (x) ;
  114. if (res < 0.0)
  115. return res + 1.0 ;
  116. return res ;
  117. } /* fmod_one */
  118. static inline int
  119. is_bad_src_ratio (double ratio)
  120. { return (ratio < (1.0 / SRC_MAX_RATIO) || ratio > (1.0 * SRC_MAX_RATIO)) ;
  121. } /* is_bad_src_ratio */
  122. #endif /* COMMON_H_INCLUDED */