blob: d65981c40c3bcb748f0dc59e17c4c725ea2453ae
1 | /* |
2 | * Copyright (C) 2011 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 | #define LOG_TAG "audio_virtual_effect" |
18 | |
19 | #include <cutils/log.h> |
20 | #include <cutils/properties.h> |
21 | #include <pthread.h> |
22 | #include <stdlib.h> |
23 | |
24 | #include "LVCS.h" |
25 | #include "InstAlloc.h" |
26 | #include "LVCS_Private.h" |
27 | |
28 | #include "audio_virtual_effect.h" |
29 | |
30 | LVCS_Handle_t hCSInstance = LVM_NULL; /* Concert Sound instance handle */ |
31 | LVCS_Instance_t CS_Instance; /* Concert Soun= d instance */ |
32 | |
33 | LVCS_MemTab_t CS_MemTab; /* Memory table */ |
34 | LVCS_Capabilities_t CS_Capabilities; /* Initial capabilities */ |
35 | |
36 | static pthread_mutex_t audio_vir_mutex = PTHREAD_MUTEX_INITIALIZER; |
37 | |
38 | int Virtualizer_init(void) { |
39 | LVCS_ReturnStatus_en LVCS_Status; |
40 | LVCS_Params_t *CS_Params = &CS_Instance.Params; |
41 | int i = 0; |
42 | |
43 | pthread_mutex_lock(&audio_vir_mutex); |
44 | |
45 | CS_Capabilities.MaxBlockSize = 2048; |
46 | CS_Capabilities.pBundleInstance = (void*)hCSInstance; |
47 | |
48 | LVCS_Status = LVCS_Memory(LVM_NULL, |
49 | &CS_MemTab, |
50 | &CS_Capabilities); |
51 | |
52 | CS_MemTab.Region[LVCS_MEMREGION_PERSISTENT_SLOW_DATA].pBaseAddress = &CS_Instance; |
53 | |
54 | /* Allocate memory */ |
55 | for (i = 0; i < LVM_NR_MEMORY_REGIONS; i++) { |
56 | if (CS_MemTab.Region[i].Size != 0) { |
57 | CS_MemTab.Region[i].pBaseAddress = malloc(CS_MemTab.Region[i].Size); |
58 | |
59 | if (CS_MemTab.Region[i].pBaseAddress == LVM_NULL) { |
60 | ALOGV("\tLVM_ERROR :LvmBundle_init CreateInstance Failed to allocate %" |
61 | " bytes for region %u\n", CS_MemTab.Region[i].Size, i ); |
62 | return LVCS_NULLADDRESS; |
63 | } else { |
64 | ALOGV("\tLvmBundle_init CreateInstance allocated %" |
65 | " bytes for region %u at %p\n", |
66 | CS_MemTab.Region[i].Size, i, CS_MemTab.Region[i].pBaseAddress); |
67 | } |
68 | } |
69 | } |
70 | |
71 | hCSInstance = LVM_NULL; |
72 | LVCS_Status = LVCS_Init(&hCSInstance, |
73 | &CS_MemTab, |
74 | &CS_Capabilities); |
75 | |
76 | CS_Params->OperatingMode = LVCS_OFF; |
77 | CS_Params->CompressorMode = LVM_MODE_ON; |
78 | CS_Params->SourceFormat = LVCS_STEREO; |
79 | CS_Params->SpeakerType = LVCS_HEADPHONES; |
80 | CS_Params->SampleRate = LVM_FS_48000; |
81 | CS_Params->ReverbLevel = 100; |
82 | CS_Params->EffectLevel = 0; /* 0~32767 */ |
83 | |
84 | pthread_mutex_unlock(&audio_vir_mutex); |
85 | return LVCS_Status; |
86 | } |
87 | |
88 | int Virtualizer_release(void) { |
89 | int i; |
90 | pthread_mutex_lock(&audio_vir_mutex); |
91 | for (i = 0; i < LVM_NR_MEMORY_REGIONS; i++) { |
92 | if (CS_MemTab.Region[i].pBaseAddress != 0) { |
93 | free(CS_MemTab.Region[i].pBaseAddress); |
94 | CS_MemTab.Region[i].pBaseAddress = NULL; |
95 | } |
96 | } |
97 | hCSInstance = LVM_NULL; |
98 | pthread_mutex_unlock(&audio_vir_mutex); |
99 | return 0; |
100 | } |
101 | |
102 | // enable: 1; disable:0, EffectLevel:0~100 |
103 | int Virtualizer_control(int enable, int EffectLevel) { |
104 | |
105 | LVCS_ReturnStatus_en CS_Status; |
106 | LVCS_Params_t *CS_Params = &CS_Instance.Params; |
107 | |
108 | if (hCSInstance == LVM_NULL) |
109 | return LVCS_NULLADDRESS; |
110 | |
111 | pthread_mutex_lock(&audio_vir_mutex); |
112 | if (enable == 1) |
113 | CS_Params->OperatingMode = LVCS_ON; |
114 | else |
115 | CS_Params->OperatingMode = LVCS_OFF; |
116 | |
117 | if (EffectLevel > 100) |
118 | CS_Params->EffectLevel = 32700; |
119 | else if (EffectLevel < 0) |
120 | CS_Params->EffectLevel = 0; |
121 | else |
122 | CS_Params->EffectLevel = EffectLevel * 327; |
123 | |
124 | CS_Status = LVCS_Control(hCSInstance, |
125 | CS_Params); |
126 | |
127 | pthread_mutex_unlock(&audio_vir_mutex); |
128 | return CS_Status; |
129 | } |
130 | |
131 | int Virtualizer_process(int16_t *pInData, int16_t *pOutData, uint16_t NumSamples) { |
132 | LVCS_ReturnStatus_en CS_Status; |
133 | |
134 | if (hCSInstance == LVM_NULL) |
135 | return LVCS_NULLADDRESS; |
136 | |
137 | pthread_mutex_lock(&audio_vir_mutex); |
138 | CS_Status = LVCS_Process(hCSInstance, |
139 | pInData, |
140 | pOutData, |
141 | NumSamples); |
142 | pthread_mutex_unlock(&audio_vir_mutex); |
143 | return CS_Status; |
144 | } |
145 |