-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcpp-adapter.cpp
More file actions
143 lines (113 loc) · 4.08 KB
/
cpp-adapter.cpp
File metadata and controls
143 lines (113 loc) · 4.08 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
#include <jni.h>
#include "react-native-elementary.h"
#include "audioengine.h"
static std::unique_ptr<elementary::AudioEngine> audioEngine;
// Initialize the AudioEngine instance
extern "C"
JNIEXPORT void JNICALL
Java_com_elementary_ElementaryModule_nativeStartAudioEngine(JNIEnv *env, jclass type) {
audioEngine = std::make_unique<elementary::AudioEngine>();
}
extern "C"
JNIEXPORT void JNICALL
Java_com_elementary_ElementaryModule_nativeApplyInstructions(JNIEnv *env, jclass type, jstring instructions) {
if (audioEngine) {
const char *instrCStr = env->GetStringUTFChars(instructions, nullptr);
if (!instrCStr) {
return;
}
std::string instrStr(instrCStr);
env->ReleaseStringUTFChars(instructions, instrCStr);
auto jsonInstructions = elem::js::parseJSON(instrStr);
audioEngine->getRuntime().applyInstructions(jsonInstructions);
}
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_elementary_ElementaryModule_nativeGetSampleRate(JNIEnv *env, jclass type) {
return audioEngine.get() ? audioEngine->getSampleRate() : 0;
}
extern "C"
JNIEXPORT jint JNICALL
Java_com_elementary_ElementaryModule_nativeGetNumChannels(JNIEnv *env, jclass type) {
return audioEngine.get() ? audioEngine->getNumChannels() : 0;
}
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_elementary_ElementaryModule_nativeIsDeviceRunning(JNIEnv *env, jclass type) {
return audioEngine.get() ? static_cast<jboolean>(audioEngine->isDeviceRunning()) : JNI_FALSE;
}
extern "C"
JNIEXPORT void JNICALL
Java_com_elementary_ElementaryModule_nativeStopDevice(JNIEnv *env, jclass type) {
if (audioEngine) {
audioEngine->stopDevice();
}
}
extern "C"
JNIEXPORT void JNICALL
Java_com_elementary_ElementaryModule_nativeStartDevice(JNIEnv *env, jclass type) {
if (audioEngine) {
audioEngine->startDevice();
}
}
extern "C"
JNIEXPORT jobject JNICALL
Java_com_elementary_ElementaryModule_nativeLoadAudioResource(JNIEnv *env, jclass type, jstring key, jstring filePath) {
if (!audioEngine) {
return nullptr;
}
const char *keyCStr = env->GetStringUTFChars(key, nullptr);
const char *filePathCStr = env->GetStringUTFChars(filePath, nullptr);
if (!keyCStr || !filePathCStr) {
if (keyCStr) env->ReleaseStringUTFChars(key, keyCStr);
if (filePathCStr) env->ReleaseStringUTFChars(filePath, filePathCStr);
return nullptr;
}
std::string keyStr(keyCStr);
std::string filePathStr(filePathCStr);
env->ReleaseStringUTFChars(key, keyCStr);
env->ReleaseStringUTFChars(filePath, filePathCStr);
// Load the audio resource
elementary::AudioLoadResult result = audioEngine->loadAudioResource(keyStr, filePathStr);
// Find the AudioResourceInfo class
jclass infoClass = env->FindClass("com/elementary/AudioResourceInfo");
if (!infoClass) {
return nullptr;
}
// Get the constructor
jmethodID constructor = env->GetMethodID(infoClass, "<init>", "(ZLjava/lang/String;Ljava/lang/String;IJID)V");
if (!constructor) {
return nullptr;
}
// Create the result object
jstring jKey = env->NewStringUTF(result.info.key.c_str());
jstring jError = env->NewStringUTF(result.error.c_str());
jobject infoObj = env->NewObject(
infoClass,
constructor,
static_cast<jboolean>(result.success),
jError,
jKey,
static_cast<jint>(result.info.channels),
static_cast<jlong>(result.info.sampleCount),
static_cast<jint>(result.info.sampleRate),
static_cast<jdouble>(result.info.durationMs)
);
return infoObj;
}
extern "C"
JNIEXPORT jboolean JNICALL
Java_com_elementary_ElementaryModule_nativeUnloadAudioResource(JNIEnv *env, jclass type, jstring key) {
if (!audioEngine) {
return JNI_FALSE;
}
const char *keyCStr = env->GetStringUTFChars(key, nullptr);
if (!keyCStr) {
return JNI_FALSE;
}
std::string keyStr(keyCStr);
env->ReleaseStringUTFChars(key, keyCStr);
bool result = audioEngine->unloadAudioResource(keyStr);
return result ? JNI_TRUE : JNI_FALSE;
}