Skip to content

Commit 41d0d02

Browse files
committed
fix: ghost voice buffer
1 parent bad18fe commit 41d0d02

File tree

2 files changed

+27
-9
lines changed

2 files changed

+27
-9
lines changed

src/Features/Demo/NetworkGhostPlayer.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1059,18 +1059,16 @@ void NetworkManager::Treat(sf::Packet &packet, bool udp) {
10591059
// decompress.
10601060
EVoiceResult res = steam->SteamUser()->DecompressVoice(pVoiceData, pMsgVoiceData->GetDataLength(), pbUncompressedVoice, sizeof(pbUncompressedVoice), &numUncompressedBytes, sampleRate);
10611061

1062-
// continous stream for voice.
1062+
// continuous stream for voice.
10631063
static VoiceStream stream(sampleRate);
1064-
if (stream.getStatus() != sf::SoundSource::Status::Playing)
1065-
stream.play();
10661064

10671065
if (res == k_EVoiceResultOK && numUncompressedBytes > 0) {
10681066
// load from raw pcm data.
10691067
stream.pushSamples((const int16_t *)pbUncompressedVoice, numUncompressedBytes / sizeof(int16_t));
10701068

10711069
// account for ingame vol.
10721070
static auto vol = Variable("volume");
1073-
sf::Listener::setGlobalVolume(vol.GetFloat() * 10000.f);
1071+
stream.setVolume(vol.GetFloat() * 10000.f);
10741072

10751073
// proximity.
10761074
auto player = client->GetPlayer(GET_SLOT() + 1);
@@ -1087,6 +1085,9 @@ void NetworkManager::Treat(sf::Packet &packet, bool udp) {
10871085

10881086
stream.setPosition({ghost_pos.x, ghost_pos.z, ghost_pos.y});
10891087
}
1088+
1089+
if (stream.getStatus() != sf::SoundSource::Status::Playing)
1090+
stream.play();
10901091
}
10911092

10921093
break;

src/Features/Demo/NetworkGhostPlayer.hpp

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,25 @@ class VoiceStream : public sf::SoundStream {
4141
initialize(1, sampleRate, {sf::SoundChannel::Mono});
4242
}
4343

44+
~VoiceStream() override {
45+
stop();
46+
{
47+
std::unique_lock<std::mutex> lock(mutex);
48+
stopRequested = true;
49+
}
50+
cv.notify_all();
51+
}
52+
4453
bool onGetData(Chunk &data) override {
4554
std::unique_lock<std::mutex> lock(mutex);
46-
if (bufferQueue.empty()) {
47-
return true;
48-
}
55+
56+
cv.wait(lock, [&] { return !bufferQueue.empty() || stopRequested; });
57+
58+
if (stopRequested) return false;
4959

5060
currentBuffer = std::move(bufferQueue.front());
5161
bufferQueue.pop();
62+
5263
data.samples = currentBuffer.data();
5364
data.sampleCount = currentBuffer.size();
5465
return true;
@@ -57,14 +68,20 @@ class VoiceStream : public sf::SoundStream {
5768
void onSeek(sf::Time) override {}
5869

5970
void pushSamples(const int16_t *samples, std::size_t count) {
60-
std::unique_lock<std::mutex> lock(mutex);
61-
bufferQueue.emplace(samples, samples + count);
71+
{
72+
std::unique_lock<std::mutex> lock(mutex);
73+
if (stopRequested) return;
74+
bufferQueue.emplace(samples, samples + count);
75+
}
76+
cv.notify_one();
6277
}
6378

6479
private:
6580
std::mutex mutex;
81+
std::condition_variable cv;
6682
std::queue<std::vector<int16_t>> bufferQueue;
6783
std::vector<int16_t> currentBuffer;
84+
bool stopRequested = false;
6885
};
6986

7087
class NetworkManager {

0 commit comments

Comments
 (0)