diff --git a/codec2talkie/src/main/java/com/radio/codec2talkie/protocol/Freedv.java b/codec2talkie/src/main/java/com/radio/codec2talkie/protocol/Freedv.java index 2e149c9..c220c0d 100644 --- a/codec2talkie/src/main/java/com/radio/codec2talkie/protocol/Freedv.java +++ b/codec2talkie/src/main/java/com/radio/codec2talkie/protocol/Freedv.java @@ -39,9 +39,11 @@ public class Freedv implements Protocol { SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(context); int mode = SettingsWrapper.getFreeDvSoundModemModulation(sharedPreferences); - Log.i(TAG, "Using freedv mode " + AudioTools.getFreedvModeAsText(sharedPreferences)); + int gain = Integer.parseInt(sharedPreferences.getString(PreferenceKeys.PORTS_SOUND_MODEM_GAIN, "10000")); - _freedv = Codec2.freedvCreate(mode); + Log.i(TAG, "Using freedv mode " + AudioTools.getFreedvModeAsText(sharedPreferences) + " gain " + gain); + + _freedv = Codec2.freedvCreate(mode, gain); _modemTxBuffer = new short[Codec2.freedvGetNomModemSamples(_freedv)]; _speechRxBuffer = new short[Codec2.freedvGetMaxSpeechSamples(_freedv)]; } @@ -77,12 +79,15 @@ public class Freedv implements Protocol { short[] buf = new short[nin]; int bytesRead = _transport.read(buf); if (bytesRead == nin) { + //Log.i(TAG, "read " + bytesRead); long cntRead = Codec2.freedvRx(_freedv, _speechRxBuffer, buf); if (cntRead > 0) { - //Log.i(TAG, "receive " + cntRead); + Log.i(TAG, "receive " + cntRead); _parentProtocolCallback.onReceivePcmAudio(null, null, -1, Arrays.copyOf(_speechRxBuffer, (int) cntRead)); return true; } + } else { + //Log.w(TAG, bytesRead + "!=" + nin); } return false; } diff --git a/codec2talkie/src/main/java/com/radio/codec2talkie/transport/SoundModem.java b/codec2talkie/src/main/java/com/radio/codec2talkie/transport/SoundModem.java index 71059ae..504a353 100644 --- a/codec2talkie/src/main/java/com/radio/codec2talkie/transport/SoundModem.java +++ b/codec2talkie/src/main/java/com/radio/codec2talkie/transport/SoundModem.java @@ -15,6 +15,7 @@ import androidx.preference.PreferenceManager; import com.radio.codec2talkie.rigctl.RigCtl; import com.radio.codec2talkie.rigctl.RigCtlFactory; import com.radio.codec2talkie.settings.PreferenceKeys; +import com.radio.codec2talkie.tools.DebugTools; import java.io.IOException; import java.nio.BufferOverflowException; @@ -129,10 +130,10 @@ public class SoundModem implements Transport, Runnable { public int read(short[] audioSamples) throws IOException { synchronized (_recordAudioBuffer) { if (_recordAudioBuffer.position() >= audioSamples.length) { - //Log.i(TAG, "read " + _recordAudioBuffer.position()); _recordAudioBuffer.flip(); _recordAudioBuffer.get(audioSamples); _recordAudioBuffer.compact(); + //Log.i(TAG, "read " + _recordAudioBuffer.position() + " " +audioSamples.length + " " + DebugTools.shortsToHex(audioSamples)); return audioSamples.length; } } diff --git a/libcodec2-android/src/main/cpp/Codec2JNI.cpp b/libcodec2-android/src/main/cpp/Codec2JNI.cpp index d794ac3..cfaa5d5 100644 --- a/libcodec2-android/src/main/cpp/Codec2JNI.cpp +++ b/libcodec2-android/src/main/cpp/Codec2JNI.cpp @@ -33,6 +33,7 @@ namespace Java_com_ustadmobile_codec2_Codec2 { struct freedv *freeDv; short *speechSamples; short *modemSamples; + int gain; }; static Context *getContext(jlong jp) { @@ -97,10 +98,11 @@ namespace Java_com_ustadmobile_codec2_Codec2 { return reinterpret_cast(conFsk); } - static jlong freedvCreate(JNIEnv *env, jclass clazz, int mode) { + static jlong freedvCreate(JNIEnv *env, jclass clazz, int mode, int gain) { struct ContextFreedv *conFreedv; conFreedv = (struct ContextFreedv *) malloc(sizeof(struct ContextFreedv)); conFreedv->freeDv = freedv_open(mode); + conFreedv->gain = gain; conFreedv->speechSamples = static_cast(malloc( freedv_get_n_max_speech_samples(conFreedv->freeDv) * sizeof(short))); conFreedv->modemSamples = static_cast(malloc( @@ -270,6 +272,7 @@ namespace Java_com_ustadmobile_codec2_Codec2 { ContextFreedv *conFreedv = getContextFreedv(n); int nin = freedv_nin(conFreedv->freeDv); env->GetShortArrayRegion(inputModemSamples, 0, nin, conFreedv->speechSamples); + //int cntRead = freedv_shortrx(conFreedv->freeDv, conFreedv->modemSamples, conFreedv->speechSamples, conFreedv->gain); int cntRead = freedv_rx(conFreedv->freeDv, conFreedv->modemSamples, conFreedv->speechSamples); env->SetShortArrayRegion(outputSpeechSamples, 0, nin, conFreedv->modemSamples); return cntRead; @@ -295,7 +298,7 @@ namespace Java_com_ustadmobile_codec2_Codec2 { {"fskSamplesPerSymbol", "(J)I", (void *) fskSamplesPerSymbol}, {"fskNin", "(J)I", (void *) fskNin}, // freedv - {"freedvCreate", "(I)J", (void *)freedvCreate}, + {"freedvCreate", "(II)J", (void *)freedvCreate}, {"freedvDestroy", "(J)I", (void *)freedvDestroy}, {"freedvGetMaxSpeechSamples", "(J)I", (void *)freedvGetMaxSpeechSamples}, {"freedvGetMaxModemSamples", "(J)I", (void *)freedvGetMaxModemSamples}, diff --git a/libcodec2-android/src/main/java/com/ustadmobile/codec2/Codec2.java b/libcodec2-android/src/main/java/com/ustadmobile/codec2/Codec2.java index 17f9b1b..88fbffb 100644 --- a/libcodec2-android/src/main/java/com/ustadmobile/codec2/Codec2.java +++ b/libcodec2-android/src/main/java/com/ustadmobile/codec2/Codec2.java @@ -57,7 +57,7 @@ public class Codec2 { public native static long fskDemodulate(long conFsk, short[] inputSamples, byte[] outputBits); // freedv - public native static long freedvCreate(int mode); + public native static long freedvCreate(int mode, int gain); public native static int freedvDestroy(long conFreedv); public native static int freedvGetMaxSpeechSamples(long conFreedv);