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