Skip to content

Commit

Permalink
Release version
Browse files Browse the repository at this point in the history
  • Loading branch information
lincollincol committed Aug 26, 2020
1 parent 2f99573 commit 03c2b54
Show file tree
Hide file tree
Showing 4 changed files with 127 additions and 28 deletions.
2 changes: 1 addition & 1 deletion app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ float getSample(const AVCodecContext* codecCtx, uint8_t* buffer, int sampleIndex

extern "C" JNIEXPORT jstring JNICALL

Java_linc_com_amplituda_Amplituda_stringFromJNI(
Java_linc_com_amplituda_Amplituda_amplitudesFromAudioJNI(
JNIEnv* env,
jobject,
jstring audio_path
Expand Down
124 changes: 101 additions & 23 deletions app/src/main/java/linc/com/amplituda/Amplituda.java
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);

}
27 changes: 24 additions & 3 deletions example/src/main/java/linc/com/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@

import android.os.Bundle;

import java.io.FileWriter;
import java.io.IOException;
import java.util.Arrays;
import java.util.List;

import linc.com.amplituda.Amplituda;

public class MainActivity extends AppCompatActivity {
Expand All @@ -13,8 +18,24 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

new Amplituda().init((log) -> {
System.out.println("MY log " + log);
});
new Amplituda().fromPath("/storage/emulated/0/Music/Linc - Amplituda.mp3")
.amplitudesAsJson(json -> {
System.out.println("As json ====== " + json);
}).amplitudesAsList(list -> {
System.out.print("As list ====== ");
for(int tmp : list) {
System.out.print(tmp + " ");
}
System.out.println();
})
.amplitudesAsSequence(Amplituda.SINGLE_LINE_SEQUENCE_FORMAT, defSeq -> {
System.out.println("As sequence default ====== " + defSeq);
})
.amplitudesAsSequence(Amplituda.SINGLE_LINE_SEQUENCE_FORMAT, " * ", custSeq -> {
System.out.println("As sequence custom ====== " + custSeq);
})
.amplitudesAsSequence(Amplituda.NEW_LINE_SEQUENCE_FORMAT, newLineSeq -> {
System.out.println("As new line sequence ====== " + newLineSeq);
});
}
}
2 changes: 1 addition & 1 deletion example/src/main/res/values/strings.xml
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>

0 comments on commit 03c2b54

Please sign in to comment.