From e13b53e02cb396acc781700fd2f40a667d1e64e9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Henrik=20Rydg=C3=A5rd?= Date: Sun, 2 Aug 2020 19:56:57 +0200 Subject: [PATCH] BackgroundAudio: Switch to a heap allocated buffer. --- UI/BackgroundAudio.cpp | 10 +++++++++- UI/BackgroundAudio.h | 10 +++++++++- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/UI/BackgroundAudio.cpp b/UI/BackgroundAudio.cpp index dc585e6a0d..defc30068a 100644 --- a/UI/BackgroundAudio.cpp +++ b/UI/BackgroundAudio.cpp @@ -226,6 +226,14 @@ private: BackgroundAudio g_BackgroundAudio; +BackgroundAudio::BackgroundAudio() { + buffer = new int[BUFSIZE](); +} + +BackgroundAudio::~BackgroundAudio() { + delete[] buffer; +} + void BackgroundAudio::Clear(bool hard) { if (!hard) { fadingOut = true; @@ -276,7 +284,7 @@ int BackgroundAudio::Play() { double now = time_now(); if (at3Reader) { int sz = lastPlaybackTime <= 0.0 ? 44100 / 60 : (int)((now - lastPlaybackTime) * 44100); - sz = std::min((int)ARRAY_SIZE(buffer) / 2, sz); + sz = std::min(BUFSIZE / 2, sz); if (sz >= 16) { if (at3Reader->Read(buffer, sz)) { if (!fadingOut) { diff --git a/UI/BackgroundAudio.h b/UI/BackgroundAudio.h index 04adcb8a25..d5d8ca7f18 100644 --- a/UI/BackgroundAudio.h +++ b/UI/BackgroundAudio.h @@ -1,23 +1,31 @@ #pragma once #include +#include class AT3PlusReader; class BackgroundAudio { public: + BackgroundAudio(); + ~BackgroundAudio(); + void Clear(bool hard); void SetGame(const std::string &path); void Update(); int Play(); private: + enum { + BUFSIZE = 44100, + }; + std::mutex g_bgMutex; std::string bgGamePath; int playbackOffset = 0; AT3PlusReader *at3Reader; double gameLastChanged = 0.0; double lastPlaybackTime = 0.0; - int buffer[44100]; + int *buffer = nullptr; bool fadingOut = true; float volume = 0.0f; float delta = -0.0001f;