146ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenpage.title=Sample Rate Conversion
246ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten@jd:body
346ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
4bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy<!--
5bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy    Copyright 2013 The Android Open Source Project
6bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy
7bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy    Licensed under the Apache License, Version 2.0 (the "License");
8bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy    you may not use this file except in compliance with the License.
9bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy    You may obtain a copy of the License at
10bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy
11bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy        http://www.apache.org/licenses/LICENSE-2.0
12bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy
13bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy    Unless required by applicable law or agreed to in writing, software
14bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy    distributed under the License is distributed on an "AS IS" BASIS,
15bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy    See the License for the specific language governing permissions and
17bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy    limitations under the License.
18bc92aea666f04a57b8717fa0c1d43f1f287acd9aClay Murphy-->
1946ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<div id="qv-wrapper">
2046ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten  <div id="qv">
2146ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten    <h2>In this document</h2>
2246ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten    <ol id="auto-toc">
2346ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten    </ol>
2446ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten  </div>
2546ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</div>
2646ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
275d83ab4822d3a64be5564b2040f892beacdeb995Clay Murphy<h2 id="srcIntro">Introduction</h2>
285d83ab4822d3a64be5564b2040f892beacdeb995Clay Murphy
2946ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<p>
3046ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenSee the Wikipedia article
31460c688dba80441038975e41106eade3b2019a28Clay Murphy<a href="http://en.wikipedia.org/wiki/Resampling_(audio)">Resampling (audio)</a>
3246ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenfor a generic definition of sample rate conversion, also known as "resampling."
3346ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenThe remainder of this article describes resampling within Android.
3446ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</p>
3546ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
3646ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<p>
3746ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenSample rate conversion is the process of changing a
3846ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenstream of discrete samples at one sample rate to another stream at
3946ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenanother sample rate.  A sample rate converter, or resampler, is a module
4046ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenthat implements sample rate conversion.  With respect to the resampler,
4146ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenthe original stream is called the source signal, and the resampled stream is
4246ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenthe sink signal.
4346ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</p>
4446ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
4546ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<p>
4646ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenResamplers are used in several places in Android.
4746ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenFor example, an MP3 file may be encoded at 44.1 kHz sample rate and
4846ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenneeds to be played back on an Android device supporting 48 kHz audio
4946ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasteninternally. In that case, a resampler would be used to upsample the MP3
5046ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenoutput audio from 44.1 kHz source sample rate to a 48 kHz sink sample rate
5146ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenused within the Android device.
5246ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</p>
5346ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
5446ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<p>
5546ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenThe characteristics of a resampler can be expressed using metrics, including:
5646ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</p>
5746ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
5846ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<ul>
5946ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>degree of preservation of the overall amplitude of the signal</li>
6046ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>degree of preservation of the frequency bandwidth of the signal,
6146ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten    subject to limitations of the sink sample rate</li>
6246ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>overall latency through the resampler</li>
6346ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>consistent phase and group delay with respect to frequency</li>
6446ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>computational complexity, expressed in CPU cycles or power draw</li>
6546ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>permitted ratios of source and sink sample rates</li>
6646ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>ability to dynamically change sample rate ratios</li>
6746ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>which digital audio sample formats are supported</li>
6846ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</ul>
6946ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
7046ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<p>
7146ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenThe ideal resampler would exactly preserve the source signal's amplitude
7246ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenand frequency bandwidth (subject to limitations of the sink
7346ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastensample rate), have minimal and consistent delay, have minimal
7446ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastencomputational complexity, permit arbitrary and dynamic conversion ratios,
7546ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenand support all common digital audio sample formats.
7646ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenIn practice, ideal resamplers do not exist, and actual resamplers are
7746ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastena compromise among these characteristics.
7846ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenFor example, the goals of ideal quality conflict with short delay and low complexity.
7946ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</p>
8046ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
8146ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<p>
8246ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenAndroid includes a variety of audio resamplers, so that appropriate
8346ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastencompromises can be made depending on the application use case and load.
8446ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenSection <a href="#srcResamplers">Resampler implementations</a>
8546ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenbelow lists the available resamplers, summarizes their characteristics,
8646ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenand identifies where they should typically be used.
8746ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</p>
8846ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
8946ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<h2 id="srcResamplers">Resampler implementations</h2>
9046ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
9146ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<p>
9246ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenAvailable resampler implementations change frequently,
9346ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenand may be customized by OEMs.
9446ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenAs of Android 4.4, the default resamplers
9546ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenin descending order of signal distortion, and ascending order of
9646ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastencomputational complexity include:
9746ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</p>
9846ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
9946ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<ul>
10046ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>linear</li>
10146ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>cubic</li>
10246ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>sinc with original coefficients</li>
10346ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<li>sinc with revised coefficients</li>
10446ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</ul>
10546ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
10646ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<p>
10746ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenIn general, the sinc resamplers are more appropriate for higher-quality
10846ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenmusic playback, and the other resamplers should be reserved for cases
10946ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenwhere quality is less important (an example might be "key clicks" or similar).
11046ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</p>
11146ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten
11246ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<p>
11346ac61ca091b72004337804f55a50ac65b7426b9Glenn KastenThe specific resampler implementation selected depends on
11446ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenthe use case, load, and the value of system property
11546ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten<code>af.resampler.quality</code>.  For details,
11646ac61ca091b72004337804f55a50ac65b7426b9Glenn Kastenconsult the audio resampler source code in AudioFlinger.
11746ac61ca091b72004337804f55a50ac65b7426b9Glenn Kasten</p>
118