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 <cutils/log.h>
26#include <system/audio.h>
27#include <tinyalsa/asoundlib.h>
28
29__BEGIN_DECLS
30
31/* Converts audio_format to pcm_format.
32 * Parameters:
33 *  format  the audio_format_t to convert
34 *
35 * Logs a fatal error if format is not a valid convertible audio_format_t.
36 */
37static inline enum pcm_format pcm_format_from_audio_format(audio_format_t format)
38{
39    switch (format) {
40#ifdef HAVE_BIG_ENDIAN
41    case AUDIO_FORMAT_PCM_16_BIT:
42        return PCM_FORMAT_S16_BE;
43    case AUDIO_FORMAT_PCM_24_BIT_PACKED:
44        return PCM_FORMAT_S24_3BE;
45    case AUDIO_FORMAT_PCM_32_BIT:
46        return PCM_FORMAT_S32_BE;
47    case AUDIO_FORMAT_PCM_8_24_BIT:
48        return PCM_FORMAT_S24_BE;
49#else
50    case AUDIO_FORMAT_PCM_16_BIT:
51        return PCM_FORMAT_S16_LE;
52    case AUDIO_FORMAT_PCM_24_BIT_PACKED:
53        return PCM_FORMAT_S24_3LE;
54    case AUDIO_FORMAT_PCM_32_BIT:
55        return PCM_FORMAT_S32_LE;
56    case AUDIO_FORMAT_PCM_8_24_BIT:
57        return PCM_FORMAT_S24_LE;
58#endif
59    case AUDIO_FORMAT_PCM_FLOAT:  /* there is no equivalent for float */
60    default:
61        LOG_ALWAYS_FATAL("pcm_format_from_audio_format: invalid audio format %#x", format);
62        return 0;
63    }
64}
65
66/* Converts pcm_format to audio_format.
67 * Parameters:
68 *  format  the pcm_format to convert
69 *
70 * Logs a fatal error if format is not a valid convertible pcm_format.
71 */
72static inline audio_format_t audio_format_from_pcm_format(enum pcm_format format)
73{
74    switch (format) {
75#ifdef HAVE_BIG_ENDIAN
76    case PCM_FORMAT_S16_BE:
77        return AUDIO_FORMAT_PCM_16_BIT;
78    case PCM_FORMAT_S24_3BE:
79        return AUDIO_FORMAT_PCM_24_BIT_PACKED;
80    case PCM_FORMAT_S24_BE:
81        return AUDIO_FORMAT_PCM_8_24_BIT;
82    case PCM_FORMAT_S32_BE:
83        return AUDIO_FORMAT_PCM_32_BIT;
84#else
85    case PCM_FORMAT_S16_LE:
86        return AUDIO_FORMAT_PCM_16_BIT;
87    case PCM_FORMAT_S24_3LE:
88        return AUDIO_FORMAT_PCM_24_BIT_PACKED;
89    case PCM_FORMAT_S24_LE:
90        return AUDIO_FORMAT_PCM_8_24_BIT;
91    case PCM_FORMAT_S32_LE:
92        return AUDIO_FORMAT_PCM_32_BIT;
93#endif
94    default:
95        LOG_ALWAYS_FATAL("audio_format_from_pcm_format: invalid pcm format %#x", format);
96        return 0;
97    }
98}
99
100__END_DECLS
101
102#endif /* ANDROID_AUDIO_ALSAOPS_H */
103