xref: /aosp_15_r20/external/oboe/apps/fxlab/app/src/main/kotlin/com/mobileer/androidfxlab/NativeInterface.kt (revision 05767d913155b055644481607e6fa1e35e2fe72c)
1 /*
2  * Copyright  2019 Google LLC
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  *     https://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 package com.mobileer.androidfxlab
18 
19 import android.util.Log
20 import com.mobileer.androidfxlab.datatype.Effect
21 import com.mobileer.androidfxlab.datatype.EffectDescription
22 
23 object NativeInterface {
24     // Used to load the 'native-lib' library on application startup.
25     val effectDescriptionMap: Map<String, EffectDescription>
26 
27     init {
28         System.loadLibrary("native-lib")
29         effectDescriptionMap = getEffects()
<lambda>null30             .associateBy { it.name }
31         Log.d("MAP", effectDescriptionMap.toString())
32     }
33 
34     // Functions/Members called by UI code
35     // Adds effect at end of effect list
addEffectnull36     fun addEffect(effect: Effect) {
37         Log.d("INTERFACE", String.format("Effect %s added", effect.name))
38         addDefaultEffectNative(
39             convertEffectToId(
40                 effect
41             )
42         )
43     }
44 
45     // Enables effect at index
enableEffectAtnull46     fun enableEffectAt(turnOn: Boolean, index: Int) {
47         Log.d("INTERFACE", String.format("Effect %b at index %d", turnOn, index))
48         enableEffectNative(index, turnOn)
49     }
50 
51     // Signals params were updated at index
updateParamsAtnull52     fun updateParamsAt(effect: Effect, index: Int) {
53         Log.d(
54             "INTERFACE",
55             String.format(
56                 "Params were updated at index %d to %f",
57                 index,
58                 effect.paramValues[0]
59             )
60         )
61         modifyEffectNative(
62             convertEffectToId(
63                 effect
64             ), index, effect.paramValues
65         )
66     }
67 
68     // Removes effect at index
removeEffectAtnull69     fun removeEffectAt(index: Int) {
70         Log.d("INTERFACE", String.format("Effect was removed at index %d", index))
71         removeEffectNative(index)
72     }
73 
74     // Rotates existing effect from index to another
rotateEffectAtnull75     fun rotateEffectAt(from: Int, to: Int) {
76         Log.d("INTERFACE", String.format("Effect was rotated from %d to %d", from, to))
77         rotateEffectNative(from, to)
78     }
79 
enablenull80     fun enable(enable: Boolean) {
81         Log.d("INTERFACE", "Enabling effects: $enable")
82         enablePassthroughNative(enable)
83     }
84 
85     // State of audio engine
createAudioEnginenull86     external fun createAudioEngine()
87 
88     external fun destroyAudioEngine()
89 
90     // These functions populate effectDescriptionMap
91     private external fun getEffects(): Array<EffectDescription>
92 
93     // These functions mutate the function list
94     // Adds effect at index
95     private external fun addDefaultEffectNative(id: Int)
96 
97     private external fun removeEffectNative(index: Int)
98 
99     private external fun rotateEffectNative(from: Int, to: Int)
100 
101     private external fun modifyEffectNative(id: Int, index: Int, params: FloatArray)
102 
103     private external fun enableEffectNative(index: Int, enable: Boolean)
104 
105     private external fun enablePassthroughNative(enable: Boolean)
106 
107     // These are utility functions
108     private fun convertEffectToId(effect: Effect): Int =
109         effectDescriptionMap[effect.name]?.id ?: -1
110 }
111