Etterna 0.74.4
Loading...
Searching...
No Matches
RageSoundReader_ThreadedBuffer.h
1/* RageSoundReader_ThreadedBuffer - Buffer sounds into memory. */
2
3#ifndef RAGE_SOUND_READER_THREADED_BUFFER
4#define RAGE_SOUND_READER_THREADED_BUFFER
5
6#include "RageSoundReader_Filter.h"
7#include "RageUtil/Misc/RageThreads.h"
8#include "RageUtil/Utils/RageUtil_CircularBuffer.h"
9#include <list>
10
11class RageThread;
13{
14 public:
18 RageSoundReader_ThreadedBuffer* Copy() const override
19 {
20 return new RageSoundReader_ThreadedBuffer(*this);
21 }
22
23 int SetPosition(int iFrame) override;
24 int Read(float* pBuffer, int iLength) override;
25 int GetNextSourceFrame() const override;
26
27 int GetLength() const override;
28 int GetLength_Fast() const override;
29 int GetSampleRate() const override { return m_iSampleRate; }
30 unsigned GetNumChannels() const override { return m_iChannels; }
31 bool SetProperty(const std::string& sProperty, float fValue) override;
32 float GetStreamToSourceRatio() const override;
33 RageSoundReader* GetSource() override { return NULL; }
34
35 /* Enable and disable threaded buffering. Disable buffering before
36 * accessing the underlying sound. DisableBuffering returns true if
37 * buffering was enabled. */
38 void EnableBuffering();
39 void EnableBuffering() const
40 {
41 const_cast<RageSoundReader_ThreadedBuffer*>(this)->EnableBuffering();
42 }
43 bool DisableBuffering();
44 bool DisableBuffering() const
45 {
46 return const_cast<RageSoundReader_ThreadedBuffer*>(this)
47 ->DisableBuffering();
48 }
49
50 private:
51 int FillFrames(int iBytes);
52 int FillBlock();
53 int GetFilledFrames() const;
54 int GetEmptyFrames() const;
55 void WaitUntilFrames(int iWaitUntilFrames);
56
57 int m_iSampleRate;
58 int m_iChannels;
59
60 CircBuf<float> m_DataBuffer;
61
62 struct Mapping
63 {
64 int iFramesBuffered{ 0 };
65 int iPositionOfFirstFrame{ 0 };
66 float fRate{ 1.0f };
67 Mapping() = default;
68 };
69 std::list<Mapping> m_StreamPosition;
70
71 bool m_bEOF;
72
73 bool m_bEnabled;
74
75 /* If this is true, the buffering thread owns m_pSource, even
76 * if m_Event is unlocked. */
77 bool m_bFilling;
78
79 mutable RageEvent m_Event;
80
81 RageThread m_Thread;
82 bool m_bShutdownThread;
83 static int StartBufferingThread(void* p)
84 {
85 (reinterpret_cast<RageSoundReader_ThreadedBuffer*>(p))
86 ->BufferingThread();
87 return 0;
88 }
89 void BufferingThread();
90};
91
92#endif
Definition RageUtil_CircularBuffer.h:10
Definition RageThreads.h:309
Definition RageSoundReader_Filter.h:10
Definition RageSoundReader_ThreadedBuffer.h:13
Definition RageSoundReader.h:7
Thread, mutex, semaphore, and event classes.
Definition RageThreads.h:155