diff --git a/app/src/main/java/linc/com/amplituda/Amplituda.java b/app/src/main/java/linc/com/amplituda/Amplituda.java index 5d824a9..339a9b8 100644 --- a/app/src/main/java/linc/com/amplituda/Amplituda.java +++ b/app/src/main/java/linc/com/amplituda/Amplituda.java @@ -10,7 +10,6 @@ import java.util.LinkedHashMap; import java.util.LinkedList; import java.util.List; -import java.util.Locale; import java.util.Map; import linc.com.amplituda.callback.ErrorListener; @@ -28,6 +27,9 @@ public final class Amplituda { public static final int SECONDS = 2; public static final int MILLIS = 3; + private static final String OPERATION_PROCESSING = "Processing"; + private static final String OPERATION_DECODING = "Decoding"; + private final ErrorListener errorListener; private final FileManager fileManager; private final List errors = new LinkedList<>(); @@ -62,25 +64,17 @@ public synchronized Amplituda fromFile(final File audio) { throwException(new FileOpenException()); return this; } - // Save time before processing - long start = System.currentTimeMillis(); - - // Process input audio - AmplitudaResultJNI result = amplitudesFromAudioJNI(audio.getPath()); - // Log processing time - AmplitudaLogger.log(String.format( - Locale.getDefault(), - "Processing time: %.04f seconds", - ((System.currentTimeMillis() - start) / 1000f)) - ); + // Save current audio path + fileManager.retainPath(audio.getPath()); - // Copy result data - amplitudes = result.getAmplitudes(); - errors.addAll(result.getErrors()); - fileManager.stashPath(audio.getPath()); - // Emit all exceptions after subscribe - handleAmplitudaErrors(); + // Process input audio + AmplitudaLogger.logOperationTime(OPERATION_PROCESSING, new Runnable() { + @Override + public void run() { + handleAmplitudaResult(amplitudesFromAudioJNI(audio.getPath())); + } + }); } return this; } @@ -317,7 +311,7 @@ public void call(List data) { * @param format - output time format: SECONDS or MILLIS */ public long getDuration(final int format) { - String inputAudioFile = fileManager.getStashedPath(); + String inputAudioFile = fileManager.getCachePath(); if(inputAudioFile == null) { throwException(new NoInputFileException()); @@ -337,6 +331,14 @@ public long getDuration(final int format) { return duration; } + private void handleAmplitudaResult(AmplitudaResultJNI result) { + // Copy result data + amplitudes = result.getAmplitudes(); + errors.addAll(result.getErrors()); + // Emit all exceptions after subscribe + handleAmplitudaErrors(); + } + /** * Convert result amplitudes to single line string with delimiter * @param amplitudes - result from native c++ code @@ -378,7 +380,7 @@ private synchronized void handleAmplitudaErrors() { private void clearPreviousAmplitudaData() { amplitudes = null; errors.clear(); - fileManager.clearStashedPath(); + fileManager.clearPath(); } /** diff --git a/app/src/main/java/linc/com/amplituda/AmplitudaLogger.java b/app/src/main/java/linc/com/amplituda/AmplitudaLogger.java index 7292a8e..a44d8f8 100644 --- a/app/src/main/java/linc/com/amplituda/AmplitudaLogger.java +++ b/app/src/main/java/linc/com/amplituda/AmplitudaLogger.java @@ -2,12 +2,28 @@ import android.util.Log; +import java.util.Locale; + public final class AmplitudaLogger { private static final String LIB_TAG = "AMPLITUDA"; private static int priority; private static boolean enable; + synchronized static void logOperationTime( + final String operationLabel, + final Runnable operationRunnable + ) { + long start = System.currentTimeMillis(); + operationRunnable.run(); + log(String.format( + Locale.getDefault(), + "%s time: %.04f seconds", + operationLabel, + ((System.currentTimeMillis() - start) / 1000f) + )); + } + /** * Print message to logcat * @param msg - message diff --git a/app/src/main/java/linc/com/amplituda/FileManager.java b/app/src/main/java/linc/com/amplituda/FileManager.java index c911371..2e16fd5 100644 --- a/app/src/main/java/linc/com/amplituda/FileManager.java +++ b/app/src/main/java/linc/com/amplituda/FileManager.java @@ -19,7 +19,7 @@ final class FileManager { private Resources resources; - private String stashedPath; + private String cachePath; private String cache; static final String RAW_TEMP = "amplituda_tmp_raw"; @@ -51,24 +51,24 @@ synchronized void deleteFile(final File file) { } /** - * Stash path to file + * Retain current audio path */ - synchronized void stashPath(final String path) { - stashedPath = path; + synchronized void retainPath(final String path) { + cachePath = path; } /** - * Clear stashed path + * Clear saved path */ - synchronized void clearStashedPath() { - stashedPath = ""; + synchronized void clearPath() { + cachePath = ""; } /** * Return stashed path */ - synchronized String getStashedPath() { - return stashedPath; + synchronized String getCachePath() { + return cachePath; } /** diff --git a/app/src/main/java/linc/com/amplituda/entity/AmplitudaProcessedEntity.java b/app/src/main/java/linc/com/amplituda/entity/AmplitudaProcessedEntity.java new file mode 100644 index 0000000..3b9eb41 --- /dev/null +++ b/app/src/main/java/linc/com/amplituda/entity/AmplitudaProcessedEntity.java @@ -0,0 +1,26 @@ +package linc.com.amplituda.entity; + +class AmplitudaProcessedEntity { + + private final String path; + private final int duration; + private final T data; + + AmplitudaProcessedEntity(final String path, final int duration, final T data) { + this.path = path; + this.duration = duration; + this.data = data; + } + + public String getPath() { + return path; + } + + public int getDuration() { + return duration; + } + + public T getData() { + return data; + } +} diff --git a/example/src/main/java/linc/com/example/MainActivity.java b/example/src/main/java/linc/com/example/MainActivity.java index efebde2..9ef3e94 100644 --- a/example/src/main/java/linc/com/example/MainActivity.java +++ b/example/src/main/java/linc/com/example/MainActivity.java @@ -21,10 +21,11 @@ protected void onCreate(Bundle savedInstanceState) { setContentView(R.layout.activity_main); Amplituda amplituda = new Amplituda.Builder() -// .enableExtendedProcessing(this) + .enableExtendedProcessing(this) .setErrorListener(error -> { error.printStackTrace(); }) + .setLogConfig(Log.ERROR, true) .build();