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 #pragma once
18 #include <memory>
19 #include <tinyalsa/asoundlib.h>
20 
21 namespace android {
22 namespace hardware {
23 namespace audio {
24 namespace CPP_VERSION {
25 namespace implementation {
26 namespace talsa {
27 
28 constexpr unsigned int kPcmDevice = 0;
29 constexpr unsigned int kPcmCard = 0;
30 
31 struct PcmPeriodSettings {
32     unsigned periodCount;
33     unsigned periodSizeMultiplier;
34 };
35 
36 void init();
37 PcmPeriodSettings pcmGetPcmPeriodSettings();
38 unsigned pcmGetHostLatencyMs();
39 
40 typedef struct pcm pcm_t;
41 struct PcmDeleter { void operator()(pcm_t *x) const; };
42 typedef std::unique_ptr<pcm_t, PcmDeleter> PcmPtr;
43 PcmPtr pcmOpen(unsigned int dev, unsigned int card, unsigned int nChannels,
44                size_t sampleRateHz, size_t frameCount, bool isOut);
45 int pcmRead(pcm_t *pcm, void *data, int szBytes, unsigned int frameSize);
46 int pcmWrite(pcm_t *pcm, const void *data, int szBytes, unsigned int frameSize);
47 
48 class Mixer {
49 public:
50     Mixer(unsigned card);
51     ~Mixer();
52 
53     operator bool() const { return mMixer != nullptr; }
54 
55     Mixer(const Mixer &) = delete;
56     Mixer &operator=(const Mixer &) = delete;
57     Mixer(Mixer &&) = delete;
58     Mixer &operator=(Mixer &&) = delete;
59 
60 private:
61     struct mixer *mMixer;
62 };
63 
64 }  // namespace talsa
65 }  // namespace implementation
66 }  // namespace CPP_VERSION
67 }  // namespace audio
68 }  // namespace hardware
69 }  // namespace android
70