1page.title=Sample Rate Conversion 2@jd:body 3 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--> 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> 28See the Wikipedia article 29<a class="external-link" href="http://en.wikipedia.org/wiki/Resampling_(audio)" target="_android">Resampling (audio)</a> 30for a generic definition of sample rate conversion, also known as "resampling." 31The remainder of this article describes resampling within Android. 32</p> 33 34<p> 35Sample rate conversion is the process of changing a 36stream of discrete samples at one sample rate to another stream at 37another sample rate. A sample rate converter, or resampler, is a module 38that implements sample rate conversion. With respect to the resampler, 39the original stream is called the source signal, and the resampled stream is 40the sink signal. 41</p> 42 43<p> 44Resamplers are used in several places in Android. 45For example, an MP3 file may be encoded at 44.1 kHz sample rate and 46needs to be played back on an Android device supporting 48 kHz audio 47internally. In that case, a resampler would be used to upsample the MP3 48output audio from 44.1 kHz source sample rate to a 48 kHz sink sample rate 49used within the Android device. 50</p> 51 52<p> 53The 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> 69The ideal resampler would exactly preserve the source signal's amplitude 70and frequency bandwidth (subject to limitations of the sink 71sample rate), have minimal and consistent delay, have minimal 72computational complexity, permit arbitrary and dynamic conversion ratios, 73and support all common digital audio sample formats. 74In practice, ideal resamplers do not exist, and actual resamplers are 75a compromise among these characteristics. 76For example, the goals of ideal quality conflict with short delay and low complexity. 77</p> 78 79<p> 80Android includes a variety of audio resamplers, so that appropriate 81compromises can be made depending on the application use case and load. 82Section <a href="#srcResamplers">Resampler implementations</a> 83below lists the available resamplers, summarizes their characteristics, 84and 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> 96The Nyquist frequency, equal to 1/2 of a given sample rate, is the 97maximum frequency component that can be represented by a discretized 98signal at that sample rate. For example, the human hearing range is 99typically assumed to extend up to approximately 20 kHz, and so a digital 100audio signal must have a sample rate of at least 40 kHz to represent that 101range. In practice, sample rates of 44.1 kHz and 48 kHz are commonly 102used, with Nyquist frequencies of 22.05 kHz and 24 kHz respectively. 103See the Wikipedia articles 104<a class="external-link" href="http://en.wikipedia.org/wiki/Nyquist_frequency" target="_android">Nyquist frequency</a> 105and 106<a class="external-link" href="http://en.wikipedia.org/wiki/Hearing_range" target="_android">Hearing range</a> 107for 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> 133Available resampler implementations change frequently, 134and may be customized by OEMs. 135As of Android 4.4, the default resamplers 136in descending order of signal distortion, and ascending order of 137computational 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> 148In general, the sinc resamplers are more appropriate for higher-quality 149music playback, and the other resamplers should be reserved for cases 150where quality is less important (an example might be "key clicks" or similar). 151</p> 152 153<p> 154The specific resampler implementation selected depends on 155the use case, load, and the value of system property 156<code>af.resampler.quality</code>. For details, 157consult the audio resampler source code in AudioFlinger. 158</p> 159