Skip to content

Commit

Permalink
minor refactoring and cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
phandasm committed May 10, 2023
1 parent 98ace44 commit 5921a14
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 47 deletions.
9 changes: 9 additions & 0 deletions src/source.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1145,8 +1145,17 @@ void WAVSource::update(obs_data_t *settings)
void WAVSource::tick(float seconds)
{
std::lock_guard lock(m_mtx);

m_tick_ts = os_gettime_ns();

if(m_normalize_volume)
update_input_rms();

if(!check_audio_capture(seconds))
return;
if(m_capture_channels == 0)
return;

if(m_meter_mode)
tick_meter(seconds);
else
Expand Down
5 changes: 3 additions & 2 deletions src/source.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,9 @@ class WAVSource
int m_retries = 0;
float m_next_retry = 0.0f;

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_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

// settings
RenderMode m_render_mode = RenderMode::SOLID;
Expand Down
28 changes: 6 additions & 22 deletions src/source_avx.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,27 +20,19 @@
#include <immintrin.h>
#include <algorithm>
#include <cstring>
#include <util/platform.h>
#include <cassert>

// adaptation of WAVSourceAVX2 to support CPUs without AVX2
// see comments of WAVSourceAVX2
void WAVSourceAVX::tick_spectrum(float seconds)
void WAVSourceAVX::tick_spectrum([[maybe_unused]] float seconds)
{
//std::lock_guard lock(m_mtx); // now locked in tick()
auto cur_ts = os_gettime_ns();

if(!check_audio_capture(seconds))
return;

if(m_capture_channels == 0)
return;

const auto bufsz = m_fft_size * sizeof(float);
const auto outsz = m_fft_size / 2;
constexpr auto step = sizeof(__m256) / sizeof(float);

const auto dtcapture = cur_ts - m_capture_ts;
const auto dtcapture = m_tick_ts - m_capture_ts;

if(!m_show || (dtcapture > CAPTURE_TIMEOUT))
{
Expand All @@ -56,7 +48,7 @@ void WAVSourceAVX::tick_spectrum(float seconds)
return;
}

const int64_t dtaudio = get_audio_sync(cur_ts);
const int64_t dtaudio = get_audio_sync(m_tick_ts);
const size_t dtsize = ((dtaudio > 0) ? size_t(ns_to_audio_frames(m_audio_info.samples_per_sec, (uint64_t)dtaudio)) * sizeof(float) : 0) + bufsz;
auto silent_channels = 0u;
for(auto channel = 0u; channel < m_capture_channels; ++channel)
Expand Down Expand Up @@ -205,18 +197,10 @@ void WAVSourceAVX::tick_spectrum(float seconds)
}
}

void WAVSourceAVX::tick_meter(float seconds)
void WAVSourceAVX::tick_meter([[maybe_unused]] float seconds)
{
auto cur_ts = os_gettime_ns();

if(!check_audio_capture(seconds))
return;

if(m_capture_channels == 0)
return;

// handle audio dropouts
const auto dtcapture = os_gettime_ns() - m_capture_ts;
const auto dtcapture = m_tick_ts - m_capture_ts;
if(dtcapture > CAPTURE_TIMEOUT)
{
if(m_last_silent)
Expand All @@ -235,7 +219,7 @@ void WAVSourceAVX::tick_meter(float seconds)
return;
}

const int64_t dtaudio = get_audio_sync(cur_ts);
const int64_t dtaudio = get_audio_sync(m_tick_ts);
const size_t dtsize = (dtaudio > 0) ? size_t(ns_to_audio_frames(m_audio_info.samples_per_sec, (uint64_t)dtaudio)) * sizeof(float) : 0;

// repurpose m_decibels as circular buffer for sample data
Expand Down
2 changes: 1 addition & 1 deletion src/source_avx2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
#include <cstring>
#include <util/platform.h>

void WAVSourceAVX2::tick_spectrum(float seconds)
void WAVSourceAVX2::tick_spectrum([[maybe_unused]] float seconds)
{
//std::lock_guard lock(m_mtx); // now locked in tick()
auto cur_ts = os_gettime_ns();
Expand Down
28 changes: 6 additions & 22 deletions src/source_generic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,27 +19,19 @@
#include <algorithm>
#include <cstring>
#include <cmath>
#include <util/platform.h>
#include <cassert>

// portable non-SIMD implementation
// see comments of WAVSourceAVX2 and WAVSourceAVX
void WAVSourceGeneric::tick_spectrum(float seconds)
void WAVSourceGeneric::tick_spectrum([[maybe_unused]] float seconds)
{
//std::lock_guard lock(m_mtx); // now locked in tick()
auto cur_ts = os_gettime_ns();

if(!check_audio_capture(seconds))
return;

if(m_capture_channels == 0)
return;

const auto bufsz = m_fft_size * sizeof(float);
const auto outsz = m_fft_size / 2;
constexpr auto step = 1;

const auto dtcapture = cur_ts - m_capture_ts;
const auto dtcapture = m_tick_ts - m_capture_ts;

if(!m_show || (dtcapture > CAPTURE_TIMEOUT))
{
Expand All @@ -55,7 +47,7 @@ void WAVSourceGeneric::tick_spectrum(float seconds)
return;
}

const int64_t dtaudio = get_audio_sync(cur_ts);
const int64_t dtaudio = get_audio_sync(m_tick_ts);
const size_t dtsize = ((dtaudio > 0) ? size_t(ns_to_audio_frames(m_audio_info.samples_per_sec, (uint64_t)dtaudio)) * sizeof(float) : 0) + bufsz;
auto silent_channels = 0u;
for(auto channel = 0u; channel < m_capture_channels; ++channel)
Expand Down Expand Up @@ -187,17 +179,9 @@ void WAVSourceGeneric::tick_spectrum(float seconds)
}
}

void WAVSourceGeneric::tick_meter(float seconds)
void WAVSourceGeneric::tick_meter([[maybe_unused]] float seconds)
{
auto cur_ts = os_gettime_ns();

if(!check_audio_capture(seconds))
return;

if(m_capture_channels == 0)
return;

const auto dtcapture = os_gettime_ns() - m_capture_ts;
const auto dtcapture = m_tick_ts - m_capture_ts;
if(dtcapture > CAPTURE_TIMEOUT)
{
if(m_last_silent)
Expand All @@ -215,7 +199,7 @@ void WAVSourceGeneric::tick_meter(float seconds)
}

const auto outsz = m_fft_size;
const int64_t dtaudio = get_audio_sync(cur_ts);
const int64_t dtaudio = get_audio_sync(m_tick_ts);
const size_t dtsize = (dtaudio > 0) ? size_t(ns_to_audio_frames(m_audio_info.samples_per_sec, (uint64_t)dtaudio)) * sizeof(float) : 0;

for(auto channel = 0u; channel < m_capture_channels; ++channel)
Expand Down

0 comments on commit 5921a14

Please sign in to comment.