diff --git a/Components/AY38910/AY38910.cpp b/Components/AY38910/AY38910.cpp index 4bf37fafa..e4cb90027 100644 --- a/Components/AY38910/AY38910.cpp +++ b/Components/AY38910/AY38910.cpp @@ -10,13 +10,10 @@ #include "AY38910.hpp" -//namespace GI { -//namespace AY38910 { - using namespace GI::AY38910; template -AY38910::AY38910(Personality personality, Concurrency::AsyncTaskQueue &task_queue) : task_queue_(task_queue) { +AY38910SampleSource::AY38910SampleSource(Personality personality, Concurrency::AsyncTaskQueue &task_queue) : task_queue_(task_queue) { // Don't use the low bit of the envelope position if this is an AY. envelope_position_mask_ |= personality == Personality::AY38910; @@ -74,7 +71,8 @@ AY38910::AY38910(Personality personality, Concurrency::AsyncTaskQueue set_sample_volume_range(0); } -template void AY38910::set_sample_volume_range(std::int16_t range) { +template +void AY38910SampleSource::set_sample_volume_range(std::int16_t range) { // Set up volume lookup table; the function below is based on a combination of the graph // from the YM's datasheet, showing a clear power curve, and fitting that to observed // values reported elsewhere. @@ -92,7 +90,8 @@ template void AY38910::set_sample_volume_range(std:: evaluate_output_volume(); } -template void AY38910::set_output_mixing(float a_left, float b_left, float c_left, float a_right, float b_right, float c_right) { +template +void AY38910SampleSource::set_output_mixing(float a_left, float b_left, float c_left, float a_right, float b_right, float c_right) { a_left_ = uint8_t(a_left * 255.0f); b_left_ = uint8_t(b_left * 255.0f); c_left_ = uint8_t(c_left * 255.0f); @@ -101,12 +100,6 @@ template void AY38910::set_output_mixing(float a_lef c_right_ = uint8_t(c_right * 255.0f); } -template -template -void AY38910::apply_samples( - std::size_t number_of_samples, - typename Outputs::Speaker::SampleT::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. @@ -116,14 +109,8 @@ void AY38910::apply_samples( // matching the YM datasheet's depiction of envelope level 31 as equal to // programmatic volume 15, envelope level 29 as equal to programmatic 14, etc. - std::size_t c = 0; - while((master_divider_&3) && c < number_of_samples) { - Outputs::Speaker::apply(target[c], output_volume_); - master_divider_++; - c++; - } - - while(c < number_of_samples) { +template +void AY38910SampleSource::advance() { #define step_channel(c) \ if(tone_counters_[c]) tone_counters_[c]--;\ else {\ @@ -131,52 +118,42 @@ void AY38910::apply_samples( tone_counters_[c] = tone_periods_[c] << 1;\ } - // Update the tone channels. - step_channel(0); - step_channel(1); - step_channel(2); + // Update the tone channels. + step_channel(0); + step_channel(1); + step_channel(2); #undef step_channel - // Update the noise generator. This recomputes the new bit repeatedly but harmlessly, only shifting - // it into the official 17 upon divider underflow. - if(noise_counter_) noise_counter_--; - else { - noise_counter_ = noise_period_ << 1; // To cover the double resolution of envelopes. - noise_output_ ^= noise_shift_register_&1; - noise_shift_register_ |= ((noise_shift_register_ ^ (noise_shift_register_ >> 3))&1) << 17; - noise_shift_register_ >>= 1; - } - - // Update the envelope generator. Table based for pattern lookup, with a 'refill' step: a way of - // implementing non-repeating patterns by locking them to the final table position. - if(envelope_divider_) envelope_divider_--; - else { - envelope_divider_ = envelope_period_; - envelope_position_ ++; - if(envelope_position_ == 64) envelope_position_ = envelope_overflow_masks_[output_registers_[13]]; - } - - evaluate_output_volume(); - - for(int ic = 0; ic < 4 && c < number_of_samples; ic++) { - Outputs::Speaker::apply(target[c], output_volume_); - c++; - master_divider_++; - } + // Update the noise generator. This recomputes the new bit repeatedly but harmlessly, only shifting + // it into the official 17 upon divider underflow. + if(noise_counter_) noise_counter_--; + else { + noise_counter_ = noise_period_ << 1; // To cover the double resolution of envelopes. + noise_output_ ^= noise_shift_register_&1; + noise_shift_register_ |= ((noise_shift_register_ ^ (noise_shift_register_ >> 3))&1) << 17; + noise_shift_register_ >>= 1; } - master_divider_ &= 3; + // Update the envelope generator. Table based for pattern lookup, with a 'refill' step: a way of + // implementing non-repeating patterns by locking them to the final table position. + if(envelope_divider_) envelope_divider_--; + else { + envelope_divider_ = envelope_period_; + envelope_position_ ++; + if(envelope_position_ == 64) envelope_position_ = envelope_overflow_masks_[output_registers_[13]]; + } + + evaluate_output_volume(); } -template void AY38910::apply_samples(std::size_t, typename Outputs::Speaker::SampleT::type *); -template void AY38910::apply_samples(std::size_t, typename Outputs::Speaker::SampleT::type *); -template void AY38910::apply_samples(std::size_t, typename Outputs::Speaker::SampleT::type *); -template void AY38910::apply_samples(std::size_t, typename Outputs::Speaker::SampleT::type *); -template void AY38910::apply_samples(std::size_t, typename Outputs::Speaker::SampleT::type *); -template void AY38910::apply_samples(std::size_t, typename Outputs::Speaker::SampleT::type *); +template +typename Outputs::Speaker::SampleT::type AY38910SampleSource::level() const { + return output_volume_; +} -template void AY38910::evaluate_output_volume() { +template +void AY38910SampleSource::evaluate_output_volume() { int envelope_volume = envelope_shapes_[output_registers_[13]][envelope_position_ | envelope_position_mask_]; // The output level for a channel is: @@ -237,18 +214,21 @@ template void AY38910::evaluate_output_volume() { } } -template bool AY38910::is_zero_level() const { +template +bool AY38910SampleSource::is_zero_level() const { // Confirm that the AY is trivially at the zero level if all three volume controls are set to fixed zero. return output_registers_[0x8] == 0 && output_registers_[0x9] == 0 && output_registers_[0xa] == 0; } // MARK: - Register manipulation -template void AY38910::select_register(uint8_t r) { +template +void AY38910SampleSource::select_register(uint8_t r) { selected_register_ = r; } -template void AY38910::set_register_value(uint8_t value) { +template +void AY38910SampleSource::set_register_value(uint8_t value) { // There are only 16 registers. if(selected_register_ > 15) return; @@ -317,7 +297,8 @@ template void AY38910::set_register_value(uint8_t va if(update_port_a) set_port_output(false); } -template uint8_t AY38910::get_register_value() { +template +uint8_t AY38910SampleSource::get_register_value() { // This table ensures that bits that aren't defined within the AY are returned as 0s // when read, conforming to CPC-sourced unit tests. const uint8_t register_masks[16] = { @@ -331,24 +312,28 @@ template uint8_t AY38910::get_register_value() { // MARK: - Port querying -template uint8_t AY38910::get_port_output(bool port_b) { +template +uint8_t AY38910SampleSource::get_port_output(bool port_b) { return registers_[port_b ? 15 : 14]; } // MARK: - Bus handling -template void AY38910::set_port_handler(PortHandler *handler) { +template +void AY38910SampleSource::set_port_handler(PortHandler *handler) { port_handler_ = handler; set_port_output(true); set_port_output(false); } -template void AY38910::set_data_input(uint8_t r) { +template +void AY38910SampleSource::set_data_input(uint8_t r) { data_input_ = r; update_bus(); } -template void AY38910::set_port_output(bool port_b) { +template +void AY38910SampleSource::set_port_output(bool port_b) { // Per the data sheet: "each [IO] pin is provided with an on-chip pull-up resistor, // so that when in the "input" mode, all pins will read normally high". Therefore, // report programmer selection of input mode as creating an output of 0xff. @@ -358,7 +343,8 @@ template void AY38910::set_port_output(bool port_b) } } -template uint8_t AY38910::get_data_output() { +template +uint8_t AY38910SampleSource::get_data_output() { if(control_state_ == Read && selected_register_ >= 14 && selected_register_ < 16) { // Per http://cpctech.cpc-live.com/docs/psgnotes.htm if a port is defined as output then the // value returned to the CPU when reading it is the and of the output value and any input. @@ -374,7 +360,8 @@ template uint8_t AY38910::get_data_output() { return data_output_; } -template void AY38910::set_control_lines(ControlLines control_lines) { +template +void AY38910SampleSource::set_control_lines(ControlLines control_lines) { switch(int(control_lines)) { default: control_state_ = Inactive; break; @@ -389,7 +376,8 @@ template void AY38910::set_control_lines(ControlLine update_bus(); } -template void AY38910::update_bus() { +template +void AY38910SampleSource::update_bus() { // Assume no output, unless this turns out to be a read. data_output_ = 0xff; switch(control_state_) { @@ -401,5 +389,5 @@ template void AY38910::update_bus() { } // Ensure both mono and stereo versions of the AY are built. -template class GI::AY38910::AY38910; -template class GI::AY38910::AY38910; +template class GI::AY38910::AY38910SampleSource; +template class GI::AY38910::AY38910SampleSource; diff --git a/Components/AY38910/AY38910.hpp b/Components/AY38910/AY38910.hpp index 722abcba2..3d82d4f3f 100644 --- a/Components/AY38910/AY38910.hpp +++ b/Components/AY38910/AY38910.hpp @@ -66,10 +66,10 @@ enum class Personality { This AY has an attached mono or stereo mixer. */ -template class AY38910: public ::Outputs::Speaker::BufferSource, stereo> { +template class AY38910SampleSource { public: /// Creates a new AY38910. - AY38910(Personality, Concurrency::AsyncTaskQueue &); + AY38910SampleSource(Personality, Concurrency::AsyncTaskQueue &); /// Sets the value the AY would read from its data lines if it were not outputting. void set_data_input(uint8_t r); @@ -105,9 +105,9 @@ template class AY38910: public ::Outputs::Speaker::BufferSource - void apply_samples(std::size_t number_of_samples, typename Outputs::Speaker::SampleT::type *target); + // Sample generation. + typename Outputs::Speaker::SampleT::type level() const; + void advance(); bool is_zero_level() const; void set_sample_volume_range(std::int16_t range); @@ -118,8 +118,6 @@ template class AY38910: public ::Outputs::Speaker::BufferSource class AY38910: public ::Outputs::Speaker::BufferSource struct AY38910: + public AY38910SampleSource, + public Outputs::Speaker::SampleSource, stereo, 4> { + + using AY38910SampleSource::AY38910SampleSource; + + }; + /*! Provides helper code, to provide something closer to the interface exposed by many AY-deploying machines of the era. diff --git a/Components/AudioToggle/AudioToggle.hpp b/Components/AudioToggle/AudioToggle.hpp index ea304e06a..06312225c 100644 --- a/Components/AudioToggle/AudioToggle.hpp +++ b/Components/AudioToggle/AudioToggle.hpp @@ -25,6 +25,9 @@ class Toggle: public Outputs::Speaker::BufferSource { Outputs::Speaker::fill(target, target + number_of_samples, level_); } void set_sample_volume_range(std::int16_t range); + bool is_zero_level() const { + return !level_; + } void set_output(bool enabled); bool get_output() const; diff --git a/Components/OPx/OPLL.hpp b/Components/OPx/OPLL.hpp index da08a3f97..bca92a969 100644 --- a/Components/OPx/OPLL.hpp +++ b/Components/OPx/OPLL.hpp @@ -28,6 +28,7 @@ class OPLL: public OPLBase { template void apply_samples(std::size_t number_of_samples, Outputs::Speaker::MonoSample *target); void set_sample_volume_range(std::int16_t range); + bool is_zero_level() const { return false; } // TODO. // The OPLL is generally 'half' as loud as it's told to be. This won't strictly be true in // rhythm mode, but it's correct for melodic output. diff --git a/Machines/Apple/AppleIIgs/Sound.hpp b/Machines/Apple/AppleIIgs/Sound.hpp index ea25afba7..711873cbd 100644 --- a/Machines/Apple/AppleIIgs/Sound.hpp +++ b/Machines/Apple/AppleIIgs/Sound.hpp @@ -37,6 +37,7 @@ class GLU: public Outputs::Speaker::BufferSource { // TODO: isn't th template void apply_samples(std::size_t number_of_samples, Outputs::Speaker::MonoSample *target); void set_sample_volume_range(std::int16_t range); + bool is_zero_level() const { return false; } // TODO. private: Concurrency::AsyncTaskQueue &audio_queue_; diff --git a/Outputs/Speaker/Implementation/BufferSource.hpp b/Outputs/Speaker/Implementation/BufferSource.hpp index f5d4f7573..3ac7eb323 100644 --- a/Outputs/Speaker/Implementation/BufferSource.hpp +++ b/Outputs/Speaker/Implementation/BufferSource.hpp @@ -76,13 +76,13 @@ class BufferSource { fill the target with zeroes; @c false if a call might return all zeroes or might not. */ - bool is_zero_level() const { return false; } +// bool is_zero_level() const { return false; } /*! Sets the proper output range for this sample source; it should write values between 0 and volume. */ - void set_sample_volume_range(std::int16_t volume); +// void set_sample_volume_range(std::int16_t volume); /*! Permits a sample source to declare that, averaged over time, it will use only @@ -101,13 +101,13 @@ class BufferSource { template struct SampleSource: public BufferSource { public: - template + template void apply_samples(std::size_t number_of_samples, typename SampleT::type *target) { - const auto &source = *static_cast(this); + auto &source = *static_cast(this); if constexpr (divider == 1) { while(number_of_samples--) { - apply(*target, source.level()); + apply(*target, source.level()); ++target; source.advance(); } @@ -117,34 +117,32 @@ struct SampleSource: public BufferSource { // Fill in the tail of any partially-captured level. auto level = source.level(); while(c < number_of_samples && master_divider_ != divider) { - apply(target[c], level); + apply(target[c], level); ++c; ++master_divider_; } source.advance(); // Provide all full levels. - int whole_steps = (number_of_samples - c) / divider; + auto whole_steps = static_cast((number_of_samples - c) / divider); while(whole_steps--) { - fill(&target[c], &target[c + divider], source.level()); + fill(&target[c], &target[c + divider], source.level()); c += divider; source.advance(); } // Provide the head of a further partial capture. level = source.level(); - master_divider_ = number_of_samples - c; - fill(&target[c], &target[number_of_samples], source.level()); + master_divider_ = static_cast(number_of_samples - c); + fill(&target[c], &target[number_of_samples], source.level()); } } // TODO: use a concept here, when C++20 filters through. // // Until then: sample sources should implement this. - auto level() const { - return typename SampleT::type(); - } - void advance() {} +// typename SampleT::type level() const; +// void advance(); private: int master_divider_{};