1page.title=Floating-Point Audio
2@jd:body
3
4<div id="qv-wrapper">
5    <div id="qv">
6      <h2>On this page</h2>
7
8      <ol>
9        <li><a href="#best">Best Practices for Floating-Point Audio</a></li>
10        <li><a href="#support">Floating-Point Audio in Android SDK</a></li>
11        <li><a href="#more">For More Information</a></li>
12      </ol>
13    </div>
14  </div>
15
16<a href="https://www.youtube.com/watch?v=sIcieUqMml8" class="notice-developers-video">
17<div>
18    <h3>Video</h3>
19    <p>Will it Float? The Glory and Shame of Floating-Point Audio</p>
20</div>
21</a>
22
23<p>Using floating-point numbers to represent audio data can significantly enhance audio
24 quality in high-performance audio applications. Floating point offers the following
25 advantages:</p>
26
27<ul>
28<li>Wider dynamic range.</li>
29<li>Consistent accuracy across the dynamic range.</li>
30<li>More headroom to avoid clipping during intermediate calculations and transients.</li>
31</ul>
32
33<p>While floating-point can enhance audio quality, it does present certain disadvantages:</p>
34
35<ul>
36<li>Floating-point numbers use more memory.</li>
37<li>Floating-point operations employ unexpected properties, for example, addition is
38 not associative.</li>
39<li>Floating-point calculations can sometimes lose arithmetic precision due to rounding or
40 numerically unstable algorithms.</li>
41<li>Using floating-point effectively requires greater understanding to achieve accurate
42 and reproducible results.</li>
43</ul>
44
45<p>
46  Formerly, floating-point was notorious for being unavailable or slow. This is
47  still true for low-end and embedded processors. But processors on modern
48  mobile devices now have hardware floating-point with performance that is
49  similar (or in some cases even faster) than integer. Modern CPUs also support
50  <a href="http://en.wikipedia.org/wiki/SIMD" class="external-link">SIMD</a>
51  (Single instruction, multiple data), which can improve performance further.
52</p>
53
54<h2 id="best">Best Practices for Floating-Point Audio</h2>
55<p>The following best practices help you avoid problems with floating-point calculations:</p>
56<ul>
57<li>Use double precision floating-point for infrequent calculations,
58such as computing filter coefficients.</li>
59<li>Pay attention to the order of operations.</li>
60<li>Declare explicit variables for intermediate values.</li>
61<li>Use parentheses liberally.</li>
62<li>If you get a NaN or infinity result, use binary search to discover
63where it was introduced.</li>
64</ul>
65
66<h2 id="support">Floating-Point Audio in Android SDK</h2>
67
68<p>For floating-point audio, the audio format encoding
69 <code>AudioFormat.ENCODING_PCM_FLOAT</code> is used similarly to
70 <code>ENCODING_PCM_16_BIT</code> or <code>ENCODING_PCM_8_BIT</code> for specifying
71 AudioTrack data
72formats. The corresponding overloaded method <code>AudioTrack.write()</code>
73 takes in a float array to deliver data.</p>
74
75<pre>
76   public int write(float[] audioData,
77        int offsetInFloats,
78        int sizeInFloats,
79        int writeMode)
80</pre>
81
82<h2 id="more">For More Information</h2>
83
84<p>The following Wikipedia pages are helpful in understanding floating-point audio:</p>
85
86<ul>
87<li><a href="http://en.wikipedia.org/wiki/Audio_bit_depth" class="external-link" >Audio bit depth</a></li>
88<li><a href="http://en.wikipedia.org/wiki/Floating_point" class="external-link" >Floating point</a></li>
89<li><a href="http://en.wikipedia.org/wiki/IEEE_floating_point" class="external-link" >IEEE 754 floating-point</a></li>
90<li><a href="http://en.wikipedia.org/wiki/Loss_of_significance" class="external-link" >Loss of significance</a>
91 (catastrophic cancellation)</li>
92<li><a href="https://en.wikipedia.org/wiki/Numerical_stability" class="external-link" >Numerical stability</a></li>
93</ul>
94
95<p>The following article provides information on those aspects of floating-point that have a
96 direct impact on designers of computer systems:</p>
97<ul>
98<li><a href="http://docs.oracle.com/cd/E19957-01/806-3568/ncg_goldberg.html" class="external-link" >What every
99 computer scientist should know about floating-point arithmetic</a>
100by David Goldberg, Xerox PARC (edited reprint).</li>
101</ul>
102