1 /*
2 * Copyright 2020 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 "AudioSource.h"
19
20 //TODO - Probably wrap the JNI handling in a class with a pointer held in the Java Object
21 // so as to support multiple instances... maybe.
22
23 // JNI Stuff
24 static float* sAudioBuffer;
25
26 extern "C" {
27
28 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_player_NativeAudioSource_initN(JNIEnv * env,jobject thiz,jlong native_source_ptr,jint num_frames,jint num_chans)29 Java_org_hyphonate_megaaudio_player_NativeAudioSource_initN(
30 JNIEnv *env, jobject thiz, jlong native_source_ptr, jint num_frames, jint num_chans) {
31 sAudioBuffer = new float[num_frames * num_chans];
32 }
33
34 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_player_NativeAudioSource_resetN(JNIEnv * env,jobject thiz,jlong native_source_ptr)35 Java_org_hyphonate_megaaudio_player_NativeAudioSource_resetN(
36 JNIEnv *env, jobject thiz, jlong native_source_ptr) {
37 // TODO: implement reset()
38 }
39
40 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_player_NativeAudioSource_triggerN(JNIEnv * env,jobject thiz,jlong native_source_ptr)41 Java_org_hyphonate_megaaudio_player_NativeAudioSource_triggerN(
42 JNIEnv *env, jobject thiz, jlong native_source_ptr) {
43 AudioSource* audioSource = (AudioSource*)native_source_ptr;
44 audioSource->trigger();
45 }
46
47 JNIEXPORT jint JNICALL
Java_org_hyphonate_megaaudio_player_NativeAudioSource_pullN(JNIEnv * env,jobject thiz,jlong native_source_ptr,jfloatArray audio_data,jint num_frames,jint num_chans)48 Java_org_hyphonate_megaaudio_player_NativeAudioSource_pullN(
49 JNIEnv *env, jobject thiz, jlong native_source_ptr,
50 jfloatArray audio_data, jint num_frames, jint num_chans) {
51 AudioSource* audioSource = (AudioSource*)native_source_ptr;
52 int numFrames = audioSource->pull(sAudioBuffer, num_frames, num_chans);
53
54 // Convert to Java float[]
55 env->SetFloatArrayRegion(audio_data, 0, numFrames * num_chans, sAudioBuffer);
56
57 return numFrames;
58 }
59
60 } // extern "C"
61