0.40.0 (75): Sampler One-shot (Android and iOS)

- Sampler parameter oneShot (Off/On): note-offs don't release its pads; they
  play their whole slice. Transport stop/pause still releases (SampleVoice::
  release), panic kills
- Engine in core/ and the Kotlin reference both implement it; the golden
  Sampler "high" scenario covers it; oneshot_test checks MIDI, song, stop, panic
- ONE-SHOT toggle in both apps' Sampler editors; specs and layouts regenerated

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Reactorcoremeltdown
2026-11-16 16:53:13 +01:00
co-authored by Claude Opus 5.5
parent 5240019b47
commit 7041f762e5
17 changed files with 182 additions and 16 deletions
+3 -1
View File
@@ -64,8 +64,10 @@ inline const ParamSpec NES_SYNTH_params[] = {
{"m3amt", "Mod4 Amt", 0.0f, -1.0f, 1.0f, false, nullptr},
};
inline const char *const SAMPLER_c1[] = {"Off", "On", nullptr};
inline const ParamSpec SAMPLER_params[] = {
{"volume", "Volume", 0.8f, 0.0f, 1.0f, false, nullptr},
{"oneShot", "One-shot", 0.0f, 0.0f, 1.0f, false, SAMPLER_c1},
};
inline const char *const SOUNDFONT_c6[] = {"Off", "On", nullptr};
@@ -309,7 +311,7 @@ inline const ParamSpec GRANULAR_params[] = {
inline const DeviceSpec devices[] = {
{"NES_SYNTH", "Synth", Kind::Instrument, NES_SYNTH_params, 43},
{"SAMPLER", "Sampler", Kind::Instrument, SAMPLER_params, 1},
{"SAMPLER", "Sampler", Kind::Instrument, SAMPLER_params, 2},
{"SOUNDFONT", "SF2 / XI Loader", Kind::Instrument, SOUNDFONT_params, 8},
{"MORPH_PAD", "Morph Pad", Kind::Instrument, MORPH_PAD_params, 36},
{"GRAIN_CLOUD", "Grain Cloud", Kind::Instrument, GRAIN_CLOUD_params, 24},
+4 -1
View File
@@ -855,6 +855,7 @@ void Engine::Impl::playNote(Project &proj, int bus, int note, int velocity, int
v.ownerTrack = ownerTrack;
v.noteOn(*vs.sample, note, velocity, vs.root, vs.sliceStart, vs.sliceEnd, vs.volume,
vs.attack, vs.decay, vs.sustain, vs.release, vs.loop, vs.crossfadeMs);
v.oneShot = vs.oneShot;
} else if (!hasType(slot) || is(slot, "SAMPLER")) {
// Empty bus or a Sampler with an empty pad: silent.
} else if (is(slot, "GRAIN_CLOUD")) {
@@ -926,6 +927,7 @@ void Engine::Impl::auditionSlotOn(int slotIndex, int midi) {
if (!v) { v = auditionSampleVoices[0].get(); v->kill(); }
v->noteOn(*vs.sample, midi, 110, vs.root, vs.sliceStart, vs.sliceEnd, vs.volume,
vs.attack, vs.decay, vs.sustain, vs.release, vs.loop, vs.crossfadeMs);
v->oneShot = vs.oneShot;
return;
}
if (is(slot, "SAMPLER")) return;
@@ -972,6 +974,7 @@ void Engine::Impl::busNoteOn(int midiChannel, int midi, int velocity) {
if (!v) { v = busSampleVoices[base].get(); v->kill(); }
v->noteOn(*vs.sample, midi, velocity, vs.root, vs.sliceStart, vs.sliceEnd, vs.volume,
vs.attack, vs.decay, vs.sustain, vs.release, vs.loop, vs.crossfadeMs);
v->oneShot = vs.oneShot;
} else if (is(slot, "SAMPLER")) {
} else if (is(slot, "GRAIN_CLOUD")) {
clouds[ch]->noteOn(midi, velocity, GrainCloud::OWNER_LIVE);
@@ -1146,7 +1149,7 @@ void Engine::Impl::refreshSynthPatches(Project &proj) {
void Engine::Impl::allSeqVoicesOff() {
for (auto &c : clouds) c->releaseSequenced();
for (auto &v : seqVoices) v->noteOff();
for (auto &v : sampleVoices) v->noteOff();
for (auto &v : sampleVoices) v->release(); // a stopped song stops One-shot pads too
for (auto &a : arps) a.stop();
allMidiNotesOff();
}
+3 -1
View File
@@ -50,6 +50,7 @@ bool voiceSample(const char *type, const Params &slot, const SampleStore &store,
out.sliceStart = slot.f(SamplerPads::sliceStartKey(pad).s, 0.f);
out.sliceEnd = slot.f(SamplerPads::sliceEndKey(pad).s, 1.f);
out.volume = slot.f(SamplerPads::volumeKey(pad).s, SamplerPads::DEFAULT_VOLUME);
out.oneShot = std::strcmp(slot.s("oneShot", "Off"), "On") == 0;
return true;
}
if (std::strcmp(type, "SOUNDFONT") == 0) {
@@ -93,6 +94,7 @@ void SampleVoice::noteOn(const Sample &s, int midi, int velocity, int root,
const int len = (int)s.data.size();
// An empty sample would make Kotlin's coerceIn(startIndex + 1, 0) throw; play nothing.
if (len <= 0) return;
oneShot = false; // the caller sets it again for a One-shot Sampler
// Carry the level we're interrupting so the new note starts continuously.
declick += lastY;
setSample(&s);
@@ -142,7 +144,7 @@ void SampleVoice::noteOn(const Sample &s, int midi, int velocity, int root,
endGain = 1.f;
}
void SampleVoice::noteOff() {
void SampleVoice::release() {
const float sr = (float)engineSampleRate;
releaseInc = releaseSeconds <= 0.f ? 1.f : env / (releaseSeconds * sr);
if (releaseInc <= 0.f) releaseInc = 1.f;
+14 -4
View File
@@ -54,6 +54,8 @@ struct VoiceSample {
float sustain = 1.f, release = 0.02f;
bool loop = false;
float crossfadeMs = 0.f;
/** A Sampler with One-shot on: note-offs don't release the voice. */
bool oneShot = false;
};
/**
@@ -83,6 +85,9 @@ public:
int pitch = -1;
/** Which tracker (lane,track) started this voice, or -1 if unowned. */
int ownerTrack = -1;
/** Playing a One-shot Sampler pad: noteOff() leaves it be, it plays to its slice
* end; release() (transport stop / pause) still fades it. Reset by noteOn. */
bool oneShot = false;
bool isActive() const { return sample != nullptr; }
/** Note is still held (not in its release stage). */
@@ -98,11 +103,16 @@ public:
float attack = 0.002f, float decay = 0.f, float sustain = 1.f, float release = 0.02f,
bool loop = false, float crossfadeMs = 0.f);
void noteOn(const VoiceSample &v, int midi, int velocity) {
if (v.sample) noteOn(*v.sample, midi, velocity, v.root, v.sliceStart, v.sliceEnd, v.volume,
v.attack, v.decay, v.sustain, v.release, v.loop, v.crossfadeMs);
if (!v.sample) return;
noteOn(*v.sample, midi, velocity, v.root, v.sliceStart, v.sliceEnd, v.volume,
v.attack, v.decay, v.sustain, v.release, v.loop, v.crossfadeMs);
oneShot = v.oneShot;
}
/** Enter the release stage: fade from the current level to 0 over the release time. */
void noteOff();
/** A note-off: enter the release stage (fade from the current level to 0 over
* the release time), unless the voice is one-shot. */
void noteOff() { if (!oneShot) release(); }
/** Enter the release stage whatever the note (transport stop / pause). */
void release();
void kill();
float render();
+79
View File
@@ -0,0 +1,79 @@
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
// SPDX-License-Identifier: GPL-3.0-or-later
// The Sampler's One-shot: a note-off (MIDI or the tracker) doesn't stop the pad,
// it plays its whole slice; with One-shot off it fades out; stopping the song
// stops it either way.
#include "sampler/SampleStore.hpp"
#include "sizzle/Engine.hpp"
#include "sizzle/Model.hpp"
#include <cmath>
#include <cstdio>
#include <vector>
using namespace sizzle;
static int failures = 0;
#define CHECK(c) do { if (!(c)) { std::printf("FAIL %s:%d %s\n", __FILE__, __LINE__, #c); ++failures; } } while (0)
constexpr int SR = 48000;
/// Peak of [frames] rendered frames.
static float peak(engine::Engine &e, int frames) {
std::vector<float> buf(192 * 2);
float p = 0;
for (int done = 0; done < frames; done += 192) {
e.fillBlock(buf.data(), 192);
for (float v : buf) p = std::max(p, std::fabs(v));
}
return p;
}
enum class Way { Midi, MidiThenPanic, Song, SongThenStop };
/// Pad 0 (C2, MIDI 36) holds a one-second tone; channel 1 plays the Sampler. The
/// pad starts and gets its note-off 125 ms later, from MIDI or from the song (a
/// note on line 0, "===" on line 1 at 120 BPM); returns the peak at 300..400 ms.
static float afterNoteOff(const char *oneShot, Way way) {
sampler::SampleStore store;
std::vector<float> tone(SR);
for (int i = 0; i < SR; ++i) tone[(size_t)i] = 0.5f * std::sin(i * 0.05f);
store.put("tone", Sample(std::move(tone), SR));
model::Project p;
p.toolbox[0].fill("SAMPLER");
p.toolbox[0].set("pad0", "tone");
p.toolbox[0].set("oneShot", oneShot);
p.mixer.channels[0].instrumentSlot = 0;
p.mixer.channels[0].midiChannel = 1;
p.patterns[0].tracks[0][0] = model::Cell{36, 127, 1};
p.patterns[0].tracks[0][1] = model::Cell{model::Cell::OFF, 127, 1};
engine::Engine e(SR, &store);
e.setProject(&p);
e.rebuildArrangement();
e.rebuildFxChains();
const bool song = way == Way::Song || way == Way::SongThenStop;
if (song) e.play(); else e.busNoteOn(1, 36, 127);
peak(e, SR / 8);
if (!song) e.busNoteOff(1, 36);
peak(e, SR / 16); // the song's "===" lands meanwhile
if (way == Way::SongThenStop) e.stop();
if (way == Way::MidiThenPanic) e.panic();
peak(e, SR / 8); // past any release
return peak(e, SR / 10); // ~300..400 ms: still inside the one-second tone
}
int main() {
const float midiOn = afterNoteOff("On", Way::Midi), midiOff = afterNoteOff("Off", Way::Midi);
const float songOn = afterNoteOff("On", Way::Song), songOff = afterNoteOff("Off", Way::Song);
const float stopped = afterNoteOff("On", Way::SongThenStop), panicked = afterNoteOff("On", Way::MidiThenPanic);
std::printf(" after a MIDI note-off: one-shot %.3f, normal %.3f\n", midiOn, midiOff);
std::printf(" after the song's ===: one-shot %.3f, normal %.3f\n", songOn, songOff);
std::printf(" one-shot after Stop %.3f, after panic %.3f\n", stopped, panicked);
CHECK(midiOn > 0.05f && songOn > 0.05f);
CHECK(midiOff < 1e-4f && songOff < 1e-4f);
CHECK(stopped < 1e-4f && panicked < 1e-4f);
std::printf(failures ? "oneshot_test: %d failure(s)\n" : "oneshot_test: ok\n", failures);
return failures ? 1 : 0;
}