Skip to content

Commit

Permalink
add option to adjust audio sync
Browse files Browse the repository at this point in the history
  • Loading branch information
phandasm committed Feb 25, 2024
1 parent b6d7f98 commit a8a0d53
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 2 deletions.
3 changes: 3 additions & 0 deletions data/locale/en-US.ini
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,8 @@ step_width="Step Width"
step_gap="Step Gap"
min_bar_height="Minimum Bar Height"

audio_sync_offset="Audio Sync Offset"

chan_desc="Graph separate L/R channels, mono mixdown, or individual channel."
auto_fft_desc="Calculate FFT size based on FPS and sample rate. You probably don't want this. Retained for backwards compatibility."
fft_desc="Larger values increase frequency resolution, but are more CPU intensive and increase latency."
Expand All @@ -128,3 +130,4 @@ mirror_desc="Reflect graph horizontally around the center."
radial_arc_desc="Arc angle of radial display in degrees."
ignore_mute_desc="Continue processing audio even when source is muted."
lage_fft_desc="Allow large FFT sizes that may significantly increase latency and resource consumption."
audio_sync_desc="Positive values delay visuals, negative values may not work depending on source."
2 changes: 2 additions & 0 deletions src/settings.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ static inline bool p_equ(const char *s1, const char *s2) { return std::strcmp(s1
#define P_STEP_GAP "step_gap"
#define P_MIN_BAR_HEIGHT "min_bar_height"

#define P_AUDIO_SYNC_OFFSET "audio_sync_offset"

// tooltip descriptions
#define P_CHAN_DESC "chan_desc"
Expand All @@ -153,3 +154,4 @@ static inline bool p_equ(const char *s1, const char *s2) { return std::strcmp(s1
#define P_RADIAL_ARC_DESC "radial_arc_desc"
#define P_IGNORE_MUTE_DESC "ignore_mute_desc"
#define P_LARGE_FFT_DESC "lage_fft_desc"
#define P_AUDIO_SYNC_DESC "audio_sync_desc"
7 changes: 7 additions & 0 deletions src/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@ namespace callbacks {
obs_data_set_default_bool(settings, P_NORMALIZE_VOLUME, false);
obs_data_set_default_int(settings, P_VOLUME_TARGET, -8);
obs_data_set_default_int(settings, P_MAX_GAIN, 30);
obs_data_set_default_int(settings, P_AUDIO_SYNC_OFFSET, 0);
}

static obs_properties_t *get_properties([[maybe_unused]] void *data)
Expand All @@ -190,6 +191,11 @@ namespace callbacks {
for(const auto& str : enumerate_audio_sources())
obs_property_list_add_string(srclist, str.c_str(), str.c_str());

// audio sync
auto audio_sync = obs_properties_add_int_slider(props, P_AUDIO_SYNC_OFFSET, T(P_AUDIO_SYNC_OFFSET), -1000, 1000, 10);
obs_property_int_set_suffix(audio_sync, " ms");
obs_property_set_long_description(audio_sync, T(P_AUDIO_SYNC_DESC));

// hide on silent audio
obs_properties_add_bool(props, P_HIDE_SILENT, T(P_HIDE_SILENT));

Expand Down Expand Up @@ -547,6 +553,7 @@ void WAVSource::get_settings(obs_data_t *settings)
m_normalize_volume = obs_data_get_bool(settings, P_NORMALIZE_VOLUME);
m_volume_target = (float)obs_data_get_int(settings, P_VOLUME_TARGET);
m_max_gain = (float)obs_data_get_int(settings, P_MAX_GAIN);
m_ts_offset = (int64_t)obs_data_get_int(settings, P_AUDIO_SYNC_OFFSET) * 1000000ll;

m_color_base = { {{(uint8_t)color_base / 255.0f, (uint8_t)(color_base >> 8) / 255.0f, (uint8_t)(color_base >> 16) / 255.0f, (uint8_t)(color_base >> 24) / 255.0f}} };
m_color_middle = { {{(uint8_t)color_middle / 255.0f, (uint8_t)(color_middle >> 8) / 255.0f, (uint8_t)(color_middle >> 16) / 255.0f, (uint8_t)(color_middle >> 24) / 255.0f}} };
Expand Down
6 changes: 4 additions & 2 deletions src/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ class WAVSource
uint64_t m_capture_ts = 0; // timestamp of last audio callback in nanoseconds
uint64_t m_audio_ts = 0; // timestamp of the end of available audio in nanoseconds
uint64_t m_tick_ts = 0; // timestamp of last 'tick' in nanoseconds
int64_t m_ts_offset = 0; // audio sync offset in nanoseconds

// settings
RenderMode m_render_mode = RenderMode::SOLID;
Expand Down Expand Up @@ -270,9 +271,10 @@ class WAVSource

int64_t get_audio_sync(uint64_t ts) // get delta between end of available audio and given time in nanoseconds
{
auto delta = std::max(m_audio_ts, ts) - std::min(m_audio_ts, ts);
auto audio_ts = m_audio_ts + m_ts_offset;
auto delta = std::max(audio_ts, ts) - std::min(audio_ts, ts);
delta = std::min(delta, MAX_TS_DELTA);
return (m_audio_ts < ts) ? -(int64_t)delta : (int64_t)delta;
return (audio_ts < ts) ? -(int64_t)delta : (int64_t)delta;
}

// constants
Expand Down

0 comments on commit a8a0d53

Please sign in to comment.