Skip to content

Commit

Permalink
run update meters on handler post
Browse files Browse the repository at this point in the history
  • Loading branch information
Shaji Khan committed Mar 22, 2024
1 parent cc75325 commit 053bb4f
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 46 deletions.
9 changes: 9 additions & 0 deletions app/src/main/cpp/Meter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -464,3 +464,12 @@ void Meter::process (int nframes, const float * data, bool isInput) {
*/
}

float Meter::rms(float *v, int n)
{
int i;
float sum = 0.0;
for(i = 0; i < n; i++)
sum += v[i] * v[i];
return sum / n;
}

2 changes: 2 additions & 0 deletions app/src/main/cpp/Meter.h
Original file line number Diff line number Diff line change
Expand Up @@ -219,6 +219,8 @@ class Meter {

public:
static unsigned char *audioToVideoBytes;

static float rms(float *v, int n);
};

#endif //AMP_RACK_METER_H
8 changes: 7 additions & 1 deletion app/src/main/java/com/shajikhan/ladspa/amprack/Camera2.java
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,14 @@ public void onInputBufferAvailable(@NonNull MediaCodec codec, int index) {
MainActivity.AVBuffer avBuffer = mainActivity.avBuffer.pop();
ByteBuffer buffer = codec.getInputBuffer(index);

for (int i = 0; i < avBuffer.size; i++)
for (int i = 0; i < avBuffer.size; i++) {
if (avBuffer.bytes[i] > 1.0f)
avBuffer.bytes[i] = 0.99f;
if (avBuffer.bytes[i] < -1.0f)
avBuffer.bytes[i] = -0.99f;

buffer.putShort((short) (avBuffer.bytes[i] * 32767.0));
}

codec.queueInputBuffer(index, 0, avBuffer.size * 2, timestamp.get(), 0);
} else
Expand Down
102 changes: 57 additions & 45 deletions app/src/main/java/com/shajikhan/ladspa/amprack/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
import android.media.AudioManager;
import android.media.AudioRecord;
import android.media.AudioTrack;
import android.media.MediaCodec;
import android.media.MediaCodecInfo;
import android.media.MediaCodecList;
import android.media.MediaPlayer;
Expand Down Expand Up @@ -122,21 +121,13 @@
import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileDescriptor;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Arrays;
Expand Down Expand Up @@ -2736,6 +2727,7 @@ static void setMixerMeter (float inputValue, float outputValue) {
static void setTuner (float [] data, int size) {
if (! mainActivity.tunerEnabled)
return ;

double freq = pitch.computePitchFrequency(data);
String note = " - " ;
double cents = 0 ;
Expand Down Expand Up @@ -2779,7 +2771,14 @@ static void setMixerMeterSwitch (float inputValue, boolean isInput) {
return;
// Log.d(TAG, "setMixerMeterSwitch() called with: inputValue = [" + inputValue + "], isInput = [" + isInput + "]");
if (isInput) {
inputMeter.setProgress((int) (inputValue * 100));
mainActivity.handler.post(() -> {
if (inputValue > 1f)
inputMeter.setProgressTintList(ColorStateList.valueOf(Color.RED));
else
inputMeter.setProgressTintList(ColorStateList.valueOf(Color.GREEN));

inputMeter.setProgress((int) (inputValue * 100));
});
/*
if (tunerBuffer.size() < 32) {
Log.d(TAG, tunerBuffer.size() + ": setMixerMeter: " + inputValue);
Expand Down Expand Up @@ -2821,47 +2820,55 @@ public void run() {
*/
}
else {
outputMeter.setProgress((int) (inputValue * 100));
mainActivity.handler.post(() -> {
outputMeter.setProgress((int) (inputValue * 100));
// Log.d(TAG, "setMixerMeterSwitch: " + inputValue);
if (mainActivity.triggerRecord) {
if (mainActivity.recording) {
if (inputValue < 0.1) {
if (mainActivity.triggerRecord) {
if (mainActivity.recording) {
if (inputValue < 0.1) {
// mainActivity.record.setChecked(false);
AudioEngine.toggleRecording(false);
mainActivity.recording = false;
mainActivity.triggerRecord = false;
outputMeter.setProgressTintList(ColorStateList.valueOf(Color.GREEN));
}
} else {
if (inputValue > 0.3) {
outputMeter.setProgressTintList(ColorStateList.valueOf(Color.RED));
// mainActivity.record.setChecked(true);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy_HH.mm.ss");
Date date = new Date();
mainActivity.lastRecordedFileName = formatter.format(date);
mainActivity.lastRecordedFileName = mainActivity.dir.getAbsolutePath() + "/" + mainActivity.lastRecordedFileName ;
AudioEngine.setFileName(mainActivity.lastRecordedFileName);
switch (mainActivity.exportFormat) {
case "0":
default:
mainActivity.lastRecordedFileName = mainActivity.lastRecordedFileName + ".wav" ;
break ;
case "1":
mainActivity.lastRecordedFileName = mainActivity.lastRecordedFileName + ".ogg" ;
break ;
case "2":
mainActivity.lastRecordedFileName = mainActivity.lastRecordedFileName + ".mp3" ;
break ;
AudioEngine.toggleRecording(false);
mainActivity.recording = false;
mainActivity.triggerRecord = false;
outputMeter.setProgressTintList(ColorStateList.valueOf(Color.GREEN));
}
} else {
if (inputValue > 0.3) {
outputMeter.setProgressTintList(ColorStateList.valueOf(Color.RED));
// mainActivity.record.setChecked(true);
SimpleDateFormat formatter = new SimpleDateFormat("dd-MM-yyyy_HH.mm.ss");
Date date = new Date();
mainActivity.lastRecordedFileName = formatter.format(date);
mainActivity.lastRecordedFileName = mainActivity.dir.getAbsolutePath() + "/" + mainActivity.lastRecordedFileName;
AudioEngine.setFileName(mainActivity.lastRecordedFileName);
switch (mainActivity.exportFormat) {
case "0":
default:
mainActivity.lastRecordedFileName = mainActivity.lastRecordedFileName + ".wav";
break;
case "1":
mainActivity.lastRecordedFileName = mainActivity.lastRecordedFileName + ".ogg";
break;
case "2":
mainActivity.lastRecordedFileName = mainActivity.lastRecordedFileName + ".mp3";
break;
}

mainActivity.triggerRecordedSomething = true ;
mainActivity.recording = true;
Log.d(TAG, "setMixerMeterSwitch: triggering recording");
AudioEngine.toggleRecording(true);
mainActivity.triggerRecordedSomething = true;
mainActivity.recording = true;
Log.d(TAG, "setMixerMeterSwitch: triggering recording");
AudioEngine.toggleRecording(true);
// mainActivity.triggerRecordToggle.setChecked(false);
}
}
} else {
if (inputValue > 1f)
outputMeter.setProgressTintList(ColorStateList.valueOf(Color.RED));
else
outputMeter.setProgressTintList(ColorStateList.valueOf(Color.GREEN));

}
}
});

}
}
Expand Down Expand Up @@ -3519,5 +3526,10 @@ public static Bitmap getBitmapFromAsset(AssetManager mgr, String path) {
return bitmap;
}


public static double rootMeanSquare(float[] nums) {
double sum = 0.0;
for (double num : nums)
sum += num * num;
return Math.sqrt(sum / nums.length);
}
}

0 comments on commit 053bb4f

Please sign in to comment.