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 
19 #include "AudioSink.h"
20 
21 //TODO - Probably wrap the JNI handling in a class with a pointer held in the Java Object
22 // so as to support multiple instances... maybe.
23 
24 // JNI Stuff
25 extern "C" {
26 
27 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_initN(JNIEnv * env,jobject thiz,jlong native_sink_ptr,jint num_frames,jint num_chans)28 Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_initN(JNIEnv * env , jobject thiz,
29         jlong native_sink_ptr , jint num_frames, jint num_chans ) {
30     // this is in the wrong place, or rather we need an init() method of AudioSink to call.
31     AudioSink* sink = (AudioSink*)native_sink_ptr;
32     sink->init(num_frames, num_chans);
33 }
34 
35 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_startN(JNIEnv * env,jobject thiz,jlong native_sink_ptr)36 Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_startN(JNIEnv *env, jobject thiz, jlong native_sink_ptr) {
37     AudioSink* sink = (AudioSink*)native_sink_ptr;
38     sink->start();
39 }
40 
41 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_stopN(JNIEnv * env,jobject thiz,jlong native_sink_ptr)42 Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_stopN(JNIEnv *env, jobject thiz, jlong native_sink_ptr) {
43     AudioSink* sink = (AudioSink*)native_sink_ptr;
44     sink->stop();
45 }
46 
47 JNIEXPORT void JNICALL
Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_pushN(JNIEnv * env,jobject thiz,jlong native_sink_ptr,jfloatArray audio_data,jint num_frames,jint num_chans)48 Java_org_hyphonate_megaaudio_recorder_NativeAudioSink_pushN(
49         JNIEnv *env, jobject thiz, jlong native_sink_ptr,
50         jfloatArray audio_data, jint num_frames, jint num_chans) {
51         AudioSink * audioSink = (AudioSink*)native_sink_ptr;
52 
53     // convert to float[]
54     float* nativeAudioData = env->GetFloatArrayElements(audio_data, 0);
55 
56     audioSink->push(nativeAudioData, num_frames, num_chans);
57 }
58 
59 } // extern "C"
60