From f14f77724d6a06b759280c0885ce1102388faa38 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Thu, 13 Jul 2023 20:47:53 +1000 Subject: more voice work!!! --- source/frontend/StarVoice.hpp | 36 ++++++++++++++++++++++++++++-------- 1 file changed, 28 insertions(+), 8 deletions(-) (limited to 'source/frontend/StarVoice.hpp') diff --git a/source/frontend/StarVoice.hpp b/source/frontend/StarVoice.hpp index 0d485db..269adb4 100644 --- a/source/frontend/StarVoice.hpp +++ b/source/frontend/StarVoice.hpp @@ -5,6 +5,7 @@ #include "StarException.hpp" #include "StarGameTypes.hpp" #include "StarMaybe.hpp" +#include "StarThread.hpp" #include "StarApplicationController.hpp" struct OpusDecoder; @@ -23,6 +24,7 @@ enum class VoiceChannelMode: uint8_t { Mono = 1, Stereo = 2 }; extern EnumMap const VoiceChannelModeNames; STAR_CLASS(Voice); +STAR_CLASS(VoiceAudioStream); STAR_CLASS(ApplicationController); class Voice { @@ -30,7 +32,8 @@ public: // Individual speakers are represented by their connection ID. typedef ConnectionId SpeakerId; - struct Speaker { + class Speaker { + public: SpeakerId speakerId = 0; EntityId entityId = 0; @@ -39,10 +42,8 @@ public: OpusDecoderPtr decoderMono; OpusDecoderPtr decoderStereo; - - atomic active = false; - atomic currentLoudness = 0.0f; - atomic> channelVolumes = Array::filled(1.0f); + VoiceAudioStreamPtr audioStream; + Mutex mutex; Speaker(SpeakerId speakerId); }; @@ -63,19 +64,29 @@ public: Voice(Voice const&) = delete; Voice& operator=(Voice const&) = delete; - void load(Json const& config); - Json save() const; - + void init(); + + void loadJson(Json const& config); + Json saveJson() const; + + void save() const; + void scheduleSave(); + // Sets the local speaker ID and returns the local speaker. Must be called upon loading into a world. SpeakerPtr setLocalSpeaker(SpeakerId speakerId); SpeakerPtr speaker(SpeakerId speakerId); + // Called when receiving input audio data from SDL, on its own thread. + void getAudioData(uint8_t* stream, int len); + // Called to mix voice audio with the game. void mix(int16_t* buffer, size_t frames, unsigned channels); typedef function PositionalAttenuationFunction; void update(PositionalAttenuationFunction positionalAttenuationFunction = {}); + void setDeviceName(Maybe device); + inline int encoderChannels() const { return m_channelMode == VoiceChannelMode::Mono ? 1 : 2; } @@ -96,12 +107,21 @@ private: OpusEncoderPtr m_encoder; + float m_outputVolume = 1.0f; + float m_inputVolume = 1.0f; + float m_threshold = -50.0f; + + bool m_enabled = true; + bool m_inputEnabled = true; + bool m_deviceOpen = false; Maybe m_deviceName; VoiceInputMode m_inputMode; VoiceChannelMode m_channelMode; ApplicationControllerPtr m_applicationController; + + double nextSaveTime = 0.0f; }; } -- cgit v1.2.3