kopia lustrzana https://github.com/sh123/codec2_talkie
Soundmodem progress
rodzic
9f24b73c64
commit
381f10b70e
|
@ -160,11 +160,9 @@ public class AppService extends Service {
|
|||
_tracker = TrackerFactory.create(trackerName);
|
||||
_tracker.initialize(getApplicationContext(), position -> { if (_appWorker != null) _appWorker.sendPositionToTnc(position); });
|
||||
|
||||
transportType = (TransportFactory.TransportType) extras.get("transportType");
|
||||
startAppWorker(transportType);
|
||||
|
||||
Notification notification = buildServiceNotification(getString(R.string.app_service_notif_text_ptt_ready), R.drawable.ic_app_action);
|
||||
startForeground(SERVICE_NOTIFICATION_ID, notification);
|
||||
|
||||
PowerManager powerManager = (PowerManager) getSystemService(POWER_SERVICE);
|
||||
|
||||
boolean noCpuSleep = _sharedPreferences.getBoolean(PreferenceKeys.APP_NO_CPU_SLEEP, false);
|
||||
|
@ -175,6 +173,9 @@ public class AppService extends Service {
|
|||
|
||||
isRunning = true;
|
||||
|
||||
transportType = (TransportFactory.TransportType) extras.get("transportType");
|
||||
startAppWorker(transportType);
|
||||
|
||||
Log.i(TAG, "App service started");
|
||||
}
|
||||
|
||||
|
|
|
@ -87,7 +87,7 @@ public class AppWorker extends Thread {
|
|||
String codec2ModeName = _sharedPreferences.getString(PreferenceKeys.CODEC2_MODE, _context.getResources().getStringArray(R.array.codec2_modes)[0]);
|
||||
_codec2Mode = AudioTools.extractCodec2ModeId(codec2ModeName);
|
||||
|
||||
_transport = TransportFactory.create(transportType, context);
|
||||
_transport = TransportFactory.create(transportType, context);
|
||||
_protocol = ProtocolFactory.create(_codec2Mode, context);
|
||||
|
||||
_processPeriodicTimer = new Timer();
|
||||
|
@ -492,6 +492,11 @@ public class AppWorker extends Thread {
|
|||
@Override
|
||||
public void run() {
|
||||
Log.i(TAG, "Starting message loop");
|
||||
try {
|
||||
sleep(1000);
|
||||
} catch (InterruptedException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
setPriority(Thread.MAX_PRIORITY);
|
||||
Looper.prepare();
|
||||
|
||||
|
|
|
@ -7,18 +7,21 @@ import android.media.AudioFormat;
|
|||
import android.media.AudioRecord;
|
||||
import android.media.AudioTrack;
|
||||
import android.media.MediaRecorder;
|
||||
import android.provider.MediaStore;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.preference.PreferenceManager;
|
||||
|
||||
import com.radio.codec2talkie.settings.PreferenceKeys;
|
||||
import com.ustadmobile.codec2.Codec2;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Arrays;
|
||||
import java.util.BitSet;
|
||||
|
||||
public class SoundModem implements Transport {
|
||||
|
||||
private static final int AUDIO_SAMPLE_SIZE = 8000;
|
||||
private static final String TAG = SoundModem.class.getSimpleName();
|
||||
|
||||
private static final int AUDIO_SAMPLE_SIZE = 12000;
|
||||
|
||||
private final String _name;
|
||||
|
||||
|
@ -62,7 +65,7 @@ public class SoundModem implements Transport {
|
|||
AUDIO_SAMPLE_SIZE,
|
||||
AudioFormat.CHANNEL_IN_MONO,
|
||||
AudioFormat.ENCODING_PCM_16BIT,
|
||||
10 * _audioRecorderMinBufferSize);
|
||||
_audioRecorderMinBufferSize);
|
||||
|
||||
int _audioPlayerMinBufferSize = AudioTrack.getMinBufferSize(
|
||||
AUDIO_SAMPLE_SIZE,
|
||||
|
@ -81,7 +84,7 @@ public class SoundModem implements Transport {
|
|||
.setChannelMask(AudioFormat.CHANNEL_OUT_MONO)
|
||||
.build())
|
||||
.setTransferMode(AudioTrack.MODE_STREAM)
|
||||
.setBufferSizeInBytes(10 * _audioPlayerMinBufferSize)
|
||||
.setBufferSizeInBytes(_audioPlayerMinBufferSize)
|
||||
.build();
|
||||
}
|
||||
|
||||
|
@ -95,8 +98,30 @@ public class SoundModem implements Transport {
|
|||
return 0;
|
||||
}
|
||||
|
||||
public static byte[] toByteBitArray(BitSet bits) {
|
||||
byte[] bytes = new byte[bits.length()];
|
||||
for (int i=0; i<bits.length(); i++) {
|
||||
bytes[i] = (byte) (bits.get(i) ? 1 : 0);
|
||||
}
|
||||
return bytes;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int write(byte[] data) throws IOException {
|
||||
_systemAudioPlayer.play();
|
||||
byte[] dataBits = toByteBitArray(BitSet.valueOf(data));
|
||||
Log.i(TAG, "write() " + data.length + " " + dataBits.length + " " + _playbackBitBuffer.length);
|
||||
int j = 0;
|
||||
for (int i = 0; i < dataBits.length; i++, j++) {
|
||||
if (j >= _playbackBitBuffer.length) {
|
||||
Log.i(TAG, "-- " + i + " " + j);
|
||||
Codec2.fskModulate(_fskModem, _playbackAudioBuffer, _playbackBitBuffer);
|
||||
_systemAudioPlayer.write(_playbackAudioBuffer, 0, _playbackAudioBuffer.length);
|
||||
j = 0;
|
||||
}
|
||||
_playbackBitBuffer[j] = dataBits[i];
|
||||
}
|
||||
Log.i(TAG, "-- " + j);
|
||||
Codec2.fskModulate(_fskModem, _playbackAudioBuffer, _playbackBitBuffer);
|
||||
_systemAudioPlayer.write(_playbackAudioBuffer, 0, _playbackAudioBuffer.length);
|
||||
return 0;
|
||||
|
|
|
@ -131,9 +131,8 @@ namespace Java_com_ustadmobile_codec2_Codec2 {
|
|||
|
||||
static jlong encode(JNIEnv *env, jclass clazz, jlong n, jshortArray inputBuffer, jcharArray outputBits) {
|
||||
Context *con = getContext(n);
|
||||
int i;
|
||||
jshort *jbuf = env->GetShortArrayElements(inputBuffer, nullptr);
|
||||
for (i = 0; i < con->nsam; i++) {
|
||||
for (int i = 0; i < con->nsam; i++) {
|
||||
auto v = (short) jbuf[i];
|
||||
con->buf[i] = v;
|
||||
}
|
||||
|
@ -143,7 +142,7 @@ namespace Java_com_ustadmobile_codec2_Codec2 {
|
|||
codec2_encode(con->c2, con->bits, con->buf);
|
||||
|
||||
jchar *jbits = env->GetCharArrayElements(outputBits, nullptr);
|
||||
for (i = 0; i < con->nbyte; i++) {
|
||||
for (int i = 0; i < con->nbyte; i++) {
|
||||
jbits[i] = con->bits[i];
|
||||
}
|
||||
env->ReleaseCharArrayElements(outputBits, jbits, 0);
|
||||
|
@ -153,9 +152,8 @@ namespace Java_com_ustadmobile_codec2_Codec2 {
|
|||
|
||||
static jlong fskModulate(JNIEnv *env, jclass clazz, jlong n, jshortArray outputSamples, jbyteArray inputBits) {
|
||||
ContextFsk *conFsk = getContextFsk(n);
|
||||
int i;
|
||||
jbyte *jbuf = env->GetByteArrayElements(inputBits, nullptr);
|
||||
for (i = 0; i < conFsk->Nbits; i++) {
|
||||
for (int i = 0; i < conFsk->Nbits; i++) {
|
||||
auto v = (unsigned char) jbuf[i];
|
||||
conFsk->modBits[i] = v;
|
||||
}
|
||||
|
@ -165,7 +163,7 @@ namespace Java_com_ustadmobile_codec2_Codec2 {
|
|||
fsk_mod(conFsk->fsk, conFsk->modBuf, conFsk->modBits, conFsk->Nbits);
|
||||
|
||||
jshort *jOutBuf = env->GetShortArrayElements(outputSamples, nullptr);
|
||||
for (i = 0; i < conFsk->N; i++) {
|
||||
for (int i = 0; i < conFsk->N; i++) {
|
||||
jOutBuf[i] = (int16_t)(conFsk->modBuf[i] * FDMDV_SCALE);
|
||||
}
|
||||
env->ReleaseShortArrayElements(outputSamples, jOutBuf, 0);
|
||||
|
|
Ładowanie…
Reference in New Issue