Add support for sound backends & some miniaudio changes (#4978)

Miniaudio now runs everything in a separate audio thread - this uses boost::asio's io_context.
Our miniaudio implementation is now also much simplified - it does not use its own resource manager or device. This might end up being stupid if sounds don't work after changing output device or locking or w/e
I've made the sound controller into an interface, meaning we can support multiple sound backends in Chatterino. I've added a Null sound backend that disables all sound. A QMediaPlayer or QSoundEffect or Qt backend could be added.
Miniaudio might idle & disable the device now too, not sure

I've added some unrelated changes in the form of a new setting type, and a new setting page helper function for it, which will hopefully make adding new enum settings easier in the future.
This setting stores its value as a string instead of an int, and uses magic_enum to convert between that string value and its enum value.
This commit is contained in:
pajlada
2023-11-26 22:06:12 +01:00
committed by GitHub
parent 1f09035bfb
commit a240797b68
17 changed files with 578 additions and 360 deletions
@@ -0,0 +1,71 @@
#pragma once
#include "controllers/sound/ISoundController.hpp"
#include "util/ThreadGuard.hpp"
#include <boost/asio.hpp>
#include <QByteArray>
#include <QString>
#include <QUrl>
#include <memory>
#include <vector>
struct ma_engine;
struct ma_device;
struct ma_resource_manager;
struct ma_context;
struct ma_sound;
struct ma_decoder;
namespace chatterino {
/**
* @brief Handles sound loading & playback
**/
class MiniaudioBackend : public ISoundController
{
void initialize(Settings &settings, Paths &paths) override;
public:
MiniaudioBackend();
~MiniaudioBackend() override;
// Play a sound from the given url
// If the url points to something that isn't a local file, it will play
// the default sound initialized in the initialize method
void play(const QUrl &sound) final;
private:
// Used for selecting & initializing an appropriate sound backend
std::unique_ptr<ma_context> context;
// The engine is a high-level API for playing sounds from paths in a simple & efficient-enough manner
std::unique_ptr<ma_engine> engine;
// Stores the data of our default ping sounds
QByteArray defaultPingData;
// Stores N decoders for simultaneous default ping playback.
// We can't use the engine API for this as this requires direct access to a custom data_source
std::vector<std::unique_ptr<ma_decoder>> defaultPingDecoders;
// Stores N sounds for simultaneous default ping playback
// We can't use the engine API for this as this requires direct access to a custom data_source
std::vector<std::unique_ptr<ma_sound>> defaultPingSounds;
// Thread guard for the play method
// Ensures play is only ever called from the same thread
ThreadGuard tgPlay;
std::chrono::system_clock::time_point lastSoundPlay;
boost::asio::io_context ioContext{1};
boost::asio::executor_work_guard<boost::asio::io_context::executor_type>
workGuard;
std::unique_ptr<std::thread> audioThread;
boost::asio::steady_timer sleepTimer;
bool initialized{false};
friend class Application;
};
} // namespace chatterino