android_sles_conversions.h revision 64c3fe7bd86951eeac27adca2219ce16eabff58c
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// Android to OpenSL ES
23//----------------------
24static inline SLuint32 android_to_sles_streamType(int type) {
25    return (SLuint32) type;
26}
27
28
29static inline SLuint32 android_to_sles_sampleRate(uint32_t srHz) {
30    // convert to milliHertz
31    return (SLuint32) srHz*1000;
32}
33
34
35//-----------------------------------------------------------------------------
36// OpenSL ES to Android
37//----------------------
38static inline int sles_to_android_streamType(SLuint32 type) {
39    return (int)type;
40}
41
42
43static inline uint32_t sles_to_android_sampleRate(SLuint32 sampleRateMilliHertz) {
44    return (uint32_t)(sampleRateMilliHertz / 1000);
45}
46
47static inline audio_format_t sles_to_android_sampleFormat(const SLDataFormat_PCM *df_pcm) {
48    switch (df_pcm->formatType) {
49    case SL_DATAFORMAT_PCM:
50        switch (df_pcm->containerSize) {
51        case 8:
52            return AUDIO_FORMAT_PCM_8_BIT;
53        case 16:
54            return AUDIO_FORMAT_PCM_16_BIT;
55        case 24:
56            return AUDIO_FORMAT_PCM_24_BIT_PACKED;
57        case 32:
58            return AUDIO_FORMAT_PCM_32_BIT;
59        default:
60            return AUDIO_FORMAT_INVALID;
61        }
62    case SL_ANDROID_DATAFORMAT_PCM_EX:
63        switch (((SLAndroidDataFormat_PCM_EX *)df_pcm)->representation) {
64        case SL_ANDROID_PCM_REPRESENTATION_UNSIGNED_INT:
65            switch (df_pcm->containerSize) {
66            case 8:
67                return AUDIO_FORMAT_PCM_8_BIT;
68            default:
69                return AUDIO_FORMAT_INVALID;
70            }
71        case SL_ANDROID_PCM_REPRESENTATION_SIGNED_INT:
72            switch (df_pcm->containerSize) {
73            case 16:
74                return AUDIO_FORMAT_PCM_16_BIT;
75            case 24:
76                return AUDIO_FORMAT_PCM_24_BIT_PACKED;
77            case 32:
78                return AUDIO_FORMAT_PCM_32_BIT;
79            default:
80                return AUDIO_FORMAT_INVALID;
81            }
82        case SL_ANDROID_PCM_REPRESENTATION_FLOAT:
83            switch (df_pcm->containerSize) {
84            case 32:
85                return AUDIO_FORMAT_PCM_FLOAT;
86            default:
87                return AUDIO_FORMAT_INVALID;
88            }
89        default:
90            return AUDIO_FORMAT_INVALID;
91        }
92    default:
93        return AUDIO_FORMAT_INVALID;
94    }
95}
96
97
98static inline audio_channel_mask_t sles_to_android_channelMaskIn(SLuint32 nbChannels,
99        SLuint32 channelMask) {
100    // FIXME handle channel mask mapping between SL ES and Android
101    return audio_channel_in_mask_from_count(nbChannels);
102}
103
104
105static inline audio_channel_mask_t sles_to_android_channelMaskOut(SLuint32 nbChannels,
106        SLuint32 channelMask) {
107    // FIXME handle channel mask mapping between SL ES and Android
108    return audio_channel_out_mask_from_count(nbChannels);
109}
110
111
112static inline float sles_to_android_amplification(SLmillibel level) {
113    // FIXME use the FX Framework conversions
114    return pow(10, (float)level/2000);
115}
116
117
118static inline uint32_t channelCountToMask(uint32_t channelCount)
119{
120    // FIXME channel mask is not yet implemented by Stagefright, so use a reasonable default
121    //       that is computed from the channel count
122    uint32_t channelMask;
123    switch (channelCount) {
124    case 1:
125        // see explanation in data.c re: default channel mask for mono
126        return SL_SPEAKER_FRONT_LEFT;
127    case 2:
128        return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
129    // Android-specific
130    case 4:
131        return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT
132               | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT;
133    case 6:
134        return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT| SL_SPEAKER_FRONT_CENTER
135               | SL_SPEAKER_LOW_FREQUENCY
136               | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT;
137    case 8:
138        return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT | SL_SPEAKER_FRONT_CENTER
139               | SL_SPEAKER_LOW_FREQUENCY
140               | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT
141               | SL_SPEAKER_SIDE_LEFT |SL_SPEAKER_SIDE_RIGHT;
142    default:
143        return UNKNOWN_CHANNELMASK;
144    }
145}
146