Skip to content

Commit

Permalink
audio: fix ossia/score#1654
Browse files Browse the repository at this point in the history
  • Loading branch information
jcelerier committed Jan 2, 2025
1 parent d0976a6 commit 8b29d06
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 9 deletions.
17 changes: 14 additions & 3 deletions src/ossia/audio/audio_parameter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,18 @@ audio_parameter::~audio_parameter() = default;

void audio_parameter::clone_value(audio_vector& res_vec) const
{
if(res_vec.size() < audio.size())
ossia::audio_buffer_pool::set_channels(res_vec, audio.size());
{
int orig_dst_size = res_vec.size();
if(orig_dst_size < audio.size())
{
ossia::audio_buffer_pool::set_channels(res_vec, audio.size());

for(std::size_t chan = orig_dst_size; chan < res_vec.size(); chan++)
{
res_vec[chan].clear();
}
}
}

auto min_chan = std::min(res_vec.size(), audio.size());
for(std::size_t chan = 0; chan < min_chan; chan++)
Expand Down Expand Up @@ -47,7 +57,8 @@ void audio_parameter::push_value(const audio_port& port)
const auto N = std::min(src.size(), (std::size_t)dst.size());
for(std::size_t i = 0; i < N; i++)
{
dst[i] += float(src[i] * m_gain);
// Important: here we must not mix
dst[i] = float(src[i] * m_gain);
}
}
}
Expand Down
1 change: 0 additions & 1 deletion src/ossia/audio/audio_parameter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,6 @@ class OSSIA_EXPORT virtual_audio_parameter final : public audio_parameter
{
m_audio_data[i].resize(bs);
audio[i] = m_audio_data[i];
ossia::fill(m_audio_data[i], 0.f);
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/ossia/dataflow/audio_port.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,19 @@ void ensure_vector_sizes(const audio_vector& src_vec, audio_vector& sink_vec);
OSSIA_EXPORT
void mix(const audio_vector& src_vec, audio_vector& sink_vec);

struct OSSIA_EXPORT audio_buffer_pool : object_pool<audio_channel>
struct OSSIA_EXPORT audio_buffer_pool : private object_pool<audio_channel>
{
audio_buffer_pool();
~audio_buffer_pool();

using object_pool::acquire;

void release(audio_channel&& b)
{
b.clear();
buffers.enqueue(std::move(b));
}

static audio_buffer_pool& instance() noexcept;

static void set_channels(audio_vector& samples, std::size_t channels);
Expand Down
14 changes: 11 additions & 3 deletions src/ossia/dataflow/execution/local_state_execution_policy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ local_state_execution_policy::~local_state_execution_policy() { }
void local_state_execution_policy::commit_common()
{
#if defined(OSSIA_PROTOCOL_AUDIO)
// FIXME to solve the deeper issue with virtual audio cables,
// execution should be :
// 1. read the addresses from all ports that need that into respective processes
// 2. clear the local state
// 3. start execution the tick
// otherwise there's no way to have mixing of data in virtual ports as we cannot clear
// at the beginning of the tick (won't be able to read from previous tick) or at the
// end of the tick (the data that was just written will get deleted).

// FIXME this does not look necessary?
// Why not just push to the audio address
for(auto& elt : m_audioState)
Expand All @@ -40,9 +49,8 @@ void local_state_execution_policy::commit_common()
elt.first->push_value(elt.second);

for(auto& vec : elt.second.get())
{
vec.clear();
}
ossia::audio_buffer_pool::instance().release(std::move(vec));
elt.second.get().clear();
}
#endif

Expand Down
2 changes: 1 addition & 1 deletion src/ossia/detail/math.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,7 @@ OSSIA_INLINE constexpr T wrap(const T val, const T low, const T high) noexcept
template <class T>
OSSIA_INLINE constexpr T fold(const T val, const T low, const T high) noexcept
{
if((val >= low) && (val <= high) || low == high)
if(((val >= low) && (val <= high)) || low == high)
return val;
else
{
Expand Down

0 comments on commit 8b29d06

Please sign in to comment.