From 17c70dede785bb570999b66315865a2d20393e7a Mon Sep 17 00:00:00 2001
From: Rasmus Karlsson <rasmus.karlsson@pajlada.com>
Date: Sun, 16 Jun 2024 13:43:30 +0200
Subject: [PATCH] chore: unsingletonize SoundController

---
 CHANGELOG.md                               |  1 +
 src/Application.cpp                        |  5 +++--
 src/Application.hpp                        |  2 +-
 src/controllers/sound/ISoundController.hpp |  9 ++-------
 src/controllers/sound/MiniaudioBackend.cpp | 18 ++++++------------
 src/controllers/sound/MiniaudioBackend.hpp |  2 --
 6 files changed, 13 insertions(+), 24 deletions(-)

diff --git a/CHANGELOG.md b/CHANGELOG.md
index 078d0ee400c..22087bec52e 100644
--- a/CHANGELOG.md
+++ b/CHANGELOG.md
@@ -21,6 +21,7 @@
 - Bugfix: Fixed message history occasionally not loading after a sleep. (#5457)
 - Dev: Update Windows build from Qt 6.5.0 to Qt 6.7.1. (#5420)
 - Dev: Update vcpkg build Qt from 6.5.0 to 6.7.0, boost from 1.83.0 to 1.85.0, openssl from 3.1.3 to 3.3.0. (#5422)
+- Dev: Unsingletonize `ISoundController`. (#5462)
 - Dev: Use Qt's high DPI scaling. (#4868, #5400)
 - Dev: Add doxygen build target. (#5377)
 - Dev: Make printing of strings in tests easier. (#5379)
diff --git a/src/Application.cpp b/src/Application.cpp
index 5a9323921a7..9e3aad55934 100644
--- a/src/Application.cpp
+++ b/src/Application.cpp
@@ -136,7 +136,7 @@ Application::Application(Settings &_settings, const Paths &paths,
     , ffzBadges(&this->emplace<FfzBadges>())
     , seventvBadges(&this->emplace<SeventvBadges>())
     , userData(&this->emplace(new UserDataController(paths)))
-    , sound(&this->emplace<ISoundController>(makeSoundController(_settings)))
+    , sound(makeSoundController(_settings))
     , twitchLiveController(&this->emplace<TwitchLiveController>())
     , twitchPubSub(new PubSub(TWITCH_PUBSUB_URL))
     , twitchBadges(new TwitchBadges)
@@ -173,6 +173,7 @@ void Application::fakeDtor()
     this->seventvEmotes.reset();
     // this->twitch.reset();
     this->fonts.reset();
+    this->sound.reset();
 }
 
 void Application::initialize(Settings &settings, const Paths &paths)
@@ -434,7 +435,7 @@ ISoundController *Application::getSound()
 {
     assertInGuiThread();
 
-    return this->sound;
+    return this->sound.get();
 }
 
 ITwitchLiveController *Application::getTwitchLiveController()
diff --git a/src/Application.hpp b/src/Application.hpp
index d2c0e2facc3..e3191d8899c 100644
--- a/src/Application.hpp
+++ b/src/Application.hpp
@@ -161,7 +161,7 @@ class Application : public IApplication
     FfzBadges *const ffzBadges{};
     SeventvBadges *const seventvBadges{};
     UserDataController *const userData{};
-    ISoundController *const sound{};
+    std::unique_ptr<ISoundController> sound;
     TwitchLiveController *const twitchLiveController{};
     std::unique_ptr<PubSub> twitchPubSub;
     std::unique_ptr<TwitchBadges> twitchBadges;
diff --git a/src/controllers/sound/ISoundController.hpp b/src/controllers/sound/ISoundController.hpp
index ebf7e3425b6..10e8c6c7332 100644
--- a/src/controllers/sound/ISoundController.hpp
+++ b/src/controllers/sound/ISoundController.hpp
@@ -1,14 +1,9 @@
 #pragma once
 
-#include "common/Singleton.hpp"
-
 #include <QUrl>
 
 namespace chatterino {
 
-class Settings;
-class Paths;
-
 enum class SoundBackend {
     Miniaudio,
     Null,
@@ -17,11 +12,11 @@ enum class SoundBackend {
 /**
  * @brief Handles sound loading & playback
  **/
-class ISoundController : public Singleton
+class ISoundController
 {
 public:
     ISoundController() = default;
-    ~ISoundController() override = default;
+    virtual ~ISoundController() = default;
     ISoundController(const ISoundController &) = delete;
     ISoundController(ISoundController &&) = delete;
     ISoundController &operator=(const ISoundController &) = delete;
diff --git a/src/controllers/sound/MiniaudioBackend.cpp b/src/controllers/sound/MiniaudioBackend.cpp
index f84ee8991c2..63b7efcf0c5 100644
--- a/src/controllers/sound/MiniaudioBackend.cpp
+++ b/src/controllers/sound/MiniaudioBackend.cpp
@@ -68,10 +68,13 @@ namespace chatterino {
 // NUM_SOUNDS specifies how many simultaneous default ping sounds & decoders to create
 constexpr const auto NUM_SOUNDS = 4;
 
-void MiniaudioBackend::initialize(Settings &settings, const Paths &paths)
+MiniaudioBackend::MiniaudioBackend()
+    : context(std::make_unique<ma_context>())
+    , engine(std::make_unique<ma_engine>())
+    , workGuard(boost::asio::make_work_guard(this->ioContext))
+    , sleepTimer(this->ioContext)
 {
-    (void)(settings);
-    (void)(paths);
+    qCInfo(chatterinoSound) << "Initializing miniaudio sound backend";
 
     boost::asio::post(this->ioContext, [this] {
         ma_result result{};
@@ -192,15 +195,6 @@ void MiniaudioBackend::initialize(Settings &settings, const Paths &paths)
     });
 }
 
-MiniaudioBackend::MiniaudioBackend()
-    : context(std::make_unique<ma_context>())
-    , engine(std::make_unique<ma_engine>())
-    , workGuard(boost::asio::make_work_guard(this->ioContext))
-    , sleepTimer(this->ioContext)
-{
-    qCInfo(chatterinoSound) << "Initializing miniaudio sound backend";
-}
-
 MiniaudioBackend::~MiniaudioBackend()
 {
     // NOTE: This destructor is never called because the `runGui` function calls _exit before that happens
diff --git a/src/controllers/sound/MiniaudioBackend.hpp b/src/controllers/sound/MiniaudioBackend.hpp
index 18ef9ed00e3..b8f359c3ce7 100644
--- a/src/controllers/sound/MiniaudioBackend.hpp
+++ b/src/controllers/sound/MiniaudioBackend.hpp
@@ -25,8 +25,6 @@ namespace chatterino {
  **/
 class MiniaudioBackend : public ISoundController
 {
-    void initialize(Settings &settings, const Paths &paths) override;
-
 public:
     MiniaudioBackend();
     ~MiniaudioBackend() override;