Change set_rate/set_channels to set_rate_near/set_channels_near
[platform/adaptation/ap_broadcom/audio-hal-bcm2837.git] / tizen-audio-util.c
1 /*
2  * audio-hal
3  *
4  * Copyright (c) 2015 - 2016 Samsung Electronics Co., Ltd. All rights reserved.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include <config.h>
22 #endif
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <assert.h>
28
29 #include "tizen-audio-internal.h"
30
31 /* ------ dump helper --------  */
32 #define MAX(a, b) ((a) > (b) ? (a) : (b))
33
34 #ifdef __USE_TINYALSA__
35 static const uint32_t g_format_convert_hal_table[] = {
36     [AUDIO_SAMPLE_U8]        = PCM_FORMAT_S8,
37     [AUDIO_SAMPLE_S16LE]     = PCM_FORMAT_S16_LE,
38     [AUDIO_SAMPLE_S32LE]     = PCM_FORMAT_S32_LE,
39     [AUDIO_SAMPLE_S24_32LE]  = PCM_FORMAT_S24_LE
40 };
41
42 static const uint32_t g_format_convert_native_table[] = {
43     [PCM_FORMAT_S8]     = AUDIO_SAMPLE_U8,
44     [PCM_FORMAT_S16_LE] = AUDIO_SAMPLE_S16LE,
45     [PCM_FORMAT_S32_LE] = AUDIO_SAMPLE_S32LE,
46     [PCM_FORMAT_S24_LE] = AUDIO_SAMPLE_S24_32LE
47 };
48 #else
49 static const uint32_t g_format_convert_hal_table[] = {
50     [AUDIO_SAMPLE_U8]        = SND_PCM_FORMAT_U8,
51     [AUDIO_SAMPLE_ALAW]      = SND_PCM_FORMAT_A_LAW,
52     [AUDIO_SAMPLE_ULAW]      = SND_PCM_FORMAT_MU_LAW,
53     [AUDIO_SAMPLE_S16LE]     = SND_PCM_FORMAT_S16_LE,
54     [AUDIO_SAMPLE_S16BE]     = SND_PCM_FORMAT_S16_BE,
55     [AUDIO_SAMPLE_FLOAT32LE] = SND_PCM_FORMAT_FLOAT_LE,
56     [AUDIO_SAMPLE_FLOAT32BE] = SND_PCM_FORMAT_FLOAT_BE,
57     [AUDIO_SAMPLE_S32LE]     = SND_PCM_FORMAT_S32_LE,
58     [AUDIO_SAMPLE_S32BE]     = SND_PCM_FORMAT_S32_BE,
59     [AUDIO_SAMPLE_S24LE]     = SND_PCM_FORMAT_S24_3LE,
60     [AUDIO_SAMPLE_S24BE]     = SND_PCM_FORMAT_S24_3BE,
61     [AUDIO_SAMPLE_S24_32LE]  = SND_PCM_FORMAT_S24_LE,
62     [AUDIO_SAMPLE_S24_32BE]  = SND_PCM_FORMAT_S24_BE
63 };
64
65 static const snd_pcm_format_t g_format_convert_native_table[] = {
66     [SND_PCM_FORMAT_U8]       = AUDIO_SAMPLE_U8,
67     [SND_PCM_FORMAT_A_LAW]    = AUDIO_SAMPLE_ALAW,
68     [SND_PCM_FORMAT_MU_LAW]   = AUDIO_SAMPLE_ULAW,
69     [SND_PCM_FORMAT_S16_LE]   = AUDIO_SAMPLE_S16LE,
70     [SND_PCM_FORMAT_S16_BE]   = AUDIO_SAMPLE_S16BE,
71     [SND_PCM_FORMAT_FLOAT_LE] = AUDIO_SAMPLE_FLOAT32LE,
72     [SND_PCM_FORMAT_FLOAT_BE] = AUDIO_SAMPLE_FLOAT32BE,
73     [SND_PCM_FORMAT_S32_LE]   = AUDIO_SAMPLE_S32LE,
74     [SND_PCM_FORMAT_S32_BE]   = AUDIO_SAMPLE_S32BE,
75     [SND_PCM_FORMAT_S24_3LE]  = AUDIO_SAMPLE_S24LE,
76     [SND_PCM_FORMAT_S24_3BE]  = AUDIO_SAMPLE_S24BE,
77     [SND_PCM_FORMAT_S24_LE]   = AUDIO_SAMPLE_S24_32LE,
78     [SND_PCM_FORMAT_S24_BE]   = AUDIO_SAMPLE_S24_32BE
79 };
80 #endif
81
82 static snd_pcm_format_t __convert_to_hal_format(audio_sample_format_s format)
83 {
84     return g_format_convert_hal_table[format];
85 }
86
87 static uint32_t __convert_to_native_format(snd_pcm_format_t format)
88 {
89     return g_format_convert_native_table[format];
90 }
91
92 /* src is pa_sample_spec which pulseaudio uses */
93 void convert_hal_format_from_sample_spec(void *src, audio_pcm_sample_spec_s *dst)
94 {
95     audio_sample_format_s format;
96
97     assert(src);
98     assert(dst);
99
100     memcpy(dst, src, sizeof(audio_pcm_sample_spec_s));
101
102     format = ((audio_pcm_sample_spec_s *)src)->format;
103
104     dst->format = __convert_to_hal_format(format);
105 }
106
107 /* dst is pa_sample_spec which pulseaudio uses */
108 void convert_hal_format_to_sample_spec(audio_pcm_sample_spec_s *src, void *dst)
109 {
110     assert(src);
111     assert(dst);
112
113     memcpy(dst, src, sizeof(audio_pcm_sample_spec_s));
114
115     ((audio_pcm_sample_spec_s *)dst)->format = __convert_to_native_format(src->format);
116 }
117
118 dump_data_t* _audio_dump_new(int length)
119 {
120     dump_data_t* dump = NULL;
121
122     if ((dump = malloc(sizeof(dump_data_t)))) {
123         memset(dump, 0, sizeof(dump_data_t));
124         if ((dump->strbuf = malloc(length))) {
125             dump->p = &dump->strbuf[0];
126             dump->left = length;
127         } else {
128             free(dump);
129             dump = NULL;
130         }
131     }
132
133     return dump;
134 }
135
136 void _audio_dump_add_str(dump_data_t *dump, const char *fmt, ...)
137 {
138     int len;
139     va_list ap;
140
141     if (!dump)
142         return;
143
144     va_start(ap, fmt);
145     len = vsnprintf(dump->p, dump->left, fmt, ap);
146     va_end(ap);
147
148     dump->p += MAX(0, len);
149     dump->left -= MAX(0, len);
150 }
151
152 char* _audio_dump_get_str(dump_data_t *dump)
153 {
154     return (dump) ? dump->strbuf : NULL;
155 }
156
157 void _audio_dump_free(dump_data_t *dump)
158 {
159     if (dump) {
160         if (dump->strbuf)
161             free(dump->strbuf);
162         free(dump);
163     }
164 }
165 /* ------ dump helper --------  */