Skip to content

Commit

Permalink
Reduce unused sample rate confurability
Browse files Browse the repository at this point in the history
  • Loading branch information
jonnyandrew committed Oct 23, 2023
1 parent 6282c2c commit 08c080f
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
@@ -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
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -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 =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down

0 comments on commit 08c080f

Please sign in to comment.