From 08c080f1b0100d7155cb561ac74516e49e2b0f9d Mon Sep 17 00:00:00 2001 From: jonnyandrew Date: Mon, 23 Oct 2023 17:06:25 +0100 Subject: [PATCH] Reduce unused sample rate confurability --- .../impl/audio/AndroidAudioReader.kt | 6 ++-- .../voicerecorder/impl/audio/AudioConfig.kt | 2 ++ .../impl/audio/DefaultEncoder.kt | 2 +- .../voicerecorder/impl/audio/SampleRate.kt | 24 +++++++++++++ .../voicerecorder/impl/audio/SampleRateExt.kt | 35 ------------------- .../impl/di/VoiceRecorderModule.kt | 12 ++++--- .../impl/file/DefaultVoiceFileManager.kt | 2 -- .../impl/VoiceRecorderImplTest.kt | 4 ++- 8 files changed, 40 insertions(+), 47 deletions(-) create mode 100644 libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/SampleRate.kt delete mode 100644 libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/SampleRateExt.kt diff --git a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/AndroidAudioReader.kt b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/AndroidAudioReader.kt index 8817d6a0b0..a2342f3c2f 100644 --- a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/AndroidAudioReader.kt +++ b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/AndroidAudioReader.kt @@ -39,7 +39,7 @@ class AndroidAudioReader private val outputBuffer: ShortArray init { - outputBuffer = createOutputBuffer(config.format.sampleRateHz()) + outputBuffer = createOutputBuffer(config.sampleRate) audioRecord = AudioRecord.Builder().setAudioSource(config.source).setAudioFormat(config.format).setBufferSizeInBytes(outputBuffer.sizeInBytes()).build() noiseSuppressor = requestNoiseSuppressor(audioRecord) automaticGainControl = requestAutomaticGainControl(audioRecord) @@ -90,9 +90,9 @@ class AndroidAudioReader automaticGainControl = null } - private fun createOutputBuffer(sampleRate: Hz): ShortArray { + private fun createOutputBuffer(sampleRate: SampleRate): ShortArray { val bufferSizeInShorts = AudioRecord.getMinBufferSize( - sampleRate.value, + sampleRate.hz, config.format.channelMask, config.format.encoding ) diff --git a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/AudioConfig.kt b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/AudioConfig.kt index b33f0a093f..6ff912c2ae 100644 --- a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/AudioConfig.kt +++ b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/AudioConfig.kt @@ -24,10 +24,12 @@ import android.media.MediaRecorder.AudioSource * * @property source the audio source to use, see constants in [AudioSource] * @property format the audio format to use, see [AudioFormat] + * @property sampleRate the sample rate to use. Ensure this matches the value set in [format]. * @property bitRate the bitrate in bps */ data class AudioConfig( val source: Int, val format: AudioFormat, + val sampleRate: SampleRate, val bitRate: Int, ) diff --git a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/DefaultEncoder.kt b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/DefaultEncoder.kt index bb20e7b272..a888824fe5 100644 --- a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/DefaultEncoder.kt +++ b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/DefaultEncoder.kt @@ -33,7 +33,7 @@ class DefaultEncoder @Inject constructor( config: AudioConfig, ) : Encoder { private val bitRate = config.bitRate - private val sampleRate = config.format.sampleRateHz().toLibModel() +private val sampleRate = config.sampleRate.asEncoderModel() private var encoder: OggOpusEncoder? = null override fun init( diff --git a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/SampleRate.kt b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/SampleRate.kt new file mode 100644 index 0000000000..b392b6e19f --- /dev/null +++ b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/SampleRate.kt @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 New Vector Ltd + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package io.element.android.libraries.voicerecorder.impl.audio + +import io.element.android.opusencoder.configuration.SampleRate as LibOpusOggSampleRate + +data object SampleRate { + const val hz = 48_000 + fun asEncoderModel() = LibOpusOggSampleRate.Rate48kHz +} diff --git a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/SampleRateExt.kt b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/SampleRateExt.kt deleted file mode 100644 index 640122ff71..0000000000 --- a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/audio/SampleRateExt.kt +++ /dev/null @@ -1,35 +0,0 @@ -/* - * Copyright (c) 2023 New Vector Ltd - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package io.element.android.libraries.voicerecorder.impl.audio - -import android.media.AudioFormat -import io.element.android.opusencoder.configuration.SampleRate - -internal fun SampleRate.toHz(): Hz = Hz(value) -@JvmInline -internal value class Hz(val value: Int) -internal fun Hz.toLibModel() = - when (this) { - SampleRate.Rate8khz.toHz() -> SampleRate.Rate8khz - SampleRate.Rate12kHz.toHz() -> SampleRate.Rate12kHz - SampleRate.Rate16kHz.toHz() -> SampleRate.Rate16kHz - SampleRate.Rate24KHz.toHz() -> SampleRate.Rate24KHz - SampleRate.Rate48kHz.toHz() -> SampleRate.Rate48kHz - else -> throw IllegalArgumentException("Unknown sample rate: $this") - } - -internal fun AudioFormat.sampleRateHz(): Hz = Hz(sampleRate) diff --git a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/di/VoiceRecorderModule.kt b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/di/VoiceRecorderModule.kt index 1e8c4adcf1..b21ab48ac3 100644 --- a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/di/VoiceRecorderModule.kt +++ b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/di/VoiceRecorderModule.kt @@ -23,25 +23,27 @@ import dagger.Module import dagger.Provides import io.element.android.libraries.di.RoomScope import io.element.android.libraries.voicerecorder.impl.audio.AudioConfig -import io.element.android.libraries.voicerecorder.impl.audio.toHz +import io.element.android.libraries.voicerecorder.impl.audio.SampleRate import io.element.android.libraries.voicerecorder.impl.file.VoiceFileConfig import io.element.android.opusencoder.OggOpusEncoder -import io.element.android.opusencoder.configuration.SampleRate @Module @ContributesTo(RoomScope::class) object VoiceRecorderModule { @Provides - fun provideAudioConfig(): AudioConfig = - AudioConfig( + fun provideAudioConfig(): AudioConfig { + val sampleRate = SampleRate + return AudioConfig( format = AudioFormat.Builder() .setEncoding(AudioFormat.ENCODING_PCM_16BIT) - .setSampleRate(SampleRate.Rate48kHz.toHz().value) + .setSampleRate(sampleRate.hz) .setChannelMask(AudioFormat.CHANNEL_IN_MONO) .build(), bitRate = 24_000, // 24 kbps + sampleRate = sampleRate, source = MediaRecorder.AudioSource.MIC, ) + } @Provides fun provideVoiceFileConfig(): VoiceFileConfig = diff --git a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/file/DefaultVoiceFileManager.kt b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/file/DefaultVoiceFileManager.kt index 78c218dfa4..07ef54991f 100644 --- a/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/file/DefaultVoiceFileManager.kt +++ b/libraries/voicerecorder/impl/src/main/kotlin/io/element/android/libraries/voicerecorder/impl/file/DefaultVoiceFileManager.kt @@ -16,10 +16,8 @@ package io.element.android.libraries.voicerecorder.impl.file -import android.content.Context import com.squareup.anvil.annotations.ContributesBinding import io.element.android.libraries.core.hash.md5 -import io.element.android.libraries.di.ApplicationContext import io.element.android.libraries.di.CacheDirectory import io.element.android.libraries.di.RoomScope import io.element.android.libraries.matrix.api.core.RoomId diff --git a/libraries/voicerecorder/impl/src/test/kotlin/io/element/android/libraries/voicerecorder/impl/VoiceRecorderImplTest.kt b/libraries/voicerecorder/impl/src/test/kotlin/io/element/android/libraries/voicerecorder/impl/VoiceRecorderImplTest.kt index 5e66318341..847e1c514f 100644 --- a/libraries/voicerecorder/impl/src/test/kotlin/io/element/android/libraries/voicerecorder/impl/VoiceRecorderImplTest.kt +++ b/libraries/voicerecorder/impl/src/test/kotlin/io/element/android/libraries/voicerecorder/impl/VoiceRecorderImplTest.kt @@ -23,6 +23,7 @@ import com.google.common.truth.Truth.assertThat import io.element.android.libraries.voicerecorder.api.VoiceRecorderState import io.element.android.libraries.voicerecorder.impl.audio.Audio import io.element.android.libraries.voicerecorder.impl.audio.AudioConfig +import io.element.android.libraries.voicerecorder.impl.audio.SampleRate import io.element.android.libraries.voicerecorder.impl.di.VoiceRecorderModule import io.element.android.libraries.voicerecorder.test.FakeAudioLevelCalculator import io.element.android.libraries.voicerecorder.test.FakeAudioRecorderFactory @@ -99,7 +100,8 @@ class VoiceRecorderImplTest { encoder = FakeEncoder(fakeFileSystem), config = AudioConfig( format = AUDIO_FORMAT, - bitRate = 24 * 1000, + bitRate = 24_000, // 24 kbps + sampleRate = SampleRate, source = MediaRecorder.AudioSource.MIC, ), fileConfig = fileConfig,