Skip to content

Commit

Permalink
Remove runnable from logs
Browse files Browse the repository at this point in the history
  • Loading branch information
lincollincol committed Aug 1, 2021
1 parent 831cf45 commit b84663c
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 70 deletions.
53 changes: 37 additions & 16 deletions app/src/main/java/linc/com/amplituda/Amplituda.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand All @@ -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 {
Expand All @@ -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;
}

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

This file was deleted.

30 changes: 5 additions & 25 deletions example/src/main/java/linc/com/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,37 +22,17 @@ 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!");
} else if(error instanceof AmplitudaProcessingException) {
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);
Expand All @@ -71,6 +51,6 @@ protected void onCreate(Bundle savedInstanceState) {
})
.amplitudesForSecond(1, amps -> {
System.out.print("For second: " + Arrays.toString(amps.toArray()));
});*/
});
}
}

0 comments on commit b84663c

Please sign in to comment.