sound: Fix wrapping bug when using fill/copy/bulk-add on write_stream_views

This commit is contained in:
Aaron Giles 2021-04-21 22:48:24 -07:00
parent b4d73d64b1
commit 20fceccf39

View File

@ -371,13 +371,9 @@ public:
}
// safely write a sample to the buffer
void put(s32 index, sample_t sample)
void put(s32 start, sample_t sample)
{
sound_assert(u32(index) < samples());
index += m_start;
if (index >= m_buffer->size())
index -= m_buffer->size();
m_buffer->put(index, sample);
m_buffer->put(index_to_buffer_index(start), sample);
}
// write a sample to the buffer, clamping to +/- the clamp value
@ -401,12 +397,9 @@ public:
}
// safely add a sample to the buffer
void add(s32 index, sample_t sample)
void add(s32 start, sample_t sample)
{
sound_assert(u32(index) < samples());
index += m_start;
if (index >= m_buffer->size())
index -= m_buffer->size();
u32 index = index_to_buffer_index(start);
m_buffer->put(index, m_buffer->get(index) + sample);
}
@ -421,7 +414,7 @@ public:
{
if (start + count > samples())
count = samples() - start;
u32 index = start + m_start;
u32 index = index_to_buffer_index(start);
for (s32 sampindex = 0; sampindex < count; sampindex++)
{
m_buffer->put(index, value);
@ -436,7 +429,7 @@ public:
{
if (start + count > samples())
count = samples() - start;
u32 index = start + m_start;
u32 index = index_to_buffer_index(start);
for (s32 sampindex = 0; sampindex < count; sampindex++)
{
m_buffer->put(index, src.get(start + sampindex));
@ -451,7 +444,7 @@ public:
{
if (start + count > samples())
count = samples() - start;
u32 index = start + m_start;
u32 index = index_to_buffer_index(start);
for (s32 sampindex = 0; sampindex < count; sampindex++)
{
m_buffer->put(index, m_buffer->get(index) + src.get(start + sampindex));
@ -460,6 +453,17 @@ public:
}
void add(read_stream_view const &src, s32 start) { add(src, start, samples() - start); }
void add(read_stream_view const &src) { add(src, 0, samples()); }
private:
// given a stream starting offset, return the buffer index
u32 index_to_buffer_index(s32 start) const
{
sound_assert(u32(start) < samples());
u32 index = start + m_start;
if (index >= m_buffer->size())
index -= m_buffer->size();
return index;
}
};