-
Notifications
You must be signed in to change notification settings - Fork 31
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
2f99573
commit 03c2b54
Showing
4 changed files
with
127 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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<Integer> 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<T> { void onSuccess(T amplitudesResult);} | ||
|
||
public interface SuccessCallback { | ||
void onSuccess(String log); | ||
/** | ||
* Callback interface for list output | ||
*/ | ||
public interface ListCallback extends AmplitudaCallback<List<Integer>> {} | ||
|
||
/** | ||
* Callback interface for string output | ||
*/ | ||
public interface StringCallback extends AmplitudaCallback<String> {} | ||
|
||
/** | ||
* NDK part | ||
*/ | ||
static { | ||
System.loadLibrary("native-lib"); | ||
} | ||
|
||
native String amplitudesFromAudioJNI(String pathToAudio); | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
<resources> | ||
<string name="app_name">example</string> | ||
<string name="app_name">Amplituda Example</string> | ||
</resources> |