1 /*
2  * Copyright (C) 2010 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 "math.h"
18 
19 #include <system/audio.h>
20 
21 //-----------------------------------------------------------------------------
22 // OpenSL ES to Android
23 //----------------------
24 
sles_to_android_sampleRate(SLuint32 sampleRateMilliHertz)25 static inline uint32_t sles_to_android_sampleRate(SLuint32 sampleRateMilliHertz) {
26     return (uint32_t)(sampleRateMilliHertz / 1000);
27 }
28 
sles_to_android_sampleFormat(const SLDataFormat_PCM * df_pcm)29 static inline audio_format_t sles_to_android_sampleFormat(const SLDataFormat_PCM *df_pcm) {
30     if (df_pcm->containerSize != df_pcm->bitsPerSample) {
31         return AUDIO_FORMAT_INVALID;
32     }
33     switch (df_pcm->formatType) {
34     case SL_DATAFORMAT_PCM:
35         switch (df_pcm->containerSize) {
36         case 8:
37             return AUDIO_FORMAT_PCM_8_BIT;
38         case 16:
39             return AUDIO_FORMAT_PCM_16_BIT;
40         case 24:
41             return AUDIO_FORMAT_PCM_24_BIT_PACKED;
42         case 32:
43             return AUDIO_FORMAT_PCM_32_BIT;
44         default:
45             return AUDIO_FORMAT_INVALID;
46         }
47     case SL_ANDROID_DATAFORMAT_PCM_EX:
48         switch (((SLAndroidDataFormat_PCM_EX *)df_pcm)->representation) {
49         case SL_ANDROID_PCM_REPRESENTATION_UNSIGNED_INT:
50             switch (df_pcm->containerSize) {
51             case 8:
52                 return AUDIO_FORMAT_PCM_8_BIT;
53             default:
54                 return AUDIO_FORMAT_INVALID;
55             }
56         case SL_ANDROID_PCM_REPRESENTATION_SIGNED_INT:
57             switch (df_pcm->containerSize) {
58             case 16:
59                 return AUDIO_FORMAT_PCM_16_BIT;
60             case 24:
61                 return AUDIO_FORMAT_PCM_24_BIT_PACKED;
62             case 32:
63                 return AUDIO_FORMAT_PCM_32_BIT;
64             default:
65                 return AUDIO_FORMAT_INVALID;
66             }
67         case SL_ANDROID_PCM_REPRESENTATION_FLOAT:
68             switch (df_pcm->containerSize) {
69             case 32:
70                 return AUDIO_FORMAT_PCM_FLOAT;
71             default:
72                 return AUDIO_FORMAT_INVALID;
73             }
74         default:
75             return AUDIO_FORMAT_INVALID;
76         }
77     default:
78         return AUDIO_FORMAT_INVALID;
79     }
80 }
81 
82 
sles_to_android_amplification(SLmillibel level)83 static inline float sles_to_android_amplification(SLmillibel level) {
84     // FIXME use the FX Framework conversions
85     return pow(10, (float)level/2000);
86 }
87