forked from timschneeb/RootlessJamesDSP
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJamesDspLocalEngine.kt
More file actions
310 lines (280 loc) · 9.25 KB
/
JamesDspLocalEngine.kt
File metadata and controls
310 lines (280 loc) · 9.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
package me.timschneeberger.rootlessjamesdsp.interop
import android.content.Context
import android.content.Intent
import me.timschneeberger.rootlessjamesdsp.interop.structure.EelVmVariable
import me.timschneeberger.rootlessjamesdsp.utils.Constants
import me.timschneeberger.rootlessjamesdsp.utils.extensions.ContextExtensions.sendLocalBroadcast
import timber.log.Timber
import java.util.Timer
import kotlin.concurrent.schedule
class JamesDspLocalEngine(context: Context, callbacks: JamesDspWrapper.JamesDspCallbacks? = null) : JamesDspBaseEngine(context, callbacks) {
var handle: JamesDspHandle = JamesDspWrapper.alloc(callbacks ?: DummyCallbacks())
override var sampleRate: Float
set(value) {
super.sampleRate = value
JamesDspWrapper.setSamplingRate(handle, value, true)
context.sendLocalBroadcast(Intent(Constants.ACTION_SAMPLE_RATE_UPDATED))
}
get() = super.sampleRate
override var enabled: Boolean = true
init {
if(BenchmarkManager.hasBenchmarksCached())
BenchmarkManager.loadBenchmarksFromCache()
}
override fun close() {
val oldHandle = handle
handle = 0
// Make sure ongoing async calls to native have enough time to finish
Timer().schedule(100) {
JamesDspWrapper.free(oldHandle)
Timber.d("Handle $oldHandle has been freed")
}
}
// Processing
fun processInt16(input: ShortArray, output: ShortArray, offset: Int = -1, length: Int = -1)
{
if(!enabled || handle == 0L)
{
if(offset < 0 && length < 0) {
input.copyInto(output)
}
else {
input.copyInto(output, 0, offset, offset + length)
}
}
else {
JamesDspWrapper.processInt16(handle, input, output, offset, length)
}
}
fun processInt32(input: IntArray, output: IntArray, offset: Int = -1, length: Int = -1)
{
if(!enabled || handle == 0L)
{
if(offset < 0 && length < 0) {
input.copyInto(output)
}
else {
input.copyInto(output, 0, offset, offset + length)
}
}
else {
JamesDspWrapper.processInt32(handle, input, output, offset, length)
}
}
fun processFloat(input: FloatArray, output: FloatArray, offset: Int = -1, length: Int = -1)
{
if(!enabled || handle == 0L)
{
if(offset < 0 && length < 0) {
input.copyInto(output)
}
else {
input.copyInto(output, 0, offset, offset + length)
}
}
else {
JamesDspWrapper.processFloat(handle, input, output, offset, length)
}
}
// Effect config
override fun setOutputControl(threshold: Float, release: Float, postGain: Float): Boolean {
return JamesDspWrapper.setLimiter(handle, threshold, release) and JamesDspWrapper.setPostGain(handle, postGain)
}
override fun setReverb(enable: Boolean, preset: Int): Boolean
{
return JamesDspWrapper.setReverb(handle, enable, preset)
}
override fun setCrossfeed(enable: Boolean, mode: Int): Boolean
{
return JamesDspWrapper.setCrossfeed(handle, enable, mode, 0, 0)
}
override fun setCrossfeedCustom(enable: Boolean, fcut: Int, feed: Int): Boolean
{
return JamesDspWrapper.setCrossfeed(handle, enable, 99, fcut, feed)
}
override fun setBassBoost(enable: Boolean, maxGain: Float): Boolean
{
return JamesDspWrapper.setBassBoost(handle, enable, maxGain)
}
override fun setStereoEnhancement(enable: Boolean, level: Float): Boolean
{
return JamesDspWrapper.setStereoEnhancement(handle, enable, level)
}
protected override fun setFieldSurroundInternal(
enable: Boolean,
outputMode: Int,
widening: Int,
midImage: Int,
depth: Int,
phaseOffset: Int,
monoSumMix: Int,
monoSumPan: Int,
delayLeftMs: Float,
delayRightMs: Float,
hpfFrequencyHz: Float,
hpfGainDb: Float,
hpfQ: Float,
branchThreshold: Int,
gainScaleDb: Float,
gainOffsetDb: Float,
gainCap: Float,
stereoFloor: Float,
stereoFallback: Float
): Boolean {
return JamesDspWrapper.setFieldSurround(
handle,
enable,
outputMode,
widening,
midImage,
depth,
phaseOffset,
monoSumMix,
monoSumPan,
delayLeftMs,
delayRightMs,
hpfFrequencyHz,
hpfGainDb,
hpfQ,
branchThreshold,
gainScaleDb,
gainOffsetDb,
gainCap,
stereoFloor,
stereoFallback
)
}
override fun setVacuumTube(enable: Boolean, level: Float): Boolean
{
return JamesDspWrapper.setVacuumTube(handle, enable, level)
}
override fun setMultiEqualizerInternal(
enable: Boolean,
filterType: Int,
interpolationMode: Int,
bands: DoubleArray
): Boolean {
// ViPER wrapper compatibility transport is 65551 (enable) + 65552 (band index + centi-dB).
// Local libjamesdsp has no safe per-command equivalent and applies EQ via one JNI payload,
// so we keep behavior parity by normalizing to canonical axes before forwarding.
return JamesDspWrapper.setMultiEqualizer(
handle,
enable,
filterType,
interpolationMode,
EqNormalization.normalizeMultiEqBands(filterType, bands, EQ_FILTER_TYPE_VIPER_ORIGINAL)
)
}
override fun setCompanderInternal(
enable: Boolean,
timeConstant: Float,
granularity: Int,
tfTransforms: Int,
bands: DoubleArray
): Boolean {
return JamesDspWrapper.setCompander(handle, enable, timeConstant, granularity, tfTransforms, bands)
}
override fun setVdcInternal(enable: Boolean, vdc: String): Boolean {
return JamesDspWrapper.setVdc(handle, enable, vdc)
}
override fun setConvolverInternal(
enable: Boolean,
impulseResponse: FloatArray,
irChannels: Int,
irFrames: Int,
irCrc: Int
): Boolean {
return JamesDspWrapper.setConvolver(handle, enable, impulseResponse, irChannels, irFrames)
}
override fun setGraphicEqInternal(enable: Boolean, bands: String): Boolean {
return JamesDspWrapper.setGraphicEq(handle, enable, bands)
}
override fun setLiveprogInternal(enable: Boolean, name: String, script: String): Boolean {
return JamesDspWrapper.setLiveprog(handle, enable, name, script)
}
override fun setSpectrumExtensionInternal(
enable: Boolean,
strengthLinear: Float,
referenceFreq: Int,
wetMix: Float,
wetOnlyMonitor: Boolean,
postGainDb: Float,
safetyEnabled: Boolean,
hpQ: Float,
lpQ: Float,
lpCutoffOffsetHz: Int,
harmonics: DoubleArray
): Boolean {
// Local engine talks to libjamesdsp directly, so ViPER transport IDs 65548/65549/65550
// are represented by this single JNI call instead of discrete parameter writes.
return JamesDspWrapper.setSpectrumExtension(
handle,
enable,
strengthLinear,
referenceFreq,
wetMix,
wetOnlyMonitor,
postGainDb,
safetyEnabled,
hpQ,
lpQ,
lpCutoffOffsetHz,
harmonics
)
}
override fun setClarityInternal(
enable: Boolean,
mode: Int,
gain: Float,
postGainDb: Float,
safetyEnabled: Boolean,
safetyThresholdDb: Float,
safetyReleaseMs: Float,
naturalLpfOffsetHz: Int,
ozoneFreqHz: Int,
xhifiLowCutHz: Int,
xhifiHighCutHz: Int,
xhifiHpMix: Float,
xhifiBpMix: Float,
xhifiBpDelayDivisor: Int,
xhifiLpDelayDivisor: Int
): Boolean {
return JamesDspWrapper.setClarity(
handle,
enable,
mode,
gain,
postGainDb,
safetyEnabled,
safetyThresholdDb,
safetyReleaseMs,
naturalLpfOffsetHz,
ozoneFreqHz,
xhifiLowCutHz,
xhifiHighCutHz,
xhifiHpMix,
xhifiBpMix,
xhifiBpDelayDivisor,
xhifiLpDelayDivisor
)
}
// Feature support
override fun supportsEelVmAccess(): Boolean { return true }
override fun supportsCustomCrossfeed(): Boolean { return true }
// EEL VM utilities
override fun enumerateEelVariables(): ArrayList<EelVmVariable>
{
return JamesDspWrapper.enumerateEelVariables(handle)
}
override fun manipulateEelVariable(name: String, value: Float): Boolean
{
return JamesDspWrapper.manipulateEelVariable(handle, name, value)
}
override fun freezeLiveprogExecution(freeze: Boolean)
{
JamesDspWrapper.freezeLiveprogExecution(handle, freeze)
}
companion object {
private const val EQ_FILTER_TYPE_VIPER_ORIGINAL = 6
}
}