blob: 77e4f65d2096ffb71faf51ee4149d348e0f45bd9 [file] [log] [blame]
Mikhail Naganovc337a872023-07-07 12:01:17 -07001/*
2 * Copyright (C) 2023 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <map>
18#include <set>
19
20#define LOG_TAG "AHAL_AlsaUtils"
21#include <Utils.h>
22#include <aidl/android/media/audio/common/AudioFormatType.h>
23#include <aidl/android/media/audio/common/PcmType.h>
24#include <android-base/logging.h>
Weilin Xu122597a2024-07-18 11:43:48 -070025#include <audio_utils/primitives.h>
26#include <cutils/compiler.h>
Mikhail Naganovc337a872023-07-07 12:01:17 -070027
28#include "Utils.h"
29#include "core-impl/utils.h"
30
31using aidl::android::hardware::audio::common::getChannelCount;
32using aidl::android::media::audio::common::AudioChannelLayout;
33using aidl::android::media::audio::common::AudioDeviceAddress;
34using aidl::android::media::audio::common::AudioFormatDescription;
35using aidl::android::media::audio::common::AudioFormatType;
36using aidl::android::media::audio::common::AudioIoFlags;
37using aidl::android::media::audio::common::AudioPortExt;
38using aidl::android::media::audio::common::PcmType;
39
40namespace aidl::android::hardware::audio::core::alsa {
41
Weilin Xu550a2442024-09-19 20:33:36 -070042const float kUnityGainFloat = 1.0f;
43
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -070044DeviceProxy::DeviceProxy() : mProfile(nullptr), mProxy(nullptr, alsaProxyDeleter) {}
45
46DeviceProxy::DeviceProxy(const DeviceProfile& deviceProfile)
47 : mProfile(new alsa_device_profile), mProxy(new alsa_device_proxy, alsaProxyDeleter) {
48 profile_init(mProfile.get(), deviceProfile.direction);
49 mProfile->card = deviceProfile.card;
50 mProfile->device = deviceProfile.device;
51 memset(mProxy.get(), 0, sizeof(alsa_device_proxy));
52}
53
54void DeviceProxy::alsaProxyDeleter(alsa_device_proxy* proxy) {
55 if (proxy != nullptr) {
56 proxy_close(proxy);
57 delete proxy;
58 }
59}
60
Mikhail Naganovc337a872023-07-07 12:01:17 -070061namespace {
62
63using AudioChannelCountToMaskMap = std::map<unsigned int, AudioChannelLayout>;
64using AudioFormatDescToPcmFormatMap = std::map<AudioFormatDescription, enum pcm_format>;
65using PcmFormatToAudioFormatDescMap = std::map<enum pcm_format, AudioFormatDescription>;
66
67AudioChannelLayout getInvalidChannelLayout() {
68 static const AudioChannelLayout invalidChannelLayout =
69 AudioChannelLayout::make<AudioChannelLayout::Tag::invalid>(0);
70 return invalidChannelLayout;
71}
72
73static AudioChannelCountToMaskMap make_ChannelCountToMaskMap(
74 const std::set<AudioChannelLayout>& channelMasks) {
75 AudioChannelCountToMaskMap channelMaskToCountMap;
76 for (const auto& channelMask : channelMasks) {
77 channelMaskToCountMap.emplace(getChannelCount(channelMask), channelMask);
78 }
79 return channelMaskToCountMap;
80}
81
82#define DEFINE_CHANNEL_LAYOUT_MASK(n) \
83 AudioChannelLayout::make<AudioChannelLayout::Tag::layoutMask>(AudioChannelLayout::LAYOUT_##n)
84
85const AudioChannelCountToMaskMap& getSupportedChannelOutLayoutMap() {
86 static const std::set<AudioChannelLayout> supportedOutChannelLayouts = {
Mikhail Naganov8c27e462024-07-18 17:03:53 -070087 DEFINE_CHANNEL_LAYOUT_MASK(MONO),
88 DEFINE_CHANNEL_LAYOUT_MASK(STEREO),
Mikhail Naganovc337a872023-07-07 12:01:17 -070089 };
90 static const AudioChannelCountToMaskMap outLayouts =
91 make_ChannelCountToMaskMap(supportedOutChannelLayouts);
92 return outLayouts;
93}
94
95const AudioChannelCountToMaskMap& getSupportedChannelInLayoutMap() {
96 static const std::set<AudioChannelLayout> supportedInChannelLayouts = {
97 DEFINE_CHANNEL_LAYOUT_MASK(MONO),
98 DEFINE_CHANNEL_LAYOUT_MASK(STEREO),
99 };
100 static const AudioChannelCountToMaskMap inLayouts =
101 make_ChannelCountToMaskMap(supportedInChannelLayouts);
102 return inLayouts;
103}
104
105#undef DEFINE_CHANNEL_LAYOUT_MASK
106#define DEFINE_CHANNEL_INDEX_MASK(n) \
107 AudioChannelLayout::make<AudioChannelLayout::Tag::indexMask>(AudioChannelLayout::INDEX_MASK_##n)
108
109const AudioChannelCountToMaskMap& getSupportedChannelIndexLayoutMap() {
110 static const std::set<AudioChannelLayout> supportedIndexChannelLayouts = {
111 DEFINE_CHANNEL_INDEX_MASK(1), DEFINE_CHANNEL_INDEX_MASK(2),
112 DEFINE_CHANNEL_INDEX_MASK(3), DEFINE_CHANNEL_INDEX_MASK(4),
113 DEFINE_CHANNEL_INDEX_MASK(5), DEFINE_CHANNEL_INDEX_MASK(6),
114 DEFINE_CHANNEL_INDEX_MASK(7), DEFINE_CHANNEL_INDEX_MASK(8),
115 DEFINE_CHANNEL_INDEX_MASK(9), DEFINE_CHANNEL_INDEX_MASK(10),
116 DEFINE_CHANNEL_INDEX_MASK(11), DEFINE_CHANNEL_INDEX_MASK(12),
117 DEFINE_CHANNEL_INDEX_MASK(13), DEFINE_CHANNEL_INDEX_MASK(14),
118 DEFINE_CHANNEL_INDEX_MASK(15), DEFINE_CHANNEL_INDEX_MASK(16),
119 DEFINE_CHANNEL_INDEX_MASK(17), DEFINE_CHANNEL_INDEX_MASK(18),
120 DEFINE_CHANNEL_INDEX_MASK(19), DEFINE_CHANNEL_INDEX_MASK(20),
121 DEFINE_CHANNEL_INDEX_MASK(21), DEFINE_CHANNEL_INDEX_MASK(22),
122 DEFINE_CHANNEL_INDEX_MASK(23), DEFINE_CHANNEL_INDEX_MASK(24),
123 };
124 static const AudioChannelCountToMaskMap indexLayouts =
125 make_ChannelCountToMaskMap(supportedIndexChannelLayouts);
126 return indexLayouts;
127}
128
129#undef DEFINE_CHANNEL_INDEX_MASK
130
131AudioFormatDescription make_AudioFormatDescription(AudioFormatType type) {
132 AudioFormatDescription result;
133 result.type = type;
134 return result;
135}
136
137AudioFormatDescription make_AudioFormatDescription(PcmType pcm) {
138 auto result = make_AudioFormatDescription(AudioFormatType::PCM);
139 result.pcm = pcm;
140 return result;
141}
142
143const AudioFormatDescToPcmFormatMap& getAudioFormatDescriptorToPcmFormatMap() {
144 static const AudioFormatDescToPcmFormatMap formatDescToPcmFormatMap = {
Mikhail Naganovc337a872023-07-07 12:01:17 -0700145 {make_AudioFormatDescription(PcmType::INT_16_BIT), PCM_FORMAT_S16_LE},
146 {make_AudioFormatDescription(PcmType::FIXED_Q_8_24), PCM_FORMAT_S24_LE},
147 {make_AudioFormatDescription(PcmType::INT_24_BIT), PCM_FORMAT_S24_3LE},
148 {make_AudioFormatDescription(PcmType::INT_32_BIT), PCM_FORMAT_S32_LE},
149 {make_AudioFormatDescription(PcmType::FLOAT_32_BIT), PCM_FORMAT_FLOAT_LE},
150 };
151 return formatDescToPcmFormatMap;
152}
153
154static PcmFormatToAudioFormatDescMap make_PcmFormatToAudioFormatDescMap(
155 const AudioFormatDescToPcmFormatMap& formatDescToPcmFormatMap) {
156 PcmFormatToAudioFormatDescMap result;
157 for (const auto& formatPair : formatDescToPcmFormatMap) {
158 result.emplace(formatPair.second, formatPair.first);
159 }
160 return result;
161}
162
163const PcmFormatToAudioFormatDescMap& getPcmFormatToAudioFormatDescMap() {
164 static const PcmFormatToAudioFormatDescMap pcmFormatToFormatDescMap =
165 make_PcmFormatToAudioFormatDescMap(getAudioFormatDescriptorToPcmFormatMap());
166 return pcmFormatToFormatDescMap;
167}
168
Weilin Xu550a2442024-09-19 20:33:36 -0700169void applyGainToInt16Buffer(void* buffer, const size_t bufferSizeBytes, const float gain,
170 int channelCount) {
171 const uint16_t unityGainQ4_12 = u4_12_from_float(kUnityGainFloat);
172 const uint16_t vl = u4_12_from_float(gain);
173 const uint32_t vrl = (vl << 16) | vl;
174 int numFrames = 0;
175 if (channelCount == 2) {
176 numFrames = bufferSizeBytes / sizeof(uint32_t);
177 if (numFrames == 0) {
178 return;
179 }
180 uint32_t* intBuffer = (uint32_t*)buffer;
181 if (CC_UNLIKELY(vl > unityGainQ4_12)) {
182 do {
183 int32_t l = mulRL(1, *intBuffer, vrl) >> 12;
184 int32_t r = mulRL(0, *intBuffer, vrl) >> 12;
185 l = clamp16(l);
186 r = clamp16(r);
187 *intBuffer++ = (r << 16) | (l & 0xFFFF);
188 } while (--numFrames);
189 } else {
190 do {
191 int32_t l = mulRL(1, *intBuffer, vrl) >> 12;
192 int32_t r = mulRL(0, *intBuffer, vrl) >> 12;
193 *intBuffer++ = (r << 16) | (l & 0xFFFF);
194 } while (--numFrames);
195 }
196 } else {
197 numFrames = bufferSizeBytes / sizeof(uint16_t);
198 if (numFrames == 0) {
199 return;
200 }
201 int16_t* intBuffer = (int16_t*)buffer;
202 if (CC_UNLIKELY(vl > unityGainQ4_12)) {
203 do {
204 int32_t mono = mul(*intBuffer, static_cast<int16_t>(vl)) >> 12;
205 *intBuffer++ = clamp16(mono);
206 } while (--numFrames);
207 } else {
208 do {
209 int32_t mono = mul(*intBuffer, static_cast<int16_t>(vl)) >> 12;
210 *intBuffer++ = static_cast<int16_t>(mono & 0xFFFF);
211 } while (--numFrames);
212 }
213 }
214}
215
216void applyGainToInt32Buffer(int32_t* typedBuffer, const size_t bufferSizeBytes, const float gain) {
217 int numSamples = bufferSizeBytes / sizeof(int32_t);
218 if (numSamples == 0) {
219 return;
220 }
221 if (CC_UNLIKELY(gain > kUnityGainFloat)) {
222 do {
223 float multiplied = (*typedBuffer) * gain;
224 if (multiplied > INT32_MAX) {
225 *typedBuffer++ = INT32_MAX;
226 } else if (multiplied < INT32_MIN) {
227 *typedBuffer++ = INT32_MIN;
228 } else {
229 *typedBuffer++ = multiplied;
230 }
231 } while (--numSamples);
232 } else {
233 do {
234 *typedBuffer++ = (*typedBuffer) * gain;
235 } while (--numSamples);
236 }
237}
238
239void applyGainToFloatBuffer(float* floatBuffer, const size_t bufferSizeBytes, const float gain) {
240 int numSamples = bufferSizeBytes / sizeof(float);
241 if (numSamples == 0) {
242 return;
243 }
244 if (CC_UNLIKELY(gain > kUnityGainFloat)) {
245 do {
246 *floatBuffer++ = std::clamp((*floatBuffer) * gain, -kUnityGainFloat, kUnityGainFloat);
247 } while (--numSamples);
248 } else {
249 do {
250 *floatBuffer++ = (*floatBuffer) * gain;
251 } while (--numSamples);
252 }
253}
254
Mikhail Naganovc337a872023-07-07 12:01:17 -0700255} // namespace
256
257std::ostream& operator<<(std::ostream& os, const DeviceProfile& device) {
258 return os << "<" << device.card << "," << device.device << ">";
259}
260
261AudioChannelLayout getChannelLayoutMaskFromChannelCount(unsigned int channelCount, int isInput) {
262 return findValueOrDefault(
263 isInput ? getSupportedChannelInLayoutMap() : getSupportedChannelOutLayoutMap(),
264 channelCount, getInvalidChannelLayout());
265}
266
267AudioChannelLayout getChannelIndexMaskFromChannelCount(unsigned int channelCount) {
268 return findValueOrDefault(getSupportedChannelIndexLayoutMap(), channelCount,
269 getInvalidChannelLayout());
270}
271
272unsigned int getChannelCountFromChannelMask(const AudioChannelLayout& channelMask, bool isInput) {
273 switch (channelMask.getTag()) {
274 case AudioChannelLayout::Tag::layoutMask: {
275 return findKeyOrDefault(
276 isInput ? getSupportedChannelInLayoutMap() : getSupportedChannelOutLayoutMap(),
277 static_cast<unsigned>(getChannelCount(channelMask)), 0u /*defaultValue*/);
278 }
279 case AudioChannelLayout::Tag::indexMask: {
280 return findKeyOrDefault(getSupportedChannelIndexLayoutMap(),
281 static_cast<unsigned>(getChannelCount(channelMask)),
282 0u /*defaultValue*/);
283 }
284 case AudioChannelLayout::Tag::none:
285 case AudioChannelLayout::Tag::invalid:
286 case AudioChannelLayout::Tag::voiceMask:
287 default:
288 return 0;
289 }
290}
291
292std::vector<AudioChannelLayout> getChannelMasksFromProfile(const alsa_device_profile* profile) {
293 const bool isInput = profile->direction == PCM_IN;
294 std::vector<AudioChannelLayout> channels;
295 for (size_t i = 0; i < AUDIO_PORT_MAX_CHANNEL_MASKS && profile->channel_counts[i] != 0; ++i) {
296 auto layoutMask =
297 alsa::getChannelLayoutMaskFromChannelCount(profile->channel_counts[i], isInput);
298 if (layoutMask.getTag() == AudioChannelLayout::Tag::layoutMask) {
299 channels.push_back(layoutMask);
300 }
301 auto indexMask = alsa::getChannelIndexMaskFromChannelCount(profile->channel_counts[i]);
302 if (indexMask.getTag() == AudioChannelLayout::Tag::indexMask) {
303 channels.push_back(indexMask);
304 }
305 }
306 return channels;
307}
308
309std::optional<DeviceProfile> getDeviceProfile(
310 const ::aidl::android::media::audio::common::AudioDevice& audioDevice, bool isInput) {
311 if (audioDevice.address.getTag() != AudioDeviceAddress::Tag::alsa) {
312 LOG(ERROR) << __func__ << ": not alsa address: " << audioDevice.toString();
313 return std::nullopt;
314 }
315 auto& alsaAddress = audioDevice.address.get<AudioDeviceAddress::Tag::alsa>();
316 if (alsaAddress.size() != 2 || alsaAddress[0] < 0 || alsaAddress[1] < 0) {
317 LOG(ERROR) << __func__
318 << ": malformed alsa address: " << ::android::internal::ToString(alsaAddress);
319 return std::nullopt;
320 }
321 return DeviceProfile{.card = alsaAddress[0],
322 .device = alsaAddress[1],
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700323 .direction = isInput ? PCM_IN : PCM_OUT,
324 .isExternal = !audioDevice.type.connection.empty()};
Mikhail Naganovc337a872023-07-07 12:01:17 -0700325}
326
327std::optional<DeviceProfile> getDeviceProfile(
328 const ::aidl::android::media::audio::common::AudioPort& audioPort) {
329 if (audioPort.ext.getTag() != AudioPortExt::Tag::device) {
330 LOG(ERROR) << __func__ << ": port id " << audioPort.id << " is not a device port";
331 return std::nullopt;
332 }
333 auto& devicePort = audioPort.ext.get<AudioPortExt::Tag::device>();
334 return getDeviceProfile(devicePort.device, audioPort.flags.getTag() == AudioIoFlags::input);
335}
336
337std::optional<struct pcm_config> getPcmConfig(const StreamContext& context, bool isInput) {
338 struct pcm_config config;
339 config.channels = alsa::getChannelCountFromChannelMask(context.getChannelLayout(), isInput);
340 if (config.channels == 0) {
341 LOG(ERROR) << __func__ << ": invalid channel=" << context.getChannelLayout().toString();
342 return std::nullopt;
343 }
344 config.format = alsa::aidl2c_AudioFormatDescription_pcm_format(context.getFormat());
345 if (config.format == PCM_FORMAT_INVALID) {
346 LOG(ERROR) << __func__ << ": invalid format=" << context.getFormat().toString();
347 return std::nullopt;
348 }
349 config.rate = context.getSampleRate();
350 if (config.rate == 0) {
351 LOG(ERROR) << __func__ << ": invalid sample rate=" << config.rate;
352 return std::nullopt;
353 }
354 return config;
355}
356
357std::vector<int> getSampleRatesFromProfile(const alsa_device_profile* profile) {
358 std::vector<int> sampleRates;
359 for (int i = 0; i < std::min(MAX_PROFILE_SAMPLE_RATES, AUDIO_PORT_MAX_SAMPLING_RATES) &&
360 profile->sample_rates[i] != 0;
361 i++) {
362 sampleRates.push_back(profile->sample_rates[i]);
363 }
364 return sampleRates;
365}
366
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700367DeviceProxy openProxyForAttachedDevice(const DeviceProfile& deviceProfile,
368 struct pcm_config* pcmConfig, size_t bufferFrameCount) {
369 if (deviceProfile.isExternal) {
370 LOG(FATAL) << __func__ << ": called for an external device, address=" << deviceProfile;
371 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700372 DeviceProxy proxy(deviceProfile);
373 if (!profile_fill_builtin_device_info(proxy.getProfile(), pcmConfig, bufferFrameCount)) {
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700374 LOG(FATAL) << __func__ << ": failed to init for built-in device, address=" << deviceProfile;
375 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700376 if (int err = proxy_prepare_from_default_config(proxy.get(), proxy.getProfile()); err != 0) {
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700377 LOG(FATAL) << __func__ << ": fail to prepare for device address=" << deviceProfile
378 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700379 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700380 }
381 if (int err = proxy_open(proxy.get()); err != 0) {
382 LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile
383 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700384 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700385 }
386 return proxy;
387}
388
389DeviceProxy openProxyForExternalDevice(const DeviceProfile& deviceProfile,
390 struct pcm_config* pcmConfig, bool requireExactMatch) {
391 if (!deviceProfile.isExternal) {
392 LOG(FATAL) << __func__ << ": called for an attached device, address=" << deviceProfile;
393 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700394 auto proxy = readAlsaDeviceInfo(deviceProfile);
395 if (proxy.get() == nullptr) {
396 return proxy;
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700397 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700398 if (int err = proxy_prepare(proxy.get(), proxy.getProfile(), pcmConfig, requireExactMatch);
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700399 err != 0) {
400 LOG(ERROR) << __func__ << ": fail to prepare for device address=" << deviceProfile
401 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700402 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700403 }
404 if (int err = proxy_open(proxy.get()); err != 0) {
405 LOG(ERROR) << __func__ << ": failed to open device, address=" << deviceProfile
406 << " error=" << err;
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700407 return DeviceProxy();
Mikhail Naganov422f7e62023-07-13 16:32:08 -0700408 }
409 return proxy;
410}
411
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700412DeviceProxy readAlsaDeviceInfo(const DeviceProfile& deviceProfile) {
413 DeviceProxy proxy(deviceProfile);
414 if (!profile_read_device_info(proxy.getProfile())) {
415 LOG(ERROR) << __func__ << ": unable to read device info, device address=" << deviceProfile;
416 return DeviceProxy();
Mikhail Naganovc337a872023-07-07 12:01:17 -0700417 }
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700418 return proxy;
Mikhail Naganovc337a872023-07-07 12:01:17 -0700419}
420
Mikhail Naganovcf824f62023-07-24 14:51:36 -0700421void resetTransferredFrames(DeviceProxy& proxy, uint64_t frames) {
Mikhail Naganov5d2bfba2023-09-21 17:40:56 -0700422 if (proxy.get() != nullptr) {
423 proxy.get()->transferred = frames;
Mikhail Naganovcf824f62023-07-24 14:51:36 -0700424 }
425}
426
Mikhail Naganovc337a872023-07-07 12:01:17 -0700427AudioFormatDescription c2aidl_pcm_format_AudioFormatDescription(enum pcm_format legacy) {
428 return findValueOrDefault(getPcmFormatToAudioFormatDescMap(), legacy, AudioFormatDescription());
429}
430
431pcm_format aidl2c_AudioFormatDescription_pcm_format(const AudioFormatDescription& aidl) {
432 return findValueOrDefault(getAudioFormatDescriptorToPcmFormatMap(), aidl, PCM_FORMAT_INVALID);
433}
434
Weilin Xu550a2442024-09-19 20:33:36 -0700435void applyGain(void* buffer, float gain, size_t bufferSizeBytes, enum pcm_format pcmFormat,
Weilin Xu122597a2024-07-18 11:43:48 -0700436 int channelCount) {
437 if (channelCount != 1 && channelCount != 2) {
438 LOG(WARNING) << __func__ << ": unsupported channel count " << channelCount;
439 return;
440 }
441 if (!getPcmFormatToAudioFormatDescMap().contains(pcmFormat)) {
442 LOG(WARNING) << __func__ << ": unsupported pcm format " << pcmFormat;
443 return;
444 }
Weilin Xu550a2442024-09-19 20:33:36 -0700445 if (std::abs(gain - kUnityGainFloat) < 1e-6) {
Weilin Xu122597a2024-07-18 11:43:48 -0700446 return;
447 }
Weilin Xu122597a2024-07-18 11:43:48 -0700448 switch (pcmFormat) {
Weilin Xu550a2442024-09-19 20:33:36 -0700449 case PCM_FORMAT_S16_LE:
450 applyGainToInt16Buffer(buffer, bufferSizeBytes, gain, channelCount);
451 break;
452 case PCM_FORMAT_FLOAT_LE: {
453 float* floatBuffer = (float*)buffer;
454 applyGainToFloatBuffer(floatBuffer, bufferSizeBytes, gain);
455 } break;
456 case PCM_FORMAT_S24_LE:
457 // PCM_FORMAT_S24_LE buffer is composed of signed fixed-point 32-bit Q8.23 data with
458 // min and max limits of the same bit representation as min and max limits of
459 // PCM_FORMAT_S32_LE buffer.
460 case PCM_FORMAT_S32_LE: {
461 int32_t* typedBuffer = (int32_t*)buffer;
462 applyGainToInt32Buffer(typedBuffer, bufferSizeBytes, gain);
463 } break;
464 case PCM_FORMAT_S24_3LE: {
465 int numSamples = bufferSizeBytes / (sizeof(uint8_t) * 3);
466 if (numSamples == 0) {
467 return;
Weilin Xu122597a2024-07-18 11:43:48 -0700468 }
Weilin Xu550a2442024-09-19 20:33:36 -0700469 std::unique_ptr<int32_t[]> typedBuffer(new int32_t[numSamples]);
470 memcpy_to_i32_from_p24(typedBuffer.get(), (uint8_t*)buffer, numSamples);
471 applyGainToInt32Buffer(typedBuffer.get(), numSamples * sizeof(int32_t), gain);
472 memcpy_to_p24_from_i32((uint8_t*)buffer, typedBuffer.get(), numSamples);
Weilin Xu122597a2024-07-18 11:43:48 -0700473 } break;
474 default:
Weilin Xu550a2442024-09-19 20:33:36 -0700475 LOG(FATAL) << __func__ << ": unsupported pcm format " << pcmFormat;
Weilin Xu122597a2024-07-18 11:43:48 -0700476 break;
477 }
478}
479
Mikhail Naganovc337a872023-07-07 12:01:17 -0700480} // namespace aidl::android::hardware::audio::core::alsa