diff --git a/Machines/Apple/Macintosh/Audio.cpp b/Machines/Apple/Macintosh/Audio.cpp index 2f9147b6b..06b891458 100644 --- a/Machines/Apple/Macintosh/Audio.cpp +++ b/Machines/Apple/Macintosh/Audio.cpp @@ -13,88 +13,100 @@ using namespace Apple::Macintosh; namespace { +constexpr HalfCycles MasterClockDivider = 8; // Period at which to generate output samples; higher than the sampling + // rate to capture VIA activity — volume changes and the enable/disable + // toggle — which might occur at higher frequencies. -// The sample_length is coupled with the clock rate selected within the Macintosh proper; -// as per the header-declaration a divide-by-two clock is expected to arrive here. -const std::size_t sample_length = 352 / 2; - +constexpr auto MaxBufferSize = 1024; // Maximum number of samples to accumulate on the emulator thread before + // pushing to the speaker. } -Audio::Audio(Concurrency::AsyncTaskQueue &task_queue) : task_queue_(task_queue) {} +// MARK: - Setup +Audio::Audio(HalfCycles clock_rate, const Video &video, DriveSpeedAccumulator &drive_speed_accumulator) : + phase_(video.audio_phase()), + drive_speed_accumulator_(drive_speed_accumulator) { + + speaker_.speaker().set_input_rate(clock_rate.as() / MasterClockDivider.as()); +} + +void Audio::set_ram(const uint16_t *const ram, const uint32_t mask) { + ram_ = ram; + ram_mask_ = mask; +} + +void Audio::set_use_alternate_buffer(const bool use_alternate_buffer) { + use_alternate_buffer_ = use_alternate_buffer; +} // MARK: - Inputs -void Audio::post_sample(uint8_t sample) { - // Store sample directly indexed by current write pointer; this ensures that collected samples - // directly map to volume and enabled/disabled states. - sample_queue_.buffer[sample_queue_.write_pointer].store(sample, std::memory_order::relaxed); - sample_queue_.write_pointer = (sample_queue_.write_pointer + 1) % sample_queue_.buffer.size(); +void Audio::operator += (const HalfCycles duration) { + HalfCycles remaining = duration; + while(remaining > HalfCycles(0)) { + const auto next = std::min(remaining.as(), phase_.sample_phase.remaining()); + remaining -= HalfCycles(next); + time_ += HalfCycles(next); + + if(phase_.sample_phase.advance_divide(duration.as())) { + const size_t base = + use_alternate_buffer_ ? (0xffffa100 >> 1) : (0xfffffd00 >> 1); + const uint16_t audio_word = ram_[(address_ + base) & ram_mask_]; + ++address_; + + post_sample(audio_word >> 8); + drive_speed_accumulator_.post_sample(audio_word & 0xff); + + if(phase_.buffer_phase.advance_divide(1)) { + address_ = 0; + } + } + + static constexpr auto MaxBufferDuration = HalfCycles(Cycles(MaxBufferSize)) * MasterClockDivider; + if(time_ >= MaxBufferDuration) { + generate(); + } + } } -void Audio::set_volume(int volume) { - // Do nothing if the volume hasn't changed. - if(posted_volume_ == volume) return; - posted_volume_ = volume; +void Audio::generate() { + const auto samples = time_.divide(MasterClockDivider).as(); + const auto level = int16_t(sample_ * volume_multiplier_); - // Post the volume change as a deferred event. - task_queue_.enqueue([this, volume] () { - volume_ = volume; - set_volume_multiplier(); - }); + std::fill_n(std::back_inserter(samples_), samples, level); + if(samples_.size() >= MaxBufferSize) { + flush(); + } } -void Audio::set_enabled(bool on) { - // Do nothing if the mask hasn't changed. - if(posted_enable_mask_ == int(on)) return; - posted_enable_mask_ = int(on); - - // Post the enabled mask change as a deferred event. - task_queue_.enqueue([this, on] () { - enabled_mask_ = int(on); - set_volume_multiplier(); - }); +void Audio::flush() { + speaker_.push_swap(samples_); } -// MARK: - Output generation +// MARK: - Setters. -bool Audio::is_zero_level() const { - return !volume_ || !enabled_mask_; +void Audio::post_sample(const uint8_t sample) { + if(sample_ == sample) return; + generate(); + + sample_ = sample; } -void Audio::set_sample_volume_range(std::int16_t range) { - // Some underflow here doesn't really matter. - output_volume_ = range / (7 * 255); +void Audio::set_volume(const int volume) { + if(volume_ == volume) return; + generate(); + + volume_ = volume; + set_volume_multiplier(); +} + +void Audio::set_enabled(const bool on) { + if(enable_mask_ == int(on)) return; + generate(); + + enable_mask_ = int(on); set_volume_multiplier(); } void Audio::set_volume_multiplier() { - volume_multiplier_ = int16_t(output_volume_ * volume_ * enabled_mask_); + volume_multiplier_ = int16_t(volume_ * enable_mask_ * (127 / 7)); } - -template -void Audio::apply_samples(std::size_t number_of_samples, Outputs::Speaker::MonoSample *target) { - // TODO: the implementation below acts as if the hardware uses pulse-amplitude modulation; - // in fact it uses pulse-width modulation. But the scale for pulses isn't specified, so - // that's something to return to. - - while(number_of_samples) { - // Determine how many output samples will be at the same level. - const auto cycles_left_in_sample = std::min(number_of_samples, sample_length - subcycle_offset_); - - // Determine the output level, and output that many samples. - const int16_t output_level = volume_multiplier_ * (int16_t(sample_queue_.buffer[sample_queue_.read_pointer].load(std::memory_order::relaxed)) - 128); - Outputs::Speaker::fill(target, target + cycles_left_in_sample, output_level); - target += cycles_left_in_sample; - - // Advance the sample pointer. - subcycle_offset_ += cycles_left_in_sample; - sample_queue_.read_pointer = (sample_queue_.read_pointer + (subcycle_offset_ / sample_length)) % sample_queue_.buffer.size(); - subcycle_offset_ %= sample_length; - - // Decreate the number of samples left to write. - number_of_samples -= cycles_left_in_sample; - } -} -template void Audio::apply_samples(std::size_t, Outputs::Speaker::MonoSample *); -template void Audio::apply_samples(std::size_t, Outputs::Speaker::MonoSample *); -template void Audio::apply_samples(std::size_t, Outputs::Speaker::MonoSample *); diff --git a/Machines/Apple/Macintosh/Audio.hpp b/Machines/Apple/Macintosh/Audio.hpp index 1b2c1e865..8e13f4467 100644 --- a/Machines/Apple/Macintosh/Audio.hpp +++ b/Machines/Apple/Macintosh/Audio.hpp @@ -8,12 +8,10 @@ #pragma once -#include "Concurrency/AsyncTaskQueue.hpp" -#include "ClockReceiver/ClockReceiver.hpp" -#include "Outputs/Speaker/Implementation/BufferSource.hpp" +#include "DriveSpeedAccumulator.hpp" +#include "Video.hpp" -#include -#include +#include "Outputs/Speaker/Implementation/LowpassSpeaker.hpp" namespace Apple::Macintosh { @@ -23,9 +21,11 @@ namespace Apple::Macintosh { Designed to be clocked at half the rate of the real hardware — i.e. a shade less than 4Mhz. */ -class Audio: public ::Outputs::Speaker::BufferSource { +class Audio { public: - Audio(Concurrency::AsyncTaskQueue &task_queue); + Audio(HalfCycles clock_rate, const Video &, DriveSpeedAccumulator &); + void set_ram(const uint16_t *ram, uint32_t mask); + void set_use_alternate_buffer(bool); /*! Macintosh audio is (partly) sourced by the same scanning @@ -34,7 +34,7 @@ public: Use this method to add a newly-collected sample to the queue. */ - void post_sample(uint8_t sample); + void post_sample(uint8_t); /*! Macintosh audio also separately receives an output volume @@ -42,43 +42,45 @@ public: Use this method to set the current output volume. */ - void set_volume(int volume); + void set_volume(int); /*! A further factor in audio output is the on-off toggle. */ - void set_enabled(bool on); + void set_enabled(bool); - // to satisfy ::Outputs::Speaker (included via ::Outputs::Filter. - template - void apply_samples(std::size_t number_of_samples, Outputs::Speaker::MonoSample *target); - bool is_zero_level() const; - void set_sample_volume_range(std::int16_t range); + Outputs::Speaker::Speaker &speaker() { + return speaker_.speaker(); + } + + void operator += (const HalfCycles); + + void flush(); private: - Concurrency::AsyncTaskQueue &task_queue_; + Outputs::Speaker::AsyncPushLowpass speaker_; + std::vector samples_; + HalfCycles time_; - // A queue of fetched samples; read from by one thread, - // written to by another. - struct { - std::array, 740> buffer; - size_t read_pointer = 0, write_pointer = 0; - } sample_queue_; + // Fetch state. + size_t address_ = 0; + bool use_alternate_buffer_ = false; + + AudioPhase phase_; + const uint16_t *ram_ = nullptr; + uint32_t ram_mask_ = 0; + DriveSpeedAccumulator &drive_speed_accumulator_; // Emulator-thread stateful variables, to avoid work posting // deferral updates if possible. - int posted_volume_ = 0; - int posted_enable_mask_ = 0; - - // Stateful variables, modified from the audio generation - // thread only. int volume_ = 0; - int enabled_mask_ = 0; - std::int16_t output_volume_ = 0; + int enable_mask_ = 0; - std::int16_t volume_multiplier_ = 0; - std::size_t subcycle_offset_ = 0; + int volume_multiplier_ = 0; + uint8_t sample_ = 0; void set_volume_multiplier(); + + void generate(); }; } diff --git a/Machines/Apple/Macintosh/DeferredAudio.hpp b/Machines/Apple/Macintosh/DeferredAudio.hpp deleted file mode 100644 index d8b191a9f..000000000 --- a/Machines/Apple/Macintosh/DeferredAudio.hpp +++ /dev/null @@ -1,29 +0,0 @@ -// -// DeferredAudio.hpp -// Clock Signal -// -// Created by Thomas Harte on 01/06/2019. -// Copyright © 2019 Thomas Harte. All rights reserved. -// - -#pragma once - -#include "Audio.hpp" -#include "Outputs/Speaker/Implementation/LowpassSpeaker.hpp" - -namespace Apple::Macintosh { - -struct DeferredAudio { - Concurrency::AsyncTaskQueue queue; - Audio audio; - Outputs::Speaker::PullLowpass