Skip to content

Commit

Permalink
little bit of gain
Browse files Browse the repository at this point in the history
  • Loading branch information
crc-32 committed Dec 2, 2024
1 parent a436039 commit 22b779b
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
10 changes: 10 additions & 0 deletions android/speex_codec/src/main/cpp/speex_codec.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
static jfieldID speexDecBits;
static jfieldID speexDecState;
static jfieldID speexPreprocessState;
static jfieldID gainField;

static const int FLAG_PREPROCESSOR_DENOISE = 1;
static const int FLAG_PREPROCESSOR_AGC = 2;
Expand All @@ -20,6 +21,7 @@ Java_com_example_speex_1codec_SpeexCodec_decode(JNIEnv *env, jobject thiz,
auto *out_frame_data = reinterpret_cast<spx_int16_t *>(env->GetDirectBufferAddress(out_frame));
auto *bits = reinterpret_cast<SpeexBits *>(env->GetLongField(thiz, speexDecBits));
auto *dec_state = reinterpret_cast<void *>(env->GetLongField(thiz, speexDecState));
auto gain = env->GetFloatField(thiz, gainField);
int offset = has_header_byte ? 1 : 0;
speex_bits_read_from(bits, reinterpret_cast<char *>(encoded_frame_data)+offset, encoded_frame_length-offset);
int result = speex_decode_int(dec_state, bits, out_frame_data);
Expand All @@ -29,6 +31,13 @@ Java_com_example_speex_1codec_SpeexCodec_decode(JNIEnv *env, jobject thiz,
if (preprocess_state != nullptr) {
speex_preprocess_run(preprocess_state, out_frame_data);
}
int output_size = 0;
speex_decoder_ctl(dec_state, SPEEX_GET_FRAME_SIZE, &output_size);
for (int i = 0; i < output_size; i++) {
float gained = static_cast<float>(out_frame_data[i]) * gain;
float gained_clipped = std::clamp(gained, static_cast<float>(SHRT_MIN), static_cast<float>(SHRT_MAX));
out_frame_data[i] = static_cast<spx_int16_t>(gained_clipped);
}
return result;
}
extern "C"
Expand Down Expand Up @@ -67,6 +76,7 @@ Java_com_example_speex_1codec_SpeexCodec_initNative(JNIEnv *env, jobject thiz) {
speexDecBits = env->GetFieldID(clazz, "speexDecBits", "J");
speexDecState = env->GetFieldID(clazz, "speexDecState", "J");
speexPreprocessState = env->GetFieldID(clazz, "speexPreprocessState", "J");
gainField = env->GetFieldID(clazz, "gain", "F");
}
extern "C"
JNIEXPORT void JNICALL
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package com.example.speex_codec
import android.media.MediaCodec
import java.nio.ByteBuffer

class SpeexCodec(private val sampleRate: Long, private val bitRate: Int, private val frameSize: Int, private val preprocessors: Set<Preprocessor> = emptySet()): AutoCloseable {
class SpeexCodec(private val sampleRate: Long, private val bitRate: Int, private val frameSize: Int, private val preprocessors: Set<Preprocessor> = emptySet(), private val gain: Float = 4.0f): AutoCloseable {
enum class Preprocessor(val flagValue: Int) {
DENOISE(1),
AGC(2),
Expand Down

0 comments on commit 22b779b

Please sign in to comment.