-
-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
8adacbe
commit dde6db4
Showing
10 changed files
with
348 additions
and
105 deletions.
There are no files selected for viewing
138 changes: 138 additions & 0 deletions
138
...ithub/thibaultbee/streampack/core/internal/sources/audio/audiorecord/AudioRecordEffect.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,138 @@ | ||
/* | ||
* Copyright (C) 2021 Thibault B. | ||
* | ||
* 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.github.thibaultbee.streampack.core.internal.sources.audio.audiorecord | ||
|
||
import android.media.audiofx.AcousticEchoCanceler | ||
import android.media.audiofx.AudioEffect | ||
import android.media.audiofx.AutomaticGainControl | ||
import android.media.audiofx.BassBoost | ||
import android.media.audiofx.NoiseSuppressor | ||
import android.os.Build | ||
import java.util.UUID | ||
|
||
/** | ||
* Audio effect interface | ||
*/ | ||
internal class AudioRecordEffect { | ||
internal interface IFactory { | ||
fun build(audioSessionId: Int): AudioEffect? | ||
} | ||
|
||
companion object { | ||
private const val TAG = "AudioRecordEffect" | ||
|
||
fun getSupportedEffects(): List<UUID> { | ||
val descriptors = AudioEffect.queryEffects() | ||
return descriptors.map { it.type } | ||
} | ||
} | ||
|
||
internal abstract class Factory<T : AudioEffect> : IFactory { | ||
protected abstract val name: String | ||
|
||
protected abstract fun isAvailable(): Boolean | ||
protected abstract fun create(audioSessionId: Int): T? | ||
protected fun applyConfigurationFrom(audioEffect: T) = Unit | ||
|
||
override fun build(audioSessionId: Int): T { | ||
if (!isAvailable()) { | ||
throw IllegalStateException("$name is not available") | ||
} | ||
|
||
val audioEffect = try { | ||
create(audioSessionId) | ||
} catch (t: Throwable) { | ||
throw Exception("Failed to create $name", t) | ||
} | ||
|
||
requireNotNull(audioEffect) { | ||
"Failed to create $name" | ||
} | ||
|
||
val result = audioEffect.setEnabled(true) | ||
if (result != AudioEffect.SUCCESS) { | ||
audioEffect.release() | ||
throw Exception("Failed to enable $name") | ||
} | ||
|
||
return audioEffect | ||
} | ||
|
||
companion object { | ||
fun getFactoryForEffectType(effectType: UUID): Factory<*> { | ||
return when (effectType) { | ||
AudioEffect.EFFECT_TYPE_AEC -> AcousticEchoCancelerFactory() | ||
AudioEffect.EFFECT_TYPE_AGC -> AutomaticGainControlFactory() | ||
AudioEffect.EFFECT_TYPE_BASS_BOOST -> BassBoostFactory() | ||
AudioEffect.EFFECT_TYPE_NS -> NoiseSuppressorFactory() | ||
else -> throw IllegalArgumentException("Unknown effect type: $effectType") | ||
} | ||
} | ||
|
||
fun isValidUUID(uuid: UUID): Boolean { | ||
return uuid == AudioEffect.EFFECT_TYPE_AEC || | ||
uuid == AudioEffect.EFFECT_TYPE_AGC || | ||
uuid == AudioEffect.EFFECT_TYPE_BASS_BOOST || | ||
((Build.VERSION.SDK_INT >= Build.VERSION_CODES.P) && | ||
uuid == AudioEffect.EFFECT_TYPE_DYNAMICS_PROCESSING) || | ||
uuid == AudioEffect.EFFECT_TYPE_ENV_REVERB || | ||
uuid == AudioEffect.EFFECT_TYPE_EQUALIZER || | ||
((Build.VERSION.SDK_INT >= Build.VERSION_CODES.S) && | ||
uuid == AudioEffect.EFFECT_TYPE_HAPTIC_GENERATOR) || | ||
uuid == AudioEffect.EFFECT_TYPE_LOUDNESS_ENHANCER || | ||
uuid == AudioEffect.EFFECT_TYPE_NS || | ||
uuid == AudioEffect.EFFECT_TYPE_PRESET_REVERB || | ||
uuid == AudioEffect.EFFECT_TYPE_VIRTUALIZER | ||
} | ||
} | ||
} | ||
|
||
internal class AcousticEchoCancelerFactory : Factory<AcousticEchoCanceler>() { | ||
override val name: String = "Acoustic echo canceler" | ||
|
||
override fun isAvailable(): Boolean = AcousticEchoCanceler.isAvailable() | ||
|
||
override fun create(audioSessionId: Int): AcousticEchoCanceler? = | ||
AcousticEchoCanceler.create(audioSessionId) | ||
} | ||
|
||
internal class AutomaticGainControlFactory : Factory<AutomaticGainControl>() { | ||
override val name: String = "Automatic gain control" | ||
|
||
override fun isAvailable(): Boolean = AutomaticGainControl.isAvailable() | ||
|
||
override fun create(audioSessionId: Int): AutomaticGainControl? = | ||
AutomaticGainControl.create(audioSessionId) | ||
} | ||
|
||
internal class BassBoostFactory(private val priority: Int = 0) : Factory<BassBoost>() { | ||
override val name: String = "Bass boost" | ||
|
||
override fun isAvailable(): Boolean = | ||
getSupportedEffects().contains(AudioEffect.EFFECT_TYPE_BASS_BOOST) | ||
|
||
override fun create(audioSessionId: Int) = BassBoost(priority, audioSessionId) | ||
} | ||
|
||
internal class NoiseSuppressorFactory : Factory<NoiseSuppressor>() { | ||
override val name: String = "Noise suppressor" | ||
|
||
override fun isAvailable(): Boolean = NoiseSuppressor.isAvailable() | ||
|
||
override fun create(audioSessionId: Int): NoiseSuppressor? = | ||
NoiseSuppressor.create(audioSessionId) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.