2016-12-18 02:13:57 +00:00
|
|
|
//
|
|
|
|
// PCMSegment.hpp
|
|
|
|
// Clock Signal
|
|
|
|
//
|
|
|
|
// Created by Thomas Harte on 17/12/2016.
|
2018-05-13 19:19:52 +00:00
|
|
|
// Copyright 2016 Thomas Harte. All rights reserved.
|
2016-12-18 02:13:57 +00:00
|
|
|
//
|
|
|
|
|
|
|
|
#ifndef PCMSegment_hpp
|
|
|
|
#define PCMSegment_hpp
|
|
|
|
|
|
|
|
#include <cstdint>
|
2017-11-10 03:04:49 +00:00
|
|
|
#include <memory>
|
2016-12-18 02:13:57 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2017-09-23 02:39:23 +00:00
|
|
|
#include "../../Storage.hpp"
|
2020-01-20 04:14:35 +00:00
|
|
|
#include "../../../Numeric/LFSR.hpp"
|
2017-09-23 02:39:23 +00:00
|
|
|
#include "Track.hpp"
|
2016-12-18 02:13:57 +00:00
|
|
|
|
|
|
|
namespace Storage {
|
|
|
|
namespace Disk {
|
|
|
|
|
|
|
|
/*!
|
|
|
|
A segment of PCM-sampled data.
|
|
|
|
*/
|
|
|
|
struct PCMSegment {
|
2018-07-01 16:05:41 +00:00
|
|
|
/*!
|
|
|
|
Determines the amount of space that each bit of data occupies;
|
|
|
|
allows PCMSegments of different densities.
|
|
|
|
*/
|
2018-04-27 02:49:07 +00:00
|
|
|
Time length_of_a_bit = Time(1);
|
2016-12-22 03:17:00 +00:00
|
|
|
|
2018-07-01 16:05:41 +00:00
|
|
|
/*!
|
|
|
|
This is the actual data, taking advantage of the std::vector<bool>
|
|
|
|
specialisation to use whatever one-bit-per-value encoding is
|
|
|
|
most suited to this architecture.
|
|
|
|
|
|
|
|
If a value is @c true then a flux transition occurs in that window.
|
|
|
|
If it is @c false then no flux transition occurs.
|
|
|
|
*/
|
|
|
|
std::vector<bool> data;
|
|
|
|
|
2020-01-20 01:09:11 +00:00
|
|
|
/*!
|
|
|
|
If a segment has a fuzzy mask then anywhere the mask has a value
|
|
|
|
of @c true, a random bit will be ORd onto whatever is in the
|
|
|
|
corresponding slot in @c data.
|
|
|
|
*/
|
|
|
|
std::vector<bool> fuzzy_mask;
|
|
|
|
|
2018-07-01 16:05:41 +00:00
|
|
|
/*!
|
|
|
|
Constructs an instance of PCMSegment with the specified @c length_of_a_bit
|
|
|
|
and @c data.
|
|
|
|
*/
|
|
|
|
PCMSegment(Time length_of_a_bit, const std::vector<bool> &data)
|
|
|
|
: length_of_a_bit(length_of_a_bit), data(data) {}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs an instance of PCMSegment where each bit window is 1 unit of time
|
|
|
|
long and @c data is populated from the supplied @c source by serialising it
|
|
|
|
from MSB to LSB for @c number_of_bits.
|
|
|
|
*/
|
|
|
|
PCMSegment(size_t number_of_bits, const uint8_t *source)
|
|
|
|
: data(number_of_bits, false) {
|
|
|
|
for(size_t c = 0; c < number_of_bits; ++c) {
|
|
|
|
if((source[c >> 3] >> (7 ^ (c & 7)))&1) {
|
|
|
|
data[c] = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-25 02:41:16 +00:00
|
|
|
|
2018-07-01 16:05:41 +00:00
|
|
|
/*!
|
|
|
|
Constructs an instance of PCMSegment where each bit window is the length
|
|
|
|
specified by @c length_of_a_bit, and @c data is populated from the supplied
|
|
|
|
@c source by serialising it from MSB to LSB for @c number_of_bits.
|
|
|
|
*/
|
|
|
|
PCMSegment(Time length_of_a_bit, size_t number_of_bits, const uint8_t *source)
|
|
|
|
: PCMSegment(number_of_bits, source) {
|
|
|
|
this->length_of_a_bit = length_of_a_bit;
|
2017-09-25 02:41:16 +00:00
|
|
|
}
|
2018-01-10 03:12:34 +00:00
|
|
|
|
2018-07-01 16:05:41 +00:00
|
|
|
/*!
|
|
|
|
Constructs an instance of PCMSegment where each bit window is the length
|
|
|
|
specified by @c length_of_a_bit, and @c data is populated from the supplied
|
|
|
|
@c source by serialising it from MSB to LSB for @c number_of_bits.
|
|
|
|
*/
|
|
|
|
PCMSegment(Time length_of_a_bit, size_t number_of_bits, const std::vector<uint8_t> &source) :
|
|
|
|
PCMSegment(length_of_a_bit, number_of_bits, source.data()) {}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs an instance of PCMSegment where each bit window is 1 unit of time
|
|
|
|
long and @c data is populated from the supplied @c source by serialising it
|
|
|
|
from MSB to LSB for @c number_of_bits.
|
|
|
|
*/
|
|
|
|
PCMSegment(size_t number_of_bits, const std::vector<uint8_t> &source) :
|
|
|
|
PCMSegment(number_of_bits, source.data()) {}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs an instance of PCMSegment where each bit window is 1 unit of time
|
|
|
|
long and @c data is populated from the supplied @c source by serialising it
|
|
|
|
from MSB to LSB, assuming every bit provided is used.
|
|
|
|
*/
|
|
|
|
PCMSegment(const std::vector<uint8_t> &source) :
|
|
|
|
PCMSegment(source.size() * 8, source.data()) {}
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Constructs an instance of PCMSegment where each bit window is 1 unit of time
|
|
|
|
long and @c data is empty.
|
|
|
|
*/
|
|
|
|
PCMSegment() {}
|
|
|
|
|
|
|
|
/// Empties the PCMSegment.
|
2018-01-10 03:12:34 +00:00
|
|
|
void clear() {
|
|
|
|
data.clear();
|
|
|
|
}
|
2018-05-02 00:31:42 +00:00
|
|
|
|
2018-08-28 00:56:25 +00:00
|
|
|
/*!
|
|
|
|
Rotates all bits in this segment by @c length bits.
|
|
|
|
|
|
|
|
@c length is signed; to rotate left provide a negative number.
|
|
|
|
*/
|
|
|
|
void rotate_right(size_t length);
|
|
|
|
|
2018-07-01 16:05:41 +00:00
|
|
|
/*!
|
|
|
|
Produces a byte buffer where the contents of @c data are serialised into bytes
|
|
|
|
|
|
|
|
If @c msb_first is @c true then each byte is expected to be deserialised from
|
|
|
|
MSB to LSB.
|
|
|
|
|
|
|
|
If @c msb_first is @c false then each byte is expected to be deserialised from
|
|
|
|
LSB to MSB.
|
|
|
|
*/
|
|
|
|
std::vector<uint8_t> byte_data(bool msb_first = true) const {
|
|
|
|
std::vector<uint8_t> bytes((data.size() + 7) >> 3);
|
|
|
|
size_t pointer = 0;
|
|
|
|
const size_t pointer_mask = msb_first ? 7 : 0;
|
|
|
|
for(const auto bit: data) {
|
|
|
|
if(bit) bytes[pointer >> 3] |= 1 << ((pointer & 7) ^ pointer_mask);
|
|
|
|
++pointer;
|
|
|
|
}
|
|
|
|
return bytes;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// Appends the data of @c rhs to the current data. Does not adjust @c length_of_a_bit.
|
2018-05-02 00:31:42 +00:00
|
|
|
PCMSegment &operator +=(const PCMSegment &rhs);
|
2018-07-01 16:05:41 +00:00
|
|
|
|
|
|
|
/// @returns the total amount of time occupied by all the data stored in this segment.
|
|
|
|
Time length() const {
|
2020-05-10 03:00:39 +00:00
|
|
|
return length_of_a_bit * unsigned(data.size());
|
2018-07-01 16:05:41 +00:00
|
|
|
}
|
2016-12-18 02:13:57 +00:00
|
|
|
};
|
|
|
|
|
2016-12-19 03:53:24 +00:00
|
|
|
/*!
|
|
|
|
Provides a stream of events by inspecting a PCMSegment.
|
|
|
|
*/
|
2016-12-18 02:13:57 +00:00
|
|
|
class PCMSegmentEventSource {
|
|
|
|
public:
|
2016-12-19 03:53:24 +00:00
|
|
|
/*!
|
|
|
|
Constructs a @c PCMSegmentEventSource that will derive events from @c segment.
|
|
|
|
The event source is initially @c reset.
|
|
|
|
*/
|
2016-12-30 19:23:26 +00:00
|
|
|
PCMSegmentEventSource(const PCMSegment &);
|
|
|
|
|
|
|
|
/*!
|
|
|
|
Copy constructor; produces a segment event source with the same underlying segment
|
|
|
|
but a unique pointer into it.
|
|
|
|
*/
|
|
|
|
PCMSegmentEventSource(const PCMSegmentEventSource &);
|
2016-12-18 02:13:57 +00:00
|
|
|
|
2016-12-19 03:53:24 +00:00
|
|
|
/*!
|
|
|
|
@returns the next event that will occur in this event stream.
|
|
|
|
*/
|
2016-12-18 02:13:57 +00:00
|
|
|
Track::Event get_next_event();
|
2016-12-19 03:53:24 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
Resets the event source to the beginning of its event stream, exactly as if
|
|
|
|
it has just been constructed.
|
|
|
|
*/
|
2016-12-18 02:13:57 +00:00
|
|
|
void reset();
|
|
|
|
|
2016-12-19 03:53:24 +00:00
|
|
|
/*!
|
|
|
|
Seeks as close to @c time_from_start as the event source can manage while not
|
|
|
|
exceeding it.
|
|
|
|
|
|
|
|
@returns the time the source is now at.
|
|
|
|
*/
|
2020-07-18 02:08:58 +00:00
|
|
|
float seek_to(float time_from_start);
|
2016-12-19 03:53:24 +00:00
|
|
|
|
|
|
|
/*!
|
|
|
|
@returns the total length of the stream of data that the source will provide.
|
|
|
|
*/
|
2016-12-18 03:44:33 +00:00
|
|
|
Time get_length();
|
|
|
|
|
2018-07-01 16:05:41 +00:00
|
|
|
/*!
|
|
|
|
@returns a reference to the underlying segment.
|
|
|
|
*/
|
|
|
|
const PCMSegment &segment() const;
|
2018-07-01 22:28:25 +00:00
|
|
|
PCMSegment &segment();
|
2018-07-01 16:05:41 +00:00
|
|
|
|
2016-12-18 02:13:57 +00:00
|
|
|
private:
|
2016-12-30 19:23:26 +00:00
|
|
|
std::shared_ptr<PCMSegment> segment_;
|
2017-11-11 20:28:40 +00:00
|
|
|
std::size_t bit_pointer_;
|
2016-12-18 02:13:57 +00:00
|
|
|
Track::Event next_event_;
|
2020-01-20 04:14:35 +00:00
|
|
|
Numeric::LFSR<uint64_t> lfsr_;
|
2016-12-18 02:13:57 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* PCMSegment_hpp */
|