1 /*
2  * Copyright (C) 2014 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 /* This file contains shared utility functions to handle the tinyalsa
18  * implementation for Android internal audio, generally in the hardware layer.
19  * Some routines may log a fatal error on failure, as noted.
20  */
21 
22 #ifndef ANDROID_AUDIO_ALSAOPS_H
23 #define ANDROID_AUDIO_ALSAOPS_H
24 
25 #include <log/log.h>
26 
27 #include <system/audio.h>
28 #include <tinyalsa/asoundlib.h>
29 
30 __BEGIN_DECLS
31 
32 /* Converts audio_format to pcm_format.
33  * Parameters:
34  *  format  the audio_format_t to convert
35  *
36  * Logs a fatal error if format is not a valid convertible audio_format_t.
37  */
pcm_format_from_audio_format(audio_format_t format)38 static inline enum pcm_format pcm_format_from_audio_format(audio_format_t format)
39 {
40     switch (format) {
41 #if HAVE_BIG_ENDIAN
42     case AUDIO_FORMAT_PCM_16_BIT:
43         return PCM_FORMAT_S16_BE;
44     case AUDIO_FORMAT_PCM_24_BIT_PACKED:
45         return PCM_FORMAT_S24_3BE;
46     case AUDIO_FORMAT_PCM_32_BIT:
47         return PCM_FORMAT_S32_BE;
48     case AUDIO_FORMAT_PCM_8_24_BIT:
49         return PCM_FORMAT_S24_BE;
50 #else
51     case AUDIO_FORMAT_PCM_16_BIT:
52         return PCM_FORMAT_S16_LE;
53     case AUDIO_FORMAT_PCM_24_BIT_PACKED:
54         return PCM_FORMAT_S24_3LE;
55     case AUDIO_FORMAT_PCM_32_BIT:
56         return PCM_FORMAT_S32_LE;
57     case AUDIO_FORMAT_PCM_8_24_BIT:
58         return PCM_FORMAT_S24_LE;
59 #endif
60     case AUDIO_FORMAT_PCM_FLOAT:  /* there is no equivalent for float */
61     default:
62         LOG_ALWAYS_FATAL("pcm_format_from_audio_format: invalid audio format %#x", format);
63         return PCM_FORMAT_INVALID;  /* doesn't get here, assert called above */
64     }
65 }
66 
67 /* Converts pcm_format to audio_format.
68  * Parameters:
69  *  format  the pcm_format to convert
70  *
71  * Logs a fatal error if format is not a valid convertible pcm_format.
72  */
audio_format_from_pcm_format(enum pcm_format format)73 static inline audio_format_t audio_format_from_pcm_format(enum pcm_format format)
74 {
75     switch (format) {
76 #if HAVE_BIG_ENDIAN
77     case PCM_FORMAT_S16_BE:
78         return AUDIO_FORMAT_PCM_16_BIT;
79     case PCM_FORMAT_S24_3BE:
80         return AUDIO_FORMAT_PCM_24_BIT_PACKED;
81     case PCM_FORMAT_S24_BE:
82         return AUDIO_FORMAT_PCM_8_24_BIT;
83     case PCM_FORMAT_S32_BE:
84         return AUDIO_FORMAT_PCM_32_BIT;
85 #else
86     case PCM_FORMAT_S16_LE:
87         return AUDIO_FORMAT_PCM_16_BIT;
88     case PCM_FORMAT_S24_3LE:
89         return AUDIO_FORMAT_PCM_24_BIT_PACKED;
90     case PCM_FORMAT_S24_LE:
91         return AUDIO_FORMAT_PCM_8_24_BIT;
92     case PCM_FORMAT_S32_LE:
93         return AUDIO_FORMAT_PCM_32_BIT;
94 #endif
95     default:
96         LOG_ALWAYS_FATAL("audio_format_from_pcm_format: invalid pcm format %#x", format);
97         return AUDIO_FORMAT_INVALID;  /* doesn't get here, assert called above */
98     }
99 }
100 
101 __END_DECLS
102 
103 #endif /* ANDROID_AUDIO_ALSAOPS_H */
104