From d682b164aa87435183a5ad3196b25b5ff8a5ad18 Mon Sep 17 00:00:00 2001 From: Kae <80987908+Novaenia@users.noreply.github.com> Date: Wed, 19 Jul 2023 21:12:14 +1000 Subject: more Lua voice callbacks --- source/frontend/StarVoice.cpp | 39 +++++++++++++++++++++++++++++++++++---- 1 file changed, 35 insertions(+), 4 deletions(-) (limited to 'source/frontend/StarVoice.cpp') diff --git a/source/frontend/StarVoice.cpp b/source/frontend/StarVoice.cpp index a4641ab..b7f8a6c 100644 --- a/source/frontend/StarVoice.cpp +++ b/source/frontend/StarVoice.cpp @@ -1,5 +1,6 @@ #include "StarVoice.hpp" #include "StarFormat.hpp" +#include "StarJsonExtra.hpp" #include "StarApplicationController.hpp" #include "StarTime.hpp" #include "StarRoot.hpp" @@ -103,6 +104,19 @@ Voice::Speaker::Speaker(SpeakerId id) audioStream = make_shared(); } +Json Voice::Speaker::toJson() const { + return JsonObject{ + {"speakerId", speakerId}, + {"entityId", entityId }, + {"name", name }, + {"playing", (bool)playing}, + {"muted", (bool)muted }, + {"decibels", (float)decibelLevel}, + {"smoothDecibels", (float)smoothDb }, + {"position", jsonFromVec2F(position)} + }; +} + Voice* Voice::s_singleton; Voice* Voice::singletonPtr() { @@ -258,7 +272,11 @@ Voice::SpeakerPtr Voice::speaker(SpeakerId speakerId) { } } -List Voice::speakers(bool onlyPlaying) { +HashMap& Voice::speakers() { + return m_speakers; +} + +List Voice::sortedSpeakers(bool onlyPlaying) { List result; auto sorter = [](SpeakerPtr const& a, SpeakerPtr const& b) -> bool { @@ -276,6 +294,16 @@ List Voice::speakers(bool onlyPlaying) { return result; } +void Voice::clearSpeakers() { + auto it = m_speakers.begin(); + while (it != m_speakers.end()) { + if (it->second == m_clientSpeaker) + it = ++it; + else + it = m_speakers.erase(it); + } +} + void Voice::readAudioData(uint8_t* stream, int len) { auto now = Time::monotonicMilliseconds(); bool active = m_encoder && m_encodedChunksLength < 2048 @@ -335,7 +363,7 @@ void Voice::mix(int16_t* buffer, size_t frameCount, unsigned channels) { SpeakerPtr const& speaker = *it; VoiceAudioStream* audio = speaker->audioStream.get(); MutexLocker audioLock(audio->mutex); - if (!audio->samples.empty()) { + if (speaker->playing && !audio->samples.empty()) { if (!speaker->muted) { mix = true; for (size_t i = 0; i != samples; ++i) @@ -343,9 +371,10 @@ void Voice::mix(int16_t* buffer, size_t frameCount, unsigned channels) { speaker->decibelLevel = getAudioLoudness(speakerBuffer.data(), samples); - auto levels = speaker->channelVolumes.load(); + float volume = speaker->volume; + Array2F levels = speaker->channelVolumes; for (size_t i = 0; i != samples; ++i) - sharedBuffer[i] += (int32_t)(speakerBuffer[i]) * levels[i % 2]; + sharedBuffer[i] += (int32_t)(speakerBuffer[i]) * levels[i % 2] * volume; //Blends the weaker channel into the stronger one, /* unused, is a bit too strong on stereo music. float maxLevel = max(levels[0], levels[1]); @@ -606,6 +635,8 @@ void Voice::closeDevice() { return; m_applicationController->closeAudioInputDevice(); + if (!m_loopback) + m_clientSpeaker->playing = false; m_deviceOpen = false; } -- cgit v1.2.3