Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 1 | page.title=Audio |
| 2 | @jd:body |
| 3 | |
| 4 | <!-- |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 5 | Copyright 2014 The Android Open Source Project |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 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 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 27 | <p>This page explains how to implement the audio Hardware Abstraction Layer (HAL) and configure the |
| 28 | shared library.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 29 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 30 | <h2 id="implementing">Implementing the HAL</h2> |
| 31 | |
| 32 | <p>The audio HAL is composed of three different interfaces that you must implement:</p> |
| 33 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 34 | <ul> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 35 | <li><code>hardware/libhardware/include/hardware/audio.h</code> - represents the main functions |
| 36 | of an audio device.</li> |
| 37 | <li><code>hardware/libhardware/include/hardware/audio_policy.h</code> - represents the audio policy |
| 38 | manager, which handles things like audio routing and volume control policies.</li> |
| 39 | <li><code>hardware/libhardware/include/hardware/audio_effect.h</code> - represents effects that can |
| 40 | be applied to audio such as downmixing, echo, or noise suppression.</li> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 41 | </ul> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 42 | |
| 43 | <p>For an example, refer to the implementation for the Galaxy Nexus at |
| 44 | <code>device/samsung/tuna/audio</code>.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 45 | |
| 46 | <p>In addition to implementing the HAL, you need to create a |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 47 | <code>device/<company_name>/<device_name>/audio/audio_policy.conf</code> file that |
| 48 | declares the audio devices present on your product. For an example, see the file for the Galaxy |
| 49 | Nexus audio hardware in <code>device/samsung/tuna/audio/audio_policy.conf</code>. Also, see the |
| 50 | <code>system/core/include/system/audio.h</code> and |
| 51 | <code>system/core/include/system/audio_policy.h</code> header files for a reference of the |
| 52 | properties that you can define.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 53 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 54 | <h3 id="multichannel">Multi-channel support</h3> |
| 55 | |
| 56 | <p>If your hardware and driver supports multichannel audio via HDMI, you can output the audio |
| 57 | stream directly to the audio hardware. This bypasses the AudioFlinger mixer so it doesn't get |
| 58 | downmixed to two channels.</p> |
| 59 | |
| 60 | <p>The audio HAL must expose whether an output stream profile supports multichannel audio |
| 61 | capabilities. If the HAL exposes its capabilities, the default policy manager allows multichannel |
| 62 | playback over HDMI.</p> |
| 63 | |
| 64 | <p>For more implementation details, see the <code>device/samsung/tuna/audio/audio_hw.c</code> in |
| 65 | the Android 4.1 release.</p> |
| 66 | |
| 67 | <p>To specify that your product contains a multichannel audio output, edit the |
| 68 | <code>audio_policy.conf</code> file to describe the multichannel output for your product. The |
| 69 | following is an example from the Galaxy Nexus that shows a "dynamic" channel mask, which means the |
| 70 | audio policy manager queries the actual channel masks supported by the HDMI sink after connection. |
| 71 | You can also specify a static channel mask like <code>AUDIO_CHANNEL_OUT_5POINT1</code>.</p> |
| 72 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 73 | <pre> |
| 74 | audio_hw_modules { |
| 75 | primary { |
| 76 | outputs { |
| 77 | ... |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 78 | hdmi { |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 79 | sampling_rates 44100|48000 |
| 80 | channel_masks dynamic |
| 81 | formats AUDIO_FORMAT_PCM_16_BIT |
| 82 | devices AUDIO_DEVICE_OUT_AUX_DIGITAL |
| 83 | flags AUDIO_OUTPUT_FLAG_DIRECT |
| 84 | } |
| 85 | ... |
| 86 | } |
| 87 | ... |
| 88 | } |
| 89 | ... |
| 90 | } |
| 91 | </pre> |
| 92 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 93 | <p>AudioFlinger's mixer downmixes the content to stereo automatically when sent to an audio device |
| 94 | that does not support multichannel audio.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 95 | |
| 96 | <h3 id="codecs">Media codecs</h3> |
| 97 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 98 | <p>Ensure the audio codecs your hardware and drivers support are properly declared for your |
| 99 | product. For details on declaring supported codecs, see <a href="media.html#expose"> Exposing Codecs |
| 100 | to the Framework</a>.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 101 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 102 | <h2 id="configuring">Configuring the shared library</h2> |
| 103 | |
| 104 | <p>You need to package the HAL implementation into a shared library and copy it to the appropriate |
| 105 | location by creating an <code>Android.mk</code> file:</p> |
| 106 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 107 | <ol> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 108 | <li>Create a <code>device/<company_name>/<device_name>/audio</code> directory to |
| 109 | contain your library's source files.</li> |
| 110 | <li>Create an <code>Android.mk</code> file to build the shared library. Ensure that the Makefile |
| 111 | contains the following line: |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 112 | <pre> |
| 113 | LOCAL_MODULE := audio.primary.<device_name> |
| 114 | </pre> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 115 | |
| 116 | <p>Notice your library must be named <code>audio_primary.<device_name>.so</code> so |
| 117 | that Android can correctly load the library. The "<code>primary</code>" portion of this filename |
| 118 | indicates that this shared library is for the primary audio hardware located on the device. The |
| 119 | module names <code>audio.a2dp.<device_name></code> and |
| 120 | <code>audio.usb.<device_name></code> are also available for bluetooth and USB audio |
| 121 | interfaces. Here is an example of an <code>Android.mk</code> from the Galaxy Nexus audio hardware: |
| 122 | </p> |
| 123 | |
| 124 | <pre> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 125 | LOCAL_PATH := $(call my-dir) |
| 126 | |
| 127 | include $(CLEAR_VARS) |
| 128 | |
| 129 | LOCAL_MODULE := audio.primary.tuna |
Clay Murphy | ccbdc67 | 2014-03-11 20:41:29 +0000 | [diff] [blame] | 130 | LOCAL_MODULE_RELATIVE_PATH := hw |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 131 | LOCAL_SRC_FILES := audio_hw.c ril_interface.c |
| 132 | LOCAL_C_INCLUDES += \ |
| 133 | external/tinyalsa/include \ |
| 134 | $(call include-path-for, audio-utils) \ |
| 135 | $(call include-path-for, audio-effects) |
| 136 | LOCAL_SHARED_LIBRARIES := liblog libcutils libtinyalsa libaudioutils libdl |
| 137 | LOCAL_MODULE_TAGS := optional |
| 138 | |
| 139 | include $(BUILD_SHARED_LIBRARY) |
| 140 | </pre> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 141 | |
| 142 | </li> |
| 143 | |
| 144 | <li>If your product supports low latency audio as specified by the Android CDD, copy the |
| 145 | corresponding XML feature file into your product. For example, in your product's |
| 146 | <code>device/<company_name>/<device_name>/device.mk</code> Makefile: |
| 147 | |
| 148 | <pre> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 149 | PRODUCT_COPY_FILES := ... |
| 150 | |
| 151 | PRODUCT_COPY_FILES += \ |
| 152 | frameworks/native/data/etc/android.android.hardware.audio.low_latency.xml:system/etc/permissions/android.hardware.audio.low_latency.xml \ |
| 153 | </pre> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 154 | |
| 155 | </li> |
| 156 | |
| 157 | <li>Copy the <code>audio_policy.conf</code> file that you created earlier to the |
| 158 | <code>system/etc/</code> directory in your product's |
| 159 | <code>device/<company_name>/<device_name>/device.mk</code> Makefile. For example: |
| 160 | |
| 161 | <pre> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 162 | PRODUCT_COPY_FILES += \ |
| 163 | device/samsung/tuna/audio/audio_policy.conf:system/etc/audio_policy.conf |
| 164 | </pre> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 165 | |
| 166 | </li> |
| 167 | |
| 168 | <li>Declare the shared modules of your audio HAL that are required by your product in the |
| 169 | product's <code>device/<company_name>/<device_name>/device.mk</code> Makefile. For |
| 170 | example, the Galaxy Nexus requires the primary and bluetooth audio HAL modules: |
| 171 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 172 | <pre> |
| 173 | PRODUCT_PACKAGES += \ |
| 174 | audio.primary.tuna \ |
| 175 | audio.a2dp.default |
| 176 | </pre> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 177 | |
| 178 | </li> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 179 | </ol> |
| 180 | |
| 181 | <h2 id="preprocessing">Audio pre-processing effects</h2> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 182 | |
| 183 | <p>The Android platform provides audio effects on supported devices in the |
| 184 | <a href="http://developer.android.com/reference/android/media/audiofx/package-summary.html">audiofx |
| 185 | </a> package, which is available for developers to access. For example, on the Nexus 10, the |
| 186 | following pre-processing effects are supported:</p> |
| 187 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 188 | <ul> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 189 | <li> |
| 190 | <a href="http://developer.android.com/reference/android/media/audiofx/AcousticEchoCanceler.html"> |
| 191 | Acoustic Echo Cancellation</a></li> |
| 192 | <li> |
| 193 | <a href="http://developer.android.com/reference/android/media/audiofx/AutomaticGainControl.html"> |
| 194 | Automatic Gain Control</a></li> |
| 195 | <li> |
| 196 | <a href="http://developer.android.com/reference/android/media/audiofx/NoiseSuppressor.html"> |
| 197 | Noise Suppression</a></li> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 198 | </ul> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 199 | |
| 200 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 201 | <p>Pre-processing effects are paired with the use case mode in which the pre-processing is requested |
| 202 | . In Android app development, a use case is referred to as an <code>AudioSource</code>; and app |
| 203 | developers request to use the <code>AudioSource</code> abstraction instead of the actual audio |
| 204 | hardware device. The Android Audio Policy Manager maps an <code>AudioSource</code> to the actual |
| 205 | hardware with <code>AudioPolicyManagerBase::getDeviceForInputSource(int inputSource)</code>. The |
| 206 | following sources are exposed to developers:</p> |
| 207 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 208 | <ul> |
Glenn Kasten | 795a9de | 2014-01-24 08:58:56 -0800 | [diff] [blame] | 209 | <li><code>android.media.MediaRecorder.AudioSource.CAMCORDER</code></li> |
| 210 | <li><code>android.media.MediaRecorder.AudioSource.VOICE_COMMUNICATION</code></li> |
| 211 | <li><code>android.media.MediaRecorder.AudioSource.VOICE_CALL</code></li> |
| 212 | <li><code>android.media.MediaRecorder.AudioSource.VOICE_DOWNLINK</code></li> |
| 213 | <li><code>android.media.MediaRecorder.AudioSource.VOICE_UPLINK</code></li> |
| 214 | <li><code>android.media.MediaRecorder.AudioSource.VOICE_RECOGNITION</code></li> |
| 215 | <li><code>android.media.MediaRecorder.AudioSource.MIC</code></li> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 216 | <li><code>android.media.MediaRecorder.AudioSource.DEFAULT</code></li> </ul> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 217 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 218 | <p>The default pre-processing effects applied for each <code>AudioSource</code> are specified in |
| 219 | the <code>/system/etc/audio_effects.conf</code> file. To specify your own default effects for every |
| 220 | <code>AudioSource</code>, create a <code>/system/vendor/etc/audio_effects.conf</code> file and |
| 221 | specify the pre-processing effects to turn on. For an example, see the implementation for the Nexus |
| 222 | 10 in <code>device/samsung/manta/audio_effects.conf</code>. AudioEffect instances acquire and |
| 223 | release a session when created and destroyed, enabling the effects (such as the Loudness Enhancer) |
| 224 | to persist throughout the duration of the session. </p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 225 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 226 | <p class="warning"><strong>Warning:</strong> For the <code>VOICE_RECOGNITION</code> use case, do |
| 227 | not enable the noise suppression pre-processing effect. It should not be turned on by default when |
| 228 | recording from this audio source, and you should not enable it in your own audio_effects.conf file. |
| 229 | Turning on the effect by default will cause the device to fail the <a |
| 230 | href="/compatibility/index.html"> compatibility requirement</a> regardless of whether this was on by |
| 231 | default due to configuration file , or the audio HAL implementation's default behavior.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 232 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 233 | <p>The following example enables pre-processing for the VoIP <code>AudioSource</code> and Camcorder |
| 234 | <code>AudioSource</code>. By declaring the <code>AudioSource</code> configuration in this manner, |
| 235 | the framework will automatically request from the audio HAL the use of those effects.</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 236 | |
| 237 | <pre> |
| 238 | pre_processing { |
| 239 | voice_communication { |
| 240 | aec {} |
| 241 | ns {} |
| 242 | } |
| 243 | camcorder { |
| 244 | agc {} |
| 245 | } |
| 246 | } |
| 247 | </pre> |
| 248 | |
| 249 | <h3 id="tuning">Source tuning</h3> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 250 | |
| 251 | <p>For <code>AudioSource</code> tuning, there are no explicit requirements on audio gain or audio |
| 252 | processing with the exception of voice recognition (<code>VOICE_RECOGNITION</code>).</p> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 253 | |
| 254 | <p>The following are the requirements for voice recognition:</p> |
| 255 | |
| 256 | <ul> |
| 257 | <li>"flat" frequency response (+/- 3dB) from 100Hz to 4kHz</li> |
| 258 | <li>close-talk config: 90dB SPL reads RMS of 2500 (16bit samples)</li> |
| 259 | <li>level tracks linearly from -18dB to +12dB relative to 90dB SPL</li> |
| 260 | <li>THD < 1% (90dB SPL in 100 to 4000Hz range)</li> |
| 261 | <li>8kHz sampling rate (anti-aliasing)</li> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 262 | <li>Effects/pre-processing must be disabled by default</li> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 263 | </ul> |
| 264 | |
| 265 | <p>Examples of tuning different effects for different sources are:</p> |
| 266 | |
| 267 | <ul> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 268 | <li>Noise Suppressor |
| 269 | <ul> |
| 270 | <li>Tuned for wind noise suppressor for <code>CAMCORDER</code></li> |
| 271 | <li>Tuned for stationary noise suppressor for <code>VOICE_COMMUNICATION</code></li> |
| 272 | </ul> |
| 273 | </li> |
| 274 | <li>Automatic Gain Control |
| 275 | <ul> |
| 276 | <li>Tuned for close-talk for <code>VOICE_COMMUNICATION</code> and main phone mic</li> |
| 277 | <li>Tuned for far-talk for <code>CAMCORDER</code></li> |
| 278 | </ul> |
| 279 | </li> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 280 | </ul> |
| 281 | |
| 282 | <h3 id="more">More information</h3> |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 283 | |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 284 | <p>For more information, see:</p> |
| 285 | |
| 286 | <ul> |
| 287 | <li>Android documentation for |
| 288 | <a href="http://developer.android.com/reference/android/media/audiofx/package-summary.html"> |
| 289 | audiofx package</a> |
| 290 | |
| 291 | <li>Android documentation for |
| 292 | <a href="http://developer.android.com/reference/android/media/audiofx/NoiseSuppressor.html"> |
| 293 | Noise Suppression audio effect</a></li> |
| 294 | |
Clay Murphy | 47b1d3f | 2013-10-03 10:02:22 -0700 | [diff] [blame] | 295 | <li><code>device/samsung/manta/audio_effects.conf</code> file for the Nexus 10</li> |
Heidi Miller | 54362a6 | 2014-10-24 16:00:04 -0700 | [diff] [blame^] | 296 | </ul> |