Files
sizzletracker-android/core/tests/oneshot_test.cpp
T
ReactorcoremeltdownandClaude Opus 5.5 7041f762e5 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>
2026-11-16 16:53:13 +01:00

80 lines
3.2 KiB
C++

// 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;
}