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 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(); @@ -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 SDLGameControllerUPtr; StableHashMap 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(m_mainMixer->mixer(), appController); m_input = make_shared(); - m_voice = make_shared(); + m_voice = make_shared(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 similarity index 84% rename from source/game/StarVoice.cpp rename to source/frontend/StarVoice.cpp index 081e2a6..436461b 100644 --- a/source/game/StarVoice.cpp +++ b/source/frontend/StarVoice.cpp @@ -1,23 +1,10 @@ #include "StarVoice.hpp" #include "StarFormat.hpp" +#include "StarApplicationController.hpp" #include "opus/include/opus.h" #include "SDL.h" -namespace Star { - -EnumMap const VoiceTriggerModeNames{ - {VoiceTriggerMode::VoiceActivity, "VoiceActivity"}, - {VoiceTriggerMode::PushToTalk, "PushToTalk"} -}; - -EnumMap const VoiceChannelModeNames{ - {VoiceChannelMode::Mono, "Mono"}, - {VoiceChannelMode::Stereo, "Stereo"} -}; - -static SDL_AudioDeviceID sdlInputDevice = 0; - constexpr int VOICE_SAMPLE_RATE = 48000; constexpr int VOICE_FRAME_SIZE = 960; @@ -26,6 +13,18 @@ constexpr int VOICE_MAX_PACKET_SIZE = 3 * 1276; constexpr uint16_t VOICE_VERSION = 1; +namespace Star { + +EnumMap const VoiceInputModeNames{ + {VoiceInputMode::VoiceActivity, "VoiceActivity"}, + {VoiceInputMode::PushToTalk, "PushToTalk"} +}; + +EnumMap const VoiceChannelModeNames{ + {VoiceChannelMode::Mono, "Mono"}, + {VoiceChannelMode::Stereo, "Stereo"} +}; + 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(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 similarity index 84% rename from source/game/StarVoice.hpp rename to 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 OpusDecoderPtr; @@ -14,13 +16,14 @@ namespace Star { STAR_EXCEPTION(VoiceException, StarException); -enum class VoiceTriggerMode : uint8_t { VoiceActivity, PushToTalk }; -extern EnumMap const VoiceTriggerModeNames; +enum class VoiceInputMode : uint8_t { VoiceActivity, PushToTalk }; +extern EnumMap const VoiceInputModeNames; enum class VoiceChannelMode: uint8_t { Mono = 1, Stereo = 2 }; extern EnumMap 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 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