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.

362 lines
13KB

  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
  2. <HTML>
  3. <HEAD>
  4. <TITLE>
  5. Secret Rabbit Code (aka libsamplerate)
  6. </TITLE>
  7. <META NAME="Author" CONTENT="Erik de Castro Lopo (erikd AT mega-nerd DOT com)">
  8. <META NAME="Version" CONTENT="libsamplerate-0.1.8">
  9. <META NAME="Description" CONTENT="The Secret Rabbit Code Home Page">
  10. <META NAME="Keywords" CONTENT="libsamplerate sound resample audio dsp Linux">
  11. <LINK REL=StyleSheet HREF="SRC.css" TYPE="text/css" MEDIA="all">
  12. </HEAD>
  13. <BODY TEXT="#FFFFFF" BGCOLOR="#000000" LINK="#FB1465" VLINK="#FB1465" ALINK="#FB1465">
  14. <!-- pepper -->
  15. <CENTER>
  16. <IMG SRC="SRC.png" HEIGHT=100 WIDTH=760 ALT="SRC.png">
  17. </CENTER>
  18. <!-- pepper -->
  19. <BR>
  20. <!-- pepper -->
  21. <TABLE ALIGN="center" WIDTH="98%">
  22. <TR>
  23. <TD VALIGN="top">
  24. <BR>
  25. <DIV CLASS="nav">
  26. <BR>
  27. <A HREF="index.html">Home</A><BR>
  28. <A HREF="license.html">License</A><BR>
  29. <A HREF="history.html">History</A><BR>
  30. <A HREF="download.html">Download</A><BR>
  31. <A HREF="quality.html">Quality</A><BR>
  32. <A HREF="api.html">API</A><BR>
  33. <A HREF="bugs.html">Bug Reporting</A><BR>
  34. <A HREF="win32.html">On Win32</A><BR>
  35. <A HREF="faq.html">FAQ</A><BR>
  36. <A HREF="lists.html">Mailing Lists</A><BR>
  37. <A HREF="ChangeLog">ChangeLog</A><BR>
  38. <BR>
  39. <DIV CLASS="block">
  40. Author :<BR>Erik de Castro Lopo
  41. <!-- pepper -->
  42. <BR><BR>
  43. <!-- pepper -->
  44. </DIV>
  45. <IMG SRC=
  46. "/cgi-bin/Count.cgi?ft=6|frgb=55;55;55|tr=0|md=6|dd=B|st=1|sh=1|df=src_api.dat"
  47. HEIGHT=30 WIDTH=100 ALT="counter.gif">
  48. </DIV>
  49. </TD>
  50. <!-- pepper -->
  51. <!-- ######################################################################## -->
  52. <!-- pepper -->
  53. <TD VALIGN="top">
  54. <DIV CLASS="block">
  55. <H1><B>Frequently Asked Questions</B></H1>
  56. <P>
  57. <A HREF="#Q001">Q1 : Is it normal for the output of libsamplerate to be louder
  58. than its input?</A><BR><BR>
  59. <A HREF="#Q002">Q2 : On Unix/Linux/MacOSX, what is the best way of detecting
  60. the presence and location of libsamplerate and its header file using
  61. autoconf?</A><BR><BR>
  62. <A HREF="#Q003">Q3 : If I upsample and downsample to the original rate, for
  63. example 44.1->96->44.1, do I get an identical signal as the one before the
  64. up/down resampling?</A><BR><BR>
  65. <A HREF="#Q004">Q4 : If I ran src_simple (libsamplerate) on small chunks (160
  66. frames) would that sound bad?</A><BR><BR>
  67. <A HREF="#Q005">Q5 : I'm using libsamplerate but the high quality settings
  68. sound worse than the SRC_LINEAR converter. Why?</A><BR><BR>
  69. <A HREF="#Q006">Q6 : I'm use the SRC_SINC_* converters and up-sampling by a ratio of
  70. 2. I reset the converter and put in 1000 samples and I expect to get 2000
  71. samples out, but I'm getting less than that. Why?</A><BR><BR>
  72. <A HREF="#Q007">Q7 : I have input and output sample rates that are integer
  73. values, but the API wants me to divide one by the other and put the result
  74. in a floating point number. Won't this case problems for long running
  75. conversions?</A><BR><BR>
  76. </P>
  77. <HR>
  78. <!-- ========================================================================= -->
  79. <A NAME="Q001"></A>
  80. <H2><BR><B>Q1 : Is it normal for the output of libsamplerate to be louder
  81. than its input?</B></H2>
  82. <P>
  83. The output of libsamplerate will be roughly the same volume as the input.
  84. However, even if the input is strictly in the range (-1.0, 1.0), it is still
  85. possible for the output to contain peak values outside this range.
  86. </P>
  87. <P>
  88. Consider four consecutive samples of [0.5 0.999 0.999 0.5].
  89. If we are up sampling by a factor of two we need to insert samples between
  90. each of the existing samples.
  91. Its pretty obvious then, that the sample between the two 0.999 values should
  92. and will be bigger than 0.999.
  93. </P>
  94. <P>
  95. This means that anyone using libsamplerate should normalize its output before
  96. doing things like saving the audio to a 16 bit WAV file.
  97. </P>
  98. <!-- pepper -->
  99. <!-- ========================================================================= -->
  100. <a NAME="Q002"></a>
  101. <h2><br><b>Q2 : On Unix/Linux/MacOSX, what is the best way of detecting
  102. the presence and location of libsamplerate and its header file using
  103. autoconf?</b></h2>
  104. <p>
  105. libsamplerate uses the pkg-config (man pkg-config) method of registering itself
  106. with the host system.
  107. The best way of detecting its presence is using something like this in configure.ac
  108. (or configure.in):
  109. </p>
  110. <pre>
  111. PKG_CHECK_MODULES(SAMPLERATE, samplerate >= 0.1.3,
  112. ac_cv_samplerate=1, ac_cv_samplerate=0)
  113. AC_DEFINE_UNQUOTED([HAVE_SAMPLERATE],${ac_cv_samplerate},
  114. [Set to 1 if you have libsamplerate.])
  115. AC_SUBST(SAMPLERATE_CFLAGS)
  116. AC_SUBST(SAMPLERATE_LIBS)
  117. </pre>
  118. <p>
  119. This will automatically set the <b>SAMPLERATE_CFLAGS</b> and <b>SAMPLERATE_LIBS</b>
  120. variables which can be used in Makefile.am or Makefile.in like this:
  121. </p>
  122. <pre>
  123. SAMPLERATE_CFLAGS = @SAMPLERATE_CFLAGS@
  124. SAMPLERATE_LIBS = @SAMPLERATE_LIBS@
  125. </pre>
  126. <p>
  127. If you install libsamplerate from source, you will probably need to set the
  128. <b>PKG_CONFIG_PATH</b> environment variable's suggested at the end of the
  129. libsamplerate configure process. For instance on my system I get this:
  130. </p>
  131. <pre>
  132. -=-=-=-=-=-=-=-=-=-= Configuration Complete =-=-=-=-=-=-=-=-=-=-=-
  133. Configuration summary :
  134. Version : ..................... 0.1.3
  135. Enable debugging : ............ no
  136. Tools :
  137. Compiler is GCC : ............. yes
  138. GCC major version : ........... 3
  139. Extra tools required for testing and examples :
  140. Have FFTW : ................... yes
  141. Have libsndfile : ............. yes
  142. Have libefence : .............. no
  143. Installation directories :
  144. Library directory : ........... /usr/local/lib
  145. Program directory : ........... /usr/local/bin
  146. Pkgconfig directory : ......... /usr/local/lib/pkgconfig
  147. </pre>
  148. <!-- pepper -->
  149. <!-- ========================================================================= -->
  150. <A NAME="Q003"></A>
  151. <H2><BR><B>Q3 : If I upsample and downsample to the original rate, for
  152. example 44.1->96->44.1, do I get an identical signal as the one before the
  153. up/down resampling?</B></H2>
  154. <P>
  155. The short answer is that for the general case, no, you don't.
  156. The long answer is that for some signals, with some converters, you will
  157. get very, very close.
  158. </P>
  159. <P>
  160. In order to resample correctly (ie using the <B>SRC_SINC_*</B> converters),
  161. filtering needs to be applied, regardless of whether its upsampling or
  162. downsampling.
  163. This filter needs to attenuate all frequencies above 0.5 times the minimum of
  164. the source and destination sample rate (call this fshmin).
  165. Since the filter needed to achieve full attenuation at this point, it has to
  166. start rolling off a some frequency below this point.
  167. It is this rolloff of the very highest frequencies which causes some of the
  168. loss.
  169. </P>
  170. <P>
  171. The other factor is that the filter itself can introduce transient artifacts
  172. which causes the output to be different to the input.
  173. </P>
  174. <!-- pepper -->
  175. <!-- ========================================================================= -->
  176. <A NAME="Q004"></A>
  177. <H2><BR><B>Q4 : If I ran src_simple on small chunks (say 160 frames) would that
  178. sound bad?</B></H2>
  179. <P>
  180. Well if you are after odd sound effects, it might sound OK.
  181. If you are after high quality sample rate conversion you will be disappointed.
  182. </P>
  183. <P>
  184. The src_simple() was designed to provide a simple to use interface for people
  185. who wanted to do sample rate conversion on say, a whole file all at once.
  186. </P>
  187. <!-- pepper -->
  188. <!-- ========================================================================= -->
  189. <A NAME="Q005"></A>
  190. <H2><BR><B>Q5 : I'm using libsamplerate but the high quality settings
  191. sound worse than the SRC_LINEAR converter. Why?</B></H2>
  192. <P>
  193. There are two possible problems.
  194. Firstly, if you are using the src_simple() function on successive blocks
  195. of a stream of samples, you will get bad results. The src_simple() function
  196. is designed for use on a whole sound file, all at once, not on contiguous
  197. segments of the same sound file.
  198. To fix the problem, you need to move to the src_process() API or the callback
  199. based API.
  200. </P>
  201. <P>
  202. If you are already using the src_process() API or the callback based API and
  203. the high quality settings sound worse than SRC_LINEAR, then you have other
  204. problems.
  205. Read on for more debugging hints.
  206. </P>
  207. <P>
  208. All of the higher quality converters need to keep state while doing conversions
  209. on segments of a large chunk of audio.
  210. This state information is kept inside the private data pointed to by the
  211. SRC_STATE pointer returned by the src_new() function.
  212. This means, that when you want to start doing sample rate conversion on a
  213. stream of data, you should call src_new() to get a new SRC_STATE pointer
  214. (or alternatively, call src_reset() on an existing SRC_STATE pointer).
  215. You should then pass this SRC_STATE pointer to the src_process() function
  216. with each new block of audio data.
  217. When you have completed the conversion, you can then call src_delete() on
  218. the SRC_STATE pointer.
  219. </P>
  220. <P>
  221. If you are doing all of the above correctly, you need to examine your usage
  222. of the values passed to src_process() in the
  223. <A HREF="api_misc.html#SRC_DATA">SRC_DATA</A>
  224. struct.
  225. Specifically:
  226. </P>
  227. <UL>
  228. <LI> Check that input_frames and output_frames fields are being set in
  229. terms of frames (number of sample values times channels) instead
  230. of just the number of samples.
  231. <LI> Check that you are using the return values input_frames_used and
  232. output_frames_gen to update your source and destination pointers
  233. correctly.
  234. <LI> Check that you are updating the data_in and data_out pointers
  235. correctly for each successive call.
  236. </UL>
  237. <P>
  238. While doing the above, it is probably useful to compare what you are doing to
  239. what is done in the example programs in the examples/ directory of the source
  240. code tarball.
  241. </P>
  242. <P>
  243. If you have done all of the above and are still having problems then its
  244. probably time to email the author with the smallest chunk of code that
  245. adequately demonstrates your problem.
  246. This chunk should not need to be any more than 100 lines of code.
  247. </P>
  248. <!-- pepper -->
  249. <!-- ========================================================================= -->
  250. <A NAME="Q006"></A>
  251. <H2><BR><B>Q6 : I'm use the SRC_SINC_* converters and up-sampling by a ratio of
  252. 2. I reset the converter and put in 1000 samples and I expect to get 2000
  253. samples out, but I'm getting less than that. Why?</B></H2>
  254. <P>
  255. The short answer is that there is a transport delay inside the converter itself.
  256. Long answer follows.
  257. </P>
  258. <P>
  259. By way of example, the first time you call src_process() you might only get 1900
  260. samples out.
  261. However, after that first call all subsequent calls will probably get you about
  262. 2000 samples out for every 1000 samples you put in.
  263. </P>
  264. <P>
  265. The main problems people have with this transport delay is that they need to read
  266. out an exact number of samples and the transport delay scews this up.
  267. The best way to overcome this problem is to always supply more samples on the
  268. input than is actually needed to create the required number of output samples.
  269. With reference to the example above, if you always supply 1500 samples at the
  270. input, you will always get 2000 samples at the output.
  271. You will always need to keep track of the number of input frames used on each
  272. call to src_process() and deal with these values appropriately.
  273. </P>
  274. <!-- pepper -->
  275. <!-- ========================================================================= -->
  276. <A NAME="Q007"></A>
  277. <H2><BR><B>Q7 : I have input and output sample rates that are integer
  278. values, but the API wants me to divide one by the other and put the result
  279. in a floating point number. Won't this case problems for long running
  280. conversions?</B></H2>
  281. <P>
  282. The short answer is no, the precision of the ratio is many orders of magnitude
  283. more than is really needed.
  284. </P>
  285. <P>
  286. For the long answer, lets do come calculations.
  287. Firstly, the <tt>src_ratio</tt> field is double precision floating point number
  288. which has
  289. <a href="http://en.wikipedia.org/wiki/Double_precision">
  290. 53 bits of precision</a>.
  291. </P>
  292. <P>
  293. That means that the maximum error in your ratio converted to a double is one
  294. bit in 2^53 which means the the double float value would be wrong by one sample
  295. after 9007199254740992 samples have passed or wrong by more than half a sample
  296. wrong after half that many (4503599627370496 samples) have passed.
  297. </P>
  298. <P>
  299. Now if for example our output sample rate is 96kHz then
  300. </P>
  301. <pre>
  302. 4503599627370496 samples at 96kHz is 46912496118 seconds
  303. 46912496118 seconds is 781874935 minutes
  304. 781874935 minutes is 13031248 hours
  305. 13031248 hours is 542968 days
  306. 542968 days is 1486 years
  307. </pre>
  308. <P>
  309. So, after 1486 years, the input will be wrong by more than half of one sampling
  310. period.
  311. </P>
  312. <p>
  313. All this assumes that the crystal oscillators uses to sample the audio stream
  314. is perfect.
  315. This is not the case.
  316. According to
  317. <a href="http://www.ieee-uffc.org/freqcontrol/quartz/vig/vigcomp.htm">
  318. this web site</a>,
  319. the accuracy of standard crystal oscillators (XO, TCXO, OCXO) is at best
  320. 1 in 100 million.
  321. The <tt>src_ratio</tt> is therefore 45035996 times more accurate than the
  322. crystal clock source used to sample the original audio signal and any potential
  323. problem with the <tt>src_ratio</tt> being a floating point number will be
  324. completely swamped by sampling inaccuracies.
  325. </p>
  326. <!-- <A HREF="mailto:aldel@mega-nerd.com">For the spam bots</A> -->
  327. </DIV>
  328. </TD></TR>
  329. </TABLE>
  330. </BODY>
  331. </HTML>