2021-11-09 12:11:23 +00:00
|
|
|
|
//
|
|
|
|
|
// Audio.hpp
|
|
|
|
|
// Clock Signal
|
|
|
|
|
//
|
|
|
|
|
// Created by Thomas Harte on 09/11/2021.
|
|
|
|
|
// Copyright © 2021 Thomas Harte. All rights reserved.
|
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
#ifndef Audio_hpp
|
|
|
|
|
#define Audio_hpp
|
|
|
|
|
|
2021-12-01 10:37:58 +00:00
|
|
|
|
#include <atomic>
|
|
|
|
|
#include <cstdint>
|
|
|
|
|
|
2021-11-09 12:11:23 +00:00
|
|
|
|
#include "DMADevice.hpp"
|
2021-11-11 14:24:15 +00:00
|
|
|
|
#include "../../ClockReceiver/ClockReceiver.hpp"
|
|
|
|
|
#include "../../Concurrency/AsyncTaskQueue.hpp"
|
2021-12-01 10:37:58 +00:00
|
|
|
|
#include "../../Outputs/Speaker/Implementation/LowpassSpeaker.hpp"
|
2021-11-09 12:11:23 +00:00
|
|
|
|
|
|
|
|
|
namespace Amiga {
|
|
|
|
|
|
|
|
|
|
class Audio: public DMADevice<4> {
|
|
|
|
|
public:
|
2021-12-01 10:37:58 +00:00
|
|
|
|
Audio(Chipset &chipset, uint16_t *ram, size_t word_size, float output_rate);
|
2021-11-09 12:11:23 +00:00
|
|
|
|
|
2021-11-11 14:24:15 +00:00
|
|
|
|
/// Idiomatic call-in for DMA scheduling; indicates that this class may
|
|
|
|
|
/// perform a DMA access for the stated channel now.
|
2021-11-13 20:53:41 +00:00
|
|
|
|
bool advance_dma(int channel);
|
2021-11-09 12:11:23 +00:00
|
|
|
|
|
2021-11-13 20:53:41 +00:00
|
|
|
|
/// Advances output by one DMA window, which is implicitly two cycles
|
|
|
|
|
/// at the output rate that was specified to the constructor.
|
|
|
|
|
void output();
|
2021-11-11 14:24:15 +00:00
|
|
|
|
|
|
|
|
|
/// Sets the total number of words to fetch for the given channel.
|
|
|
|
|
void set_length(int channel, uint16_t);
|
|
|
|
|
|
|
|
|
|
/// Sets the number of DMA windows between each 8-bit output,
|
|
|
|
|
/// in the same time base as @c ticks_per_line.
|
|
|
|
|
void set_period(int channel, uint16_t);
|
|
|
|
|
|
|
|
|
|
/// Sets the output volume for the given channel; if bit 6 is set
|
|
|
|
|
/// then output is maximal; otherwise bits 0–5 select
|
|
|
|
|
/// a volume of [0–63]/64, on a logarithmic scale.
|
|
|
|
|
void set_volume(int channel, uint16_t);
|
|
|
|
|
|
|
|
|
|
/// Sets the next two samples of audio to output.
|
2021-12-06 11:35:08 +00:00
|
|
|
|
template <bool is_external = true> void set_data(int channel, uint16_t);
|
2021-11-11 14:24:15 +00:00
|
|
|
|
|
|
|
|
|
/// Provides a copy of the DMA enable flags, for the purpose of
|
|
|
|
|
/// determining which channels are enabled for DMA.
|
|
|
|
|
void set_channel_enables(uint16_t);
|
|
|
|
|
|
|
|
|
|
/// Sets which channels, if any, modulate period or volume of
|
|
|
|
|
/// their neighbours.
|
|
|
|
|
void set_modulation_flags(uint16_t);
|
|
|
|
|
|
2021-11-13 16:05:39 +00:00
|
|
|
|
/// Sets which interrupt requests are currently active.
|
|
|
|
|
void set_interrupt_requests(uint16_t);
|
|
|
|
|
|
2021-12-01 10:37:58 +00:00
|
|
|
|
/// Obtains the output source.
|
|
|
|
|
Outputs::Speaker::Speaker *get_speaker() {
|
|
|
|
|
return &speaker_;
|
|
|
|
|
}
|
|
|
|
|
|
2021-11-11 14:24:15 +00:00
|
|
|
|
private:
|
|
|
|
|
struct Channel {
|
|
|
|
|
// The data latch plus a count of unused samples
|
|
|
|
|
// in the latch, which will always be 0, 1 or 2.
|
|
|
|
|
uint16_t data = 0x0000;
|
2021-12-02 14:41:16 +00:00
|
|
|
|
bool wants_data = false;
|
2021-11-14 15:48:50 +00:00
|
|
|
|
uint16_t data_latch = 0x0000;
|
2021-11-11 14:24:15 +00:00
|
|
|
|
|
2021-12-04 22:58:41 +00:00
|
|
|
|
// The DMA address; unlike most of the Amiga Chipset,
|
|
|
|
|
// the user posts a value to feed a pointer, rather
|
|
|
|
|
// than having access to the pointer itself.
|
|
|
|
|
bool should_reload_address = false;
|
|
|
|
|
uint32_t data_address = 0x0000'0000;
|
|
|
|
|
|
2021-11-11 14:24:15 +00:00
|
|
|
|
// Number of words remaining in DMA data.
|
|
|
|
|
uint16_t length = 0;
|
2021-11-14 15:48:50 +00:00
|
|
|
|
uint16_t length_counter = 0;
|
2021-11-11 14:24:15 +00:00
|
|
|
|
|
|
|
|
|
// Number of ticks between each sample, plus the
|
|
|
|
|
// current counter, which counts downward.
|
|
|
|
|
uint16_t period = 0;
|
|
|
|
|
uint16_t period_counter = 0;
|
|
|
|
|
|
2021-12-04 23:02:43 +00:00
|
|
|
|
// Modulation / attach flags.
|
|
|
|
|
bool attach_period = false;
|
|
|
|
|
bool attach_volume = false;
|
|
|
|
|
|
2021-11-11 14:24:15 +00:00
|
|
|
|
// Output volume, [0, 64].
|
2021-12-06 11:35:08 +00:00
|
|
|
|
uint8_t volume = 0;
|
|
|
|
|
uint8_t volume_latch = 0;
|
2021-11-11 14:24:15 +00:00
|
|
|
|
|
|
|
|
|
// Indicates whether DMA is enabled for this channel.
|
|
|
|
|
bool dma_enabled = false;
|
2021-11-12 20:30:52 +00:00
|
|
|
|
|
2021-11-13 16:05:39 +00:00
|
|
|
|
// Records whether this audio interrupt is pending.
|
|
|
|
|
bool interrupt_pending = false;
|
2021-11-15 17:29:32 +00:00
|
|
|
|
bool will_request_interrupt = false;
|
2021-11-13 16:05:39 +00:00
|
|
|
|
|
2021-11-12 20:30:52 +00:00
|
|
|
|
// Replicates the Hardware Reference Manual state machine;
|
|
|
|
|
// comments indicate which of the documented states each
|
|
|
|
|
// label refers to.
|
|
|
|
|
enum class State {
|
|
|
|
|
Disabled, // 000
|
|
|
|
|
WaitingForDummyDMA, // 001
|
|
|
|
|
WaitingForDMA, // 101
|
|
|
|
|
PlayingHigh, // 010
|
|
|
|
|
PlayingLow, // 011
|
2021-11-13 16:05:39 +00:00
|
|
|
|
} state = State::Disabled;
|
2021-11-14 15:48:50 +00:00
|
|
|
|
|
2021-12-04 12:20:17 +00:00
|
|
|
|
/// Dispatches to the appropriate templatised output for the current state.
|
2021-12-05 11:38:55 +00:00
|
|
|
|
/// @param moduland The channel to modulate, if modulation is enabled.
|
2021-12-04 12:20:17 +00:00
|
|
|
|
/// @returns @c true if an interrupt should be posted; @c false otherwise.
|
2021-12-05 11:38:55 +00:00
|
|
|
|
bool output(Channel *moduland);
|
2021-12-04 12:20:17 +00:00
|
|
|
|
|
|
|
|
|
/// Applies dynamic logic for @c state, mostly testing for potential state transitions.
|
2021-12-05 11:38:55 +00:00
|
|
|
|
/// @param moduland The channel to modulate, if modulation is enabled.
|
2021-12-04 12:20:17 +00:00
|
|
|
|
/// @returns @c true if an interrupt should be posted; @c false otherwise.
|
2021-12-05 11:38:55 +00:00
|
|
|
|
template <State state> bool output(Channel *moduland);
|
2021-12-04 12:20:17 +00:00
|
|
|
|
|
|
|
|
|
/// Transitions from @c begin to @c end, calling the appropriate @c begin_state
|
|
|
|
|
/// and taking any steps specific to that particular transition.
|
2021-12-05 11:38:55 +00:00
|
|
|
|
/// @param moduland The channel to modulate, if modulation is enabled.
|
2021-12-04 12:20:17 +00:00
|
|
|
|
/// @returns @c true if an interrupt should be posted; @c false otherwise.
|
2021-12-05 11:38:55 +00:00
|
|
|
|
template <State begin, State end> bool transit(Channel *moduland);
|
2021-12-01 11:01:58 +00:00
|
|
|
|
|
2021-12-04 12:20:17 +00:00
|
|
|
|
/// Begins @c state, performing all fixed logic that would otherwise have to be
|
|
|
|
|
/// repeated endlessly in the relevant @c output.
|
2021-12-05 11:38:55 +00:00
|
|
|
|
/// @param moduland The channel to modulate, if modulation is enabled.
|
|
|
|
|
template <State state> void begin_state(Channel *moduland);
|
2021-12-04 12:20:17 +00:00
|
|
|
|
|
2021-12-05 20:27:35 +00:00
|
|
|
|
/// Provides the common length-decrementing logic used when transitioning
|
|
|
|
|
/// between PlayingHigh and PlayingLow in either direction.
|
|
|
|
|
void decrement_length();
|
|
|
|
|
|
2021-12-01 11:01:58 +00:00
|
|
|
|
// Output state.
|
2021-12-02 16:15:29 +00:00
|
|
|
|
int8_t output_level = 0;
|
2021-12-06 11:35:08 +00:00
|
|
|
|
uint8_t output_phase = 0;
|
2021-12-01 11:01:58 +00:00
|
|
|
|
bool output_enabled = false;
|
2021-12-06 11:35:08 +00:00
|
|
|
|
|
|
|
|
|
void reset_output_phase() {
|
|
|
|
|
output_phase = 0;
|
|
|
|
|
output_enabled = (volume_latch > 0) && !attach_period && !attach_volume;
|
|
|
|
|
}
|
2021-11-11 14:24:15 +00:00
|
|
|
|
} channels_[4];
|
2021-12-01 10:37:58 +00:00
|
|
|
|
|
|
|
|
|
// Transient output state, and its destination.
|
|
|
|
|
Outputs::Speaker::PushLowpass<true> speaker_;
|
2022-07-16 18:41:04 +00:00
|
|
|
|
Concurrency::AsyncTaskQueue<true> queue_;
|
2021-12-01 10:37:58 +00:00
|
|
|
|
|
|
|
|
|
using AudioBuffer = std::array<int16_t, 4096>;
|
|
|
|
|
static constexpr int BufferCount = 3;
|
|
|
|
|
AudioBuffer buffer_[BufferCount];
|
|
|
|
|
std::atomic<bool> buffer_available_[BufferCount];
|
|
|
|
|
size_t buffer_pointer_ = 0, sample_pointer_ = 0;
|
2021-11-09 12:11:23 +00:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#endif /* Audio_hpp */
|