12#include "OfflineProduct.h"
32 double shift_, sine_ = 0;
33 std::optional<OfflineConvolutionWindow> near_;
37 : work_(work), shift_(checkedShift(shift)), sine_(sineOf(shift_)), near_(makeNear(work, *this))
42 template <
class Reader>
45 const int b = work_.
block();
46 if (leaf >= work_.
leaves())
48 const auto first =
static_cast<std::int64_t
>(leaf) * b;
51 work_.
read(source, first, b, out);
55 work_.
read(source, first - b, 3 * b, near_->input());
56 const auto *close = near_->process();
57 for (
int i = 0; i < b; ++i)
58 out[i] = close[i] + (i % 2 ? -sine_ : sine_) * out[i];
62 static double checkedShift(
double shift)
64 if (!std::isfinite(shift) || std::abs(shift) >= 1)
70 static double sineOf(
double shift)
74 const double reduced = shift > .5 ? 1 - shift : shift < -.5 ? -1 - shift : shift;
75 return std::sin(std::numbers::pi * reduced);
78 static std::optional<OfflineConvolutionWindow> makeNear(OfflineProductWorkspace &work,
const SincPhase &self)
82 return std::optional<OfflineConvolutionWindow>(
83 std::in_place, work.job(), work.block(), [&self](
int lag)
84 { return (lag % 2 ? -self.sine_ : self.sine_) / (std::numbers::pi * (lag + self.shift_)); });
96 std::array<double, 32> sine_{};
100 void operator()(std::int64_t first,
int count,
double *out)
102 owner->source_(first, count, out);
103 for (
int i = 0; i < count; ++i)
105 owner->sine_[
static_cast<std::size_t
>((first + i) % (2 * owner->factor_))];
108 std::optional<OfflineHilbertMap> map_;
113 : source_(std::move(source)),
116 if (factor < 1 || factor > 16 || (factor & (factor - 1)))
118 for (
int p = 1; p < factor; ++p)
120 sine_[p] = std::sin(std::numbers::pi * p / factor);
121 sine_[p + factor] = -sine_[p];
123 map_.emplace(work_, modulated_);
133 map_->rebuild(work_, modulated_);
139 [[nodiscard]] std::span<const double>
get(std::size_t leaf)
143 const int block = work_.
block();
144 const auto first =
static_cast<std::int64_t
>(leaf) * block;
145 const int count =
static_cast<int>(std::min<std::int64_t>(block, work_.
frames() - first));
146 map_->evaluateCauchy(work_, leaf, modulated_, transform_.get());
147 work_.
read(source_, first, count, raw_.get());
148 const int written = (count + factor_ - 1) / factor_;
149 for (
int k = 0; k < written; ++k)
151 const auto frame = first / factor_ + k;
153 raw_[k * factor_] / factor_ + (frame % 2 ? 1 : -1) * transform_[k * factor_];
155 return {output_.get(),
static_cast<std::size_t
>(written)};
158template <
class Reader,
class Consumer>
160 Consumer consume,
int block = 4096)
163 for (std::size_t leaf = 0; leaf < projection.leaves(); ++leaf)
165 const auto data = projection.get(leaf);
166 consume(
static_cast<std::int64_t
>(leaf) * (block / factor),
static_cast<int>(data.size()),
void evaluateCauchyFar(std::size_t leaf, double shift, double *output, bool alternating=false) const
int block() const noexcept
std::int64_t frames() const noexcept
std::size_t leaves() const noexcept
void read(Reader &source, std::int64_t first, int count, double *out) const
OfflineScratchArray< U > allocateScratch(std::uint64_t count)
SincPhase & operator=(const SincPhase &)=delete
void evaluate(const OfflineHilbertMap &map, std::size_t leaf, Reader &source, double *out)
SincPhase(OfflineProductWorkspace &work, double shift)
SincPhase(const SincPhase &)=delete
std::size_t leaves() const
SincProjection(OfflineSession &job, std::int64_t frames, int factor, Reader source, int block)
SincProjection & operator=(const SincProjection &)=delete
SincProjection(const SincProjection &)=delete
std::span< const double > get(std::size_t leaf)
void projectSinc(OfflineSession &job, std::int64_t frames, int factor, Reader source, Consumer consume, int block=4096)
void offlineFail(OfflineStatus status)
std::unique_ptr< T[], OfflineScratchDeleter< T > > OfflineScratchArray