Class thl::dsp::pitch::PitchBank#

class PitchBank#

Builds a bank of pitch-shifted copies of a sample, one slot per semitone, for players that switch pitch by switching source.

Slot index_of(s) holds the root transposed by s semitones over [min_semitones, max_semitones]; the root sits at root_index(). Every copy keeps the root’s length and timing (a pitch shift, not a resample), so the slots are equal-length and time-aligned, which is what sampler::SamplePlayer and granular::GrainEngine require to switch between them mid-play.

Shifting uses Signalsmith Stretch (cheaper preset, tonality limit m_tonality_limit_hz) with short linear fades at both edges, spread across worker threads.

Real-Time Safety

Not real-time safe: allocates, spawns threads and takes seconds for long samples. Build on a loader thread and publish the finished bank.

Public Functions

PitchBank() = default#
inline explicit PitchBank(const Settings &settings)#
inline const Settings &settings() const#
inline size_t num_slots() const#

Slots in a full bank (max - min + 1).

inline size_t index_of(int semitones) const#

Slot of a transposition (caller keeps it in range).

inline int semitones_of(size_t index) const#

Transposition of a slot.

inline size_t root_index() const#
bool build(std::vector<core::BufferF> &bank, std::span<const int> semitones, double sample_rate) const#

Fill the selected slots of bank from its root.

bank must hold num_slots() buffers with the root at root_index(). Each selected transposition (out-of-range, duplicate and root entries are skipped) gets a buffer of the root’s shape; other slots are left as they are.

Parameters:

sample_rate – The root’s sample rate (the shifter’s analysis depends on it).

Returns:

false, building nothing, when sample_rate is not positive.

bool shift(core::BufferF &output, const core::BufferF &input, float semitones, double sample_rate) const#

Shift input by semitones into output (same shape, pre-allocated).

Returns:

false, writing nothing, when sample_rate is not positive.

struct Settings#

Public Members

int m_min_semitones = {-24}#
int m_max_semitones = {24}#
unsigned m_max_threads = {0}#

Worker threads; 0 = half the hardware threads (at least one).

float m_tonality_limit_hz = {8000.0f}#
size_t m_edge_fade_frames = {64}#

Linear fade at both edges of every shifted copy.

bool m_copy_only = {false}#

Copy the root into the selected slots instead of shifting (fast, for debugging and tests).