diff --git a/app/src/main/java/xdsopl/robot36/Decoder.java b/app/src/main/java/xdsopl/robot36/Decoder.java index 379055b..df0cf39 100644 --- a/app/src/main/java/xdsopl/robot36/Decoder.java +++ b/app/src/main/java/xdsopl/robot36/Decoder.java @@ -43,7 +43,7 @@ public class Decoder { private final int[] savedBuffer; private final int[] savedWidth; private final int[] savedHeight; - private final int[] volume; + private final float[] volume; private final RenderScript rs; private final Allocation rsDecoderAudioBuffer; @@ -110,7 +110,7 @@ public class Decoder { currentMode = new int[1]; savedWidth = new int[1]; savedHeight = new int[1]; - volume = new int[1]; + volume = new float[1]; savedBuffer = new int[pixelBuffer.length]; rs = RenderScript.create(activity.getApplicationContext()); @@ -121,7 +121,7 @@ public class Decoder { rsDecoderCurrentMode = Allocation.createSized(rs, Element.I32(rs), 1, Allocation.USAGE_SHARED | Allocation.USAGE_SCRIPT); rsDecoderSavedWidth = Allocation.createSized(rs, Element.I32(rs), 1, Allocation.USAGE_SHARED | Allocation.USAGE_SCRIPT); rsDecoderSavedHeight = Allocation.createSized(rs, Element.I32(rs), 1, Allocation.USAGE_SHARED | Allocation.USAGE_SCRIPT); - rsDecoderVolume = Allocation.createSized(rs, Element.I32(rs), 1, Allocation.USAGE_SHARED | Allocation.USAGE_SCRIPT); + rsDecoderVolume = Allocation.createSized(rs, Element.F32(rs), 1, Allocation.USAGE_SHARED | Allocation.USAGE_SCRIPT); rsDecoderSavedBuffer = Allocation.createSized(rs, Element.I32(rs), savedBuffer.length, Allocation.USAGE_SHARED | Allocation.USAGE_SCRIPT); rsDecoder = new ScriptC_decoder(rs); rsDecoder.bind_audio_buffer(rsDecoderAudioBuffer); @@ -249,7 +249,7 @@ public class Decoder { switch_mode(currentMode[0]); rsDecoderVolume.copyTo(volume); - meter.volume = volume[0] / 1023.0f; + meter.volume = volume[0]; rsDecoderSavedHeight.copyTo(savedHeight); if (savedHeight[0] > 0) { diff --git a/app/src/main/rs/decoder.rs b/app/src/main/rs/decoder.rs index 68e9f23..be3c0c2 100644 --- a/app/src/main/rs/decoder.rs +++ b/app/src/main/rs/decoder.rs @@ -212,14 +212,14 @@ void decode(int samples) { *saved_height = 0; for (int sample = 0; sample < samples; ++sample, ++buffer_pos) { int amp = audio_buffer[sample]; - float avg_rms = filter(&avg_amplitude, amp * amp); - float avg_amp = sqrt(2.0f * avg_rms); - *volume = clamp(avg_amp / 32.0f, 0.0f, 1023.0f); + float avg_pow = filter(&avg_power, amp * amp); + float avg_amp = min(sqrt(2.0f * avg_pow), 32767.0f); + *volume = avg_amp / 32767.0f; if (avg_amp < 16.0f) continue; - float norm_amp = amp / avg_amp; + float norm_amp = (127 * amp) / avg_amp; - spectrum_analyzer(127.0f * norm_amp); + spectrum_analyzer(norm_amp); complex_t cnt_baseband = convert(&cnt_ddc, norm_amp); complex_t dat_baseband = convert(&dat_ddc, norm_amp); diff --git a/app/src/main/rs/exports.rsh b/app/src/main/rs/exports.rsh index a1ed8df..718d3b7 100644 --- a/app/src/main/rs/exports.rsh +++ b/app/src/main/rs/exports.rsh @@ -25,6 +25,6 @@ uchar4 *saved_buffer; int *saved_width; int *saved_height; int *current_mode; -int *volume; +float *volume; #endif \ No newline at end of file diff --git a/app/src/main/rs/initialization.rsh b/app/src/main/rs/initialization.rsh index 2aa90aa..0905404 100644 --- a/app/src/main/rs/initialization.rsh +++ b/app/src/main/rs/initialization.rsh @@ -87,7 +87,7 @@ void initialize(float rate, int length, int iw, int ih, int sw, int sh) const float dat_bandwidth = 800.0f; const float cnt_bandwidth = 200.0f; - avg_amplitude = ema_cutoff(10.0f, sample_rate); + avg_power = ema_cutoff(10.0f, sample_rate); leader_lowpass = ema_cutoff(100.0f, sample_rate); cnt_ddc = ddc(cnt_carrier, cnt_bandwidth, sample_rate); diff --git a/app/src/main/rs/state.rsh b/app/src/main/rs/state.rsh index 6107099..fc96d96 100644 --- a/app/src/main/rs/state.rsh +++ b/app/src/main/rs/state.rsh @@ -17,7 +17,7 @@ limitations under the License. #ifndef STATE_RSH #define STATE_RSH -static ema_t avg_amplitude, leader_lowpass; +static ema_t avg_power, leader_lowpass; static ddc_t cnt_ddc, dat_ddc; static fmd_t cnt_fmd, dat_fmd; static int disable_analyzer;