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 #include <jni.h>
17 
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_setSampleRateN(JNIEnv * env,jobject thiz,jlong native_source_ptr,jint sample_rate)35 Java_org_hyphonate_megaaudio_player_NativeAudioSource_setSampleRateN(
36         JNIEnv *env, jobject thiz, jlong native_source_ptr, jint sample_rate) {
37     AudioSource* audioSource = (AudioSource*)native_source_ptr;
38     audioSource->setSampleRate(sample_rate);
39 }
40 
41 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_player_NativeAudioSource_setFreqN(JNIEnv * env,jobject thiz,jlong native_source_ptr,jfloat freq)42 Java_org_hyphonate_megaaudio_player_NativeAudioSource_setFreqN(
43         JNIEnv *env, jobject thiz, jlong native_source_ptr, jfloat freq) {
44     AudioSource* audioSource = (AudioSource*)native_source_ptr;
45     audioSource->setFreq(freq);
46 }
47 
48 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_player_NativeAudioSource_resetN(JNIEnv * env,jobject thiz,jlong native_source_ptr)49 Java_org_hyphonate_megaaudio_player_NativeAudioSource_resetN(
50         JNIEnv *env, jobject thiz, jlong native_source_ptr) {
51     // TODO: implement reset()
52 }
53 
54 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_player_NativeAudioSource_triggerN(JNIEnv * env,jobject thiz,jlong native_source_ptr)55 Java_org_hyphonate_megaaudio_player_NativeAudioSource_triggerN(
56         JNIEnv *env, jobject thiz, jlong native_source_ptr) {
57     AudioSource* audioSource = (AudioSource*)native_source_ptr;
58     audioSource->trigger();
59 }
60 
61 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)62 Java_org_hyphonate_megaaudio_player_NativeAudioSource_pullN(
63         JNIEnv *env, jobject thiz, jlong native_source_ptr,
64         jfloatArray audio_data, jint num_frames, jint num_chans) {
65     AudioSource* audioSource = (AudioSource*)native_source_ptr;
66     int numFrames = audioSource->pull(sAudioBuffer, num_frames, num_chans);
67 
68     // Convert to Java float[]
69     env->SetFloatArrayRegion(audio_data, 0, numFrames * num_chans, sAudioBuffer);
70 
71     return numFrames;
72 }
73 
74 } // extern "C"
75