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