diff --git a/app/src/main/java/linc/com/amplituda/Amplituda.java b/app/src/main/java/linc/com/amplituda/Amplituda.java index 339a9b8..2b3380c 100644 --- a/app/src/main/java/linc/com/amplituda/Amplituda.java +++ b/app/src/main/java/linc/com/amplituda/Amplituda.java @@ -28,7 +28,7 @@ public final class Amplituda { public static final int MILLIS = 3; private static final String OPERATION_PROCESSING = "Processing"; - private static final String OPERATION_DECODING = "Decoding"; + private static final String OPERATION_PREPARING = "Preparing"; private final ErrorListener errorListener; private final FileManager fileManager; @@ -65,16 +65,24 @@ public synchronized Amplituda fromFile(final File audio) { return this; } + // Save start time + long startTime = System.currentTimeMillis(); + // Save current audio path fileManager.retainPath(audio.getPath()); // Process input audio - AmplitudaLogger.logOperationTime(OPERATION_PROCESSING, new Runnable() { - @Override - public void run() { - handleAmplitudaResult(amplitudesFromAudioJNI(audio.getPath())); - } - }); + AmplitudaResultJNI result = amplitudesFromAudioJNI(audio.getPath()); + + // Copy result data + amplitudes = result.getAmplitudes(); + errors.addAll(result.getErrors()); + + // Emit all exceptions after subscribe + handleAmplitudaErrors(); + + // Log operation time + AmplitudaLogger.logOperationTime(OPERATION_PROCESSING, startTime); } return this; } @@ -89,11 +97,21 @@ public Amplituda fromFile(final String audio) { throwException(new ExtendedProcessingDisabledException()); return this; } + + // Save start time + long startTime = System.currentTimeMillis(); + + // Copy audio from url to local storage File tempAudio = fileManager.getUrlFile(audio); if(tempAudio == null) { throwException(new InvalidAudioUrlException()); return this; } + + // Log operation time + AmplitudaLogger.logOperationTime(OPERATION_PREPARING, startTime); + + // Process local audio fromFile(tempAudio); fileManager.deleteFile(tempAudio); } else { @@ -106,18 +124,29 @@ public Amplituda fromFile(final String audio) { * Calculate amplitudes from file * @param rawId - path to source file */ - public Amplituda fromFile(int rawId) { + public Amplituda fromFile(final int rawId) { if(!fileManager.cacheNotNull()) { throwException(new ExtendedProcessingDisabledException()); return this; } + + // Save start time + long startTime = System.currentTimeMillis(); + + // Copy raw to local file File tempAudio = fileManager.getRawFile(rawId); if(tempAudio == null) { throwException(new InvalidRawResourceException()); return this; } + + // Log operation time + AmplitudaLogger.logOperationTime(OPERATION_PREPARING, startTime); + + // Process local raw file fromFile(tempAudio); fileManager.deleteFile(tempAudio); + return this; } @@ -331,14 +360,6 @@ 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 diff --git a/app/src/main/java/linc/com/amplituda/AmplitudaLogger.java b/app/src/main/java/linc/com/amplituda/AmplitudaLogger.java index a44d8f8..93d7017 100644 --- a/app/src/main/java/linc/com/amplituda/AmplitudaLogger.java +++ b/app/src/main/java/linc/com/amplituda/AmplitudaLogger.java @@ -10,12 +10,15 @@ public final class AmplitudaLogger { private static int priority; private static boolean enable; + /** + * Print message to logcat + * @param operationLabel - operation label for message + * @param start - operation start time in millis + */ synchronized static void logOperationTime( final String operationLabel, - final Runnable operationRunnable + final long start ) { - long start = System.currentTimeMillis(); - operationRunnable.run(); log(String.format( Locale.getDefault(), "%s time: %.04f seconds", diff --git a/app/src/main/java/linc/com/amplituda/FileManager.java b/app/src/main/java/linc/com/amplituda/FileManager.java index 2e16fd5..289569c 100644 --- a/app/src/main/java/linc/com/amplituda/FileManager.java +++ b/app/src/main/java/linc/com/amplituda/FileManager.java @@ -23,6 +23,9 @@ final class FileManager { private String cache; static final String RAW_TEMP = "amplituda_tmp_raw"; + /** + * Check not null for cache directory + */ boolean cacheNotNull() { return cache != null; } diff --git a/app/src/main/java/linc/com/amplituda/entity/AmplitudaProcessedEntity.java b/app/src/main/java/linc/com/amplituda/entity/AmplitudaProcessedEntity.java deleted file mode 100644 index 3b9eb41..0000000 --- a/app/src/main/java/linc/com/amplituda/entity/AmplitudaProcessedEntity.java +++ /dev/null @@ -1,26 +0,0 @@ -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 9ef3e94..bc6041b 100644 --- a/example/src/main/java/linc/com/example/MainActivity.java +++ b/example/src/main/java/linc/com/example/MainActivity.java @@ -22,29 +22,6 @@ protected void onCreate(Bundle savedInstanceState) { Amplituda amplituda = new Amplituda.Builder() .enableExtendedProcessing(this) - .setErrorListener(error -> { - error.printStackTrace(); - }) - .setLogConfig(Log.ERROR, true) - .build(); - - -// amplituda.fromFile("/storage/9016-4EF8/MUSIC/Hosini - Froozen.mp3") - Thread processing = new Thread(() -> { - amplituda.fromFile("http://commondatastorage.googleapis.com/codeskulptor-demos/DDR_assets/Kangaroo_MusiQue_-_The_Neverwritten_Role_Playing_Game.mp3"); - }); - - processing.start(); - try { - processing.join(); - amplituda.amplitudesAsJson(System.out::println); - } catch (InterruptedException e) { - e.printStackTrace(); - } - - - /*Amplituda amplituda = new Amplituda(this); - amplituda.fromFile("/storage/emulated/0/Music/Linc - Amplituda.mp3") .setErrorListener(error -> { if(error instanceof AmplitudaIOException) { System.out.println("IO exception!"); @@ -52,7 +29,10 @@ protected void onCreate(Bundle savedInstanceState) { System.out.println("Processing exception!"); } }) - .setLogConfig(Log.DEBUG, true) + .setLogConfig(Log.ERROR, true) + .build(); + + amplituda.fromFile("/storage/emulated/0/Music/Linc - Amplituda.mp3") .compressAmplitudes(1) .amplitudesAsJson(json -> { System.out.println("As json: " + json); @@ -71,6 +51,6 @@ protected void onCreate(Bundle savedInstanceState) { }) .amplitudesForSecond(1, amps -> { System.out.print("For second: " + Arrays.toString(amps.toArray())); - });*/ + }); } }