osb/source/frontend/StarVoice.hpp

225 lines
5.7 KiB
C++
Raw Normal View History

#pragma once
2023-07-13 05:01:07 +00:00
#include "StarJson.hpp"
#include "StarBiMap.hpp"
#include "StarException.hpp"
2023-07-13 09:12:55 +00:00
#include "StarGameTypes.hpp"
#include "StarMaybe.hpp"
2023-07-13 10:47:53 +00:00
#include "StarThread.hpp"
2023-07-14 03:13:19 +00:00
#include "StarDataStreamDevices.hpp"
2023-07-13 09:12:55 +00:00
#include "StarApplicationController.hpp"
2023-07-13 05:01:07 +00:00
2023-07-14 03:13:19 +00:00
#include <queue>
2023-07-13 05:01:07 +00:00
struct OpusDecoder;
typedef std::unique_ptr<OpusDecoder, void(*)(OpusDecoder*)> OpusDecoderPtr;
struct OpusEncoder;
typedef std::unique_ptr<OpusEncoder, void(*)(OpusEncoder*)> OpusEncoderPtr;
2023-07-12 12:16:12 +00:00
namespace Star {
2023-07-15 04:01:44 +00:00
String const VoiceBroadcastPrefix = "Voice\0"s;
2023-07-13 05:01:07 +00:00
STAR_EXCEPTION(VoiceException, StarException);
2023-07-13 09:12:55 +00:00
enum class VoiceInputMode : uint8_t { VoiceActivity, PushToTalk };
extern EnumMap<VoiceInputMode> const VoiceInputModeNames;
2023-07-13 05:01:07 +00:00
enum class VoiceChannelMode: uint8_t { Mono = 1, Stereo = 2 };
extern EnumMap<VoiceChannelMode> const VoiceChannelModeNames;
2023-07-12 12:16:12 +00:00
2023-07-13 05:01:07 +00:00
STAR_CLASS(Voice);
2023-07-13 10:47:53 +00:00
STAR_CLASS(VoiceAudioStream);
2023-07-13 09:12:55 +00:00
STAR_CLASS(ApplicationController);
2023-07-12 12:16:12 +00:00
2023-07-14 03:13:19 +00:00
struct VoiceAudioChunk {
std::unique_ptr<int16_t[]> data;
size_t remaining;
size_t offset = 0;
VoiceAudioChunk(int16_t* ptr, size_t size) {
data.reset(ptr);
remaining = size;
offset = 0;
}
inline size_t takeSamples(std::vector<int16_t>& out, size_t count) {
2023-07-14 08:29:36 +00:00
size_t toRead = min<size_t>(count, remaining);
2023-07-14 03:13:19 +00:00
int16_t* start = data.get() + offset;
out.insert(out.end(), start, start + toRead);
offset += toRead;
remaining -= toRead;
return toRead;
}
//this one's unsafe
inline int16_t takeSample() {
--remaining;
return *(data.get() + offset++);
}
inline bool exhausted() { return remaining == 0; }
};
2023-07-13 05:01:07 +00:00
class Voice {
public:
// Individual speakers are represented by their connection ID.
typedef ConnectionId SpeakerId;
2023-07-12 12:16:12 +00:00
2023-07-13 10:47:53 +00:00
class Speaker {
public:
2023-07-13 05:01:07 +00:00
SpeakerId speakerId = 0;
EntityId entityId = 0;
2023-07-13 09:12:55 +00:00
2023-07-13 05:01:07 +00:00
Vec2F position = Vec2F();
String name = "Unnamed";
2023-07-12 12:16:12 +00:00
2023-07-13 05:01:07 +00:00
OpusDecoderPtr decoderMono;
OpusDecoderPtr decoderStereo;
2023-07-13 10:47:53 +00:00
VoiceAudioStreamPtr audioStream;
Mutex mutex;
2023-07-13 05:01:07 +00:00
int64_t lastReceiveTime = 0;
2023-07-18 07:36:51 +00:00
int64_t lastPlayTime = 0;
float smoothDb = -96.0f;
Array<float, 10> dbHistory = Array<float, 10>::filled(0);
2023-07-14 03:13:19 +00:00
atomic<bool> muted = false;
2023-07-18 07:36:51 +00:00
atomic<bool> playing = 0;
atomic<float> decibelLevel = -96.0f;
2023-07-19 11:12:14 +00:00
atomic<float> volume = 1.0f;
atomic<Array<float, 2>> channelVolumes = Array<float, 2>::filled(1);
2023-07-14 03:13:19 +00:00
unsigned int minimumPlaySamples = 4096;
2023-07-13 05:01:07 +00:00
Speaker(SpeakerId speakerId);
2023-07-19 11:12:14 +00:00
Json toJson() const;
2023-07-12 12:16:12 +00:00
};
2023-07-13 05:01:07 +00:00
typedef std::shared_ptr<Speaker> SpeakerPtr;
// Get pointer to the singleton Voice instance, if it exists. Otherwise,
// returns nullptr.
static Voice* singletonPtr();
// Gets reference to Voice singleton, throws VoiceException if root
// is not initialized.
static Voice& singleton();
2023-07-13 09:12:55 +00:00
Voice(ApplicationControllerPtr appController);
2023-07-13 05:01:07 +00:00
~Voice();
Voice(Voice const&) = delete;
Voice& operator=(Voice const&) = delete;
2023-07-13 10:47:53 +00:00
void init();
void loadJson(Json const& config, bool skipSave = false);
2023-07-13 10:47:53 +00:00
Json saveJson() const;
void save() const;
void scheduleSave();
2023-07-13 05:01:07 +00:00
// Sets the local speaker ID and returns the local speaker. Must be called upon loading into a world.
SpeakerPtr setLocalSpeaker(SpeakerId speakerId);
2023-07-18 07:36:51 +00:00
SpeakerPtr localSpeaker();
2023-07-13 05:01:07 +00:00
SpeakerPtr speaker(SpeakerId speakerId);
2023-07-19 11:12:14 +00:00
HashMap<SpeakerId, SpeakerPtr>& speakers();
List<Voice::SpeakerPtr> sortedSpeakers(bool onlyPlaying);
void clearSpeakers();
2023-07-13 05:01:07 +00:00
2023-07-13 10:47:53 +00:00
// Called when receiving input audio data from SDL, on its own thread.
2023-07-14 03:13:19 +00:00
void readAudioData(uint8_t* stream, int len);
2023-07-13 10:47:53 +00:00
2023-07-13 05:01:07 +00:00
// Called to mix voice audio with the game.
void mix(int16_t* buffer, size_t frames, unsigned channels);
typedef function<float(unsigned, Vec2F, float)> PositionalAttenuationFunction;
void update(float dt, PositionalAttenuationFunction positionalAttenuationFunction = {});
2023-07-13 05:01:07 +00:00
2023-07-13 10:47:53 +00:00
void setDeviceName(Maybe<String> device);
StringList availableDevices();
2023-07-13 10:47:53 +00:00
2023-07-19 13:16:59 +00:00
int send(DataStreamBuffer& out, size_t budget = 0);
2023-07-14 03:13:19 +00:00
bool receive(SpeakerPtr speaker, std::string_view view);
// Must be called every frame with input state, expires after 1s.
void setInput(bool input = true);
2023-07-14 08:29:36 +00:00
inline int encoderChannels() const { return (int)m_channelMode; }
2023-07-13 05:01:07 +00:00
static OpusDecoder* createDecoder(int channels);
static OpusEncoder* createEncoder(int channels);
private:
static Voice* s_singleton;
2023-07-13 05:01:07 +00:00
void resetEncoder();
void resetDevice();
2023-07-13 09:12:55 +00:00
void openDevice();
void closeDevice();
inline bool shouldEnableInput() const { return m_enabled && m_inputEnabled; }
2023-07-13 05:01:07 +00:00
2023-07-14 03:13:19 +00:00
bool playSpeaker(SpeakerPtr const& speaker, int channels);
void thread();
2023-07-13 05:01:07 +00:00
SpeakerId m_speakerId = 0;
SpeakerPtr m_clientSpeaker;
HashMap<SpeakerId, SpeakerPtr> m_speakers;
2023-07-14 03:13:19 +00:00
Mutex m_activeSpeakersMutex;
2023-07-13 05:01:07 +00:00
HashSet<SpeakerPtr> m_activeSpeakers;
2023-07-13 05:01:07 +00:00
OpusEncoderPtr m_encoder;
2023-07-13 10:47:53 +00:00
float m_outputVolume = 1.0f;
float m_inputVolume = 1.0f;
float m_threshold = -50.0f;
2023-07-14 03:13:19 +00:00
int64_t m_lastSentTime = 0;
int64_t m_lastInputTime = 0;
int64_t m_lastThresholdTime = 0;
int64_t m_nextSaveTime = 0;
2023-07-13 10:47:53 +00:00
bool m_enabled = true;
bool m_inputEnabled = false;
2023-07-19 09:06:53 +00:00
bool m_loopback = false;
2023-07-13 10:47:53 +00:00
2023-07-14 03:13:19 +00:00
int m_deviceChannels = 1;
2023-07-13 09:12:55 +00:00
bool m_deviceOpen = false;
Maybe<String> m_deviceName;
VoiceInputMode m_inputMode;
2023-07-13 05:01:07 +00:00
VoiceChannelMode m_channelMode;
2023-07-13 09:12:55 +00:00
ThreadFunction<void> m_thread;
Mutex m_threadMutex;
ConditionVariable m_threadCond;
atomic<bool> m_stopThread;
2023-07-19 08:59:35 +00:00
std::vector<int16_t> m_decodeBuffer;
std::vector<int16_t> m_resampleBuffer;
2023-07-13 09:12:55 +00:00
ApplicationControllerPtr m_applicationController;
2023-07-13 10:47:53 +00:00
2023-07-14 03:13:19 +00:00
struct EncodedChunk {
std::unique_ptr<unsigned char[]> data;
size_t size;
EncodedChunk(unsigned char* _data, size_t len) {
data.reset(_data);
size = len;
}
};
Mutex m_encodeMutex;
2023-07-14 03:13:19 +00:00
std::vector<ByteArray> m_encodedChunks;
size_t m_encodedChunksLength = 0;
Mutex m_captureMutex;
2023-07-14 03:13:19 +00:00
std::queue<VoiceAudioChunk> m_capturedChunks;
size_t m_capturedChunksFrames = 0;
2023-07-13 05:01:07 +00:00
};
2023-07-12 12:16:12 +00:00
}