Skip to content

Commit

Permalink
NAM plugin UI
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaji Khan committed Apr 20, 2024
1 parent 2746051 commit 1ab4f8a
Show file tree
Hide file tree
Showing 4 changed files with 144 additions and 8 deletions.
15 changes: 14 additions & 1 deletion .idea/deploymentTargetDropDown.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

84 changes: 84 additions & 0 deletions app/src/main/java/com/shajikhan/ladspa/amprack/DataAdapter.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
import android.graphics.Typeface;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.text.LineBreaker;
import android.net.Uri;
import android.os.Environment;
import android.text.Editable;
import android.text.InputFilter;
import android.text.InputType;
Expand All @@ -31,6 +33,7 @@
import android.widget.ToggleButton;

import androidx.annotation.NonNull;
import androidx.documentfile.provider.DocumentFile;
import androidx.recyclerview.widget.RecyclerView;

import com.google.android.material.button.MaterialButton;
Expand All @@ -41,6 +44,7 @@
import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
Expand Down Expand Up @@ -659,6 +663,86 @@ public void onClick(View v) {
mainActivity.startActivityForResult(intent_upload,requestCode);
}
});

if (pluginName.equals("Neural Amp Modeler")) {
String dir = context.getExternalFilesDir(
Environment.DIRECTORY_DOWNLOADS) + "/NamModels";

DocumentFile root = DocumentFile.fromFile(new File(dir));
DocumentFile [] files = root.listFiles() ;
ArrayList <String> models = new ArrayList<>();
for (DocumentFile file: files) {
Log.d(TAG, String.format ("%s: %s", file.getName(), file.getUri()));
models.add(file.getName());
}

Spinner spinner = new Spinner(context);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(mainActivity,
android.R.layout.simple_spinner_item, models);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);

LinearLayout hz = new LinearLayout(mainActivity);
Button prev = new Button(context);
Button next = new Button(context);

prev.setText("<");
next.setText(">");

prev.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selected = spinner.getSelectedItemPosition();
if (selected > 0)
spinner.setSelection(selected - 1);
}
});

next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int selected = spinner.getSelectedItemPosition();
if (selected < adapter.getCount() - 1)
spinner.setSelection(selected + 1);
}
});

layout.addView(hz);
hz.addView(prev);
hz.addView(spinner);
hz.addView(next);

LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, ViewGroup.LayoutParams.WRAP_CONTENT);
next.setLayoutParams(layoutParams);
prev.setLayoutParams(layoutParams);

prev.setBackgroundColor(mainActivity.getResources().getColor(com.firebase.ui.auth.R.color.fui_transparent));
next.setBackgroundColor(mainActivity.getResources().getColor(com.firebase.ui.auth.R.color.fui_transparent));

LinearLayout.LayoutParams l3 = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
hz.setLayoutParams(l3);

LinearLayout.LayoutParams spinnerLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT, 1);
spinnerLayoutParams.setMargins(0,20,0,20);
spinner.setLayoutParams(spinnerLayoutParams);

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int _position, long id) {
String m = adapter.getItem(_position);
String dir = context.getExternalFilesDir(
Environment.DIRECTORY_DOWNLOADS) + "/NamModels/";
String s = mainActivity.getFileContent(Uri.parse(dir + m));
Log.d(TAG, String.format("[content]: %s", s));
AudioEngine.setPluginFilename(s, holder.getAdapterPosition());
}

@Override
public void onNothingSelected(AdapterView<?> parent) {

}
});
}
}

if (mainActivity.useTheme) {
Expand Down
51 changes: 44 additions & 7 deletions app/src/main/java/com/shajikhan/ladspa/amprack/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import androidx.core.app.NotificationManagerCompat;
import androidx.core.content.FileProvider;
import androidx.core.splashscreen.SplashScreen;
import androidx.documentfile.provider.DocumentFile;
import androidx.fragment.app.Fragment;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.LinearLayoutManager;
Expand Down Expand Up @@ -130,6 +131,7 @@
import java.io.BufferedReader;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -1688,13 +1690,22 @@ public void onActivityResult(int requestCode, int resultCode, Intent data) {
String mimeType = getContentResolver().getType(returnUri);
if (!mimeType.startsWith("audio")) {
String path = returnUri.getPath();
if (path != null) {
Log.d(TAG, String.format ("setFileName: %s", returnUri.getPath()));
String s = getFileContent(returnUri);
Log.d(TAG, String.format ("[content]: %s", s));
AudioEngine.setPluginFilename(s, plugin);
if (path == null)
return;
String ext = path.substring(path.toString().lastIndexOf('.')+1) ;
Log.d(TAG, "onActivityResult: got mime type " + mimeType + ": " + path + " (" + ext + ")");
switch (ext.toLowerCase()) {
case "nam":
Log.d(TAG, String.format("setFileName: %s", returnUri.getPath()));
String s = getFileContent(returnUri);
Log.d(TAG, String.format("[content]: %s", s));
AudioEngine.setPluginFilename(s, plugin);
toast("Loaded model " + path);
return;
case "zip":
unzipNAMModel(returnUri);
return;
}
return;
}
}

Expand Down Expand Up @@ -3680,7 +3691,12 @@ public static abstract class OnEngineStartListener {
public String getFileContent(Uri uri) {
InputStreamReader inputStreamReader = null;
try {
inputStreamReader = new InputStreamReader(getContentResolver().openInputStream(uri));
if (! uri.getPath().startsWith("/"))
inputStreamReader = new InputStreamReader(getContentResolver().openInputStream(uri));
else {
inputStreamReader = new InputStreamReader(new FileInputStream(new File(String.valueOf(uri))));
}

} catch (FileNotFoundException e) {
Log.e(TAG, "getFileContent: ", e);
throw new RuntimeException(e);
Expand All @@ -3702,4 +3718,25 @@ public String getFileContent(Uri uri) {
String fileContent = sb.toString();
return fileContent;
}

public void unzipNAMModel (Uri uri) {
InputStream inputStream = null;
String basename = null ;
String dir = context.getExternalFilesDir(
Environment.DIRECTORY_DOWNLOADS) + "/NamModels";
try {
inputStream = getContentResolver().openInputStream(uri);
} catch (FileNotFoundException e) {
MainActivity.alert("Cannot load model", e.getMessage());
Log.e(TAG, "onActivityResult: ", e);
}
try {
basename = SkinEngine.unzip(inputStream, dir);
} catch (IOException e) {
MainActivity.alert("Cannot unzip model", e.getMessage());
Log.e(TAG, "onActivityResult: ", e);
}

alert("Extracted Model " + basename, "Model was saved to " + dir);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1917,11 +1917,13 @@ public static String unzip(InputStream zipFilePath, String destDirectory) throws
ZipEntry entry = zipIn.getNextEntry();
// iterates over entries in the zip file
while (entry != null) {
Log.d("SkinEngine", String.format ("[unzip] %s: %s", destDirectory, entry.getName()));
String filePath = destDirectory + File.separator + entry.getName();
if (dirname == null)
dirname = entry.getName();
if (!entry.isDirectory()) {
// if the entry is a file, extracts it
// Log.d("Skin Engine", String.format ("%s: %s", destDirectory, entry.getName()));
extractFile(zipIn, filePath);
} else {
// if the entry is a directory, make the directory
Expand Down

0 comments on commit 1ab4f8a

Please sign in to comment.