Skip to content

Commit

Permalink
Merge pull request #39 from lincollincol/dev
Browse files Browse the repository at this point in the history
Consumer rules. Bug fixes
  • Loading branch information
lincollincol authored Feb 19, 2022
2 parents 5809215 + de8c4bb commit adda93a
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 54 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ android {
minSdkVersion 21
targetSdkVersion 30
versionCode 1
versionName "1.8"

versionName "2.1.3"
project.archivesBaseName = "Amplituda"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
consumerProguardFiles 'consumer-rules.pro'

Expand Down
23 changes: 23 additions & 0 deletions app/consumer-rules.pro
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
# Add project specific ProGuard rules here.
# You can control the set of applied configuration files using the
# proguardFiles setting in build.gradle.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html

# If your project uses WebView with JS, uncomment the following
# and specify the fully qualified class name to the JavaScript interface
# class:
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
# public *;
#}

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile

-keep class linc.com.amplituda.** { *; }
10 changes: 7 additions & 3 deletions app/src/main/cpp/native-lib.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -366,9 +366,9 @@ Java_linc_com_amplituda_Amplituda_amplitudesFromAudioJNI(

// read frames from the file
while (av_read_frame(fmt_ctx, pkt) >= 0) {

bool is_audio_stream = pkt->stream_index == audio_stream_idx;
// check if the packet belongs to a stream we are interested in, otherwise skip it
if (pkt->stream_index == audio_stream_idx) {
if (is_audio_stream) {
ret = decode_packet(audio_dec_ctx, pkt, &temp_data, &errors_data);

// compress data when current_frame_idx is compression_divider
Expand Down Expand Up @@ -396,7 +396,11 @@ Java_linc_com_amplituda_Amplituda_amplitudesFromAudioJNI(
current_progress = progress;
}
}
current_frame_idx++;

// Count only audio stream frames (this will prevent +100% progress for video processing)
if(is_audio_stream) {
current_frame_idx++;
}
}

// make one last progress listener call
Expand Down
91 changes: 87 additions & 4 deletions app/src/main/java/linc/com/amplituda/Amplituda.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import android.webkit.URLUtil;

import java.io.File;
import java.io.InputStream;

import linc.com.amplituda.exceptions.*;
import linc.com.amplituda.exceptions.io.*;
Expand Down Expand Up @@ -41,6 +42,14 @@ public AmplitudaProcessingOutput<Integer> processAudio(final int audio) {
return processAudio(audio, null, null);
}

public AmplitudaProcessingOutput<InputStream> processAudio(final InputStream audio) {
return processAudio(audio, null, null);
}

public AmplitudaProcessingOutput<byte[]> processAudio(final byte[] audio) {
return processAudio(audio, null, null);
}

/** Audio file + compress params */

public AmplitudaProcessingOutput<File> processAudio(final File audio, final Compress compressParams) {
Expand All @@ -55,6 +64,14 @@ public AmplitudaProcessingOutput<Integer> processAudio(final int audio, final Co
return processAudio(audio, compressParams, null);
}

public AmplitudaProcessingOutput<InputStream> processAudio(final InputStream audio, final Compress compressParams) {
return processAudio(audio, compressParams, null);
}

public AmplitudaProcessingOutput<byte[]> processAudio(final byte[] audio, final Compress compressParams) {
return processAudio(audio, compressParams, null);
}

/** Audio file + progress listener */

public AmplitudaProcessingOutput<File> processAudio(final File audio, final AmplitudaProgressListener listener) {
Expand All @@ -69,6 +86,14 @@ public AmplitudaProcessingOutput<Integer> processAudio(final int audio, final Am
return processAudio(audio, null, listener);
}

public AmplitudaProcessingOutput<InputStream> processAudio(final InputStream audio, final AmplitudaProgressListener listener) {
return processAudio(audio, null, listener);
}

public AmplitudaProcessingOutput<byte[]> processAudio(final byte[] audio, final AmplitudaProgressListener listener) {
return processAudio(audio, null, listener);
}

public AmplitudaProcessingOutput<File> processAudio(
final File audio,
final Compress compress,
Expand Down Expand Up @@ -195,6 +220,68 @@ public AmplitudaProcessingOutput<Integer> processAudio(
}
}

/**
* Calculate amplitudes from file
* @param audio - uri source file
*/
public AmplitudaProcessingOutput<InputStream> processAudio(
final InputStream audio,
final Compress compress,
final AmplitudaProgressListener listener
) {
startProgress(listener);
InputAudio<InputStream> inputAudio = new InputAudio<>(audio, InputAudio.Type.INPUT_STREAM);
try {
updateProgressOperation(listener, ProgressOperation.DECODING);
File audioFile = fileManager.getUriFile(audio, listener);
AmplitudaProcessingOutput<InputStream> output = new AmplitudaProcessingOutput<>(
processFileJNI(
audioFile,
inputAudio,
getValidCompression(compress),
listener
),
inputAudio
);
fileManager.deleteFile(audioFile);
return output;
} catch (AmplitudaException exception) {
// Handle processing error
return errorOutput(exception, inputAudio, listener);
}
}

/**
* Calculate amplitudes from file
* @param audio - uri source file
*/
public AmplitudaProcessingOutput<byte[]> processAudio(
final byte[] audio,
final Compress compress,
final AmplitudaProgressListener listener
) {
startProgress(listener);
InputAudio<byte[]> inputAudio = new InputAudio<>(audio, InputAudio.Type.BYTE_ARRAY);
try {
updateProgressOperation(listener, ProgressOperation.DECODING);
File audioFile = fileManager.getByteArrayFile(audio, listener);
AmplitudaProcessingOutput<byte[]> output = new AmplitudaProcessingOutput<>(
processFileJNI(
audioFile,
inputAudio,
getValidCompression(compress),
listener
),
inputAudio
);
fileManager.deleteFile(audioFile);
return output;
} catch (AmplitudaException exception) {
// Handle processing error
return errorOutput(exception, inputAudio, listener);
}
}

/**
* Calculate amplitudes from file
* @param audio - source file
Expand All @@ -210,10 +297,6 @@ private <T> AmplitudaResultJNI processFileJNI(
throw new FileNotFoundException();
}

if(!fileManager.isAudioFile(audio.getPath())) {
throw new FileOpenException();
}

// Save start time
long startTime = System.currentTimeMillis();

Expand Down
65 changes: 36 additions & 29 deletions app/src/main/java/linc/com/amplituda/FileManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import android.content.Context;
import android.content.res.Resources;
import android.media.MediaMetadataRetriever;
import android.net.Uri;
import android.webkit.MimeTypeMap;

import java.io.BufferedInputStream;
Expand All @@ -12,15 +12,14 @@
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.util.Locale;


final class FileManager {

static final String RAW_TEMP = "amplituda_tmp_raw";
static final String AMPLITUDA_INTERNAL_TEMP = "amplituda_internal_temp";
private final Resources resources;
private final String cache;

Expand All @@ -29,23 +28,6 @@ final class FileManager {
cache = context.getCacheDir().getPath() + File.separator;
}

/**
* Validate audio file
* @param path - audio file path
* @return true when file with path is audio file.
*/
synchronized boolean isAudioFile(final String path) {
try {
MediaMetadataRetriever mediaMetadataRetriever = new MediaMetadataRetriever();
mediaMetadataRetriever.setDataSource(path);
return mediaMetadataRetriever.extractMetadata(
MediaMetadataRetriever.METADATA_KEY_HAS_AUDIO
).equalsIgnoreCase("yes");
} catch (Exception ignored) {
return false;
}
}

/**
* Delete local storage file
*/
Expand All @@ -61,7 +43,7 @@ synchronized void deleteFile(final File file) {
* @return raw file from local storage
*/
synchronized File getRawFile(final int resource, final AmplitudaProgressListener listener) {
File temp = new File(cache, RAW_TEMP);
File temp = new File(cache, AMPLITUDA_INTERNAL_TEMP);
try {
InputStream inputStream = resources.openRawResource(resource);
streamToFile(inputStream, temp, 1024 * 4, inputStream.available(), listener);
Expand All @@ -77,14 +59,7 @@ synchronized File getRawFile(final int resource, final AmplitudaProgressListener
* @return audio file from local storage
*/
synchronized File getUrlFile(final String audioUrl, final AmplitudaProgressListener listener) {
File temp = new File(String.format(
Locale.US,
"%s%s.%s",
cache,
RAW_TEMP,
MimeTypeMap.getFileExtensionFromUrl(audioUrl)
));

File temp = new File(cache, AMPLITUDA_INTERNAL_TEMP);
try {
URL url = new URL(audioUrl);
URLConnection connection = url.openConnection();
Expand All @@ -103,6 +78,38 @@ synchronized File getUrlFile(final String audioUrl, final AmplitudaProgressListe
return temp;
}

/**
* Copy audio from Uri to local storage
* @param audioStream - audio file stream
* @return audio file from local storage
*/
synchronized File getUriFile(final InputStream audioStream, final AmplitudaProgressListener listener) {
File temp = new File(cache, AMPLITUDA_INTERNAL_TEMP);
try {
streamToFile(audioStream, temp, 1024 * 4, audioStream.available(), listener);
return temp;
} catch (Resources.NotFoundException | IOException ignored) {
return null;
}
}

/**
* Copy audio from Uri to local storage
* @param audioByteArray - audio file stream
* @return audio file from local storage
*/
synchronized File getByteArrayFile(final byte[] audioByteArray, final AmplitudaProgressListener listener) {
File temp = new File(cache, AMPLITUDA_INTERNAL_TEMP);
try (FileOutputStream outputStream = new FileOutputStream(temp)) {
listener.onProgressInternal(0);
outputStream.write(audioByteArray);
listener.onProgressInternal(100);
return temp;
} catch (IOException ignored) {
return null;
}
}

/**
* Copy audio from URL to local storage
* @param inputStream - audio file input stream
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/java/linc/com/amplituda/InputAudio.java
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@ void setType(Type type) {
}

public enum Type {
FILE, PATH, URL, RESOURCE
FILE, PATH, URL, RESOURCE, INPUT_STREAM, BYTE_ARRAY
}
}
8 changes: 0 additions & 8 deletions example/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,4 @@ dependencies {
implementation fileTree(dir: "libs", include: ["*.jar"])
implementation 'androidx.appcompat:appcompat:1.2.0'
implementation project(":app")



// ReactiveX
// def rx_version = "3.0.0"
// implementation "io.reactivex.rxjava3:rxandroid:$rx_version"
// implementation "io.reactivex.rxjava3:rxjava:$rx_version"

}
4 changes: 3 additions & 1 deletion example/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,6 @@

# If you keep the line number information, uncomment this to
# hide the original source file name.
#-renamesourcefileattribute SourceFile
#-renamesourcefileattribute SourceFile

-keep class linc.com.amplituda.** { *; }
1 change: 0 additions & 1 deletion example/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
package="linc.com.example">

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.INTERNET"/>

<application
Expand Down
7 changes: 2 additions & 5 deletions example/src/main/java/linc/com/example/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

import androidx.appcompat.app.AppCompatActivity;

import android.os.Build;
import android.os.Bundle;

import java.io.File;
import java.util.Arrays;
import java.util.Locale;
import java.util.concurrent.CompletableFuture;

import linc.com.amplituda.Amplituda;
import linc.com.amplituda.AmplitudaProgressListener;
Expand Down Expand Up @@ -47,14 +45,13 @@ public void onProgress(ProgressOperation operation, int progress) {
String currentOperation = "";
switch (operation) {
case PROCESSING: currentOperation = "Process audio"; break;
case DECODING: currentOperation = "Decode resource"; break;
case DECODING: currentOperation = "Decode audio"; break;
case DOWNLOADING: currentOperation = "Download audio from url"; break;
}
System.out.printf("%s: %d%% %n", currentOperation, progress);
}
}
).get(result -> { printResult(result);
}, exception -> { exception.printStackTrace(); });
).get(result -> printResult(result), exception -> exception.printStackTrace());
}

private void printResult(AmplitudaResult<?> result) {
Expand Down

0 comments on commit adda93a

Please sign in to comment.