2019-06-01 18:39:40 +00:00
|
|
|
//
|
|
|
|
// Audio.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 31/05/2019.
|
|
|
|
// Copyright © 2019 Thomas Harte. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#include "Audio.hpp"
|
|
|
|
|
|
|
|
using namespace Apple::Macintosh;
|
|
|
|
|
2019-06-01 19:18:27 +00:00
|
|
|
namespace {
|
2019-06-01 19:44:29 +00:00
|
|
|
const std::size_t sample_length = 352;
|
2019-06-01 19:18:27 +00:00
|
|
|
}
|
|
|
|
|
2019-06-01 18:39:40 +00:00
|
|
|
Audio::Audio(Concurrency::DeferringAsyncTaskQueue &task_queue) : task_queue_(task_queue) {}
|
|
|
|
|
|
|
|
// MARK: - Inputs
|
|
|
|
|
|
|
|
void Audio::post_sample(uint8_t sample) {
|
2019-06-01 19:18:27 +00:00
|
|
|
// Grab the read and write pointers, ensure there's room for a new sample and, if not,
|
|
|
|
// drop this one.
|
2019-06-01 18:39:40 +00:00
|
|
|
const auto write_pointer = sample_queue_.write_pointer.load();
|
|
|
|
const auto read_pointer = sample_queue_.read_pointer.load();
|
2019-06-01 19:18:27 +00:00
|
|
|
const decltype(write_pointer) next_write_pointer = (write_pointer + 1) % sample_queue_.buffer.size();
|
2019-06-01 21:29:57 +00:00
|
|
|
if(next_write_pointer == read_pointer) {
|
|
|
|
return;
|
|
|
|
}
|
2019-06-01 19:18:27 +00:00
|
|
|
|
|
|
|
sample_queue_.buffer[write_pointer] = sample;
|
|
|
|
sample_queue_.write_pointer.store(next_write_pointer);
|
2019-06-01 18:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::set_volume(int volume) {
|
2019-06-01 19:18:27 +00:00
|
|
|
// Post the volume change as a deferred event.
|
2019-06-01 18:39:40 +00:00
|
|
|
task_queue_.defer([=] () {
|
|
|
|
volume_ = volume;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::set_enabled(bool on) {
|
2019-06-01 19:18:27 +00:00
|
|
|
// Post the enabled mask change as a deferred event.
|
2019-06-01 18:39:40 +00:00
|
|
|
task_queue_.defer([=] () {
|
2019-06-01 19:18:27 +00:00
|
|
|
enabled_mask_ = on ? 1 : 0;
|
2019-06-01 18:39:40 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
// MARK: - Output generation
|
|
|
|
|
|
|
|
bool Audio::is_zero_level() {
|
2019-06-01 19:18:27 +00:00
|
|
|
return !volume_ || !enabled_mask_;
|
2019-06-01 18:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::set_sample_volume_range(std::int16_t range) {
|
2019-06-01 19:18:27 +00:00
|
|
|
// Some underflow here doesn't really matter.
|
2019-06-01 19:44:29 +00:00
|
|
|
volume_multiplier_ = range / (7 * 255);
|
2019-06-01 18:39:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::get_samples(std::size_t number_of_samples, int16_t *target) {
|
2019-06-01 19:44:29 +00:00
|
|
|
const auto write_pointer = sample_queue_.write_pointer.load();
|
|
|
|
auto read_pointer = sample_queue_.read_pointer.load();
|
|
|
|
|
|
|
|
// 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.
|
|
|
|
|
|
|
|
// TODO: temporary implementation. Very inefficient. Replace.
|
2019-06-01 21:29:57 +00:00
|
|
|
for(std::size_t sample = 0; sample < number_of_samples; ++sample) {
|
2019-06-18 14:34:31 +00:00
|
|
|
// if(volume_ && enabled_mask_) printf("%d\n", sample_queue_.buffer[read_pointer]);
|
2019-06-11 23:53:22 +00:00
|
|
|
target[sample] = volume_multiplier_ * int16_t(sample_queue_.buffer[read_pointer] * volume_ * enabled_mask_);
|
2019-06-01 19:44:29 +00:00
|
|
|
++subcycle_offset_;
|
|
|
|
|
|
|
|
if(subcycle_offset_ == sample_length) {
|
2019-06-01 21:29:57 +00:00
|
|
|
// printf("%d: %d\n", sample_queue_.buffer[read_pointer], volume_multiplier_ * int16_t(sample_queue_.buffer[read_pointer]));
|
2019-06-01 19:44:29 +00:00
|
|
|
subcycle_offset_ = 0;
|
|
|
|
const unsigned int next_read_pointer = (read_pointer + 1) % sample_queue_.buffer.size();
|
|
|
|
if(next_read_pointer != write_pointer) {
|
|
|
|
read_pointer = next_read_pointer;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
sample_queue_.read_pointer.store(read_pointer);
|
2019-06-01 19:18:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Audio::skip_samples(std::size_t number_of_samples) {
|
2019-06-01 19:44:29 +00:00
|
|
|
const auto write_pointer = sample_queue_.write_pointer.load();
|
|
|
|
auto read_pointer = sample_queue_.read_pointer.load();
|
|
|
|
|
|
|
|
// Number of samples that would be consumed is (number_of_samples + subcycle_offset_) / sample_length.
|
|
|
|
const unsigned int samples_passed = static_cast<unsigned int>((number_of_samples + subcycle_offset_) / sample_length);
|
|
|
|
subcycle_offset_ = (number_of_samples + subcycle_offset_) % sample_length;
|
|
|
|
|
|
|
|
// Get also number of samples available.
|
|
|
|
const unsigned int samples_available = static_cast<unsigned int>((write_pointer + sample_queue_.buffer.size() - read_pointer) % sample_queue_.buffer.size());
|
|
|
|
|
|
|
|
// Advance by whichever of those is the lower number.
|
|
|
|
const auto samples_to_consume = std::min(samples_available, samples_passed);
|
|
|
|
read_pointer = (read_pointer + samples_to_consume) % sample_queue_.buffer.size();
|
|
|
|
|
|
|
|
sample_queue_.read_pointer.store(read_pointer);
|
2019-06-01 18:39:40 +00:00
|
|
|
}
|