Repository navigation
Expand file tree
/
Copy pathentry.cpp
More file actions
141 lines (116 loc) · 3.99 KB
/
Copy pathentry.cpp
File metadata and controls
141 lines (116 loc) · 3.99 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
#include <cstdint>
#include <new>
#include <stdexcept>
#include <jni.h>
#include "audio_processor.h"
namespace
{
constexpr char kAudioProcessorNativeClassName[] = "me/ley/jloopback/AudioProcessorNative";
void throw_java_exception(JNIEnv* env, const char* class_name, const char* message)
{
if (env == nullptr || env->ExceptionCheck()) {
return;
}
jclass exception_class = env->FindClass(class_name);
if (exception_class == nullptr) {
return;
}
env->ThrowNew(exception_class, message);
env->DeleteLocalRef(exception_class);
}
void throw_runtime_exception(JNIEnv* env, const char* message)
{
throw_java_exception(env, "java/lang/RuntimeException", message);
}
void throw_illegal_state(JNIEnv* env, const char* message)
{
throw_java_exception(env, "java/lang/IllegalStateException", message);
}
c_audio_processor* from_handle(jlong handle)
{
return reinterpret_cast<c_audio_processor*>(static_cast<std::intptr_t>(handle));
}
jlong to_handle(c_audio_processor* processor)
{
return static_cast<jlong>(reinterpret_cast<std::intptr_t>(processor));
}
jlong JNICALL native_create(JNIEnv* env, jclass)
{
try {
return to_handle(new c_audio_processor());
} catch (const std::exception& ex) {
throw_runtime_exception(env, ex.what());
} catch (...) {
throw_runtime_exception(env, "native audio processor initialization failed");
}
return 0;
}
void JNICALL native_destroy(JNIEnv*, jclass, jlong handle)
{
delete from_handle(handle);
}
jboolean JNICALL native_update(JNIEnv* env, jclass, jlong handle)
{
c_audio_processor* processor = from_handle(handle);
if (processor == nullptr) {
throw_illegal_state(env, "audio processor handle is null");
return JNI_FALSE;
}
try {
return processor->update() ? JNI_TRUE : JNI_FALSE;
} catch (const std::exception& ex) {
throw_runtime_exception(env, ex.what());
} catch (...) {
throw_runtime_exception(env, "native audio update failed");
}
return JNI_FALSE;
}
jfloatArray JNICALL native_get_bands(JNIEnv* env, jclass, jlong handle)
{
c_audio_processor* processor = from_handle(handle);
if (processor == nullptr) {
throw_illegal_state(env, "audio processor handle is null");
return nullptr;
}
try {
const auto& bands = processor->get_bands();
jfloatArray result = env->NewFloatArray(static_cast<jsize>(bands.size()));
if (result == nullptr) {
return nullptr;
}
env->SetFloatArrayRegion(result, 0, static_cast<jsize>(bands.size()), bands.data());
return result;
} catch (const std::exception& ex) {
throw_runtime_exception(env, ex.what());
} catch (...) {
throw_runtime_exception(env, "native band retrieval failed");
}
return nullptr;
}
const JNINativeMethod kNativeMethods[] = {
{ const_cast<char*>("nativeCreate"), const_cast<char*>("()J"), reinterpret_cast<void*>(native_create) },
{ const_cast<char*>("nativeDestroy"), const_cast<char*>("(J)V"), reinterpret_cast<void*>(native_destroy) },
{ const_cast<char*>("nativeUpdate"), const_cast<char*>("(J)Z"), reinterpret_cast<void*>(native_update) },
{ const_cast<char*>("nativeGetBands"), const_cast<char*>("(J)[F"), reinterpret_cast<void*>(native_get_bands) },
};
} // namespace
extern "C" JNIEXPORT jint JNICALL JNI_OnLoad(JavaVM* vm, void*)
{
JNIEnv* env = nullptr;
if (vm == nullptr || vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_8) != JNI_OK || env == nullptr) {
return JNI_ERR;
}
jclass bridge_class = env->FindClass(kAudioProcessorNativeClassName);
if (bridge_class == nullptr) {
return JNI_ERR;
}
if (env->RegisterNatives(
bridge_class,
kNativeMethods,
static_cast<jint>(sizeof(kNativeMethods) / sizeof(kNativeMethods[0]))) != 0) {
env->DeleteLocalRef(bridge_class);
return JNI_ERR;
}
env->DeleteLocalRef(bridge_class);
return JNI_VERSION_1_8;
}