DSPark 1.9.0
Header-only C++20 DSP for real-time and offline audio
Loading...
Searching...
No Matches
OfflineSinc.h
1// DSPark - Professional Audio DSP Framework
2// Copyright (c) 2026 Cristian Moresi - MIT License
3
4#pragma once
5
12#include "OfflineProduct.h"
13#if DSPARK_HAS_OFFLINE
14#include <algorithm>
15#include <array>
16#include <cmath>
17#include <cstdint>
18#include <memory>
19#include <numbers>
20#include <optional>
21#include <span>
22#include <utility>
23#include <vector>
24
25// Finite cardinal-sinc phase, using the existing
26// Core finite convolution window and the same source's shared Cauchy moments.
28{
30{
32 double shift_, sine_ = 0;
33 std::optional<OfflineConvolutionWindow> near_;
34
35 public:
37 : work_(work), shift_(checkedShift(shift)), sine_(sineOf(shift_)), near_(makeNear(work, *this))
38 {
39 }
40 SincPhase(const SincPhase &) = delete;
41 SincPhase &operator=(const SincPhase &) = delete;
42 template <class Reader>
43 void evaluate(const OfflineHilbertMap &map, std::size_t leaf, Reader &source, double *out)
44 {
45 const int b = work_.block();
46 if (leaf >= work_.leaves())
48 const auto first = static_cast<std::int64_t>(leaf) * b;
49 if (shift_ == 0)
50 {
51 work_.read(source, first, b, out);
52 return;
53 }
54 map.evaluateCauchyFar(leaf, shift_, out, true);
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];
59 }
60
61 private:
62 static double checkedShift(double shift)
63 {
64 if (!std::isfinite(shift) || std::abs(shift) >= 1)
66 return shift;
67 }
68 // Preserve a shift one ulp from an integer: multiplying that shift by pi
69 // first loses relative accuracy at the nearest sinc numerator zero.
70 static double sineOf(double shift)
71 {
72 if (shift == 0)
73 return 0;
74 const double reduced = shift > .5 ? 1 - shift : shift < -.5 ? -1 - shift : shift;
75 return std::sin(std::numbers::pi * reduced);
76 }
77 // Built in place in the member initializer (the window is not movable).
78 static std::optional<OfflineConvolutionWindow> makeNear(OfflineProductWorkspace &work, const SincPhase &self)
79 {
80 if (self.shift_ == 0)
81 return std::nullopt;
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_)); });
85 }
86};
87
88// Project a finite high-rate sequence onto |f|<=1/(2*factor) and sample at
89// multiples of factor. h[k]=sin(pi*k/factor)/(pi*k) gives a single real
90// Cauchy transform of sin(pi*j/factor)*source[j], plus its diagonal term.
91template <class Reader> class SincProjection
92{
93 Reader source_;
95 int factor_;
96 std::array<double, 32> sine_{};
97 struct Modulated
98 {
99 SincProjection *owner;
100 void operator()(std::int64_t first, int count, double *out)
101 {
102 owner->source_(first, count, out);
103 for (int i = 0; i < count; ++i)
104 out[i] *=
105 owner->sine_[static_cast<std::size_t>((first + i) % (2 * owner->factor_))];
106 }
107 } modulated_{this};
108 std::optional<OfflineHilbertMap> map_;
109 OfflineScratchArray<double> transform_, raw_, output_;
110
111 public:
112 SincProjection(OfflineSession &job, std::int64_t frames, int factor, Reader source, int block)
113 : source_(std::move(source)),
114 work_(job, frames, block, OfflineProductWorkspace::NearFields::External), factor_(factor)
115 {
116 if (factor < 1 || factor > 16 || (factor & (factor - 1)))
118 for (int p = 1; p < factor; ++p)
119 {
120 sine_[p] = std::sin(std::numbers::pi * p / factor);
121 sine_[p + factor] = -sine_[p];
122 }
123 map_.emplace(work_, modulated_);
124 transform_ = job.allocateScratch<double>(block);
125 raw_ = job.allocateScratch<double>(block);
126 output_ = job.allocateScratch<double>(block / factor);
127 work_.prepareCauchy();
128 }
131 void rebuild()
132 {
133 map_->rebuild(work_, modulated_);
134 }
135 [[nodiscard]] std::size_t leaves() const
136 {
137 return work_.leaves();
138 }
139 [[nodiscard]] std::span<const double> get(std::size_t leaf)
140 {
141 if (leaf >= leaves())
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)
150 {
151 const auto frame = first / factor_ + k;
152 output_[k] =
153 raw_[k * factor_] / factor_ + (frame % 2 ? 1 : -1) * transform_[k * factor_];
154 }
155 return {output_.get(), static_cast<std::size_t>(written)};
156 }
157};
158template <class Reader, class Consumer>
159void projectSinc(OfflineSession &job, std::int64_t frames, int factor, Reader source,
160 Consumer consume, int block = 4096)
161{
162 SincProjection<Reader> projection(job, frames, factor, std::move(source), block);
163 for (std::size_t leaf = 0; leaf < projection.leaves(); ++leaf)
164 {
165 const auto data = projection.get(leaf);
166 consume(static_cast<std::int64_t>(leaf) * (block / factor), static_cast<int>(data.size()),
167 data.data());
168 }
169}
170} // namespace dspark::detail::offline_sinc
171
172#endif // DSPARK_HAS_OFFLINE
void evaluateCauchyFar(std::size_t leaf, double shift, double *output, bool alternating=false) const
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)
Definition OfflineSinc.h:43
SincPhase(OfflineProductWorkspace &work, double shift)
Definition OfflineSinc.h:36
SincPhase(const SincPhase &)=delete
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