1
0
mirror of https://github.com/TomHarte/CLK.git synced 2024-06-26 10:29:31 +00:00

Without yet much exposition, draft sample-by-sample interface.

This commit is contained in:
Thomas Harte 2024-02-01 21:56:33 -05:00
parent c6c9be0b08
commit f4d8c04f3c

View File

@ -15,8 +15,8 @@
namespace Outputs::Speaker { namespace Outputs::Speaker {
template <bool stereo> struct SampleT; template <bool stereo> struct SampleT;
template <> struct SampleT<true> { using type = std::array<std::uint16_t, 2>; }; template <> struct SampleT<true> { using type = std::array<std::int16_t, 2>; };
template <> struct SampleT<false> { using type = std::uint16_t; }; template <> struct SampleT<false> { using type = std::int16_t; };
/*! /*!
A sample source is something that can provide a stream of audio. A sample source is something that can provide a stream of audio.
@ -26,10 +26,28 @@ template <> struct SampleT<false> { using type = std::uint16_t; };
template <typename SourceT> template <typename SourceT>
class SampleSource { class SampleSource {
public: public:
/*!
Indicates whether this component will write stereo samples.
*/
static constexpr bool is_stereo = SourceT::is_stereo;
/*! /*!
Should write the next @c number_of_samples to @c target. Should write the next @c number_of_samples to @c target.
*/ */
void get_samples([[maybe_unused]] std::size_t number_of_samples, [[maybe_unused]] std::int16_t *target) {} void get_samples(std::size_t number_of_samples, std::int16_t *target) {
const auto &source = *static_cast<SourceT *>(this);
while(number_of_samples--) {
if constexpr (is_stereo) {
const auto next = source.level();
target[0] = next[0];
target[1] = next[1];
target += 2;
} else {
*target = source.level();
++target;
}
}
}
/*! /*!
Should skip the next @c number_of_samples. Subclasses of this SampleSource Should skip the next @c number_of_samples. Subclasses of this SampleSource
@ -38,6 +56,9 @@ class SampleSource {
implementation below. implementation below.
*/ */
void skip_samples(const std::size_t number_of_samples) { void skip_samples(const std::size_t number_of_samples) {
if constexpr (&SourceT::advance == &SampleSource<SourceT>::advance) {
return;
}
std::int16_t scratch_pad[number_of_samples]; std::int16_t scratch_pad[number_of_samples];
get_samples(number_of_samples, scratch_pad); get_samples(number_of_samples, scratch_pad);
} }
@ -47,9 +68,17 @@ class SampleSource {
fill the target with zeroes; @c false if a call might return all zeroes or fill the target with zeroes; @c false if a call might return all zeroes or
might not. might not.
*/ */
bool is_zero_level() const { bool is_zero_level() const { return false; }
return false; auto level() const {
typename SampleT<is_stereo>::type result;
if constexpr (is_stereo) {
result[0] = result[1] = 0;
} else {
result = 0;
}
return result;
} }
void advance() {}
/*! /*!
Sets the proper output range for this sample source; it should write values Sets the proper output range for this sample source; it should write values
@ -57,11 +86,6 @@ class SampleSource {
*/ */
void set_sample_volume_range([[maybe_unused]] std::int16_t volume) {} void set_sample_volume_range([[maybe_unused]] std::int16_t volume) {}
/*!
Indicates whether this component will write stereo samples.
*/
static constexpr bool is_stereo = SourceT::is_stereo;
/*! /*!
Permits a sample source to declare that, averaged over time, it will use only 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 a certain proportion of the allocated volume range. This commonly happens