android_sles_conversions.h revision bb74f23cd3dc877c7eaf4db2132f724d11aeeb8f
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_24:
56            return AUDIO_FORMAT_PCM_8_24_BIT;
57            // Maybe one of these???
58            // return AUDIO_FORMAT_PCM_32_BIT;
59            // return AUDIO_FORMAT_PCM_24_BIT_PACKED;
60            break;
61        case SL_PCMSAMPLEFORMAT_FIXED_20:
62        case SL_PCMSAMPLEFORMAT_FIXED_28:
63        case SL_PCMSAMPLEFORMAT_FIXED_32:
64        default:
65            return AUDIO_FORMAT_INVALID;
66    }
67}
68
69
70static inline audio_channel_mask_t sles_to_android_channelMaskIn(SLuint32 nbChannels,
71        SLuint32 channelMask) {
72    // FIXME handle channel mask mapping between SL ES and Android
73    return audio_channel_in_mask_from_count(nbChannels);
74}
75
76
77static inline audio_channel_mask_t sles_to_android_channelMaskOut(SLuint32 nbChannels,
78        SLuint32 channelMask) {
79    // FIXME handle channel mask mapping between SL ES and Android
80    return audio_channel_out_mask_from_count(nbChannels);
81}
82
83
84static inline float sles_to_android_amplification(SLmillibel level) {
85    // FIXME use the FX Framework conversions
86    return pow(10, (float)level/2000);
87}
88
89
90static inline uint32_t channelCountToMask(uint32_t channelCount)
91{
92    // FIXME channel mask is not yet implemented by Stagefright, so use a reasonable default
93    //       that is computed from the channel count
94    uint32_t channelMask;
95    switch (channelCount) {
96    case 1:
97        // see explanation in data.c re: default channel mask for mono
98        return SL_SPEAKER_FRONT_LEFT;
99    case 2:
100        return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT;
101    // Android-specific
102    case 4:
103        return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT
104               | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT;
105    case 6:
106        return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT| SL_SPEAKER_FRONT_CENTER
107               | SL_SPEAKER_LOW_FREQUENCY
108               | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT;
109    case 8:
110        return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT | SL_SPEAKER_FRONT_CENTER
111               | SL_SPEAKER_LOW_FREQUENCY
112               | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT
113               | SL_SPEAKER_SIDE_LEFT |SL_SPEAKER_SIDE_RIGHT;
114    default:
115        return UNKNOWN_CHANNELMASK;
116    }
117}
118