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 /* PresetReverb implementation */
18 
19 #include "sles_allinclusive.h"
20 
21 #if defined(ANDROID)
22 /**
23  * returns true if this interface is not associated with an initialized PresetReverb effect
24  */
NO_PRESETREVERB(IPresetReverb * ipr)25 static inline bool NO_PRESETREVERB(IPresetReverb* ipr) {
26     return (ipr->mPresetReverbEffect == 0);
27 }
28 #endif
29 
IPresetReverb_SetPreset(SLPresetReverbItf self,SLuint16 preset)30 static SLresult IPresetReverb_SetPreset(SLPresetReverbItf self, SLuint16 preset)
31 {
32     SL_ENTER_INTERFACE
33 
34     IPresetReverb *thiz = (IPresetReverb *) self;
35     switch (preset) {
36     case SL_REVERBPRESET_NONE:
37     case SL_REVERBPRESET_SMALLROOM:
38     case SL_REVERBPRESET_MEDIUMROOM:
39     case SL_REVERBPRESET_LARGEROOM:
40     case SL_REVERBPRESET_MEDIUMHALL:
41     case SL_REVERBPRESET_LARGEHALL:
42     case SL_REVERBPRESET_PLATE:
43         interface_lock_exclusive(thiz);
44         thiz->mPreset = preset;
45 #if !defined(ANDROID)
46         result = SL_RESULT_SUCCESS;
47 #else
48         if (NO_PRESETREVERB(thiz)) {
49             result = SL_RESULT_CONTROL_LOST;
50         } else {
51             android::status_t status = android_prev_setPreset(thiz->mPresetReverbEffect, preset);
52             result = android_fx_statusToResult(status);
53         }
54 #endif
55         interface_unlock_exclusive(thiz);
56         break;
57     default:
58         result = SL_RESULT_PARAMETER_INVALID;
59         break;
60     }
61 
62     SL_LEAVE_INTERFACE
63 }
64 
IPresetReverb_GetPreset(SLPresetReverbItf self,SLuint16 * pPreset)65 static SLresult IPresetReverb_GetPreset(SLPresetReverbItf self, SLuint16 *pPreset)
66 {
67     SL_ENTER_INTERFACE
68 
69     if (NULL == pPreset) {
70         result = SL_RESULT_PARAMETER_INVALID;
71     } else {
72         IPresetReverb *thiz = (IPresetReverb *) self;
73         interface_lock_shared(thiz);
74         SLuint16 preset = SL_REVERBPRESET_NONE;
75 #if 1 // !defined(ANDROID)
76         preset = thiz->mPreset;
77         result = SL_RESULT_SUCCESS;
78 #else
79         if (NO_PRESETREVERB(thiz)) {
80             preset = thiz->mPreset;
81             result = SL_RESULT_CONTROL_LOST;
82         } else {
83             android::status_t status = android_prev_getPreset(thiz->mPresetReverbEffect, &preset);
84             result = android_fx_statusToResult(status);
85         }
86         // OpenSL ES 1.0.1 spec and conformance test do not permit SL_RESULT_CONTROL_LOST
87         if (SL_RESULT_CONTROL_LOST == result) {
88             result = SL_RESULT_SUCCESS;
89         }
90 #endif
91         interface_unlock_shared(thiz);
92         *pPreset = preset;
93     }
94 
95     SL_LEAVE_INTERFACE
96 }
97 
98 static const struct SLPresetReverbItf_ IPresetReverb_Itf = {
99     IPresetReverb_SetPreset,
100     IPresetReverb_GetPreset
101 };
102 
IPresetReverb_init(void * self)103 void IPresetReverb_init(void *self)
104 {
105     IPresetReverb *thiz = (IPresetReverb *) self;
106     thiz->mItf = &IPresetReverb_Itf;
107     thiz->mPreset = SL_REVERBPRESET_NONE;
108 #if defined(ANDROID)
109     memset(&thiz->mPresetReverbDescriptor, 0, sizeof(effect_descriptor_t));
110     // placement new (explicit constructor)
111     (void) new (&thiz->mPresetReverbEffect) android::sp<android::AudioEffect>();
112 #endif
113 }
114 
IPresetReverb_deinit(void * self)115 void IPresetReverb_deinit(void *self)
116 {
117 #if defined(ANDROID)
118     IPresetReverb *thiz = (IPresetReverb *) self;
119     // explicit destructor
120     thiz->mPresetReverbEffect.~sp();
121 #endif
122 }
123 
IPresetReverb_Expose(void * self)124 bool IPresetReverb_Expose(void *self)
125 {
126 #if defined(ANDROID)
127     IPresetReverb *thiz = (IPresetReverb *) self;
128     if (!android_fx_initEffectDescriptor(SL_IID_PRESETREVERB, &thiz->mPresetReverbDescriptor)) {
129         SL_LOGE("PresetReverb initialization failed.");
130         return false;
131     }
132 #endif
133     return true;
134 }
135