IVirtualizer.c revision 262059f71a68edc5e510427c63f5f1623d3672a8
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/* Virtualizer implementation */
18
19#include "sles_allinclusive.h"
20
21#define VIRTUALIZER_STRENGTH_MIN 0
22#define VIRTUALIZER_STRENGTH_MAX 1000
23
24
25#if defined(ANDROID)
26/**
27 * returns true if this interface is not associated with an initialized Virtualizer effect
28 */
29static inline bool NO_VIRTUALIZER(IVirtualizer* v) {
30    return (v->mVirtualizerEffect == 0);
31}
32#endif
33
34
35static SLresult IVirtualizer_SetEnabled(SLVirtualizerItf self, SLboolean enabled)
36{
37    SL_ENTER_INTERFACE
38
39    IVirtualizer *this = (IVirtualizer *) self;
40    interface_lock_exclusive(this);
41    this->mEnabled = (SLboolean) enabled;
42#if !defined(ANDROID)
43    result = SL_RESULT_SUCCESS;
44#else
45    if (NO_VIRTUALIZER(this)) {
46        result = SL_RESULT_CONTROL_LOST;
47    } else {
48        android::status_t status =
49                this->mVirtualizerEffect->setEnabled((bool) this->mEnabled);
50        result = android_fx_statusToResult(status);
51    }
52#endif
53    interface_unlock_exclusive(this);
54
55    SL_LEAVE_INTERFACE
56
57}
58
59
60static SLresult IVirtualizer_IsEnabled(SLVirtualizerItf self, SLboolean *pEnabled)
61{
62    SL_ENTER_INTERFACE
63
64     if (NULL == pEnabled) {
65         result = SL_RESULT_PARAMETER_INVALID;
66     } else {
67         IVirtualizer *this = (IVirtualizer *) self;
68         interface_lock_exclusive(this);
69         SLboolean enabled = this->mEnabled;
70#if !defined(ANDROID)
71         *pEnabled = enabled;
72         result = SL_RESULT_SUCCESS;
73#else
74         if (NO_VIRTUALIZER(this)) {
75             result = SL_RESULT_CONTROL_LOST;
76         } else {
77             *pEnabled = (SLboolean) this->mVirtualizerEffect->getEnabled();
78             result = SL_RESULT_SUCCESS;
79         }
80#endif
81         interface_unlock_exclusive(this);
82     }
83
84     SL_LEAVE_INTERFACE
85}
86
87
88static SLresult IVirtualizer_SetStrength(SLVirtualizerItf self, SLpermille strength)
89{
90    SL_ENTER_INTERFACE
91
92     if ((VIRTUALIZER_STRENGTH_MIN > strength) || (VIRTUALIZER_STRENGTH_MAX < strength)) {
93         result = SL_RESULT_PARAMETER_INVALID;
94     } else {
95         IVirtualizer *this = (IVirtualizer *) self;
96         interface_lock_exclusive(this);
97#if !defined(ANDROID)
98         this->mStrength = strength;
99         result = SL_RESULT_SUCCESS;
100#else
101         if (NO_VIRTUALIZER(this)) {
102             result = SL_RESULT_CONTROL_LOST;
103         } else {
104             android::status_t status = android_virt_setParam(this->mVirtualizerEffect,
105                     VIRTUALIZER_PARAM_STRENGTH, &strength);
106             result = android_fx_statusToResult(status);
107         }
108#endif
109         interface_unlock_exclusive(this);
110     }
111
112     SL_LEAVE_INTERFACE
113}
114
115
116static SLresult IVirtualizer_GetRoundedStrength(SLVirtualizerItf self, SLpermille *pStrength)
117{
118    SL_ENTER_INTERFACE
119
120    if (NULL == pStrength) {
121        result = SL_RESULT_PARAMETER_INVALID;
122    } else {
123        IVirtualizer *this = (IVirtualizer *) self;
124        interface_lock_exclusive(this);
125        SLpermille strength = this->mStrength;;
126#if !defined(ANDROID)
127        result = SL_RESULT_SUCCESS;
128#else
129        if (NO_VIRTUALIZER(this)) {
130            result = SL_RESULT_CONTROL_LOST;
131        } else {
132            android::status_t status = android_virt_getParam(this->mVirtualizerEffect,
133                           VIRTUALIZER_PARAM_STRENGTH, &strength);
134            result = android_fx_statusToResult(status);
135        }
136#endif
137        interface_unlock_exclusive(this);
138        *pStrength = strength;
139    }
140
141    SL_LEAVE_INTERFACE
142}
143
144
145static SLresult IVirtualizer_IsStrengthSupported(SLVirtualizerItf self, SLboolean *pSupported)
146{
147    SL_ENTER_INTERFACE
148
149    if (NULL == pSupported) {
150        result = SL_RESULT_PARAMETER_INVALID;
151    } else {
152#if !defined(ANDROID)
153        *pSupported = SL_BOOLEAN_TRUE;
154        result = SL_RESULT_SUCCESS;
155#else
156        IVirtualizer *this = (IVirtualizer *) self;
157        int32_t supported = 0;
158        interface_lock_exclusive(this);
159        if (NO_VIRTUALIZER(this)) {
160            result = SL_RESULT_CONTROL_LOST;
161        } else {
162            android::status_t status =
163                android_virt_getParam(this->mVirtualizerEffect,
164                        VIRTUALIZER_PARAM_STRENGTH_SUPPORTED, &supported);
165            result = android_fx_statusToResult(status);
166        }
167        interface_unlock_exclusive(this);
168        *pSupported = (SLboolean) (supported != 0);
169#endif
170    }
171
172    SL_LEAVE_INTERFACE
173}
174
175
176static const struct SLVirtualizerItf_ IVirtualizer_Itf = {
177    IVirtualizer_SetEnabled,
178    IVirtualizer_IsEnabled,
179    IVirtualizer_SetStrength,
180    IVirtualizer_GetRoundedStrength,
181    IVirtualizer_IsStrengthSupported
182};
183
184void IVirtualizer_init(void *self)
185{
186    IVirtualizer *this = (IVirtualizer *) self;
187    this->mItf = &IVirtualizer_Itf;
188    this->mEnabled = SL_BOOLEAN_FALSE;
189    this->mStrength = 0;
190#if defined(ANDROID)
191    memset(&this->mVirtualizerDescriptor, 0, sizeof(effect_descriptor_t));
192    // placement new (explicit constructor)
193    (void) new (&this->mVirtualizerEffect) android::sp<android::AudioEffect>();
194#endif
195}
196
197void IVirtualizer_deinit(void *self)
198{
199#if defined(ANDROID)
200    IVirtualizer *this = (IVirtualizer *) self;
201    // explicit destructor
202    this->mVirtualizerEffect.~sp();
203#endif
204}
205
206bool IVirtualizer_Expose(void *self)
207{
208#if defined(ANDROID)
209    IVirtualizer *this = (IVirtualizer *) self;
210    if (!android_fx_initEffectDescriptor(SL_IID_VIRTUALIZER, &this->mVirtualizerDescriptor)) {
211        SL_LOGE("Virtualizer initialization failed.");
212        return false;
213    }
214#endif
215    return true;
216}
217