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