From 08bdab3b6550b87ca839ebf9357598bcb0de614c Mon Sep 17 00:00:00 2001 From: intealls Date: Tue, 17 Jan 2017 23:50:06 +0100 Subject: [PATCH 1/4] fix allowed latency range --- src/osd/modules/lib/osdobj_common.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/osd/modules/lib/osdobj_common.cpp b/src/osd/modules/lib/osdobj_common.cpp index dcca53b4361..5bacf0c341d 100644 --- a/src/osd/modules/lib/osdobj_common.cpp +++ b/src/osd/modules/lib/osdobj_common.cpp @@ -131,7 +131,7 @@ const options_entry osd_options::s_option_entries[] = { nullptr, nullptr, OPTION_HEADER, "PORTAUDIO OPTIONS" }, { OSDOPTION_PA_API, OSDOPTVAL_NONE, OPTION_STRING, "PortAudio API" }, { OSDOPTION_PA_DEVICE, OSDOPTVAL_NONE, OPTION_STRING, "PortAudio device" }, - { OSDOPTION_PA_LATENCY "(0.001-0.25)", "0", OPTION_FLOAT, "suggested latency in seconds, 0 for default" }, + { OSDOPTION_PA_LATENCY "(0-0.25)", "0", OPTION_FLOAT, "suggested latency in seconds, 0 for default" }, #endif #ifdef SDLMAME_MACOSX From 9422c230013f0281c0fe940fab411af6a12458f6 Mon Sep 17 00:00:00 2001 From: intealls Date: Wed, 18 Jan 2017 04:35:06 +0100 Subject: [PATCH 2/4] Simplify the audio buffer and fix a wrap-around issue --- src/osd/modules/sound/pa_sound.cpp | 28 ++++++++++++---------------- 1 file changed, 12 insertions(+), 16 deletions(-) diff --git a/src/osd/modules/sound/pa_sound.cpp b/src/osd/modules/sound/pa_sound.cpp index fb92689504f..78389d85e49 100644 --- a/src/osd/modules/sound/pa_sound.cpp +++ b/src/osd/modules/sound/pa_sound.cpp @@ -57,7 +57,7 @@ private: int reserve; std::atomic rd_pos, wr_pos; - audio_buffer(int size, int reserve) : size(size), reserve(reserve) { + audio_buffer(int size, int reserve) : size(size + reserve), reserve(reserve) { rd_pos = wr_pos = 0; buf = new T[size](); } @@ -65,11 +65,8 @@ private: ~audio_buffer() { delete[] buf; } int count() { - int diff; - diff = wr_pos - rd_pos; - diff = diff < 0 ? size + diff : diff; - diff -= reserve; - return diff < 0 ? 0 : diff; + int diff = wr_pos - rd_pos; + return diff < 0 ? size + diff : diff; } void increment_wrpos(int n) { @@ -77,7 +74,7 @@ private: } int write(const T* src, int n, int attenuation) { - n = std::min(n, size - count()); + n = std::min(n, size - reserve - count()); if (wr_pos + n > size) { att_memcpy(buf + wr_pos, src, sizeof(T) * (size - wr_pos), attenuation); @@ -111,7 +108,7 @@ private: } int clear(int n) { - n = std::min(n, size - count()); + n = std::min(n, size - reserve - count()); if (wr_pos + n > size) { std::memset(buf + wr_pos, 0, sizeof(T) * (size - wr_pos)); @@ -186,6 +183,8 @@ int sound_pa::init(osd_options const &options) m_has_overflowed = false; m_has_underflowed = false; m_skip_threshold_ticks = 0; + m_osd_ticks = 0; + m_osd_tps = osd_ticks_per_second(); try { m_ab = new audio_buffer(m_sample_rate, 2); @@ -194,8 +193,6 @@ int sound_pa::init(osd_options const &options) goto error; } - m_osd_tps = osd_ticks_per_second(); - err = Pa_Initialize(); if (err != paNoError) goto pa_error; @@ -355,9 +352,8 @@ int sound_pa::callback(s16* output_buffer, size_t number_of_samples) m_ab->read(output_buffer, buf_ct); std::memset(output_buffer + buf_ct, 0, (number_of_samples - buf_ct) * sizeof(s16)); - // rd_pos == wr_pos only happens when buffer hasn't received any samples, - // i.e. before update_audio_stream has been called - if (m_ab->rd_pos != m_ab->wr_pos) + // if update_audio_stream has been called, note the underflow + if (m_osd_ticks) m_has_underflowed = true; m_skip_threshold_ticks = m_osd_ticks; @@ -371,9 +367,6 @@ void sound_pa::update_audio_stream(bool is_throttled, const s16 *buffer, int sam if (!sample_rate()) return; - // for determining buffer overflows, take the sample here instead of in the callback - m_osd_ticks = osd_ticks(); - #if LOG_BUFCNT if (m_log.good()) m_log << m_ab->count() << std::endl; @@ -394,6 +387,9 @@ void sound_pa::update_audio_stream(bool is_throttled, const s16 *buffer, int sam } m_ab->write(buffer, samples_this_frame * 2, m_attenuation); + + // for determining buffer overflows, take the sample here instead of in the callback + m_osd_ticks = osd_ticks(); } void sound_pa::set_mastervolume(int attenuation) From 86cbd2acf60dcf4bc2bd4a95eaf12d8088f20978 Mon Sep 17 00:00:00 2001 From: intealls Date: Thu, 19 Jan 2017 00:21:10 +0100 Subject: [PATCH 3/4] fix up initialization --- src/osd/modules/sound/pa_sound.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/osd/modules/sound/pa_sound.cpp b/src/osd/modules/sound/pa_sound.cpp index 78389d85e49..7e65bc143d8 100644 --- a/src/osd/modules/sound/pa_sound.cpp +++ b/src/osd/modules/sound/pa_sound.cpp @@ -182,9 +182,10 @@ int sound_pa::init(osd_options const &options) m_overflows = 0; m_has_overflowed = false; m_has_underflowed = false; - m_skip_threshold_ticks = 0; m_osd_ticks = 0; + m_skip_threshold_ticks = 0; m_osd_tps = osd_ticks_per_second(); + m_buffer_min_ct = INT_MAX; try { m_ab = new audio_buffer(m_sample_rate, 2); From ff0f5b22417b56d32af73e11eaecaeb9207667a6 Mon Sep 17 00:00:00 2001 From: intealls Date: Thu, 19 Jan 2017 00:37:38 +0100 Subject: [PATCH 4/4] rename variables to maintain consistent with coreaudio/sdl etc --- src/osd/modules/sound/pa_sound.cpp | 48 +++++++++++++++--------------- 1 file changed, 24 insertions(+), 24 deletions(-) diff --git a/src/osd/modules/sound/pa_sound.cpp b/src/osd/modules/sound/pa_sound.cpp index 7e65bc143d8..a54adca958b 100644 --- a/src/osd/modules/sound/pa_sound.cpp +++ b/src/osd/modules/sound/pa_sound.cpp @@ -49,60 +49,60 @@ public: virtual void set_mastervolume(int attenuation); private: - /* Lock free SPSC ring buffer */ + // Lock free SPSC ring buffer template struct audio_buffer { T* buf; int size; int reserve; - std::atomic rd_pos, wr_pos; + std::atomic playpos, writepos; audio_buffer(int size, int reserve) : size(size + reserve), reserve(reserve) { - rd_pos = wr_pos = 0; + playpos = writepos = 0; buf = new T[size](); } ~audio_buffer() { delete[] buf; } int count() { - int diff = wr_pos - rd_pos; + int diff = writepos - playpos; return diff < 0 ? size + diff : diff; } - void increment_wrpos(int n) { - wr_pos = (wr_pos + n) % size; + void increment_writepos(int n) { + writepos.store((writepos + n) % size); } int write(const T* src, int n, int attenuation) { n = std::min(n, size - reserve - count()); - if (wr_pos + n > size) { - att_memcpy(buf + wr_pos, src, sizeof(T) * (size - wr_pos), attenuation); - att_memcpy(buf, src + (size - wr_pos), sizeof(T) * (n - (size - wr_pos)), attenuation); + if (writepos + n > size) { + att_memcpy(buf + writepos, src, sizeof(T) * (size - writepos), attenuation); + att_memcpy(buf, src + (size - writepos), sizeof(T) * (n - (size - writepos)), attenuation); } else { - att_memcpy(buf + wr_pos, src, sizeof(T) * n, attenuation); + att_memcpy(buf + writepos, src, sizeof(T) * n, attenuation); } - increment_wrpos(n); + increment_writepos(n); return n; } - void increment_rdpos(int n) { - rd_pos = (rd_pos + n) % size; + void increment_playpos(int n) { + playpos.store((playpos + n) % size); } int read(T* dst, int n) { n = std::min(n, count()); - if (rd_pos + n > size) { - std::memcpy(dst, buf + rd_pos, sizeof(T) * (size - rd_pos)); - std::memcpy(dst + (size - rd_pos), buf, sizeof(T) * (n - (size - rd_pos))); + if (playpos + n > size) { + std::memcpy(dst, buf + playpos, sizeof(T) * (size - playpos)); + std::memcpy(dst + (size - playpos), buf, sizeof(T) * (n - (size - playpos))); } else { - std::memcpy(dst, buf + rd_pos, sizeof(T) * n); + std::memcpy(dst, buf + playpos, sizeof(T) * n); } - increment_rdpos(n); + increment_playpos(n); return n; } @@ -110,14 +110,14 @@ private: int clear(int n) { n = std::min(n, size - reserve - count()); - if (wr_pos + n > size) { - std::memset(buf + wr_pos, 0, sizeof(T) * (size - wr_pos)); - std::memset(buf, 0, sizeof(T) * (n - (size - wr_pos))); + if (writepos + n > size) { + std::memset(buf + writepos, 0, sizeof(T) * (size - writepos)); + std::memset(buf, 0, sizeof(T) * (n - (size - writepos))); } else { - std::memset(buf + wr_pos, 0, sizeof(T) * n); + std::memset(buf + writepos, 0, sizeof(T) * n); } - increment_wrpos(n); + increment_writepos(n); return n; } @@ -340,7 +340,7 @@ int sound_pa::callback(s16* output_buffer, size_t number_of_samples) // if adjustment is less than two milliseconds, don't bother if (adjust / 2 > sample_rate() / 500) { - m_ab->increment_rdpos(adjust); + m_ab->increment_playpos(adjust); m_has_overflowed = true; }