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