Skip to content

Commit

Permalink
Refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
lincollincol committed Aug 1, 2021
1 parent 71a1f1e commit 831cf45
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 30 deletions.
42 changes: 22 additions & 20 deletions app/src/main/java/linc/com/amplituda/Amplituda.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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<AmplitudaException> errors = new LinkedList<>();
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -317,7 +311,7 @@ public void call(List<Integer> 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());
Expand All @@ -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
Expand Down Expand Up @@ -378,7 +380,7 @@ private synchronized void handleAmplitudaErrors() {
private void clearPreviousAmplitudaData() {
amplitudes = null;
errors.clear();
fileManager.clearStashedPath();
fileManager.clearPath();
}

/**
Expand Down
16 changes: 16 additions & 0 deletions app/src/main/java/linc/com/amplituda/AmplitudaLogger.java
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
18 changes: 9 additions & 9 deletions app/src/main/java/linc/com/amplituda/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package linc.com.amplituda.entity;

class AmplitudaProcessedEntity<T> {

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;
}
}
3 changes: 2 additions & 1 deletion example/src/main/java/linc/com/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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();


Expand Down

0 comments on commit 831cf45

Please sign in to comment.