1 /*
2 * Copyright (C) 2020 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 "talsa.h"
18 #include <log/log.h>
19
20 namespace android {
21 namespace hardware {
22 namespace audio {
23 namespace V6_0 {
24 namespace implementation {
25 namespace talsa {
26
operator ()(pcm_t * x) const27 void PcmDeleter::operator()(pcm_t *x) const {
28 LOG_ALWAYS_FATAL_IF(pcm_close(x) != 0);
29 };
30
operator ()(struct mixer * x) const31 void MixerDeleter::operator()(struct mixer *x) const {
32 mixer_close(x);
33 }
34
pcmOpen(const unsigned int dev,const unsigned int card,const unsigned int nChannels,const size_t sampleRateHz,const size_t frameCount,const bool isOut)35 std::unique_ptr<pcm_t, PcmDeleter> pcmOpen(const unsigned int dev,
36 const unsigned int card,
37 const unsigned int nChannels,
38 const size_t sampleRateHz,
39 const size_t frameCount,
40 const bool isOut) {
41 struct pcm_config pcm_config;
42 memset(&pcm_config, 0, sizeof(pcm_config));
43
44 pcm_config.channels = nChannels;
45 pcm_config.rate = sampleRateHz;
46 pcm_config.period_size = frameCount; // Approx frames between interrupts
47 pcm_config.period_count = 4; // Approx interrupts per buffer
48 pcm_config.format = PCM_FORMAT_S16_LE;
49 pcm_config.start_threshold = 0;
50 pcm_config.stop_threshold = isOut ? 0 : INT_MAX;
51
52 PcmPtr pcm =
53 PcmPtr(::pcm_open(dev, card,
54 (isOut ? PCM_OUT : PCM_IN) | PCM_MONOTONIC,
55 &pcm_config));
56 if (::pcm_is_ready(pcm.get())) {
57 return pcm;
58 } else {
59 ALOGE("%s:%d pcm_open failed for nChannels=%u sampleRateHz=%zu "
60 "frameCount=%zu isOut=%d with %s", __func__, __LINE__,
61 nChannels, sampleRateHz, frameCount, isOut,
62 pcm_get_error(pcm.get()));
63 return nullptr;
64 }
65 }
66
mixerOpen(unsigned int card)67 MixerPtr mixerOpen(unsigned int card) {
68 return MixerPtr(::mixer_open(card));
69 }
70
mixerSetValueAll(mixer_ctl_t * ctl,int value)71 void mixerSetValueAll(mixer_ctl_t *ctl, int value) {
72 const unsigned int n = mixer_ctl_get_num_values(ctl);
73 for (unsigned int i = 0; i < n; i++) {
74 mixer_ctl_set_value(ctl, i, value);
75 }
76 }
77
mixerSetPercentAll(mixer_ctl_t * ctl,int percent)78 void mixerSetPercentAll(mixer_ctl_t *ctl, int percent) {
79 const unsigned int n = mixer_ctl_get_num_values(ctl);
80 for (unsigned int i = 0; i < n; i++) {
81 mixer_ctl_set_percent(ctl, i, percent);
82 }
83 }
84
85
86 } // namespace talsa
87 } // namespace implementation
88 } // namespace V6_0
89 } // namespace audio
90 } // namespace hardware
91 } // namespace android
92