2017-12-19 02:39:23 +00:00
|
|
|
//
|
|
|
|
// SampleSource.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 17/12/2017.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2017 Thomas Harte. All rights reserved.
|
2017-12-19 02:39:23 +00:00
|
|
|
//
|
|
|
|
|
2024-01-17 04:34:46 +00:00
|
|
|
#pragma once
|
2017-12-19 02:39:23 +00:00
|
|
|
|
2024-02-12 15:55:52 +00:00
|
|
|
#include <algorithm>
|
2024-02-02 02:47:44 +00:00
|
|
|
#include <array>
|
2017-12-19 02:39:23 +00:00
|
|
|
#include <cstddef>
|
|
|
|
#include <cstdint>
|
|
|
|
|
2024-02-09 14:15:48 +00:00
|
|
|
#include "../Speaker.hpp"
|
2017-12-19 02:39:23 +00:00
|
|
|
|
2024-02-09 14:15:48 +00:00
|
|
|
namespace Outputs::Speaker {
|
2024-02-02 02:47:44 +00:00
|
|
|
|
2024-02-12 15:55:52 +00:00
|
|
|
enum class Action {
|
|
|
|
/// New values should be _stored_ to the sample buffer.
|
|
|
|
Store,
|
|
|
|
/// New values should be _added_ to the sample buffer.
|
|
|
|
Mix,
|
|
|
|
/// New values shouldn't be stored; the source can skip generation of them if desired.
|
|
|
|
Ignore,
|
|
|
|
};
|
|
|
|
|
|
|
|
template <Action action, typename SampleT> void apply(SampleT &lhs, SampleT rhs) {
|
|
|
|
switch(action) {
|
|
|
|
case Action::Mix: lhs += rhs; break;
|
|
|
|
case Action::Store: lhs = rhs; break;
|
|
|
|
case Action::Ignore: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
template <Action action, typename IteratorT, typename SampleT> void fill(IteratorT begin, IteratorT end, SampleT value) {
|
|
|
|
switch(action) {
|
|
|
|
case Action::Mix:
|
|
|
|
while(begin != end) {
|
|
|
|
apply<action>(*begin, value);
|
|
|
|
++begin;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case Action::Store:
|
|
|
|
std::fill(begin, end, value);
|
|
|
|
break;
|
|
|
|
case Action::Ignore: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-12-19 02:39:23 +00:00
|
|
|
/*!
|
|
|
|
A sample source is something that can provide a stream of audio.
|
|
|
|
This optional base class provides the interface expected to be exposed
|
|
|
|
by the template parameter to LowpassSpeaker.
|
|
|
|
*/
|
2024-02-08 20:21:47 +00:00
|
|
|
template <typename SourceT, bool stereo>
|
2024-02-09 19:25:40 +00:00
|
|
|
class BufferSource {
|
2017-12-19 02:39:23 +00:00
|
|
|
public:
|
2024-02-02 02:56:33 +00:00
|
|
|
/*!
|
|
|
|
Indicates whether this component will write stereo samples.
|
|
|
|
*/
|
2024-02-08 20:21:47 +00:00
|
|
|
static constexpr bool is_stereo = stereo;
|
2024-02-02 02:56:33 +00:00
|
|
|
|
2017-12-19 02:39:23 +00:00
|
|
|
/*!
|
2024-02-12 15:55:52 +00:00
|
|
|
Should 'apply' the next @c number_of_samples to @c target ; application means applying @c action which can be achieved either via the
|
2024-02-12 18:59:03 +00:00
|
|
|
helper functions above — @c apply and @c fill — or by semantic inspection (primarily, if an obvious quick route for @c Action::Ignore is available).
|
|
|
|
|
|
|
|
No default implementation is provided.
|
2017-12-19 02:39:23 +00:00
|
|
|
*/
|
2024-02-12 15:55:52 +00:00
|
|
|
template <Action action>
|
2024-02-12 18:59:03 +00:00
|
|
|
void apply_samples([[maybe_unused]] std::size_t number_of_samples, [[maybe_unused]] typename SampleT<stereo>::type *target);
|
2018-01-06 23:50:26 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns @c true if it is trivially true that a call to get_samples would just
|
|
|
|
fill the target with zeroes; @c false if a call might return all zeroes or
|
|
|
|
might not.
|
|
|
|
*/
|
2024-02-09 19:25:40 +00:00
|
|
|
bool is_zero_level() const { return false; }
|
2018-03-09 18:23:18 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Sets the proper output range for this sample source; it should write values
|
|
|
|
between 0 and volume.
|
|
|
|
*/
|
2020-05-30 04:37:06 +00:00
|
|
|
void set_sample_volume_range([[maybe_unused]] std::int16_t volume) {}
|
2020-04-04 00:05:36 +00:00
|
|
|
|
2020-05-09 21:57:21 +00:00
|
|
|
/*!
|
|
|
|
Permits a sample source to declare that, averaged over time, it will use only
|
|
|
|
a certain proportion of the allocated volume range. This commonly happens
|
|
|
|
in sample sources that use a time-multiplexed sound output — for example, if
|
|
|
|
one were to output only every other sample then it would return 0.5.
|
2020-05-10 04:44:03 +00:00
|
|
|
|
|
|
|
This is permitted to vary over time but there is no contract as to when it will be
|
|
|
|
used by a speaker. If it varies, it should do so very infrequently and only to
|
|
|
|
represent changes in hardware configuration.
|
2020-05-09 21:57:21 +00:00
|
|
|
*/
|
2024-02-02 02:29:00 +00:00
|
|
|
double average_output_peak() const { return 1.0; }
|
2017-12-19 02:39:23 +00:00
|
|
|
};
|
|
|
|
|
2024-02-09 19:25:40 +00:00
|
|
|
///
|
|
|
|
template <typename SourceT, bool stereo, int divider = 1>
|
|
|
|
struct SampleSource: public BufferSource<SourceT, stereo> {
|
|
|
|
public:
|
2024-02-12 15:55:52 +00:00
|
|
|
template <bool mix>
|
|
|
|
void apply_samples(std::size_t number_of_samples, typename SampleT<stereo>::type *target) {
|
2024-02-09 19:25:40 +00:00
|
|
|
const auto &source = *static_cast<SourceT *>(this);
|
|
|
|
|
|
|
|
if constexpr (divider == 1) {
|
|
|
|
while(number_of_samples--) {
|
2024-02-12 15:55:52 +00:00
|
|
|
apply<mix>(*target, source.level());
|
2024-02-09 19:25:40 +00:00
|
|
|
++target;
|
2024-02-12 15:55:52 +00:00
|
|
|
source.advance();
|
2024-02-09 19:25:40 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
std::size_t c = 0;
|
|
|
|
|
2024-02-09 19:26:02 +00:00
|
|
|
// Fill in the tail of any partially-captured level.
|
2024-02-09 19:25:40 +00:00
|
|
|
auto level = source.level();
|
|
|
|
while(c < number_of_samples && master_divider_ != divider) {
|
2024-02-12 15:55:52 +00:00
|
|
|
apply<mix>(target[c], level);
|
2024-02-09 19:25:40 +00:00
|
|
|
++c;
|
|
|
|
++master_divider_;
|
|
|
|
}
|
|
|
|
source.advance();
|
|
|
|
|
|
|
|
// Provide all full levels.
|
|
|
|
int whole_steps = (number_of_samples - c) / divider;
|
|
|
|
while(whole_steps--) {
|
2024-02-12 15:55:52 +00:00
|
|
|
fill<mix>(&target[c], &target[c + divider], source.level());
|
2024-02-09 19:25:40 +00:00
|
|
|
c += divider;
|
|
|
|
source.advance();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Provide the head of a further partial capture.
|
|
|
|
level = source.level();
|
|
|
|
master_divider_ = number_of_samples - c;
|
2024-02-12 15:55:52 +00:00
|
|
|
fill<mix>(&target[c], &target[number_of_samples], source.level());
|
2024-02-09 19:25:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// TODO: use a concept here, when C++20 filters through.
|
|
|
|
//
|
|
|
|
// Until then: sample sources should implement this.
|
|
|
|
auto level() const {
|
|
|
|
return typename SampleT<stereo>::type();
|
|
|
|
}
|
|
|
|
void advance() {}
|
|
|
|
|
|
|
|
private:
|
|
|
|
int master_divider_{};
|
|
|
|
};
|
|
|
|
|
2017-12-19 02:39:23 +00:00
|
|
|
}
|