2016-07-10 22:36:52 +00:00
|
|
|
//
|
|
|
|
// PCMTrack.cpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 10/07/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-07-10 22:36:52 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#include "PCMTrack.hpp"
|
2018-07-02 02:49:57 +00:00
|
|
|
#include "../../../Outputs/Log.hpp"
|
2016-07-10 22:36:52 +00:00
|
|
|
|
2016-08-27 21:15:09 +00:00
|
|
|
using namespace Storage::Disk;
|
2016-07-10 22:36:52 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
PCMTrack::PCMTrack() : segment_pointer_(0) {}
|
2016-12-19 02:37:05 +00:00
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
PCMTrack::PCMTrack(const std::vector<PCMSegment> &segments) : PCMTrack() {
|
2016-12-19 02:37:05 +00:00
|
|
|
// sum total length of all segments
|
|
|
|
Time total_length;
|
2018-05-01 02:23:57 +00:00
|
|
|
for(const auto &segment : segments) {
|
2018-07-01 16:05:41 +00:00
|
|
|
total_length += segment.length_of_a_bit * static_cast<unsigned int>(segment.data.size());
|
2016-12-19 02:37:05 +00:00
|
|
|
}
|
2016-12-19 03:53:24 +00:00
|
|
|
total_length.simplify();
|
2016-12-19 02:37:05 +00:00
|
|
|
|
|
|
|
// each segment is then some proportion of the total; for them all to sum to 1 they'll
|
|
|
|
// need to be adjusted to be
|
2018-05-01 02:23:57 +00:00
|
|
|
for(const auto &segment : segments) {
|
2018-07-01 16:05:41 +00:00
|
|
|
Time original_length_of_segment = segment.length_of_a_bit * static_cast<unsigned int>(segment.data.size());
|
2016-12-19 02:37:05 +00:00
|
|
|
Time proportion_of_whole = original_length_of_segment / total_length;
|
2016-12-19 03:53:24 +00:00
|
|
|
proportion_of_whole.simplify();
|
2016-12-19 02:37:05 +00:00
|
|
|
PCMSegment length_adjusted_segment = segment;
|
2018-07-01 16:05:41 +00:00
|
|
|
length_adjusted_segment.length_of_a_bit = proportion_of_whole / static_cast<unsigned int>(segment.data.size());
|
2016-12-19 02:37:05 +00:00
|
|
|
length_adjusted_segment.length_of_a_bit.simplify();
|
|
|
|
segment_event_sources_.emplace_back(length_adjusted_segment);
|
|
|
|
}
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
PCMTrack::PCMTrack(const PCMSegment &segment) : PCMTrack() {
|
2016-12-19 02:37:05 +00:00
|
|
|
// a single segment necessarily fills the track
|
|
|
|
PCMSegment length_adjusted_segment = segment;
|
|
|
|
length_adjusted_segment.length_of_a_bit.length = 1;
|
2018-07-01 16:05:41 +00:00
|
|
|
length_adjusted_segment.length_of_a_bit.clock_rate = static_cast<unsigned int>(segment.data.size());
|
2018-07-01 00:03:18 +00:00
|
|
|
segment_event_sources_.emplace_back(std::move(length_adjusted_segment));
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
PCMTrack::PCMTrack(const PCMTrack &original) : PCMTrack() {
|
2016-12-30 19:23:26 +00:00
|
|
|
segment_event_sources_ = original.segment_event_sources_;
|
|
|
|
}
|
|
|
|
|
2018-07-01 00:03:18 +00:00
|
|
|
PCMTrack::PCMTrack(unsigned int bits_per_track) : PCMTrack() {
|
|
|
|
PCMSegment segment;
|
|
|
|
segment.length_of_a_bit.length = 1;
|
|
|
|
segment.length_of_a_bit.clock_rate = bits_per_track;
|
2018-07-01 22:28:25 +00:00
|
|
|
segment.data.resize(bits_per_track);
|
2018-07-01 00:03:18 +00:00
|
|
|
segment_event_sources_.emplace_back(segment);
|
|
|
|
}
|
|
|
|
|
2018-07-02 02:49:57 +00:00
|
|
|
PCMTrack *PCMTrack::resampled_clone(Track *original, size_t bits_per_track) {
|
|
|
|
PCMTrack *pcm_original = dynamic_cast<PCMTrack *>(original);
|
|
|
|
if(pcm_original) {
|
|
|
|
return pcm_original->resampled_clone(bits_per_track);
|
|
|
|
}
|
|
|
|
|
|
|
|
ERROR("NOT IMPLEMENTED: resampling non-PCMTracks");
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PCMTrack::is_resampled_clone() {
|
|
|
|
return is_resampled_clone_;
|
|
|
|
}
|
|
|
|
|
2018-05-19 03:03:28 +00:00
|
|
|
Track *PCMTrack::clone() const {
|
2016-12-30 22:25:39 +00:00
|
|
|
return new PCMTrack(*this);
|
|
|
|
}
|
|
|
|
|
2018-07-02 02:49:57 +00:00
|
|
|
PCMTrack *PCMTrack::resampled_clone(size_t bits_per_track) {
|
2018-07-01 22:28:25 +00:00
|
|
|
// Create an empty track.
|
|
|
|
PCMTrack *const new_track = new PCMTrack(static_cast<unsigned int>(bits_per_track));
|
2018-07-01 00:03:18 +00:00
|
|
|
|
2018-07-01 22:28:25 +00:00
|
|
|
// Plot all segments from this track onto the destination.
|
2018-07-01 16:05:41 +00:00
|
|
|
Time start_time;
|
2018-07-01 22:28:25 +00:00
|
|
|
for(const auto &event_source: segment_event_sources_) {
|
2018-07-01 16:05:41 +00:00
|
|
|
const PCMSegment &source = event_source.segment();
|
|
|
|
new_track->add_segment(start_time, source, true);
|
|
|
|
start_time += source.length();
|
|
|
|
}
|
2018-07-01 00:03:18 +00:00
|
|
|
|
2018-07-02 02:49:57 +00:00
|
|
|
new_track->is_resampled_clone_ = true;
|
2018-07-01 00:03:18 +00:00
|
|
|
return new_track;
|
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
Track::Event PCMTrack::get_next_event() {
|
2016-12-19 02:37:05 +00:00
|
|
|
// ask the current segment for a new event
|
|
|
|
Track::Event event = segment_event_sources_[segment_pointer_].get_next_event();
|
|
|
|
|
|
|
|
// if it was a flux transition, that's code for end-of-segment, so dig deeper
|
2017-03-26 18:34:47 +00:00
|
|
|
if(event.type == Track::Event::IndexHole) {
|
2016-12-19 02:37:05 +00:00
|
|
|
// multiple segments may be crossed, so start summing lengths in case the net
|
|
|
|
// effect is an index hole
|
|
|
|
Time total_length = event.length;
|
2016-07-31 17:32:30 +00:00
|
|
|
|
2016-12-19 02:37:05 +00:00
|
|
|
// continue until somewhere no returning an index hole
|
2017-03-26 18:34:47 +00:00
|
|
|
while(event.type == Track::Event::IndexHole) {
|
2016-12-19 02:37:05 +00:00
|
|
|
// advance to the [start of] the next segment
|
|
|
|
segment_pointer_ = (segment_pointer_ + 1) % segment_event_sources_.size();
|
|
|
|
segment_event_sources_[segment_pointer_].reset();
|
|
|
|
|
|
|
|
// if this is all the way back to the start, that's a genuine index hole,
|
|
|
|
// so set the summed length and return
|
2017-03-26 18:34:47 +00:00
|
|
|
if(!segment_pointer_) {
|
2016-12-19 02:37:05 +00:00
|
|
|
return event;
|
|
|
|
}
|
|
|
|
|
|
|
|
// otherwise get the next event (if it's not another index hole, the loop will end momentarily),
|
|
|
|
// summing in any prior accumulated time
|
|
|
|
event = segment_event_sources_[segment_pointer_].get_next_event();
|
|
|
|
total_length += event.length;
|
|
|
|
event.length = total_length;
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-12-19 02:37:05 +00:00
|
|
|
return event;
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|
|
|
|
|
2017-03-26 18:34:47 +00:00
|
|
|
Storage::Time PCMTrack::seek_to(const Time &time_since_index_hole) {
|
2016-12-19 02:37:05 +00:00
|
|
|
// initial condition: no time yet accumulated, the whole thing requested yet to navigate
|
|
|
|
Storage::Time accumulated_time;
|
|
|
|
Storage::Time time_left_to_seek = time_since_index_hole;
|
2016-08-03 10:59:45 +00:00
|
|
|
|
2016-12-19 02:37:05 +00:00
|
|
|
// search from the first segment
|
|
|
|
segment_pointer_ = 0;
|
2017-03-26 18:34:47 +00:00
|
|
|
do {
|
2016-12-19 02:37:05 +00:00
|
|
|
// if this segment extends beyond the amount of time left to seek, trust it to complete
|
|
|
|
// the seek
|
|
|
|
Storage::Time segment_time = segment_event_sources_[segment_pointer_].get_length();
|
2017-03-26 18:34:47 +00:00
|
|
|
if(segment_time > time_left_to_seek) {
|
2016-12-19 02:37:05 +00:00
|
|
|
return accumulated_time + segment_event_sources_[segment_pointer_].seek_to(time_left_to_seek);
|
2016-08-03 10:59:45 +00:00
|
|
|
}
|
|
|
|
|
2016-12-19 02:37:05 +00:00
|
|
|
// otherwise swallow this segment, updating the time left to seek and time so far accumulated
|
|
|
|
time_left_to_seek -= segment_time;
|
|
|
|
accumulated_time += segment_time;
|
|
|
|
segment_pointer_ = (segment_pointer_ + 1) % segment_event_sources_.size();
|
2017-03-26 18:34:47 +00:00
|
|
|
} while(segment_pointer_);
|
2016-07-10 22:36:52 +00:00
|
|
|
|
2016-12-19 02:37:05 +00:00
|
|
|
// if all segments have now been swallowed, the closest we can get is the very end of
|
|
|
|
// the list of segments
|
|
|
|
return accumulated_time;
|
2016-07-10 22:36:52 +00:00
|
|
|
}
|
2018-07-01 00:03:18 +00:00
|
|
|
|
2018-07-01 16:05:41 +00:00
|
|
|
void PCMTrack::add_segment(const Time &start_time, const PCMSegment &segment, bool clamp_to_index_hole) {
|
2018-07-01 22:28:25 +00:00
|
|
|
// Get a reference to the destination.
|
|
|
|
PCMSegment &destination = segment_event_sources_.front().segment();
|
|
|
|
|
|
|
|
// Determine the range to fill on the target segment.
|
|
|
|
const Time end_time = start_time + segment.length();
|
|
|
|
const size_t start_bit = start_time.length * destination.data.size() / start_time.clock_rate;
|
|
|
|
const size_t end_bit = end_time.length * destination.data.size() / end_time.clock_rate;
|
2018-07-02 02:49:57 +00:00
|
|
|
const size_t target_width = end_bit - start_bit;
|
2018-07-02 23:35:49 +00:00
|
|
|
const size_t half_offset = target_width / (2 * segment.data.size());
|
2018-07-02 02:49:57 +00:00
|
|
|
|
2018-07-02 23:35:49 +00:00
|
|
|
if(clamp_to_index_hole || end_bit <= destination.data.size()) {
|
|
|
|
// If clamping is applied, just write a single segment, from the start_bit to whichever is
|
|
|
|
// closer of the end of track and the end_bit.
|
|
|
|
const size_t selected_end_bit = std::min(end_bit, destination.data.size());
|
2018-07-01 22:28:25 +00:00
|
|
|
|
2018-07-02 23:35:49 +00:00
|
|
|
// Reset the destination.
|
2019-07-16 22:05:58 +00:00
|
|
|
std::fill(destination.data.begin() + off_t(start_bit), destination.data.begin() + off_t(selected_end_bit), false);
|
2018-07-01 22:28:25 +00:00
|
|
|
|
2018-07-02 23:35:49 +00:00
|
|
|
// Step through the source data from start to finish, stopping early if it goes out of bounds.
|
|
|
|
for(size_t bit = 0; bit < segment.data.size(); ++bit) {
|
|
|
|
if(segment.data[bit]) {
|
|
|
|
const size_t output_bit = start_bit + half_offset + (bit * target_width) / segment.data.size();
|
|
|
|
if(output_bit >= destination.data.size()) return;
|
|
|
|
destination.data[output_bit] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Clamping is not enabled, so the supplied segment loops over the index hole, arbitrarily many times.
|
|
|
|
// So work backwards unless or until the original start position is reached, then stop.
|
|
|
|
|
|
|
|
// This definitely runs over the index hole; check whether the whole track needs clearing, or whether
|
|
|
|
// a centre segment is untouched.
|
|
|
|
if(target_width >= destination.data.size()) {
|
|
|
|
std::fill(destination.data.begin(), destination.data.end(), false);
|
|
|
|
} else {
|
2019-07-16 22:05:58 +00:00
|
|
|
std::fill(destination.data.begin(), destination.data.begin() + off_t(end_bit % destination.data.size()), false);
|
|
|
|
std::fill(destination.data.begin() + off_t(start_bit), destination.data.end(), false);
|
2018-07-02 23:35:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Run backwards from final bit back to first, stopping early if overlapping the beginning.
|
2019-07-16 22:05:58 +00:00
|
|
|
for(off_t bit = off_t(segment.data.size()-1); bit >= 0; --bit) {
|
|
|
|
// Store flux transitions only; non-transitions can be ignored.
|
|
|
|
if(segment.data[size_t(bit)]) {
|
|
|
|
// Map to the proper output destination; stop if now potentially overwriting where we began.
|
|
|
|
const size_t output_bit = start_bit + half_offset + (size_t(bit) * target_width) / segment.data.size();
|
|
|
|
if(output_bit < end_bit - destination.data.size()) return;
|
|
|
|
|
|
|
|
// Store.
|
2018-07-02 23:35:49 +00:00
|
|
|
destination.data[output_bit % destination.data.size()] = true;
|
|
|
|
}
|
2018-07-01 22:28:25 +00:00
|
|
|
}
|
|
|
|
}
|
2018-07-01 16:05:41 +00:00
|
|
|
}
|