android_sles_conversions.h revision 5cb9343d2739da755186f8dcdf1da307671e7353
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(SLuint32 pcmFormat) {
48    switch (pcmFormat) {
49        case SL_PCMSAMPLEFORMAT_FIXED_16:
50            return AUDIO_FORMAT_PCM_16_BIT;
51            break;
52        case SL_PCMSAMPLEFORMAT_FIXED_8:
53            return AUDIO_FORMAT_PCM_8_BIT;
54            break;
55        case SL_PCMSAMPLEFORMAT_FIXED_20:
56        case SL_PCMSAMPLEFORMAT_FIXED_24:
57        case SL_PCMSAMPLEFORMAT_FIXED_28:
58        case SL_PCMSAMPLEFORMAT_FIXED_32:
59        default:
60            return AUDIO_FORMAT_INVALID;
61    }
62}
63
64
65static inline int sles_to_android_channelMaskIn(SLuint32 nbChannels, SLuint32 channelMask) {
66    // FIXME handle channel mask mapping between SL ES and Android
67    return audio_channel_in_mask_from_count(nbChannels);
68}
69
70
71static inline int sles_to_android_channelMaskOut(SLuint32 nbChannels, SLuint32 channelMask) {
72    // FIXME handle channel mask mapping between SL ES and Android
73    return audio_channel_out_mask_from_count(nbChannels);
74}
75
76
77static inline float sles_to_android_amplification(SLmillibel level) {
78    // FIXME use the FX Framework conversions
79    return pow(10, (float)level/2000);
80}
81
82
83static inline uint32_t channelCountToMask(uint32_t channelCount)
84{
85    // FIXME channel mask is not yet implemented by Stagefright, so use a reasonable default
86    //       that is computed from the channel count
87    uint32_t channelMask;
88    switch (channelCount) {
89    case 1:
90        // see explanation in data.c re: default channel mask for mono
91        return SL_SPEAKER_FRONT_LEFT;
92    case 2:
93        return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
94    default:
95        return UNKNOWN_CHANNELMASK;
96    }
97}
98