Ξ-W2-T1: the resample bake chain — instrument renders, extension banks, one click re-points and resets
This commit is contained in:
@@ -0,0 +1,53 @@
|
||||
// See bake_plan.h.
|
||||
|
||||
#include "core/instrument/bake/bake_plan.h"
|
||||
|
||||
#include <algorithm>
|
||||
#include <cmath>
|
||||
|
||||
namespace reasampler::instrument::bake {
|
||||
|
||||
using note::NoteProgram;
|
||||
using note::ResolvedNote;
|
||||
|
||||
namespace {
|
||||
|
||||
// Seconds -> frames by round-half-away-from-zero, the one conversion every field here
|
||||
// uses, so the window and its two event frames cannot round against each other.
|
||||
std::int64_t toFrames(double seconds, int rate) {
|
||||
return static_cast<std::int64_t>(std::llround(seconds * static_cast<double>(rate)));
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
NoteProgram defaultBakeProgram() {
|
||||
NoteProgram p;
|
||||
p.end = note::EndOffset(note::offsetFromMs(kDefaultReleaseTailMs));
|
||||
return p;
|
||||
}
|
||||
|
||||
std::optional<BakePlan> planBake(const ResolvedNote& resolved, int sampleRate,
|
||||
int rootNote) {
|
||||
if (resolved.windowCollapsed) return std::nullopt;
|
||||
if (sampleRate <= 0) return std::nullopt;
|
||||
|
||||
BakePlan plan;
|
||||
plan.sampleRate = sampleRate;
|
||||
plan.totalFrames = toFrames(resolved.captureLengthSeconds(), sampleRate);
|
||||
if (plan.totalFrames <= 0) return std::nullopt;
|
||||
|
||||
// Note-on sits at -captureStart into the window: a negative start offset (the capture
|
||||
// opens early) pushes it later, a positive one has already been clamped away by
|
||||
// resolveNote's own window.
|
||||
plan.noteOnFrame = std::clamp(toFrames(-resolved.captureStartSeconds, sampleRate),
|
||||
std::int64_t{0}, plan.totalFrames);
|
||||
plan.noteOffFrame =
|
||||
std::clamp(toFrames(resolved.noteOffSeconds - resolved.captureStartSeconds,
|
||||
sampleRate),
|
||||
plan.noteOnFrame, plan.totalFrames);
|
||||
plan.note = std::clamp(rootNote, 0, 127);
|
||||
plan.velocity = std::clamp(static_cast<int>(resolved.velocity), 1, 127);
|
||||
return plan;
|
||||
}
|
||||
|
||||
} // namespace reasampler::instrument::bake
|
||||
Reference in New Issue
Block a user