Skip to content

Commit 376f6e5

Browse files
committed
Improve SoundQueue
1 parent e48f138 commit 376f6e5

File tree

2 files changed

+20
-5
lines changed

2 files changed

+20
-5
lines changed

platforms/shared/desktop/sound_queue.cpp

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -186,20 +186,35 @@ void SoundQueue::Write(int16_t* samples, int count, bool sync)
186186
if (m_write_position >= m_buffer_size)
187187
{
188188
m_write_position = 0;
189-
m_write_buffer = (m_write_buffer + 1) % m_buffer_count;
190-
189+
191190
if (m_sync_output)
191+
{
192+
m_write_buffer = (m_write_buffer + 1) % m_buffer_count;
192193
SDL_SemWait(m_free_sem);
194+
}
195+
else
196+
{
197+
int next = (m_write_buffer + 1) % m_buffer_count;
198+
if (next != m_read_buffer)
199+
m_write_buffer = next;
200+
}
193201
}
194202
}
195203
}
196204

197205
void SoundQueue::FillBuffer(uint8_t* buffer, int count)
198206
{
199-
if ((SDL_SemValue(m_free_sem) < (unsigned int)m_buffer_count - 1) || !m_sync_output)
207+
bool has_data;
208+
209+
if (m_sync_output)
210+
has_data = (SDL_SemValue(m_free_sem) < (unsigned int)m_buffer_count - 1);
211+
else
212+
has_data = (m_read_buffer != m_write_buffer);
213+
214+
if (has_data)
200215
{
201216
m_currently_playing = Buffer(m_read_buffer);
202-
memcpy( buffer, Buffer(m_read_buffer), count);
217+
memcpy(buffer, Buffer(m_read_buffer), count);
203218
m_read_buffer = (m_read_buffer + 1) % m_buffer_count;
204219

205220
if (m_sync_output)

platforms/shared/desktop/sound_queue.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ class SoundQueue
4141
SDL_sem* volatile m_free_sem;
4242
int16_t* volatile m_currently_playing;
4343
int volatile m_read_buffer;
44-
int m_write_buffer;
44+
int volatile m_write_buffer;
4545
int m_write_position;
4646
bool m_sound_open;
4747
bool m_sync_output;

0 commit comments

Comments
 (0)