mirror of
https://github.com/TomHarte/CLK.git
synced 2024-12-23 20:29:42 +00:00
Introduce [Mono/Stereo]Sample types.
This commit is contained in:
parent
a4a983eb81
commit
f3d0827d14
@ -17,7 +17,7 @@
|
||||
namespace MOS::MOS6560 {
|
||||
|
||||
// audio state
|
||||
class AudioGenerator: public ::Outputs::Speaker::SampleSource<AudioGenerator, false> {
|
||||
class AudioGenerator: public Outputs::Speaker::SampleSource<AudioGenerator, false> {
|
||||
public:
|
||||
AudioGenerator(Concurrency::AsyncTaskQueue<false> &audio_queue);
|
||||
|
||||
@ -25,7 +25,7 @@ class AudioGenerator: public ::Outputs::Speaker::SampleSource<AudioGenerator, fa
|
||||
void set_control(int channel, uint8_t value);
|
||||
|
||||
// For ::SampleSource.
|
||||
void get_samples(std::size_t number_of_samples, int16_t *target);
|
||||
void get_samples(std::size_t number_of_samples, Outputs::Speaker::MonoSample *target);
|
||||
void skip_samples(std::size_t number_of_samples);
|
||||
void set_sample_volume_range(std::int16_t range);
|
||||
|
||||
|
@ -101,7 +101,11 @@ template <bool is_stereo> void AY38910<is_stereo>::set_output_mixing(float a_lef
|
||||
c_right_ = uint8_t(c_right * 255.0f);
|
||||
}
|
||||
|
||||
template <bool is_stereo> void AY38910<is_stereo>::get_samples(std::size_t number_of_samples, int16_t *target) {
|
||||
template <bool is_stereo>
|
||||
void AY38910<is_stereo>::get_samples(
|
||||
std::size_t number_of_samples,
|
||||
typename Outputs::Speaker::SampleT<is_stereo>::type *target
|
||||
) {
|
||||
// Note on structure below: the real AY has a built-in divider of 8
|
||||
// prior to applying its tone and noise dividers. But the YM fills the
|
||||
// same total periods for noise and tone with double-precision envelopes.
|
||||
@ -113,11 +117,7 @@ template <bool is_stereo> void AY38910<is_stereo>::get_samples(std::size_t numbe
|
||||
|
||||
std::size_t c = 0;
|
||||
while((master_divider_&3) && c < number_of_samples) {
|
||||
if constexpr (is_stereo) {
|
||||
reinterpret_cast<uint32_t *>(target)[c] = output_volume_;
|
||||
} else {
|
||||
target[c] = int16_t(output_volume_);
|
||||
}
|
||||
target[c] = output_volume_;
|
||||
master_divider_++;
|
||||
c++;
|
||||
}
|
||||
@ -159,11 +159,7 @@ template <bool is_stereo> void AY38910<is_stereo>::get_samples(std::size_t numbe
|
||||
evaluate_output_volume();
|
||||
|
||||
for(int ic = 0; ic < 4 && c < number_of_samples; ic++) {
|
||||
if constexpr (is_stereo) {
|
||||
reinterpret_cast<uint32_t *>(target)[c] = output_volume_;
|
||||
} else {
|
||||
target[c] = int16_t(output_volume_);
|
||||
}
|
||||
target[c] = output_volume_;
|
||||
c++;
|
||||
master_divider_++;
|
||||
}
|
||||
@ -214,19 +210,19 @@ template <bool is_stereo> void AY38910<is_stereo>::evaluate_output_volume() {
|
||||
|
||||
// Mix additively, weighting if in stereo.
|
||||
if constexpr (is_stereo) {
|
||||
int16_t *const output_volumes = reinterpret_cast<int16_t *>(&output_volume_);
|
||||
output_volumes[0] = int16_t((
|
||||
// int16_t *const output_volumes = reinterpret_cast<int16_t *>(&output_volume_);
|
||||
output_volume_[0] = int16_t((
|
||||
volumes_[volumes[0]] * channel_levels[0] * a_left_ +
|
||||
volumes_[volumes[1]] * channel_levels[1] * b_left_ +
|
||||
volumes_[volumes[2]] * channel_levels[2] * c_left_
|
||||
) >> 8);
|
||||
output_volumes[1] = int16_t((
|
||||
output_volume_[1] = int16_t((
|
||||
volumes_[volumes[0]] * channel_levels[0] * a_right_ +
|
||||
volumes_[volumes[1]] * channel_levels[1] * b_right_ +
|
||||
volumes_[volumes[2]] * channel_levels[2] * c_right_
|
||||
) >> 8);
|
||||
} else {
|
||||
output_volume_ = uint32_t(
|
||||
output_volume_ = int16_t(
|
||||
volumes_[volumes[0]] * channel_levels[0] +
|
||||
volumes_[volumes[1]] * channel_levels[1] +
|
||||
volumes_[volumes[2]] * channel_levels[2]
|
||||
|
@ -106,7 +106,7 @@ template <bool stereo> class AY38910: public ::Outputs::Speaker::SampleSource<AY
|
||||
void set_output_mixing(float a_left, float b_left, float c_left, float a_right = 1.0, float b_right = 1.0, float c_right = 1.0);
|
||||
|
||||
// to satisfy ::Outputs::Speaker (included via ::Outputs::Filter.
|
||||
void get_samples(std::size_t number_of_samples, int16_t *target);
|
||||
void get_samples(std::size_t number_of_samples, typename Outputs::Speaker::SampleT<stereo>::type *target);
|
||||
bool is_zero_level() const;
|
||||
void set_sample_volume_range(std::int16_t range);
|
||||
|
||||
@ -149,7 +149,7 @@ template <bool stereo> class AY38910: public ::Outputs::Speaker::SampleSource<AY
|
||||
|
||||
uint8_t data_input_, data_output_;
|
||||
|
||||
uint32_t output_volume_;
|
||||
typename Outputs::Speaker::SampleT<stereo>::type output_volume_;
|
||||
|
||||
void update_bus();
|
||||
PortHandler *port_handler_ = nullptr;
|
||||
|
@ -20,7 +20,7 @@ class Toggle: public Outputs::Speaker::SampleSource<Toggle, false> {
|
||||
public:
|
||||
Toggle(Concurrency::AsyncTaskQueue<false> &audio_queue);
|
||||
|
||||
void get_samples(std::size_t number_of_samples, std::int16_t *target);
|
||||
void get_samples(std::size_t number_of_samples, Outputs::Speaker::MonoSample *target);
|
||||
void set_sample_volume_range(std::int16_t range);
|
||||
void skip_samples(const std::size_t number_of_samples);
|
||||
|
||||
|
@ -99,17 +99,14 @@ void Audio::update_channel(int c) {
|
||||
channels_[c].output |= output;
|
||||
}
|
||||
|
||||
void Audio::get_samples(std::size_t number_of_samples, int16_t *target) {
|
||||
struct Frame {
|
||||
int16_t left, right;
|
||||
} output_level;
|
||||
Frame *target_frames = reinterpret_cast<Frame *>(target);
|
||||
void Audio::get_samples(std::size_t number_of_samples, Outputs::Speaker::StereoSample *target) {
|
||||
Outputs::Speaker::StereoSample output_level;
|
||||
|
||||
size_t c = 0;
|
||||
while(c < number_of_samples) {
|
||||
// I'm unclear on the details of the time division multiplexing so,
|
||||
// for now, just sum the outputs.
|
||||
output_level.left =
|
||||
output_level[0] =
|
||||
volume_ *
|
||||
(use_direct_output_[0] ?
|
||||
channels_[0].amplitude[0]
|
||||
@ -120,7 +117,7 @@ void Audio::get_samples(std::size_t number_of_samples, int16_t *target) {
|
||||
noise_.amplitude[0] * noise_.final_output
|
||||
));
|
||||
|
||||
output_level.right =
|
||||
output_level[1] =
|
||||
volume_ *
|
||||
(use_direct_output_[1] ?
|
||||
channels_[0].amplitude[1]
|
||||
@ -133,14 +130,11 @@ void Audio::get_samples(std::size_t number_of_samples, int16_t *target) {
|
||||
|
||||
while(global_divider_ && c < number_of_samples) {
|
||||
--global_divider_;
|
||||
target_frames[c] = output_level;
|
||||
target[c] = output_level;
|
||||
++c;
|
||||
}
|
||||
|
||||
global_divider_ = global_divider_reload_;
|
||||
if(!global_divider_) {
|
||||
global_divider_ = global_divider_reload_;
|
||||
}
|
||||
|
||||
poly_state_[int(Channel::Distortion::FourBit)] = poly4_.next();
|
||||
poly_state_[int(Channel::Distortion::FiveBit)] = poly5_.next();
|
||||
poly_state_[int(Channel::Distortion::SevenBit)] = poly7_.next();
|
||||
|
@ -37,7 +37,7 @@ class Audio: public Outputs::Speaker::SampleSource<Audio, true> {
|
||||
|
||||
// MARK: - SampleSource.
|
||||
void set_sample_volume_range(int16_t range);
|
||||
void get_samples(std::size_t number_of_samples, int16_t *target);
|
||||
void get_samples(std::size_t number_of_samples, Outputs::Speaker::StereoSample *target);
|
||||
|
||||
private:
|
||||
Concurrency::AsyncTaskQueue<false> &audio_queue_;
|
||||
|
@ -404,7 +404,12 @@ template <typename SampleSource> class PullLowpass: public LowpassBase<PullLowpa
|
||||
}
|
||||
|
||||
void get_samples(size_t length, int16_t *target) {
|
||||
sample_source_.get_samples(length, target);
|
||||
if constexpr (SampleSource::is_stereo) {
|
||||
StereoSample *const stereo_target = reinterpret_cast<StereoSample *>(target);
|
||||
sample_source_.get_samples(length, stereo_target);
|
||||
} else {
|
||||
sample_source_.get_samples(length, target);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
|
@ -12,11 +12,9 @@
|
||||
#include <cstddef>
|
||||
#include <cstdint>
|
||||
|
||||
namespace Outputs::Speaker {
|
||||
#include "../Speaker.hpp"
|
||||
|
||||
template <bool stereo> struct SampleT;
|
||||
template <> struct SampleT<true> { using type = std::array<std::int16_t, 2>; };
|
||||
template <> struct SampleT<false> { using type = std::int16_t; };
|
||||
namespace Outputs::Speaker {
|
||||
|
||||
/*!
|
||||
A sample source is something that can provide a stream of audio.
|
||||
@ -34,18 +32,11 @@ class SampleSource {
|
||||
/*!
|
||||
Should write the next @c number_of_samples to @c target.
|
||||
*/
|
||||
void get_samples(std::size_t number_of_samples, std::int16_t *target) {
|
||||
void get_samples(std::size_t number_of_samples, typename SampleT<stereo>::type *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;
|
||||
}
|
||||
*target = source.level();
|
||||
++target;
|
||||
}
|
||||
}
|
||||
|
||||
@ -59,7 +50,7 @@ class SampleSource {
|
||||
if constexpr (&SourceT::advance == &SampleSource<SourceT, stereo>::advance) {
|
||||
return;
|
||||
}
|
||||
std::int16_t scratch_pad[number_of_samples];
|
||||
typename SampleT<stereo>::type scratch_pad[number_of_samples];
|
||||
get_samples(number_of_samples, scratch_pad);
|
||||
}
|
||||
|
||||
@ -68,16 +59,8 @@ class SampleSource {
|
||||
fill the target with zeroes; @c false if a call might return all zeroes or
|
||||
might not.
|
||||
*/
|
||||
bool is_zero_level() const { 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;
|
||||
}
|
||||
bool is_zero_level() const { return false; }
|
||||
auto level() const { return typename SampleT<stereo>::type(); }
|
||||
void advance() {}
|
||||
|
||||
/*!
|
||||
|
@ -14,6 +14,14 @@
|
||||
|
||||
namespace Outputs::Speaker {
|
||||
|
||||
template <bool stereo> struct SampleT;
|
||||
template <> struct SampleT<true> { using type = std::array<std::int16_t, 2>; }; // TODO: adopt left/right as per Dave?
|
||||
template <> struct SampleT<false> { using type = std::int16_t; };
|
||||
|
||||
// Shorthands.
|
||||
using MonoSample = SampleT<false>::type;
|
||||
using StereoSample = SampleT<true>::type;
|
||||
|
||||
/*!
|
||||
Provides a communication point for sound; machines that have a speaker provide an
|
||||
audio output.
|
||||
|
Loading…
Reference in New Issue
Block a user