1page.title=Audio Input Latency 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="#check-list">Checklist</a></li> 10 <li><a href="#ways">Ways to Reduce Audio Input Latency</a></li> 11 <li><a href="#avoid">What to Avoid</a></li> 12 </ol> 13 </div> 14 </div> 15 16 17<p>This page provides guidelines to help you reduce audio input latency when recording with a 18built-in microphone or an external headset microphone.</p> 19 20<h2 id="check-list">Checklist</h2> 21 22<p>Here are a few important prerequisites:</p> 23 24<ul> 25 <li>You must use the Android-specific implementation of the 26 <a class="external-link" href="https://www.khronos.org/opensles/">OpenSL ES™</a> API. 27 28 <li>If you haven't already done so, download and install the 29 <a href="{@docRoot}tools/sdk/ndk/index.html">Android NDK</a>.</li> 30 31 <li>Many of the same requirements for low-latency audio output also apply to low-latency input, 32 so read the requirements for low-latency output in 33 <a href="{@docRoot}ndk/guides/audio/output-latency.html">Audio Output Latency</a>.</li> 34</ul> 35 36<h2 id="ways">Ways to Reduce Audio Input Latency</h2> 37 38<p>The following are some methods to help ensure low audio input latency: 39 40<ul> 41 <li>Suggest to your users, if your app relies on low-latency audio, that they use a headset 42(for example, by displaying a <em>Best with headphones</em> screen on first run). Note 43that just using the headset doesn’t guarantee the lowest possible latency. You may need to 44perform other steps to remove any unwanted signal processing from the audio path, such as by 45using the <a href="http://developer.android.com/reference/android/media/MediaRecorder.AudioSource.html#VOICE_RECOGNITION"> 46VOICE_RECOGNITION</a> preset when recording.</li> 47 48 <li>It's difficult to test audio input and output latency in isolation. The best solution to 49determine the lowest possible audio input latency is to measure round-trip audio and divide 50by two.</li> 51 <li> Be prepared to handle nominal sample rates of 44,100 and 48,000 Hz as reported by 52<a href="{@docRoot}reference/android/media/AudioManager.html#getProperty(java.lang.String)"> 53getProperty(String)</a> for 54<a href="{@docRoot}reference/android/media/AudioManager.html#PROPERTY_OUTPUT_SAMPLE_RATE"> 55PROPERTY_OUTPUT_SAMPLE_RATE</a>. Other sample rates are possible, but rare.</li> 56 57 <li>Be prepared to handle the buffer size reported by 58<a href="{@docRoot}reference/android/media/AudioManager.html#getProperty(java.lang.String)"> 59getProperty(String)</a> for 60<a href="{@docRoot}reference/android/media/AudioManager.html#PROPERTY_OUTPUT_FRAMES_PER_BUFFER"> 61PROPERTY_OUTPUT_FRAMES_PER_BUFFER</a>. Typical buffer sizes include 96, 128, 160, 192, 240, 256, 62or 512 frames, but other values are possible.</li> 63</ul> 64 65<h2 id="avoid">What to Avoid</h2> 66 67<p>Be sure to take these things into account to help avoid latency issues:</p> 68 69<ul> 70 <li>Don’t assume that the speakers and microphones used in mobile devices generally have good 71acoustics. Due to their small size, the acoustics are generally poor so signal processing is 72added to improve the sound quality. This signal processing introduces latency.</li> 73 74 <li>Don't assume that your input and output callbacks are synchronized. For simultaneous input 75and output, separate buffer queue completion handlers are used for each side. There is no 76guarantee of the relative order of these callbacks or the synchronization of the audio clocks, 77even when both sides use the same sample rate. Your application should buffer the data with 78proper buffer synchronization.</li> 79 80 <li>Don't assume that the actual sample rate exactly matches the nominal sample rate. For 81example, if the nominal sample rate is 48,000 Hz, it is normal for the audio clock to advance 82at a slightly different rate than the operating system {@code CLOCK_MONOTONIC}. This is because 83the audio and system clocks may derive from different crystals.</li> 84 85 <li>Don't assume that the actual playback sample rate exactly matches the actual capture sample 86rate, especially if the endpoints are on separate paths. For example, if you are capturing from 87the on-device microphone at 48,000 Hz nominal sample rate, and playing on USB audio 88at 48,000 Hz nominal sample rate, the actual sample rates are likely to be slightly different 89from each other.</li> 90</ul> 91 92<p>A consequence of potentially independent audio clocks is the need for asynchronous sample rate 93conversion. A simple (though not ideal for audio quality) technique for asynchronous sample rate 94conversion is to duplicate or drop samples as needed near a zero-crossing point. More 95sophisticated conversions are possible.</p> 96