fix: quiet distant and replayed chat voices

This commit is contained in:
Alexander Sellite 2026-08-17 19:06:38 -04:00
parent f0ddcccaad
commit c59328b524
4 changed files with 95 additions and 15 deletions

View file

@ -48,6 +48,7 @@ func speak_text(
voice_profile_id: String = VoiceProfilesType.DEFAULT_ID,
characters_per_second: float = -1.0,
sample_set_id: String = VoiceProfilesType.DEFAULT_SAMPLE_SET_ID,
volume_offset_db: float = 0.0,
) -> Tween:
var speech_tween := tween_owner.create_tween()
var resolved_characters_per_second := (
@ -75,6 +76,7 @@ func speak_text(
voice_key,
voice_pitch,
resolved_sample_set_id,
volume_offset_db,
)
)
return speech_tween
@ -110,6 +112,7 @@ func _play_character(
voice_key: String,
voice_pitch: float,
sample_set_id: String,
volume_offset_db: float,
) -> void:
if _playback == null or character.strip_edges().is_empty():
return
@ -145,5 +148,10 @@ func _play_character(
0.72,
1.45,
)
_playback.play_stream(sample, 0.0, volume_db, pitch)
_playback.play_stream(
sample,
0.0,
volume_db + volume_offset_db,
pitch,
)
character_sample_played.emit(sample_set_id, normalized)

View file

@ -26,6 +26,9 @@ const SPEECH_BUBBLE_WIDTH: float = 320.0
const SPEECH_POINTER_HALF_WIDTH: float = 12.0
const SPEECH_POINTER_HEIGHT: float = 14.0
const SPEECH_POINTER_OVERLAP: float = 3.0
const ANIMALESE_FULL_VOLUME_DISTANCE: float = 4.0
const ANIMALESE_SILENT_DISTANCE: float = 24.0
const ANIMALESE_SILENT_VOLUME_DB: float = -80.0
const MOBILE_COMPACT_WIDTH: float = 620.0
const MOBILE_EXPANDED_WIDTH: float = 820.0
const MOBILE_COMPACT_HEIGHT: float = 220.0
@ -1022,14 +1025,17 @@ func _show_speech_bubble(
sample_set_id = VoiceProfilesType.sanitized_sample_set_id(
speaker_avatar.get_animalese_sample_set_id()
)
_animalese_voice.speak_text(
_animalese_voice,
label.text,
str(peer_id),
voice_profile_id,
-1.0,
sample_set_id,
)
var voice_volume_offset_db := _get_voice_volume_offset_db(speaker_avatar)
if voice_volume_offset_db > ANIMALESE_SILENT_VOLUME_DB:
_animalese_voice.speak_text(
_animalese_voice,
label.text,
str(peer_id),
voice_profile_id,
-1.0,
sample_set_id,
voice_volume_offset_db,
)
_speech[peer_id] = {
"bubble": bubble,
"pointer": pointer,
@ -1042,6 +1048,28 @@ func _show_speech_bubble(
}
func _get_voice_volume_offset_db(speaker_avatar: Player) -> float:
if speaker_avatar == null or _player == null:
return ANIMALESE_SILENT_VOLUME_DB
return animalese_volume_offset_db_for_distance(
speaker_avatar.global_position.distance_to(_player.global_position)
)
static func animalese_volume_offset_db_for_distance(distance: float) -> float:
if not is_finite(distance) or distance >= ANIMALESE_SILENT_DISTANCE:
return ANIMALESE_SILENT_VOLUME_DB
if distance <= ANIMALESE_FULL_VOLUME_DISTANCE:
return 0.0
var distance_weight := inverse_lerp(
ANIMALESE_FULL_VOLUME_DISTANCE,
ANIMALESE_SILENT_DISTANCE,
distance,
)
var amplitude := pow(1.0 - distance_weight, 2.0)
return maxf(linear_to_db(amplitude), ANIMALESE_SILENT_VOLUME_DB)
func _on_history(messages: Array) -> void:
for peer_id: int in _speech.keys():
var state: Dictionary = _speech[peer_id]