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