From ab73c2f539bdebfd12391db6cb3af99a762e898c Mon Sep 17 00:00:00 2001 From: erorcun Date: Fri, 25 Jun 2021 05:06:38 +0300 Subject: Multi-threaded audio streams Under MULTITHREADED_AUDIO define. --- src/audio/oal/stream.h | 87 ++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 81 insertions(+), 6 deletions(-) (limited to 'src/audio/oal/stream.h') diff --git a/src/audio/oal/stream.h b/src/audio/oal/stream.h index 9a2a2fbe..bdbf19e0 100644 --- a/src/audio/oal/stream.h +++ b/src/audio/oal/stream.h @@ -11,6 +11,7 @@ public: virtual ~IDecoder() { } virtual bool IsOpened() = 0; + virtual void FileOpen() = 0; virtual uint32 GetSampleSize() = 0; virtual uint32 GetSampleCount() = 0; @@ -48,12 +49,70 @@ public: uint32 GetLength() { + FileOpen(); // abort deferred init, we need length now - game has to cache audio file sizes return float(GetSampleCount()) * 1000.0f / float(GetSampleRate()); } virtual uint32 Decode(void *buffer) = 0; }; +#ifdef MULTITHREADED_AUDIO +template class tsQueue +{ +public: + tsQueue() : count(0) { } + + void push(const T &value) + { + std::lock_guard lock(m_mutex); + m_queue.push(value); + count++; + } + + bool peekPop(T *retVal) + { + std::lock_guard lock(m_mutex); + if (count == 0) + return false; + + *retVal = m_queue.front(); + m_queue.pop(); + count--; + return true; + } + + void swapNts(tsQueue &replaceWith) + { + m_queue.swap(replaceWith.m_queue); + replaceWith.count = count; + } + + /* + void swapTs(tsQueue &replaceWith) + { + std::lock_guard lock(m_mutex); + std::lock_guard lock2(replaceWith.m_mutex); + swapNts(replaceWith); + } + */ + + bool emptyNts() + { + return count == 0; + } + /* + bool emptyTs() + { + std::lock_guard lock(m_mutex); + return emptyNts(); + } + */ + + std::queue m_queue; + int count; + mutable std::mutex m_mutex; +}; +#endif class CStream { char m_aFilename[128]; @@ -63,6 +122,16 @@ class CStream bool m_bPaused; bool m_bActive; +public: +#ifdef MULTITHREADED_AUDIO + std::mutex m_mutex; + std::queue> m_fillBuffers; // left and right buffer + tsQueue> m_queueBuffers; + bool m_bDoSeek; + uint32 m_SeekPos; + uint8 m_nDeleteMe; // 1: add to delete list 2: already on delete list +#endif + void *m_pBuffer; bool m_bReset; @@ -72,7 +141,14 @@ class CStream int32 m_nLoopCount; IDecoder *m_pSoundFile; - + + void BuffersShouldBeFilled(); // all + bool BufferShouldBeFilledAndQueued(std::pair*); // two (left-right) +#ifdef MULTITHREADED_AUDIO + void FlagAsToBeProcessed(bool close = false); + bool QueueBuffers(); +#endif + bool HasSource(); void SetPosition(int i, float x, float y, float z); void SetPitch(float pitch); @@ -81,15 +157,15 @@ class CStream void SetPlay(bool state); bool FillBuffer(ALuint *alBuffer); - int32 FillBuffers(); + int32 FillBuffers(); void ClearBuffers(); -public: +//public: static void Initialise(); static void Terminate(); CStream(char *filename, ALuint *sources, ALuint (&buffers)[NUM_STREAMBUFFERS], uint32 overrideSampleRate = 32000); ~CStream(); - void Delete(); + void Close(); bool IsOpened(); bool IsPlaying(); @@ -100,12 +176,11 @@ public: uint32 GetPosMS(); uint32 GetLengthMS(); - bool Setup(bool imSureQueueIsEmpty = false); + bool Setup(bool imSureQueueIsEmpty = false, bool lock = true); void Start(); void Stop(); void Update(void); void SetLoopCount(int32); - void ProviderInit(); void ProviderTerm(); -- cgit v1.2.3