android_sles_conversions.h revision bf3152158263633892ed730237388ce850cf4f1d
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 audio_channel_mask_t sles_to_android_channelMaskIn(SLuint32 nbChannels, 81 SLuint32 channelMask) { 82 // FIXME handle channel mask mapping between SL ES and Android 83 return audio_channel_in_mask_from_count(nbChannels); 84} 85 86 87static inline audio_channel_mask_t sles_to_android_channelMaskOut(SLuint32 nbChannels, 88 SLuint32 channelMask) { 89 // FIXME handle channel mask mapping between SL ES and Android 90 return audio_channel_out_mask_from_count(nbChannels); 91} 92 93 94static inline float sles_to_android_amplification(SLmillibel level) { 95 // FIXME use the FX Framework conversions 96 return pow(10, (float)level/2000); 97} 98 99 100static inline uint32_t channelCountToMask(uint32_t channelCount) 101{ 102 // FIXME channel mask is not yet implemented by Stagefright, so use a reasonable default 103 // that is computed from the channel count 104 uint32_t channelMask; 105 switch (channelCount) { 106 case 1: 107 // see explanation in data.c re: default channel mask for mono 108 return SL_SPEAKER_FRONT_LEFT; 109 case 2: 110 return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT; 111 // Android-specific 112 case 4: 113 return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT 114 | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT; 115 case 6: 116 return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT| SL_SPEAKER_FRONT_CENTER 117 | SL_SPEAKER_LOW_FREQUENCY 118 | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT; 119 case 8: 120 return SL_SPEAKER_FRONT_LEFT | SL_SPEAKER_FRONT_RIGHT | SL_SPEAKER_FRONT_CENTER 121 | SL_SPEAKER_LOW_FREQUENCY 122 | SL_SPEAKER_BACK_LEFT | SL_SPEAKER_BACK_RIGHT 123 | SL_SPEAKER_SIDE_LEFT |SL_SPEAKER_SIDE_RIGHT; 124 default: 125 return UNKNOWN_CHANNELMASK; 126 } 127} 128