Skip to content

Commit 3e43f92

Browse files
committed
Fix choppy audio, add setting to adjust buffer size as needed
1 parent dacb295 commit 3e43f92

File tree

9 files changed

+50
-10
lines changed

9 files changed

+50
-10
lines changed

app/build.gradle

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,8 @@ android {
3434
applicationId "tv.remo.android.controller"
3535
minSdkVersion 16
3636
targetSdkVersion 28
37-
versionCode 21
38-
versionName "0.19.3"
37+
versionCode 22
38+
versionName "0.20.0"
3939
multiDexEnabled true
4040
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
4141
}

app/src/main/res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@
5858
<string name="microphoneSettingsTitle">Microphone</string>
5959
<string name="microphoneSettingsSummary">Volume, Bitrate</string>
6060
<string name="micVolumeBoostTitle">Volume Boost</string>
61+
<string name="micMultiplyTitle">Mic Buffer Multiplier (Adjust if choppy)</string>
6162
<!--Audio/Speaker-->
6263
<string name="audioSettingsTitle">Audio and Commands</string>
6364
<string name="audioSettingsSummary">Text to Speech, Device Volume, Commands</string>

app/src/main/res/xml/settings_microphone.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,14 @@
1010
app:entries="@array/volume_mic_list"
1111
app:entryValues="@array/volume_mic_list_values"/>
1212

13+
<androidx.preference.ListPreference
14+
app:key="@string/micAudioBufferMultiplierKey"
15+
app:title="@string/micMultiplyTitle"
16+
app:defaultValue="2"
17+
app:useSimpleSummaryProvider="true"
18+
app:entries="@array/volume_mic_list"
19+
app:entryValues="@array/volume_mic_list_values"/>
20+
1321
<androidx.preference.ListPreference
1422
app:key="@string/micAudioBitrateKey"
1523
app:title="@string/bitrate"

sdk/src/main/java/tv/remo/android/controller/sdk/RemoSettingsUtil.kt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,7 @@ class RemoSettingsUtil(context : Context, sharedPreferences: SharedPreferences)
6565
val microphoneEnabled = BooleanPref(context, sharedPreferences, R.string.microphoneSettingsEnableKey, false)
6666
val micVolume = StringPref(context, sharedPreferences, R.string.micVolumeBoostKey, "1")
6767
val microphoneBitrate = StringPref(context, sharedPreferences, R.string.micAudioBitrateKey, "64")
68+
val microphoneBufferMultiplier = StringPref(context, sharedPreferences, R.string.micAudioBufferMultiplierKey, "2")
6869

6970
//tts related settings
7071
val textToSpeechEnabled = BooleanPref(context, sharedPreferences, R.string.audioSettingsEnableKey, false)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package tv.remo.android.controller.sdk.components.audio
2+
3+
import org.btelman.controlsdk.streaming.audio.retrievers.BasicMicrophoneAudioRetriever
4+
import tv.remo.android.controller.sdk.RemoSettingsUtil
5+
6+
class RemoAudioRetriever : BasicMicrophoneAudioRetriever() {
7+
override fun enableInternal() {
8+
context?.let {
9+
recordingThread.bufferSizeMultiplier = RemoSettingsUtil.with(it).microphoneBufferMultiplier.getPref().toFloatOrNull() ?: 2f
10+
}
11+
12+
super.enableInternal()
13+
}
14+
}

sdk/src/main/java/tv/remo/android/controller/sdk/utils/ComponentBuilderUtil.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import org.btelman.controlsdk.hardware.interfaces.HardwareDriver
88
import org.btelman.controlsdk.models.ComponentHolder
99
import org.btelman.controlsdk.streaming.enums.Orientation
1010
import org.btelman.controlsdk.streaming.factories.AudioProcessorFactory
11+
import org.btelman.controlsdk.streaming.factories.AudioRetrieverFactory
1112
import org.btelman.controlsdk.streaming.factories.VideoProcessorFactory
1213
import org.btelman.controlsdk.streaming.factories.VideoRetrieverFactory
1314
import org.btelman.controlsdk.streaming.models.CameraDeviceInfo
@@ -20,6 +21,7 @@ import tv.remo.android.controller.sdk.components.RemoCommandHandler
2021
import tv.remo.android.controller.sdk.components.RemoSocketComponent
2122
import tv.remo.android.controller.sdk.components.audio.RemoAudioComponent
2223
import tv.remo.android.controller.sdk.components.audio.RemoAudioProcessor
24+
import tv.remo.android.controller.sdk.components.audio.RemoAudioRetriever
2325
import tv.remo.android.controller.sdk.components.hardware.HardwareWatchdogComponent
2426
import tv.remo.android.controller.sdk.components.video.Camera1Override
2527
import tv.remo.android.controller.sdk.components.video.RemoVideoComponent
@@ -63,6 +65,7 @@ object ComponentBuilderUtil {
6365
}
6466

6567
if(settings.microphoneEnabled.getPref()){
68+
AudioRetrieverFactory.putClassInBundle(RemoAudioRetriever::class.java, this)
6669
val audioComponent = ComponentHolder(RemoAudioComponent::class.java, this, threadPriority = Process.THREAD_PRIORITY_MORE_FAVORABLE)
6770
streamList.add(audioComponent)
6871
}

sdk/src/main/res/values/preferences.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
<string name="microphoneSettingsEnableKey">microphoneSettingsEnable</string>
2121
<string name="micVolumeBoostKey">micVolumeBoostKey</string>
2222
<string name="micAudioBitrateKey">micAudioBitrateKey</string>
23+
<string name="micAudioBufferMultiplierKey">micAudioBufferMultiplierKey</string>
2324

2425
<string name="audioSettingsEnableKey">audioSettingsEnable</string>
2526
<string name="audioSettingsTTSGroupKey">ttsCategoryKey</string>

streaming/src/main/java/org/btelman/controlsdk/streaming/audio/retrievers/BasicMicrophoneAudioRetriever.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ import org.btelman.controlsdk.streaming.utils.AudioUtil
1010
*/
1111
open class BasicMicrophoneAudioRetriever : BaseAudioRetriever(), AudioRecordingThread.AudioDataReceivedListener {
1212

13-
private val recordingThread = AudioRecordingThread(this)
13+
protected var recordingThread = AudioRecordingThread(this)
1414

1515
private var dataArray : AudioPacket? = null
1616

streaming/src/main/java/org/btelman/controlsdk/streaming/utils/AudioRecordingThread.java

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import android.util.Log;
2121

2222
public class AudioRecordingThread {
23+
private float bufferSizeMultiplier = 1;
24+
2325
public interface AudioDataReceivedListener {
2426
void onAudioDataReceived(short[] data);
2527
}
@@ -43,12 +45,7 @@ public void startRecording() {
4345
return;
4446

4547
mShouldContinue = true;
46-
mThread = new Thread(new Runnable() {
47-
@Override
48-
public void run() {
49-
record();
50-
}
51-
});
48+
mThread = new Thread(this::record);
5249
mThread.start();
5350
}
5451

@@ -73,14 +70,17 @@ private void record() {
7370
bufferSize = SAMPLE_RATE * 2;
7471
}
7572

76-
short[] audioBuffer = new short[bufferSize / 2];
73+
short[] audioBuffer = new short[(int) (bufferSize * bufferSizeMultiplier)];
7774

7875
AudioRecord record = new AudioRecord(MediaRecorder.AudioSource.DEFAULT,
7976
SAMPLE_RATE,
8077
AudioFormat.CHANNEL_IN_MONO,
8178
AudioFormat.ENCODING_PCM_16BIT,
8279
bufferSize);
8380

81+
Log.v(LOG_TAG, "Buffer Size : " + bufferSize);
82+
Log.v(LOG_TAG, "Buffer Size Multiplier: " + bufferSizeMultiplier);
83+
8484
if (record.getState() != AudioRecord.STATE_INITIALIZED) {
8585
Log.e(LOG_TAG, "Audio Record can't initialize!");
8686
return;
@@ -103,5 +103,17 @@ private void record() {
103103

104104
Log.v(LOG_TAG, String.format("Recording stopped. Samples read: %d", shortsRead));
105105
}
106+
107+
public float getBufferSizeMultiplier() {
108+
return bufferSizeMultiplier;
109+
}
110+
111+
/**
112+
* Set the buffer size multiplier. The audio recording must be stopped and restarted for this to take affect
113+
* @param bufferSizeMultiplier
114+
*/
115+
public void setBufferSizeMultiplier(float bufferSizeMultiplier) {
116+
this.bufferSizeMultiplier = bufferSizeMultiplier;
117+
}
106118
}
107119

0 commit comments

Comments
 (0)