xref: /aosp_15_r20/external/oboe/samples/SoundBoard/src/main/cpp/native-lib.cpp (revision 05767d913155b055644481607e6fa1e35e2fe72c)
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 <string>
19 #include <vector>
20 
21 #include "SoundBoardEngine.h"
22 
23 extern "C" {
24 /**
25  * Start the audio engine
26  *
27  * @param env
28  * @param instance
29  * @param jCpuIds - CPU core IDs which the audio process should affine to
30  * @return a pointer to the audio engine. This should be passed to other methods
31  */
32 JNIEXPORT jlong JNICALL
Java_com_google_oboe_samples_soundboard_MainActivity_startEngine(JNIEnv * env,jobject,jint jNumSignals)33 Java_com_google_oboe_samples_soundboard_MainActivity_startEngine(JNIEnv *env, jobject /*unused*/,
34          jint jNumSignals) {
35     LOGD("numSignals : %d", static_cast<int>(jNumSignals));
36     SoundBoardEngine  *engine = new SoundBoardEngine(jNumSignals);
37 
38     if (!engine->start()) {
39         LOGE("Failed to start SoundBoard Engine");
40         delete engine;
41         engine = nullptr;
42     } else  {
43         LOGD("Engine Started");
44     }
45     return reinterpret_cast<jlong>(engine);
46 }
47 
48 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_soundboard_MainActivity_stopEngine(JNIEnv * env,jobject instance,jlong jEngineHandle)49 Java_com_google_oboe_samples_soundboard_MainActivity_stopEngine(JNIEnv *env, jobject instance,
50         jlong jEngineHandle) {
51     auto engine = reinterpret_cast<SoundBoardEngine*>(jEngineHandle);
52     if (engine) {
53         engine->stop();
54         delete engine;
55     } else {
56         LOGD("Engine invalid, call startEngine() to create");
57     }
58 }
59 
60 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_soundboard_MainActivity_native_1setDefaultStreamValues(JNIEnv * env,jclass type,jint sampleRate,jint framesPerBurst)61 Java_com_google_oboe_samples_soundboard_MainActivity_native_1setDefaultStreamValues(JNIEnv *env,
62                                                                             jclass type,
63                                                                             jint sampleRate,
64                                                                             jint framesPerBurst) {
65     oboe::DefaultStreamValues::SampleRate = (int32_t) sampleRate;
66     oboe::DefaultStreamValues::FramesPerBurst = (int32_t) framesPerBurst;
67 }
68 
69 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_soundboard_NoteListener_noteOff(JNIEnv * env,jobject thiz,jlong engine_handle,jint noteIndex)70 Java_com_google_oboe_samples_soundboard_NoteListener_noteOff(JNIEnv *env, jobject thiz,
71                                                          jlong engine_handle, jint noteIndex) {
72     auto *engine = reinterpret_cast<SoundBoardEngine*>(engine_handle);
73     if (engine) {
74         engine->noteOff(noteIndex);
75     } else {
76         LOGE("Engine handle is invalid, call createEngine() to create a new one");
77     }
78 }
79 
80 JNIEXPORT void JNICALL
Java_com_google_oboe_samples_soundboard_NoteListener_noteOn(JNIEnv * env,jobject thiz,jlong engine_handle,jint noteIndex)81 Java_com_google_oboe_samples_soundboard_NoteListener_noteOn(JNIEnv *env, jobject thiz,
82                                                          jlong engine_handle, jint noteIndex) {
83     auto *engine = reinterpret_cast<SoundBoardEngine*>(engine_handle);
84     if (engine) {
85         engine->noteOn(noteIndex);
86     } else {
87         LOGE("Engine handle is invalid, call createEngine() to create a new one");
88     }
89 }
90 
91 } // extern "C"
92