Repository navigation
Expand file tree
/
Copy pathAutoMixBridge.cpp
More file actions
397 lines (351 loc) · 11.1 KB
/
Copy pathAutoMixBridge.cpp
File metadata and controls
397 lines (351 loc) · 11.1 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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
#include "AutoMixEngine.h"
#include "TransitionPlanner.h"
#include <jni.h>
#include <cstdint>
#include <new>
#include <vector>
namespace {
using lastwave::audio::AutoMixEngine;
using lastwave::audio::AutoMixState;
using lastwave::audio::TransitionPlan;
using lastwave::audio::TransitionPlanner;
using lastwave::audio::PhraseLengthMode;
using lastwave::audio::LyricTimeInterval;
AutoMixEngine* fromHandle(jlong handle) noexcept {
return reinterpret_cast<AutoMixEngine*>(static_cast<std::uintptr_t>(handle));
}
} // namespace
extern "C" {
JNIEXPORT jlong JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeCreate(
JNIEnv*,
jclass) {
const auto ptr = reinterpret_cast<std::uintptr_t>(new (std::nothrow) AutoMixEngine());
return static_cast<jlong>(ptr);
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeDestroy(
JNIEnv*,
jclass,
jlong handle) {
delete fromHandle(handle);
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeConfigure(
JNIEnv*,
jclass,
jlong handle,
jdouble sampleRate) {
auto* engine = fromHandle(handle);
if (engine != nullptr) {
engine->configure(sampleRate);
}
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeSetEnabled(
JNIEnv*,
jclass,
jlong handle,
jboolean enabled) {
auto* engine = fromHandle(handle);
if (engine != nullptr) {
engine->setEnabled(enabled == JNI_TRUE);
}
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeSetBarMode(
JNIEnv*,
jclass,
jlong handle,
jint bars) {
auto* engine = fromHandle(handle);
if (engine != nullptr) {
engine->setBarMode(bars);
}
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeSetPaused(
JNIEnv*,
jclass,
jlong handle,
jboolean paused) {
auto* engine = fromHandle(handle);
if (engine != nullptr) {
engine->setPaused(paused == JNI_TRUE);
}
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeSetDeckBVocalsActive(
JNIEnv*,
jclass,
jlong handle,
jboolean active) {
auto* engine = fromHandle(handle);
if (engine != nullptr) {
engine->setDeckBVocalsActive(active == JNI_TRUE);
}
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeSetDeckAVocalFree(
JNIEnv*,
jclass,
jlong handle,
jboolean free) {
auto* engine = fromHandle(handle);
if (engine != nullptr) {
engine->setDeckAVocalFree(free == JNI_TRUE);
}
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeTriggerFadeNow(
JNIEnv*,
jclass,
jlong handle) {
auto* engine = fromHandle(handle);
if (engine != nullptr) {
engine->triggerFadeNow();
}
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeStartTransition(
JNIEnv*,
jclass,
jlong handle,
jint durationMs) {
auto* engine = fromHandle(handle);
if (engine != nullptr) {
engine->startTransition(static_cast<int>(durationMs));
}
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeSetTransitionProgress(
JNIEnv*,
jclass,
jlong handle,
jfloat progress) {
auto* engine = fromHandle(handle);
if (engine != nullptr) {
engine->setTransitionProgress(static_cast<float>(progress));
}
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeReset(
JNIEnv*,
jclass,
jlong handle) {
auto* engine = fromHandle(handle);
if (engine != nullptr) {
engine->reset();
}
}
JNIEXPORT jint JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeWriteDeckBPcm(
JNIEnv* env,
jclass,
jlong handle,
jobject directBuffer,
jint offsetBytes,
jint frameCount) {
auto* engine = fromHandle(handle);
if (engine == nullptr || directBuffer == nullptr || frameCount <= 0 || offsetBytes < 0) {
return 0;
}
auto* bufferAddress = static_cast<std::uint8_t*>(env->GetDirectBufferAddress(directBuffer));
if (bufferAddress == nullptr) {
return 0;
}
const auto* pcm = reinterpret_cast<const float*>(bufferAddress + offsetBytes);
return static_cast<jint>(engine->writeDeckBPcm(pcm, static_cast<std::size_t>(frameCount)));
}
JNIEXPORT void JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeProcess(
JNIEnv* env,
jclass,
jlong handle,
jobject directBuffer,
jint offsetBytes,
jint frameCount) {
auto* engine = fromHandle(handle);
if (engine == nullptr || directBuffer == nullptr || frameCount <= 0 || offsetBytes < 0) {
return;
}
auto* bufferAddress = static_cast<std::uint8_t*>(env->GetDirectBufferAddress(directBuffer));
if (bufferAddress == nullptr) {
return;
}
auto* pcm = reinterpret_cast<float*>(bufferAddress + offsetBytes);
engine->process(pcm, static_cast<std::size_t>(frameCount));
}
JNIEXPORT jint JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetState(
JNIEnv*,
jclass,
jlong handle) {
auto* engine = fromHandle(handle);
return (engine != nullptr) ? static_cast<jint>(engine->getState()) : 0;
}
JNIEXPORT jfloat JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetTransitionProgress(
JNIEnv*,
jclass,
jlong handle) {
auto* engine = fromHandle(handle);
return (engine != nullptr) ? engine->getTransitionProgress() : 0.0F;
}
JNIEXPORT jlong JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetDeckBLeadingTrimFrames(
JNIEnv*,
jclass,
jlong handle) {
auto* engine = fromHandle(handle);
return (engine != nullptr) ? static_cast<jlong>(engine->getDeckBLeadingTrimFrames()) : 0L;
}
JNIEXPORT jfloat JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetDeckBAvailableSeconds(
JNIEnv*,
jclass,
jlong handle) {
auto* engine = fromHandle(handle);
return (engine != nullptr) ? engine->getDeckBAvailableSeconds() : 0.0F;
}
JNIEXPORT jlong JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetDeckBAvailableFrames(
JNIEnv*,
jclass,
jlong handle) {
auto* engine = fromHandle(handle);
return (engine != nullptr) ? static_cast<jlong>(engine->getDeckBAvailableFrames()) : 0L;
}
JNIEXPORT jfloat JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetDeckABpm(
JNIEnv*,
jclass,
jlong handle) {
auto* engine = fromHandle(handle);
return (engine != nullptr) ? engine->getDeckABpm() : 120.0F;
}
JNIEXPORT jfloat JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetDeckBBpm(
JNIEnv*,
jclass,
jlong handle) {
auto* engine = fromHandle(handle);
return (engine != nullptr) ? engine->getDeckBBpm() : 120.0F;
}
JNIEXPORT jlong JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetDeckBFirstBeatMs(
JNIEnv*,
jclass,
jlong handle) {
auto* engine = fromHandle(handle);
return (engine != nullptr) ? static_cast<jlong>(engine->getDeckBFirstBeatMs()) : 0L;
}
JNIEXPORT jlong JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeGetDeckBDropBeatMs(
JNIEnv*,
jclass,
jlong handle) {
auto* engine = fromHandle(handle);
return (engine != nullptr) ? static_cast<jlong>(engine->getDeckBDropBeatMs()) : 0L;
}
JNIEXPORT jboolean JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativeProcessDeckBuffer(
JNIEnv* env,
jclass,
jlong handle,
jobject byteBuffer,
jint offset,
jint length,
jint encoding,
jint sampleRate,
jboolean isDeckA) {
auto* engine = fromHandle(handle);
if (engine == nullptr || byteBuffer == nullptr || length <= 0) return JNI_FALSE;
auto* directAddress = static_cast<std::uint8_t*>(env->GetDirectBufferAddress(byteBuffer));
if (directAddress == nullptr) return JNI_FALSE;
std::size_t bytesPerSample = 2;
switch (encoding) {
case 0: bytesPerSample = 2; break; // PCM_I16
case 1: bytesPerSample = 3; break; // PCM_I24_PACKED
case 2: bytesPerSample = 4; break; // PCM_I32
case 3: bytesPerSample = 4; break; // PCM_FLOAT
default: return JNI_FALSE;
}
const std::size_t bytesPerFrame = bytesPerSample * 2; // stereo
if (bytesPerFrame == 0) return JNI_FALSE;
const std::size_t frameCount = static_cast<std::size_t>(length) / bytesPerFrame;
if (frameCount == 0) return JNI_FALSE;
const bool ok = engine->processDeckBuffer(
directAddress + offset,
frameCount,
encoding,
sampleRate,
isDeckA == JNI_TRUE);
return ok ? JNI_TRUE : JNI_FALSE;
}
JNIEXPORT jdoubleArray JNICALL
Java_com_lastwave_app_playback_NativeAutoMixEngine_nativePlanTransition(
JNIEnv* env,
jclass,
jlong handle,
jlong durationMsA,
jlong durationMsB,
jdouble sampleRate,
jint phraseModeInt,
jboolean isManualSkip,
jlong currentDeckAPosMs,
jlongArray lyricsAIntervals, // pairs of [startMs, endMs]
jlongArray lyricsBIntervals
) {
auto* engine = fromHandle(handle);
std::vector<LyricTimeInterval> intervalsA;
if (lyricsAIntervals != nullptr) {
const jsize lenA = env->GetArrayLength(lyricsAIntervals);
std::vector<jlong> bufA(lenA);
env->GetLongArrayRegion(lyricsAIntervals, 0, lenA, bufA.data());
for (jsize i = 0; i + 1 < lenA; i += 2) {
intervalsA.push_back({static_cast<int64_t>(bufA[i]), static_cast<int64_t>(bufA[i + 1]), false});
}
}
std::vector<LyricTimeInterval> intervalsB;
if (lyricsBIntervals != nullptr) {
const jsize lenB = env->GetArrayLength(lyricsBIntervals);
std::vector<jlong> bufB(lenB);
env->GetLongArrayRegion(lyricsBIntervals, 0, lenB, bufB.data());
for (jsize i = 0; i + 1 < lenB; i += 2) {
intervalsB.push_back({static_cast<int64_t>(bufB[i]), static_cast<int64_t>(bufB[i + 1]), false});
}
}
const auto mode = static_cast<PhraseLengthMode>(phraseModeInt);
TransitionPlan plan = TransitionPlanner::planTransition(
static_cast<int64_t>(durationMsA),
nullptr, 0, sampleRate, intervalsA,
static_cast<int64_t>(durationMsB),
nullptr, 0, sampleRate, intervalsB,
mode,
isManualSkip == JNI_TRUE,
static_cast<int64_t>(currentDeckAPosMs)
);
if (engine != nullptr) {
engine->setTransitionPlan(plan);
}
jdouble results[11] = {
static_cast<double>(plan.outCueMs),
static_cast<double>(plan.inCueMs),
static_cast<double>(plan.durationMs),
static_cast<double>(plan.bars),
static_cast<double>(plan.bpmA),
static_cast<double>(plan.bpmB),
static_cast<double>(plan.bpmRatio),
static_cast<double>(plan.gainOffsetDb),
static_cast<double>(plan.textureFx),
plan.isVocalSafeLoop ? 1.0 : 0.0,
plan.isSkitSkipped ? 1.0 : 0.0
};
jdoubleArray outArr = env->NewDoubleArray(11);
if (outArr != nullptr) {
env->SetDoubleArrayRegion(outArr, 0, 11, results);
}
return outArr;
}
} // extern "C"