From 03c2b5443f56383ef6e43c910bece78c02c3de57 Mon Sep 17 00:00:00 2001 From: Andrew Date: Wed, 26 Aug 2020 17:52:13 +0300 Subject: [PATCH] Release version --- app/src/main/cpp/native-lib.cpp | 2 +- .../java/linc/com/amplituda/Amplituda.java | 124 ++++++++++++++---- .../java/linc/com/example/MainActivity.java | 27 +++- example/src/main/res/values/strings.xml | 2 +- 4 files changed, 127 insertions(+), 28 deletions(-) diff --git a/app/src/main/cpp/native-lib.cpp b/app/src/main/cpp/native-lib.cpp index 0b09a9b..90ecc5f 100644 --- a/app/src/main/cpp/native-lib.cpp +++ b/app/src/main/cpp/native-lib.cpp @@ -66,7 +66,7 @@ float getSample(const AVCodecContext* codecCtx, uint8_t* buffer, int sampleIndex extern "C" JNIEXPORT jstring JNICALL -Java_linc_com_amplituda_Amplituda_stringFromJNI( +Java_linc_com_amplituda_Amplituda_amplitudesFromAudioJNI( JNIEnv* env, jobject, jstring audio_path diff --git a/app/src/main/java/linc/com/amplituda/Amplituda.java b/app/src/main/java/linc/com/amplituda/Amplituda.java index d4ebe98..2553a51 100644 --- a/app/src/main/java/linc/com/amplituda/Amplituda.java +++ b/app/src/main/java/linc/com/amplituda/Amplituda.java @@ -1,45 +1,123 @@ package linc.com.amplituda; -import android.content.Context; +import android.text.TextUtils; +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.UnknownFormatFlagsException; public class Amplituda { - static { - System.loadLibrary("native-lib"); + public static int SINGLE_LINE_SEQUENCE_FORMAT = 0; + public static int NEW_LINE_SEQUENCE_FORMAT = 1; + + private String resultLog; + + /** + * Calculate amplitudes from file + * @param audio - source file + */ + public Amplituda fromFile(File audio) { + fromPath(audio.getPath()); + return this; } - public void init(SuccessCallback successCallback) { -// System.out.println("AMPLITUDE ===================== " + stringFromJNI()); - successCallback.onSuccess(stringFromJNI("/storage/emulated/0/viber/kygo.mp3")); - // const char *filename = "/storage/emulated/0/viber/kygo.mp3"; -// const char *filename = "/storage/emulated/0/viber/ex.wav"; -// const char *filename = "/storage/emulated/0/Android/data/org.thunderdog.challegram/files/music/f_voip_dur.opus"; -// const char *filename = "/storage/emulated/0/Android/data/org.thunderdog.challegram/files/music/Голос 0017325996153317080688.m4a"; -// const char *filename = "/storage/emulated/0/Android/data/org.thunderdog.challegram/files/video_notes/5406735884265457666.mp4"; -// const char *filename = "/storage/emulated/0/Android/data/org.thunderdog.challegram/files/voice/5382102159468791418.oga"; -// const char *filename = "/storage/emulated/0/Android/data/org.thunderdog.challegram/files/music/DiscDj_Rec_2020-06-21_18-38-44.mp3"; - // SUPPORTED AUDIO FORMATS - // [mp3, opus, oga, ogg, m4a, mp4] / 1h audio processing = 30sec + /** + * Calculate amplitudes from file + * @param audioPath - path to source file + */ + public Amplituda fromPath(String audioPath) { + resultLog = amplitudesFromAudioJNI(audioPath); + return this; + } + /** + * Convert result amplitudes to List + * @param listCallback - result callback + */ + public Amplituda amplitudesAsList(ListCallback listCallback) { + String[] log = resultLog.split("\n"); + List amplitudes = new ArrayList<>(); + for (String amplitude : log) { + amplitudes.add(Integer.valueOf(amplitude)); + } + listCallback.onSuccess(amplitudes); + return this; } - public Amplituda withContext(Context context) { + /** + * Convert result amplitudes to JSON format + * @param jsonCallback - result callback + */ + public Amplituda amplitudesAsJson(StringCallback jsonCallback) { + jsonCallback.onSuccess("[" + amplitudesToSingleLineSequence(resultLog, ", ") + "]"); return this; } - public Amplituda fromFile() { + + /** + * Overload for amplitudesAsSequence method. Use space (" ") as a default delimiter + * @param format - output format: single line or multiline output string + * @param stringCallback - result callback + */ + public Amplituda amplitudesAsSequence(int format, StringCallback stringCallback) { + amplitudesAsSequence(format, " ", stringCallback); return this; } - public Amplituda fromPath() { + + /** + * Convert result amplitudes to single line string with custom delimiter and send result to user via stringCallback + * @param format - output format: single line or multiline output string + * @param singleLineDelimiter - delimiter between amplitudes. WARNING: this parameter will be ignored when NEW_LINE_SEQUENCE_FORMAT passed as a parameter + * @param stringCallback - result callback + */ + public Amplituda amplitudesAsSequence(int format, String singleLineDelimiter, StringCallback stringCallback) { + switch (format) { + case 0: { + stringCallback.onSuccess(amplitudesToSingleLineSequence(resultLog, singleLineDelimiter)); + break; + } + case 1: { + stringCallback.onSuccess(resultLog); + break; + } + default: throw new UnknownFormatFlagsException("Use SINGLE_LINE_SEQUENCE_FORMAT or NEW_LINE_SEQUENCE_FORMAT as a parameter when you call amplitudesAsSequence!"); + } return this; } + + /** + * Convert result amplitudes to single line string with delimiter + * @param amplitudes - result from native c++ code + * @param delimiter - amplitudes separator + * @return string from amplitudes with custom delimiter. Example -> 0, 1, 2 | delimiter = ", " + */ + private String amplitudesToSingleLineSequence(String amplitudes, String delimiter) { + String[] log = amplitudes.split("\n"); + return TextUtils.join(delimiter, log); + } + /** - * A native method that is implemented by the 'native-lib' native library, - * which is packaged with this application. + * Base Callback interface */ - native String stringFromJNI(String pathToAudio); + private interface AmplitudaCallback { void onSuccess(T amplitudesResult);} - public interface SuccessCallback { - void onSuccess(String log); + /** + * Callback interface for list output + */ + public interface ListCallback extends AmplitudaCallback> {} + + /** + * Callback interface for string output + */ + public interface StringCallback extends AmplitudaCallback {} + + /** + * NDK part + */ + static { + System.loadLibrary("native-lib"); } + native String amplitudesFromAudioJNI(String pathToAudio); + } diff --git a/example/src/main/java/linc/com/example/MainActivity.java b/example/src/main/java/linc/com/example/MainActivity.java index cc297cf..8483297 100644 --- a/example/src/main/java/linc/com/example/MainActivity.java +++ b/example/src/main/java/linc/com/example/MainActivity.java @@ -4,6 +4,11 @@ import android.os.Bundle; +import java.io.FileWriter; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; + import linc.com.amplituda.Amplituda; public class MainActivity extends AppCompatActivity { @@ -13,8 +18,24 @@ protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); - new Amplituda().init((log) -> { - System.out.println("MY log " + log); - }); + new Amplituda().fromPath("/storage/emulated/0/Music/Linc - Amplituda.mp3") + .amplitudesAsJson(json -> { + System.out.println("As json ====== " + json); + }).amplitudesAsList(list -> { + System.out.print("As list ====== "); + for(int tmp : list) { + System.out.print(tmp + " "); + } + System.out.println(); + }) + .amplitudesAsSequence(Amplituda.SINGLE_LINE_SEQUENCE_FORMAT, defSeq -> { + System.out.println("As sequence default ====== " + defSeq); + }) + .amplitudesAsSequence(Amplituda.SINGLE_LINE_SEQUENCE_FORMAT, " * ", custSeq -> { + System.out.println("As sequence custom ====== " + custSeq); + }) + .amplitudesAsSequence(Amplituda.NEW_LINE_SEQUENCE_FORMAT, newLineSeq -> { + System.out.println("As new line sequence ====== " + newLineSeq); + }); } } diff --git a/example/src/main/res/values/strings.xml b/example/src/main/res/values/strings.xml index e16a3ca..87bffa3 100644 --- a/example/src/main/res/values/strings.xml +++ b/example/src/main/res/values/strings.xml @@ -1,3 +1,3 @@ - example + Amplituda Example \ No newline at end of file