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