diff options
author | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-19 21:12:14 +1000 |
---|---|---|
committer | Kae <80987908+Novaenia@users.noreply.github.com> | 2023-07-19 21:12:14 +1000 |
commit | d682b164aa87435183a5ad3196b25b5ff8a5ad18 (patch) | |
tree | 01f05fa9d5ab78711c7cd89ae5ea343dd2b427d3 /source/frontend/StarVoice.cpp | |
parent | 0c1c3611b1b1c1b17efac547ad08a6821f3b8f01 (diff) |
more Lua voice callbacks
Diffstat (limited to 'source/frontend/StarVoice.cpp')
-rw-r--r-- | source/frontend/StarVoice.cpp | 39 |
1 files changed, 35 insertions, 4 deletions
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<VoiceAudioStream>(); } +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::SpeakerPtr> Voice::speakers(bool onlyPlaying) { +HashMap<Voice::SpeakerId, Voice::SpeakerPtr>& Voice::speakers() { + return m_speakers; +} + +List<Voice::SpeakerPtr> Voice::sortedSpeakers(bool onlyPlaying) { List<SpeakerPtr> result; auto sorter = [](SpeakerPtr const& a, SpeakerPtr const& b) -> bool { @@ -276,6 +294,16 @@ List<Voice::SpeakerPtr> 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; } |