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.

207 lines
6.1KB

  1. /*
  2. ** Copyright (C) 2002-2004 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. #include <stdio.h>
  19. #include <stdlib.h>
  20. #include <string.h>
  21. #include "config.h"
  22. #include "float_cast.h"
  23. #include "common.h"
  24. static int linear_process (SRC_PRIVATE *psrc, SRC_DATA *data) ;
  25. static void linear_reset (SRC_PRIVATE *psrc) ;
  26. /*========================================================================================
  27. */
  28. #define LINEAR_MAGIC_MARKER MAKE_MAGIC ('l', 'i', 'n', 'e', 'a', 'r')
  29. #define SRC_DEBUG 0
  30. typedef struct
  31. { int linear_magic_marker ;
  32. int channels ;
  33. long in_count, in_used ;
  34. long out_count, out_gen ;
  35. float last_value [1] ;
  36. } LINEAR_DATA ;
  37. /*----------------------------------------------------------------------------------------
  38. */
  39. static int
  40. linear_process (SRC_PRIVATE *psrc, SRC_DATA *data)
  41. { LINEAR_DATA *linear ;
  42. double src_ratio, input_index ;
  43. int ch ;
  44. if (psrc->private_data == NULL)
  45. return SRC_ERR_NO_PRIVATE ;
  46. linear = (LINEAR_DATA*) psrc->private_data ;
  47. linear->in_count = data->input_frames * linear->channels ;
  48. linear->out_count = data->output_frames * linear->channels ;
  49. linear->in_used = linear->out_gen = 0 ;
  50. src_ratio = psrc->last_ratio ;
  51. input_index = psrc->last_position ;
  52. /* Calculate samples before first sample in input array. */
  53. while (input_index < 1.0 && linear->out_gen < linear->out_count)
  54. {
  55. if (linear->in_used + linear->channels * input_index > linear->in_count)
  56. break ;
  57. if (fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
  58. src_ratio = psrc->last_ratio + linear->out_gen * (data->src_ratio - psrc->last_ratio) / (linear->out_count - 1) ;
  59. for (ch = 0 ; ch < linear->channels ; ch++)
  60. { data->data_out [linear->out_gen] = linear->last_value [ch] + input_index *
  61. (data->data_in [ch] - linear->last_value [ch]) ;
  62. linear->out_gen ++ ;
  63. } ;
  64. /* Figure out the next index. */
  65. input_index += 1.0 / src_ratio ;
  66. } ;
  67. linear->in_used += linear->channels * lrint (floor (input_index)) ;
  68. input_index -= floor (input_index) ;
  69. /* Main processing loop. */
  70. while (linear->out_gen < linear->out_count && linear->in_used + linear->channels * input_index <= linear->in_count)
  71. {
  72. if (fabs (psrc->last_ratio - data->src_ratio) > SRC_MIN_RATIO_DIFF)
  73. src_ratio = psrc->last_ratio + linear->out_gen * (data->src_ratio - psrc->last_ratio) / (linear->out_count - 1) ;
  74. if (SRC_DEBUG && linear->in_used < linear->channels && input_index < 1.0)
  75. { printf ("Whoops!!!! in_used : %ld channels : %d input_index : %f\n", linear->in_used, linear->channels, input_index) ;
  76. exit (1) ;
  77. } ;
  78. for (ch = 0 ; ch < linear->channels ; ch++)
  79. { data->data_out [linear->out_gen] = data->data_in [linear->in_used - linear->channels + ch] + input_index *
  80. (data->data_in [linear->in_used + ch] - data->data_in [linear->in_used - linear->channels + ch]) ;
  81. linear->out_gen ++ ;
  82. } ;
  83. /* Figure out the next index. */
  84. input_index += 1.0 / src_ratio ;
  85. linear->in_used += linear->channels * lrint (floor (input_index)) ;
  86. input_index -= floor (input_index) ;
  87. } ;
  88. if (linear->in_used > linear->in_count)
  89. { input_index += linear->in_used - linear->in_count ;
  90. linear->in_used = linear->in_count ;
  91. } ;
  92. psrc->last_position = input_index ;
  93. if (linear->in_used > 0)
  94. for (ch = 0 ; ch < linear->channels ; ch++)
  95. linear->last_value [ch] = data->data_in [linear->in_used - linear->channels + ch] ;
  96. /* Save current ratio rather then target ratio. */
  97. psrc->last_ratio = src_ratio ;
  98. data->input_frames_used = linear->in_used / linear->channels ;
  99. data->output_frames_gen = linear->out_gen / linear->channels ;
  100. return SRC_ERR_NO_ERROR ;
  101. } /* linear_process */
  102. /*------------------------------------------------------------------------------
  103. */
  104. const char*
  105. linear_get_name (int src_enum)
  106. {
  107. if (src_enum == SRC_LINEAR)
  108. return "Linear Interpolator" ;
  109. return NULL ;
  110. } /* linear_get_name */
  111. const char*
  112. linear_get_description (int src_enum)
  113. {
  114. if (src_enum == SRC_LINEAR)
  115. return "Linear interpolator, very fast, poor quality." ;
  116. return NULL ;
  117. } /* linear_get_descrition */
  118. int
  119. linear_set_converter (SRC_PRIVATE *psrc, int src_enum)
  120. { LINEAR_DATA *linear = NULL ;
  121. if (src_enum != SRC_LINEAR)
  122. return SRC_ERR_BAD_CONVERTER ;
  123. if (psrc->private_data != NULL)
  124. { linear = (LINEAR_DATA*) psrc->private_data ;
  125. if (linear->linear_magic_marker != LINEAR_MAGIC_MARKER)
  126. { free (psrc->private_data) ;
  127. psrc->private_data = NULL ;
  128. } ;
  129. } ;
  130. if (psrc->private_data == NULL)
  131. { linear = calloc (1, sizeof (*linear) + psrc->channels * sizeof (float)) ;
  132. if (linear == NULL)
  133. return SRC_ERR_MALLOC_FAILED ;
  134. psrc->private_data = linear ;
  135. } ;
  136. linear->linear_magic_marker = LINEAR_MAGIC_MARKER ;
  137. linear->channels = psrc->channels ;
  138. psrc->process = linear_process ;
  139. psrc->reset = linear_reset ;
  140. linear_reset (psrc) ;
  141. return SRC_ERR_NO_ERROR ;
  142. } /* linear_set_converter */
  143. /*===================================================================================
  144. */
  145. static void
  146. linear_reset (SRC_PRIVATE *psrc)
  147. { LINEAR_DATA *linear = NULL ;
  148. linear = (LINEAR_DATA*) psrc->private_data ;
  149. if (linear == NULL)
  150. return ;
  151. memset (linear->last_value, 0, sizeof (linear->last_value [0]) * linear->channels) ;
  152. } /* linear_reset */
  153. /*
  154. ** Do not edit or modify anything in this comment block.
  155. ** The arch-tag line is a file identity tag for the GNU Arch
  156. ** revision control system.
  157. **
  158. ** arch-tag: 7eac3103-3d84-45d3-8bbd-f409c2b2d1a9
  159. */