galaxy 1.0.0
Real-Time C++23 Game Programming Framework. Built on data-driven design principles and agile software engineering.
Loading...
Searching...
No Matches
RingBuffer.cpp
Go to the documentation of this file.
1
7
8#include <cmath>
9#include <utility>
10
12
13#include "RingBuffer.hpp"
14
15#ifdef GALAXY_WIN_PLATFORM
16#pragma warning(push)
17#pragma warning(disable : 26401)
18#pragma warning(disable : 26409)
19#pragma warning(disable : 26461)
20#endif
21
22namespace galaxy
23{
24 namespace mem
25 {
26 RingBuffer::RingBuffer(const int capacity) noexcept
27 : m_read_index {0}
28 , m_write_index {0}
29 , m_capacity {capacity}
30 {
31 m_buffer = new float[m_capacity];
32 set_volume(1.0f);
33 }
34
36 {
37 delete[] m_buffer;
38 m_buffer = nullptr;
39 }
40
41 int RingBuffer::write(float* data, const int count) noexcept
42 {
43 for (auto i = 0; i < count; i++)
44 {
45 const auto pos = (m_write_index + i) % m_capacity;
46 m_buffer[pos] = data[i];
47 }
48
49 m_write_index = (m_write_index + count) % m_capacity;
50
51 return count;
52 }
53
54 int RingBuffer::read(float* data, int count) noexcept
55 {
56 for (auto i = 0; i < count; i++)
57 {
58 const auto pos = (m_read_index + i) % m_capacity;
59 data[i] = m_buffer[pos];
60 }
61
62 m_read_index = (m_read_index + count) % m_capacity;
63
64 return count;
65 }
66
67 float* RingBuffer::direct_read_pointer(const int count) noexcept
68 {
69 float* result = &m_buffer[m_read_index % m_capacity];
70 m_read_index = (m_read_index + count) % m_capacity;
71 return result;
72 }
73
74 int RingBuffer::available_bytes() const noexcept
75 {
77 }
78
79 void RingBuffer::set_volume(const float volume) noexcept
80 {
81 const auto gain = (std::max(std::min(volume, 1.0f), 0.0f) * 50) - 50;
82 const auto factor = std::pow(10, gain * 0.05f);
83
84 m_volume = static_cast<float>(std::max(std::min(1.0 * factor, 1.0), 0.0));
85 }
86
87 float RingBuffer::get_volume() const noexcept
88 {
89 return m_volume;
90 }
91 } // namespace mem
92} // namespace galaxy
93
94#ifdef GALAXY_WIN_PLATFORM
95#pragma warning(pop)
96#endif
float m_volume
Current volume of audio.
int read(float *data, int count) noexcept
Read data from buffer.
int m_write_index
Current index for writing to buffer.
int write(float *data, const int count) noexcept
Write data to ring buffer.
int m_capacity
Amount of data buffer can hold in total.
void set_volume(const float volume) noexcept
Sets audio volume of the buffer.
float * m_buffer
Data buffer.
int available_bytes() const noexcept
Available bytes left in the ring buffer.
RingBuffer()=delete
Constructor.
int m_read_index
Current index for reading from buffer.
float get_volume() const noexcept
Gets current buffer volume.
~RingBuffer() noexcept
Destructor.
float * direct_read_pointer(const int count) noexcept
Read the ring buffer.
Timer.hpp galaxy.
Definition Async.hpp:17