Significantly more efficient sound effect processing, more NPC sounds

This commit is contained in:
Joshua Barretto 2021-06-16 15:04:00 +01:00
parent e3c44ba7fe
commit 9510869870
9 changed files with 77 additions and 29 deletions

View File

@ -856,5 +856,29 @@
],
threshold: 4.0,
),
Utterance(Calm, Pig): (
files: [
"voxygen.audio.sfx.utterance.pig_calm",
],
threshold: 4.0,
),
Utterance(Calm, Cow): (
files: [
"voxygen.audio.sfx.utterance.cow_calm",
],
threshold: 4.0,
),
Utterance(Calm, Sheep): (
files: [
"voxygen.audio.sfx.utterance.sheep_calm",
],
threshold: 4.0,
),
Utterance(Greeting, HumanMale): (
files: [
"voxygen.audio.sfx.utterance.humanmale_greeting",
],
threshold: 4.0,
),
}
)

BIN
assets/voxygen/audio/sfx/utterance/cow_calm.ogg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/voxygen/audio/sfx/utterance/humanmale_greeting.ogg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/voxygen/audio/sfx/utterance/pig_calm.ogg (Stored with Git LFS) Normal file

Binary file not shown.

BIN
assets/voxygen/audio/sfx/utterance/sheep_calm.ogg (Stored with Git LFS) Normal file

Binary file not shown.

View File

@ -409,6 +409,14 @@ impl<'a> System<'a> for Sys {
.uid_allocator
.retrieve_entity_internal(by.id())
{
if agent.target.is_none() {
controller.push_event(
ControlEvent::Utterance(
UtteranceKind::Angry,
),
);
}
agent.target = Some(Target {
target: attacker,
hostile: true,
@ -511,6 +519,12 @@ impl<'a> System<'a> for Sys {
&mut event_emitter,
);
} else {
if agent.target.is_none() {
controller.push_event(ControlEvent::Utterance(
UtteranceKind::Angry,
));
}
agent.target = Some(Target {
target: attacker,
hostile: true,
@ -945,7 +959,7 @@ impl<'a> AgentData<'a> {
controller.actions.push(ControlAction::Unwield);
}
if thread_rng().gen_bool(0.001) {
if thread_rng().gen::<f32>() < 0.0015 {
controller.push_event(ControlEvent::Utterance(UtteranceKind::Calm));
}

View File

@ -265,8 +265,8 @@ impl AudioFrontend {
) -> Result<(), rodio::decoder::DecoderError> {
if self.audio_stream.is_some() {
let sound = OggSound::load_expect(sound)
.cloned()
.decoder()?
.read()
.to_source()
.amplify(vol.unwrap_or(1.0));
let listener = self.listener.clone();
@ -291,9 +291,7 @@ impl AudioFrontend {
) {
if self.audio_stream.is_some() {
if let Some(channel) = self.get_ambient_channel(channel_tag, volume_multiplier) {
if let Ok(sound) = OggSound::load_expect(sound).cloned().decoder() {
channel.play(sound);
}
channel.play(OggSound::load_expect(sound).read().to_source());
}
}
}
@ -349,9 +347,7 @@ impl AudioFrontend {
fn play_music(&mut self, sound: &str, channel_tag: MusicChannelTag) {
if self.music_enabled() {
if let Some(channel) = self.get_music_channel(channel_tag) {
if let Ok(sound) = OggSound::load_expect(sound).cloned().decoder() {
channel.play(sound, channel_tag);
}
channel.play(OggSound::load_expect(sound).read().to_source(), channel_tag);
}
}
}

View File

@ -91,7 +91,7 @@ use client::Client;
use common::{
assets::{self, AssetExt, AssetHandle},
comp::{
beam, biped_large,
beam, biped_large, humanoid,
item::{ItemKind, ToolKind},
object,
poise::PoiseState,
@ -189,7 +189,8 @@ pub enum SfxEvent {
#[derive(Clone, Debug, PartialEq, Deserialize, Hash, Eq)]
pub enum VoiceKind {
Mute,
Human,
HumanFemale,
HumanMale,
BipedLarge,
Wendigo,
Reptile,
@ -204,7 +205,10 @@ pub enum VoiceKind {
fn body_to_voice(body: &Body) -> VoiceKind {
match body {
Body::Humanoid(_) => VoiceKind::Human,
Body::Humanoid(body) => match &body.body_type {
humanoid::BodyType::Female => VoiceKind::HumanFemale,
humanoid::BodyType::Male => VoiceKind::HumanMale,
},
Body::QuadrupedSmall(body) => match body.species {
quadruped_small::Species::Sheep => VoiceKind::Sheep,
quadruped_small::Species::Pig | quadruped_small::Species::Boar => VoiceKind::Pig,

View File

@ -1,6 +1,7 @@
//! Handles caching and retrieval of decoded `.ogg` sfx sound data, eliminating
//! the need to decode files on each playback
use common::assets;
use common::assets::{self, Loader};
use rodio::{source::Buffered, Decoder, Source};
use std::{borrow::Cow, io, sync::Arc};
use tracing::warn;
@ -10,16 +11,16 @@ use tracing::warn;
pub struct SoundLoader;
#[derive(Clone)]
pub struct OggSound(Arc<Vec<u8>>);
pub struct OggSound(Buffered<Decoder<io::Cursor<Vec<u8>>>>);
impl AsRef<[u8]> for OggSound {
fn as_ref(&self) -> &[u8] { &self.0 }
}
// impl AsRef<[u8]> for OggSound {
// fn as_ref(&self) -> &[u8] { &self.0 }
// }
impl assets::Loader<OggSound> for SoundLoader {
impl Loader<OggSound> for SoundLoader {
fn load(content: Cow<[u8]>, _: &str) -> Result<OggSound, assets::BoxedError> {
let arc = Arc::new(content.into_owned());
Ok(OggSound(arc))
let source = Decoder::new(io::Cursor::new(content.into_owned()))?.buffered();
Ok(OggSound(source))
}
}
@ -37,16 +38,13 @@ impl assets::Asset for OggSound {
/// Wrapper for decoded audio data
impl OggSound {
pub fn decoder(
self,
) -> Result<rodio::Decoder<io::Cursor<OggSound>>, rodio::decoder::DecoderError> {
let cursor = io::Cursor::new(self);
rodio::Decoder::new(cursor)
}
pub fn to_source(&self) -> impl Source + Iterator<Item = i16> { self.0.clone() }
pub fn empty() -> OggSound {
OggSound(Arc::new(
include_bytes!("../../../assets/voxygen/audio/null.ogg").to_vec(),
))
SoundLoader::load(
Cow::Borrowed(include_bytes!("../../../assets/voxygen/audio/null.ogg")),
"empty",
)
.unwrap()
}
}