1 /**
2 * Copyright 2018 The Android Open Source Project
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 * http://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 #include <jni.h>
18 #include <logging_macros.h>
19 #include "LiveEffectEngine.h"
20
21 static const int kOboeApiAAudio = 0;
22 static const int kOboeApiOpenSLES = 1;
23
24 static LiveEffectEngine *engine = nullptr;
25
26 extern "C" {
27
28 JNIEXPORT jboolean JNICALL
Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_create(JNIEnv * env,jclass)29 Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_create(JNIEnv *env,
30 jclass) {
31 if (engine == nullptr) {
32 engine = new LiveEffectEngine();
33 }
34
35 return (engine != nullptr) ? JNI_TRUE : JNI_FALSE;
36 }
37
38 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_delete(JNIEnv * env,jclass)39 Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_delete(JNIEnv *env,
40 jclass) {
41 if (engine) {
42 engine->setEffectOn(false);
43 delete engine;
44 engine = nullptr;
45 }
46 }
47
48 JNIEXPORT jboolean JNICALL
Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_setEffectOn(JNIEnv * env,jclass,jboolean isEffectOn)49 Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_setEffectOn(
50 JNIEnv *env, jclass, jboolean isEffectOn) {
51 if (engine == nullptr) {
52 LOGE(
53 "Engine is null, you must call createEngine before calling this "
54 "method");
55 return JNI_FALSE;
56 }
57
58 return engine->setEffectOn(isEffectOn) ? JNI_TRUE : JNI_FALSE;
59 }
60
61 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_setRecordingDeviceId(JNIEnv * env,jclass,jint deviceId)62 Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_setRecordingDeviceId(
63 JNIEnv *env, jclass, jint deviceId) {
64 if (engine == nullptr) {
65 LOGE(
66 "Engine is null, you must call createEngine before calling this "
67 "method");
68 return;
69 }
70
71 engine->setRecordingDeviceId(deviceId);
72 }
73
74 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_setPlaybackDeviceId(JNIEnv * env,jclass,jint deviceId)75 Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_setPlaybackDeviceId(
76 JNIEnv *env, jclass, jint deviceId) {
77 if (engine == nullptr) {
78 LOGE(
79 "Engine is null, you must call createEngine before calling this "
80 "method");
81 return;
82 }
83
84 engine->setPlaybackDeviceId(deviceId);
85 }
86
87 JNIEXPORT jboolean JNICALL
Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_setAPI(JNIEnv * env,jclass type,jint apiType)88 Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_setAPI(JNIEnv *env,
89 jclass type,
90 jint apiType) {
91 if (engine == nullptr) {
92 LOGE(
93 "Engine is null, you must call createEngine "
94 "before calling this method");
95 return JNI_FALSE;
96 }
97
98 oboe::AudioApi audioApi;
99 switch (apiType) {
100 case kOboeApiAAudio:
101 audioApi = oboe::AudioApi::AAudio;
102 break;
103 case kOboeApiOpenSLES:
104 audioApi = oboe::AudioApi::OpenSLES;
105 break;
106 default:
107 LOGE("Unknown API selection to setAPI() %d", apiType);
108 return JNI_FALSE;
109 }
110
111 return engine->setAudioApi(audioApi) ? JNI_TRUE : JNI_FALSE;
112 }
113
114 JNIEXPORT jboolean JNICALL
Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_isAAudioRecommended(JNIEnv * env,jclass type)115 Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_isAAudioRecommended(
116 JNIEnv *env, jclass type) {
117 if (engine == nullptr) {
118 LOGE(
119 "Engine is null, you must call createEngine "
120 "before calling this method");
121 return JNI_FALSE;
122 }
123 return engine->isAAudioRecommended() ? JNI_TRUE : JNI_FALSE;
124 }
125
126 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_native_1setDefaultStreamValues(JNIEnv * env,jclass type,jint sampleRate,jint framesPerBurst)127 Java_com_google_oboe_samples_liveEffect_LiveEffectEngine_native_1setDefaultStreamValues(JNIEnv *env,
128 jclass type,
129 jint sampleRate,
130 jint framesPerBurst) {
131 oboe::DefaultStreamValues::SampleRate = (int32_t) sampleRate;
132 oboe::DefaultStreamValues::FramesPerBurst = (int32_t) framesPerBurst;
133 }
134 } // extern "C"
135