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

Adopt a full type for stereo samples, gaining + and +=.

This commit is contained in:
Thomas Harte 2024-02-09 10:48:42 -05:00
parent f3d0827d14
commit c105acf1c7
5 changed files with 41 additions and 15 deletions

View File

@ -210,13 +210,12 @@ 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_volume_[0] = int16_t((
output_volume_.left = 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_volume_[1] = int16_t((
output_volume_.right = 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_

View File

@ -106,7 +106,7 @@ void Audio::get_samples(std::size_t number_of_samples, Outputs::Speaker::StereoS
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[0] =
output_level.left =
volume_ *
(use_direct_output_[0] ?
channels_[0].amplitude[0]
@ -117,7 +117,7 @@ void Audio::get_samples(std::size_t number_of_samples, Outputs::Speaker::StereoS
noise_.amplitude[0] * noise_.final_output
));
output_level[1] =
output_level.right =
volume_ *
(use_direct_output_[1] ?
channels_[0].amplitude[1]

View File

@ -62,7 +62,7 @@
</Testables>
</TestAction>
<LaunchAction
buildConfiguration = "Debug"
buildConfiguration = "Release"
selectedDebuggerIdentifier = "Xcode.DebuggerFoundation.Debugger.LLDB"
selectedLauncherIdentifier = "Xcode.DebuggerFoundation.Launcher.LLDB"
enableASanStackUseAfterReturn = "YES"

View File

@ -59,7 +59,9 @@ template <typename... T> class CompoundSource:
static constexpr bool is_stereo = S::is_stereo || CompoundSourceHolder<R...>::is_stereo;
template <bool output_stereo> void get_samples(std::size_t number_of_samples, std::int16_t *target) {
// TODO: fix below for mixed mono/stereo sources.
template <bool output_stereo>
void get_samples(std::size_t number_of_samples, typename SampleT<output_stereo>::type *target) {
// Get the rest of the output.
next_source_.template get_samples<output_stereo>(number_of_samples, target);
@ -72,7 +74,7 @@ template <typename... T> class CompoundSource:
// Get this component's output.
auto buffer_size = number_of_samples * (output_stereo ? 2 : 1);
int16_t local_samples[buffer_size];
typename SampleT<is_stereo>::type local_samples[number_of_samples];
source_.get_samples(number_of_samples, local_samples);
// Merge it in; furthermore if total output is stereo but this source isn't,
@ -127,7 +129,7 @@ template <typename... T> class CompoundSource:
}
}
void get_samples(std::size_t number_of_samples, std::int16_t *target) {
void get_samples(std::size_t number_of_samples, typename SampleT<::Outputs::Speaker::is_stereo<T...>()>::type *target) {
source_holder_.template get_samples<::Outputs::Speaker::is_stereo<T...>()>(number_of_samples, target);
}

View File

@ -14,13 +14,38 @@
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;
using MonoSample = int16_t;
struct StereoSample {
#if TARGET_RT_BIG_ENDIAN
int16_t right, left;
#else
int16_t left, right;
#endif
StereoSample() = default;
StereoSample(const StereoSample &rhs) {
left = rhs.left;
right = rhs.right;
}
StereoSample &operator +=(const StereoSample &rhs) {
left += rhs.left;
right += rhs.right;
return *this;
}
StereoSample operator +(const StereoSample &rhs) {
StereoSample result;
result.left = left + rhs.left;
result.right = right + rhs.right;
return *this;
}
};
template <bool stereo> struct SampleT {
using type = std::conditional_t<stereo, StereoSample, MonoSample>;
};
/*!
Provides a communication point for sound; machines that have a speaker provide an