feat(audio): implement voice chat using Opus codec

Add voice chat support with Opus encoding/decoding, push-to-talk (V key), and new audio recording infrastructure.
This commit is contained in:
2026-07-20 15:14:01 +08:00
parent 5b081c0b0b
commit bf6edeb812
24 changed files with 404 additions and 31 deletions

View File

@@ -1,6 +1,7 @@
#pragma once
#include "Cubed/audio/audio_fade.hpp"
#include "Cubed/audio/audio_recording.hpp"
#include "Cubed/audio/audio_source.hpp"
#include "Cubed/audio/sound_manager.hpp"
#include "Cubed/audio/source_pool.hpp"
@@ -10,13 +11,15 @@
#include <AL/alc.h>
#include <glm/glm.hpp>
#include <memory>
#include <opus/opus.h>
#include <string>
#include <unordered_map>
namespace Cubed {
class ClientWorld;
class NetworkClient;
class AudioEngine {
public:
AudioEngine(Config& config);
AudioEngine(const AudioEngine&) = delete;
@@ -41,11 +44,22 @@ public:
float& bgm_target_volume();
void set_client(std::weak_ptr<NetworkClient> client);
void send_voice(const std::array<int16_t, AudioRecording::FRAME_SAMPLES>&);
void receive_voice(std::span<char> opus, const glm::vec3& pos);
AudioRecording& audio_recording();
private:
using FadeMap = std::unordered_map<std::string, AudioFade>;
bool m_init{false};
ALCdevice* device{nullptr};
ALCcontext* context{nullptr};
OpusEncoder* m_encoder{nullptr};
OpusDecoder* m_decoder{nullptr};
AudioRecording m_recording;
std::weak_ptr<NetworkClient> m_client;
glm::vec3 listener_pos;
std::unique_ptr<AudioSource> m_bgm;
FadeMap m_fade_map;

View File

@@ -0,0 +1,33 @@
#pragma once
#include <alc.h>
#include <array>
#include <cstdint>
namespace Cubed {
class AudioEngine;
class AudioRecording {
public:
static constexpr int SAMPLE_RATE = 48000;
static constexpr int FRAME_MS = 20;
static constexpr int FRAME_SAMPLES = SAMPLE_RATE * FRAME_MS / 1000;
AudioRecording(AudioEngine& engine);
AudioRecording(const AudioRecording&) = delete;
AudioRecording(AudioRecording&&) = delete;
AudioRecording& operator=(const AudioRecording&) = delete;
AudioRecording& operator=(AudioRecording&&) = delete;
~AudioRecording();
void update();
void init();
void start();
void stop();
bool is_recording() const;
private:
AudioEngine& m_engine;
ALCdevice* m_capture = nullptr;
bool m_recording = false;
void send_voice(const std::array<int16_t, FRAME_SAMPLES>& pcm);
};
} // namespace Cubed

View File

@@ -5,6 +5,7 @@
#include <AL/al.h>
#include <glm/glm.hpp>
#include <memory>
namespace Cubed {
enum class AudioState { INITIAL, PLAYING, PAUSED, STOPPED };
@@ -21,8 +22,9 @@ public:
void set_pitch(float pitch);
void play();
void play_2d(const AudioBuffer& buffer);
void play_2d(std::unique_ptr<AudioBuffer> buffer);
void play_3d(const AudioBuffer& buffer, const glm::vec3& pos);
void play_3d(std::unique_ptr<AudioBuffer> buffer, const glm::vec3& pos);
void stop();
void pause();
float duration() const;
@@ -42,6 +44,8 @@ public:
void clear_effect_slot();
private:
std::unique_ptr<AudioBuffer> m_buffer;
ALuint m_source = 0;
float m_target_volume = 1.0f;
float m_duration = 0.0f;

View File

@@ -4,7 +4,7 @@
namespace Cubed {
class SourcePool {
private:
std::vector<AudioSource> m_sources;
std::vector<std::unique_ptr<AudioSource>> m_sources;
public:
explicit SourcePool(size_t size);
@@ -15,9 +15,10 @@ public:
SourcePool& operator=(SourcePool&&) = delete;
void update();
[[nodiscard]]
AudioSource* acquire();
std::vector<AudioSource>& sources();
std::vector<std::unique_ptr<AudioSource>>& sources();
};
} // namespace Cubed

View File

@@ -106,7 +106,7 @@ public:
void receive_chat_message(ChatMsg& msg);
void send_chat_message(ChatMessage& message);
void receive_voice_message(VoiceMsg& msg);
template <typename Fn>
void register_ticktimer(std::string_view id, TickType threshold, Fn&& f) {
m_ticktimers.emplace(
@@ -115,6 +115,11 @@ public:
}
private:
struct VoiceMessage {
std::string data;
glm::vec3 pos;
};
std::atomic<bool> m_is_pending_delete_queue_free{false};
std::mutex m_delete_vbo_mutex;
std::mutex m_delete_vao_mutex;
@@ -153,6 +158,7 @@ private:
tbb::concurrent_queue<ChunkPos> m_dirty_chunk_queue;
tbb::concurrent_queue<PendingSound> m_pending_sound;
tbb::concurrent_queue<ChatMessage> m_message_queue;
tbb::concurrent_queue<VoiceMessage> m_voice_queue;
std::deque<ChunkPos> m_dirty_queue;
std::vector<const ChunkRenderSnapshot*> m_render_snapshots;

View File

@@ -21,6 +21,7 @@ public:
bool is_connect_error() const;
std::string get_error_string() const;
void clear_error();
ClientWorld& world();
private:
struct Task {

View File

@@ -62,6 +62,7 @@ enum class PacketEnum : uint16_t {
S2C_CLEAR_ALL_CHUNKS = 3005,
UPDATE_TIME = 3006,
CHAT_MSG = 4001,
VOICE_MSG = 4002,
PING = 9001,
PONG = 9002
@@ -125,6 +126,9 @@ template <> constexpr uint16_t get_packet_id<PlayerWaterSound>() {
template <> constexpr uint16_t get_packet_id<ChatMsg>() {
return std::to_underlying(PacketEnum::CHAT_MSG);
}
template <> constexpr uint16_t get_packet_id<VoiceMsg>() {
return std::to_underlying(PacketEnum::VOICE_MSG);
}
template <typename T>
requires std::derived_from<T, google::protobuf::Message>

View File

@@ -81,7 +81,7 @@ public:
void handle_block_change(const BlockChangeReq& req);
void handle_chat_message(ChatMsg& msg);
void handle_voice_message(VoiceMsg& msg);
int chunk_size() const;
template <typename Fn>
void register_timer(std::string_view id, TickType threshold, Fn&& f) {