diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-13 19:12:55 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-13 19:12:55 +1000 |
commit | 28f4204b09b04280340ffbbeaccf86b96f377296 (patch) | |
tree | e2a166cc3ddd57460814631b7a8258ed0ee975ea /source | |
parent | c3bf7a3c87e61c56d48dd932f295c99d64d14a38 (diff) |
more Voice work
Diffstat (limited to 'source')
-rw-r--r-- | source/application/StarApplicationController.hpp | 7 | ||||
-rw-r--r-- | source/application/StarMainApplication_sdl.cpp | 52 | ||||
-rw-r--r-- | source/client/StarClientApplication.cpp | 2 | ||||
-rw-r--r-- | source/frontend/CMakeLists.txt | 2 | ||||
-rw-r--r-- | source/frontend/StarVoice.cpp (renamed from source/game/StarVoice.cpp) | 45 | ||||
-rw-r--r-- | source/frontend/StarVoice.hpp (renamed from source/game/StarVoice.hpp) | 20 | ||||
-rw-r--r-- | source/game/CMakeLists.txt | 2 |
7 files changed, 100 insertions, 30 deletions
diff --git a/source/application/StarApplicationController.hpp b/source/application/StarApplicationController.hpp index b8d433c..922ccd5 100644 --- a/source/application/StarApplicationController.hpp +++ b/source/application/StarApplicationController.hpp @@ -44,8 +44,15 @@ public: virtual bool setCursorImage(const String& id, const ImageConstPtr& image, unsigned scale, const Vec2I& offset) = 0; virtual void setAcceptingTextInput(bool acceptingTextInput) = 0; + + virtual AudioFormat enableAudio() = 0; virtual void disableAudio() = 0; + + typedef void (__cdecl* AudioCallback)(void* userdata, uint8_t* stream, int len); + + virtual bool openAudioInputDevice(const char* name, int freq, int channels, void* userdata, AudioCallback callback) = 0; + virtual bool closeAudioInputDevice() = 0; virtual void setClipboard(String text) = 0; virtual Maybe<String> getClipboard() = 0; diff --git a/source/application/StarMainApplication_sdl.cpp b/source/application/StarMainApplication_sdl.cpp index a7dc50f..8f47eeb 100644 --- a/source/application/StarMainApplication_sdl.cpp +++ b/source/application/StarMainApplication_sdl.cpp @@ -292,15 +292,15 @@ public: }; SDL_AudioSpec obtained = {}; - m_sdlAudioDevice = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0); - if (!m_sdlAudioDevice) { + m_sdlAudioOutputDevice = SDL_OpenAudioDevice(NULL, 0, &desired, &obtained, 0); + if (!m_sdlAudioOutputDevice) { Logger::error("Application: Could not open audio device, no sound available!"); } else if (obtained.freq != desired.freq || obtained.channels != desired.channels || obtained.format != desired.format) { - SDL_CloseAudioDevice(m_sdlAudioDevice); + SDL_CloseAudioDevice(m_sdlAudioOutputDevice); Logger::error("Application: Could not open 44.1khz / 16 bit stereo audio device, no sound available!"); } else { Logger::info("Application: Opened default audio device with 44.1khz / 16 bit stereo audio, {} sample size buffer", obtained.samples); - SDL_PauseAudioDevice(m_sdlAudioDevice, 0); + SDL_PauseAudioDevice(m_sdlAudioOutputDevice, 0); } m_renderer = make_shared<OpenGl20Renderer>(); @@ -311,7 +311,10 @@ public: ~SdlPlatform() { - SDL_CloseAudioDevice(m_sdlAudioDevice); + if (m_sdlAudioOutputDevice) + SDL_CloseAudioDevice(m_sdlAudioOutputDevice); + + closeAudioInputDevice(); m_renderer.reset(); @@ -321,6 +324,32 @@ public: SDL_Quit(); } + bool openAudioInputDevice(const char* name, int freq, int channels, void* userdata, SDL_AudioCallback callback) { + SDL_AudioSpec desired = {}; + desired.freq = freq; + desired.format = AUDIO_S16SYS; + desired.samples = 1024; + desired.channels = channels; + desired.userdata = userdata; + desired.callback = callback; + + closeAudioInputDevice(); + + SDL_AudioSpec obtained = {}; + return (m_sdlAudioInputDevice = SDL_OpenAudioDevice(name, 1, &desired, &obtained, 0)) != 0; + } + + bool closeAudioInputDevice() { + if (m_sdlAudioInputDevice) { + SDL_CloseAudioDevice(m_sdlAudioInputDevice); + m_sdlAudioInputDevice = 0; + + return true; + } + + return false; + } + void cleanup() { m_cursorCache.ptr(m_currentCursor); m_cursorCache.cleanup(); @@ -397,7 +426,7 @@ public: Logger::error("Application: threw exception during shutdown: {}", outputException(e, true)); } - SDL_CloseAudioDevice(m_sdlAudioDevice); + SDL_CloseAudioDevice(m_sdlAudioOutputDevice); m_SdlControllers.clear(); SDL_SetCursor(NULL); @@ -569,6 +598,14 @@ private: SDL_PauseAudio(true); } + bool openAudioInputDevice(const char* name, int freq, int channels, void* userdata, AudioCallback callback) override { + return parent->openAudioInputDevice(name, freq, channels, userdata, callback); + }; + + bool closeAudioInputDevice() override { + return parent->closeAudioInputDevice(); + }; + float updateRate() const override { return parent->m_updateRate; } @@ -803,7 +840,8 @@ private: SDL_Window* m_sdlWindow = nullptr; SDL_GLContext m_sdlGlContext = nullptr; - SDL_AudioDeviceID m_sdlAudioDevice = 0; + SDL_AudioDeviceID m_sdlAudioOutputDevice = 0; + SDL_AudioDeviceID m_sdlAudioInputDevice = 0; typedef std::unique_ptr<SDL_GameController, decltype(&SDL_GameControllerClose)> SDLGameControllerUPtr; StableHashMap<int, SDLGameControllerUPtr> m_SdlControllers; diff --git a/source/client/StarClientApplication.cpp b/source/client/StarClientApplication.cpp index e2a1515..c58b6b5 100644 --- a/source/client/StarClientApplication.cpp +++ b/source/client/StarClientApplication.cpp @@ -174,7 +174,7 @@ void ClientApplication::applicationInit(ApplicationControllerPtr appController) m_guiContext = make_shared<GuiContext>(m_mainMixer->mixer(), appController); m_input = make_shared<Input>(); - m_voice = make_shared<Voice>(); + m_voice = make_shared<Voice>(appController); auto configuration = m_root->configuration(); bool vsync = configuration->get("vsync").toBool(); diff --git a/source/frontend/CMakeLists.txt b/source/frontend/CMakeLists.txt index ea6edc6..d8e5390 100644 --- a/source/frontend/CMakeLists.txt +++ b/source/frontend/CMakeLists.txt @@ -56,6 +56,7 @@ SET (star_frontend_HEADERS StarStatusPane.hpp StarTeleportDialog.hpp StarWireInterface.hpp + StarVoice.hpp ) SET (star_frontend_SOURCES @@ -104,6 +105,7 @@ SET (star_frontend_SOURCES StarStatusPane.cpp StarTeleportDialog.cpp StarWireInterface.cpp + StarVoice.cpp ) ADD_LIBRARY (star_frontend OBJECT ${star_frontend_SOURCES} ${star_frontend_HEADERS}) diff --git a/source/game/StarVoice.cpp b/source/frontend/StarVoice.cpp index 081e2a6..436461b 100644 --- a/source/game/StarVoice.cpp +++ b/source/frontend/StarVoice.cpp @@ -1,14 +1,23 @@ #include "StarVoice.hpp" #include "StarFormat.hpp" +#include "StarApplicationController.hpp" #include "opus/include/opus.h" #include "SDL.h" +constexpr int VOICE_SAMPLE_RATE = 48000; +constexpr int VOICE_FRAME_SIZE = 960; + +constexpr int VOICE_MAX_FRAME_SIZE = 6 * VOICE_FRAME_SIZE; +constexpr int VOICE_MAX_PACKET_SIZE = 3 * 1276; + +constexpr uint16_t VOICE_VERSION = 1; + namespace Star { -EnumMap<VoiceTriggerMode> const VoiceTriggerModeNames{ - {VoiceTriggerMode::VoiceActivity, "VoiceActivity"}, - {VoiceTriggerMode::PushToTalk, "PushToTalk"} +EnumMap<VoiceInputMode> const VoiceInputModeNames{ + {VoiceInputMode::VoiceActivity, "VoiceActivity"}, + {VoiceInputMode::PushToTalk, "PushToTalk"} }; EnumMap<VoiceChannelMode> const VoiceChannelModeNames{ @@ -16,16 +25,6 @@ EnumMap<VoiceChannelMode> const VoiceChannelModeNames{ {VoiceChannelMode::Stereo, "Stereo"} }; -static SDL_AudioDeviceID sdlInputDevice = 0; - -constexpr int VOICE_SAMPLE_RATE = 48000; -constexpr int VOICE_FRAME_SIZE = 960; - -constexpr int VOICE_MAX_FRAME_SIZE = 6 * VOICE_FRAME_SIZE; -constexpr int VOICE_MAX_PACKET_SIZE = 3 * 1276; - -constexpr uint16_t VOICE_VERSION = 1; - Voice::Speaker::Speaker(SpeakerId id) : decoderMono (createDecoder(1), opus_decoder_destroy) , decoderStereo(createDecoder(2), opus_decoder_destroy) { @@ -45,13 +44,14 @@ Voice& Voice::singleton() { return *s_singleton; } -Voice::Voice() : m_encoder(nullptr, opus_encoder_destroy) { +Voice::Voice(ApplicationControllerPtr appController) : m_encoder(nullptr, opus_encoder_destroy) { if (s_singleton) throw VoiceException("Singleton Voice has been constructed twice"); m_clientSpeaker = make_shared<Speaker>(m_speakerId); - m_triggerMode = VoiceTriggerMode::PushToTalk; + m_inputMode = VoiceInputMode::PushToTalk; m_channelMode = VoiceChannelMode::Mono; + m_applicationController = appController; resetEncoder(); s_singleton = this; @@ -128,4 +128,19 @@ void Voice::resetEncoder() { opus_encoder_ctl(m_encoder.get(), OPUS_SET_BITRATE(channels == 2 ? 50000 : 24000)); } +void Voice::openDevice() { + closeDevice(); + + m_deviceOpen = true; +} + +void Voice::closeDevice() { + if (!m_deviceOpen) + return; + + m_applicationController->closeAudioInputDevice(); + + m_deviceOpen = false; +} + }
\ No newline at end of file diff --git a/source/game/StarVoice.hpp b/source/frontend/StarVoice.hpp index e870439..0d485db 100644 --- a/source/game/StarVoice.hpp +++ b/source/frontend/StarVoice.hpp @@ -2,8 +2,10 @@ #define STAR_VOICE_HPP #include "StarJson.hpp" #include "StarBiMap.hpp" -#include "StarGameTypes.hpp" #include "StarException.hpp" +#include "StarGameTypes.hpp" +#include "StarMaybe.hpp" +#include "StarApplicationController.hpp" struct OpusDecoder; typedef std::unique_ptr<OpusDecoder, void(*)(OpusDecoder*)> OpusDecoderPtr; @@ -14,13 +16,14 @@ namespace Star { STAR_EXCEPTION(VoiceException, StarException); -enum class VoiceTriggerMode : uint8_t { VoiceActivity, PushToTalk }; -extern EnumMap<VoiceTriggerMode> const VoiceTriggerModeNames; +enum class VoiceInputMode : uint8_t { VoiceActivity, PushToTalk }; +extern EnumMap<VoiceInputMode> const VoiceInputModeNames; enum class VoiceChannelMode: uint8_t { Mono = 1, Stereo = 2 }; extern EnumMap<VoiceChannelMode> const VoiceChannelModeNames; STAR_CLASS(Voice); +STAR_CLASS(ApplicationController); class Voice { public: @@ -30,6 +33,7 @@ public: struct Speaker { SpeakerId speakerId = 0; EntityId entityId = 0; + Vec2F position = Vec2F(); String name = "Unnamed"; @@ -53,7 +57,7 @@ public: // is not initialized. static Voice& singleton(); - Voice(); + Voice(ApplicationControllerPtr appController); ~Voice(); Voice(Voice const&) = delete; @@ -81,6 +85,8 @@ private: static OpusDecoder* createDecoder(int channels); static OpusEncoder* createEncoder(int channels); void resetEncoder(); + void openDevice(); + void closeDevice(); SpeakerId m_speakerId = 0; SpeakerPtr m_clientSpeaker; @@ -90,8 +96,12 @@ private: OpusEncoderPtr m_encoder; - VoiceTriggerMode m_triggerMode; + bool m_deviceOpen = false; + Maybe<String> m_deviceName; + VoiceInputMode m_inputMode; VoiceChannelMode m_channelMode; + + ApplicationControllerPtr m_applicationController; }; } diff --git a/source/game/CMakeLists.txt b/source/game/CMakeLists.txt index fa55950..120e9e8 100644 --- a/source/game/CMakeLists.txt +++ b/source/game/CMakeLists.txt @@ -156,7 +156,6 @@ SET (star_game_HEADERS StarVehicle.hpp StarVehicleDatabase.hpp StarVersioningDatabase.hpp - StarVoice.hpp StarWarping.hpp StarWeather.hpp StarWeatherTypes.hpp @@ -415,7 +414,6 @@ SET (star_game_SOURCES StarVehicle.cpp StarVehicleDatabase.cpp StarVersioningDatabase.cpp - StarVoice.cpp StarWarping.cpp StarWeather.cpp StarWeatherTypes.cpp |