Skip to content

Commit

Permalink
🤏 [InputStream] Don't call set_nb_of_retained_samples() automatically…
Browse files Browse the repository at this point in the history
… in for_each_sample()
  • Loading branch information
JulesFouchy committed Nov 3, 2023
1 parent aca6e02 commit ce135ef
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 10 deletions.
20 changes: 12 additions & 8 deletions src/InputStream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,20 +38,24 @@ void InputStream::set_nb_of_retained_samples(size_t samples_count)
// shrink_samples_to_fit(); // Don't shrink here, this will be done during `for_each_sample()`. This avoids locking too often.
}

void InputStream::for_each_sample(size_t samples_count, std::function<void(float)> const& callback)
void InputStream::for_each_sample(int64_t samples_count, std::function<void(float)> const& callback)
{
set_nb_of_retained_samples(samples_count); // Now we know exactly how many to store, the next calls to `for_each_sample()` (if they are done with the same `samples_count`) will have the exact data that they want, with no dummy 0s to fill in the missing data.

auto const samples = [&]() {
std::lock_guard const lock{_samples_mutex}; // Lock while we copy
shrink_samples_to_fit(); // Might not be fit, if set_nb_of_retained_samples() has been called.
return _samples;
}();
if (samples_count > samples.size())
for (size_t i = 0; i < samples_count - samples.size(); ++i)
callback(0.f); // Fill in the first potentially missing samples with 0s.
for (float const sample : samples)
callback(sample);
for ( // Take the `samples_count` last elements of `samples`.
int64_t i = static_cast<int64_t>(samples.size()) - samples_count;
i < static_cast<int64_t>(samples.size());
++i
)
{
if (i < 0) // If `samples` has less than `samples_count` elements this will happen.
callback(0.f);
else
callback(samples[i]);
}
}

auto audio_input_callback(void* /* output_buffer */, void* input_buffer, unsigned int frames_count, double /* stream_time */, RtAudioStreamStatus /* status */, void* user_data) -> int
Expand Down
6 changes: 4 additions & 2 deletions src/InputStream.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,10 @@ class InputStream {

/// Calls the callback for each of the `samples_count` latest samples received through the device.
/// This data is always mono-channel, 1 sample == 1 frame.
void for_each_sample(size_t samples_count, std::function<void(float)> const& callback);
void for_each_sample(int64_t samples_count, std::function<void(float)> const& callback);
/// You MUST call this function at least once at the beginning to tell us the maximum numbers of samples you will query with `for_each_sample`.
/// If that max number changes over time, you can call this function again to update it.
void set_nb_of_retained_samples(size_t samples_count);

/// Returns the list of all the ids of input devices.
auto device_ids() const -> std::vector<unsigned int>;
Expand All @@ -34,7 +37,6 @@ class InputStream {
private:
friend auto audio_input_callback(void* output_buffer, void* input_buffer, unsigned int frames_count, double stream_time, RtAudioStreamStatus status, void* user_data) -> int;

void set_nb_of_retained_samples(size_t samples_count);
/// /!\ YOU MUST LOCK `_samples_mutex` before using this function
void shrink_samples_to_fit();

Expand Down

0 comments on commit ce135ef

Please sign in to comment.