android_sles_conversions.h revision ca39f4b4dbeb920a5b97bd65be73f2f7cac77431
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 <hardware/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 int 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 (nbChannels == 1 ?
68            AUDIO_CHANNEL_IN_MONO :
69            AUDIO_CHANNEL_IN_STEREO);
70}
71
72
73static inline int sles_to_android_channelMaskOut(SLuint32 nbChannels, SLuint32 channelMask) {
74    // FIXME handle channel mask mapping between SL ES and Android
75    return (nbChannels == 1 ?
76            AUDIO_CHANNEL_OUT_MONO :
77            AUDIO_CHANNEL_OUT_STEREO);
78}
79
80
81static inline float sles_to_android_amplification(SLmillibel level) {
82    // FIXME use the FX Framework conversions
83    return pow(10, (float)level/2000);
84}
85