Glenn Kasten | 46ac61c | 2014-01-24 08:59:11 -0800 | [diff] [blame] | 1 | page.title=Sample Rate Conversion |
| 2 | @jd:body |
| 3 | |
| 4 | <div id="qv-wrapper"> |
| 5 | <div id="qv"> |
| 6 | <h2>In this document</h2> |
| 7 | <ol id="auto-toc"> |
| 8 | </ol> |
| 9 | </div> |
| 10 | </div> |
| 11 | |
| 12 | <p> |
| 13 | See the Wikipedia article |
| 14 | <a class="external-link" href="http://en.wikipedia.org/wiki/Resampling_(audio)" target="_android">Resampling (audio)</a> |
| 15 | for a generic definition of sample rate conversion, also known as "resampling." |
| 16 | The remainder of this article describes resampling within Android. |
| 17 | </p> |
| 18 | |
| 19 | <p> |
| 20 | Sample rate conversion is the process of changing a |
| 21 | stream of discrete samples at one sample rate to another stream at |
| 22 | another sample rate. A sample rate converter, or resampler, is a module |
| 23 | that implements sample rate conversion. With respect to the resampler, |
| 24 | the original stream is called the source signal, and the resampled stream is |
| 25 | the sink signal. |
| 26 | </p> |
| 27 | |
| 28 | <p> |
| 29 | Resamplers are used in several places in Android. |
| 30 | For example, an MP3 file may be encoded at 44.1 kHz sample rate and |
| 31 | needs to be played back on an Android device supporting 48 kHz audio |
| 32 | internally. In that case, a resampler would be used to upsample the MP3 |
| 33 | output audio from 44.1 kHz source sample rate to a 48 kHz sink sample rate |
| 34 | used within the Android device. |
| 35 | </p> |
| 36 | |
| 37 | <p> |
| 38 | The characteristics of a resampler can be expressed using metrics, including: |
| 39 | </p> |
| 40 | |
| 41 | <ul> |
| 42 | <li>degree of preservation of the overall amplitude of the signal</li> |
| 43 | <li>degree of preservation of the frequency bandwidth of the signal, |
| 44 | subject to limitations of the sink sample rate</li> |
| 45 | <li>overall latency through the resampler</li> |
| 46 | <li>consistent phase and group delay with respect to frequency</li> |
| 47 | <li>computational complexity, expressed in CPU cycles or power draw</li> |
| 48 | <li>permitted ratios of source and sink sample rates</li> |
| 49 | <li>ability to dynamically change sample rate ratios</li> |
| 50 | <li>which digital audio sample formats are supported</li> |
| 51 | </ul> |
| 52 | |
| 53 | <p> |
| 54 | The ideal resampler would exactly preserve the source signal's amplitude |
| 55 | and frequency bandwidth (subject to limitations of the sink |
| 56 | sample rate), have minimal and consistent delay, have minimal |
| 57 | computational complexity, permit arbitrary and dynamic conversion ratios, |
| 58 | and support all common digital audio sample formats. |
| 59 | In practice, ideal resamplers do not exist, and actual resamplers are |
| 60 | a compromise among these characteristics. |
| 61 | For example, the goals of ideal quality conflict with short delay and low complexity. |
| 62 | </p> |
| 63 | |
| 64 | <p> |
| 65 | Android includes a variety of audio resamplers, so that appropriate |
| 66 | compromises can be made depending on the application use case and load. |
| 67 | Section <a href="#srcResamplers">Resampler implementations</a> |
| 68 | below lists the available resamplers, summarizes their characteristics, |
| 69 | and identifies where they should typically be used. |
| 70 | </p> |
| 71 | |
| 72 | <h2 id="srcTerms">Terminology</h2> |
| 73 | |
| 74 | <dl> |
| 75 | |
| 76 | <dt>downsample</dt> |
| 77 | <dd>to resample, where sink sample rate < source sample rate</dd> |
| 78 | |
| 79 | <dt>Nyquist frequency</dt> |
| 80 | <dd> |
| 81 | The Nyquist frequency, equal to 1/2 of a given sample rate, is the |
| 82 | maximum frequency component that can be represented by a discretized |
| 83 | signal at that sample rate. For example, the human hearing range is |
| 84 | typically assumed to extend up to approximately 20 kHz, and so a digital |
| 85 | audio signal must have a sample rate of at least 40 kHz to represent that |
| 86 | range. In practice, sample rates of 44.1 kHz and 48 kHz are commonly |
| 87 | used, with Nyquist frequencies of 22.05 kHz and 24 kHz respectively. |
| 88 | See the Wikipedia articles |
| 89 | <a class="external-link" href="http://en.wikipedia.org/wiki/Nyquist_frequency" target="_android">Nyquist frequency</a> |
| 90 | and |
| 91 | <a class="external-link" href="http://en.wikipedia.org/wiki/Hearing_range" target="_android">Hearing range</a> |
| 92 | for more information. |
| 93 | </dd> |
| 94 | |
| 95 | <dt>resampler</dt> |
| 96 | <dd>synonym for sample rate converter</dd> |
| 97 | |
| 98 | <dt>resampling</dt> |
| 99 | <dd>the process of converting sample rate</dd> |
| 100 | |
| 101 | <dt>sample rate converter</dt> |
| 102 | <dd>a module that resamples</dd> |
| 103 | |
| 104 | <dt>sink</dt> |
| 105 | <dd>the output of a resampler</dd> |
| 106 | |
| 107 | <dt>source</dt> |
| 108 | <dd>the input to a resampler</dd> |
| 109 | |
| 110 | <dt>upsample</dt> |
| 111 | <dd>to resample, where sink sample rate > source sample rate</dd> |
| 112 | |
| 113 | </dl> |
| 114 | |
| 115 | <h2 id="srcResamplers">Resampler implementations</h2> |
| 116 | |
| 117 | <p> |
| 118 | Available resampler implementations change frequently, |
| 119 | and may be customized by OEMs. |
| 120 | As of Android 4.4, the default resamplers |
| 121 | in descending order of signal distortion, and ascending order of |
| 122 | computational complexity include: |
| 123 | </p> |
| 124 | |
| 125 | <ul> |
| 126 | <li>linear</li> |
| 127 | <li>cubic</li> |
| 128 | <li>sinc with original coefficients</li> |
| 129 | <li>sinc with revised coefficients</li> |
| 130 | </ul> |
| 131 | |
| 132 | <p> |
| 133 | In general, the sinc resamplers are more appropriate for higher-quality |
| 134 | music playback, and the other resamplers should be reserved for cases |
| 135 | where quality is less important (an example might be "key clicks" or similar). |
| 136 | </p> |
| 137 | |
| 138 | <p> |
| 139 | The specific resampler implementation selected depends on |
| 140 | the use case, load, and the value of system property |
| 141 | <code>af.resampler.quality</code>. For details, |
| 142 | consult the audio resampler source code in AudioFlinger. |
| 143 | </p> |