- Three source trees: core/ (shared C++ engine, moved from ios/core), app/ (Android UI), ios/app/ (iOS UI) - Android plays through core/ via JNI (EngineHost, jni_engine.cpp): the Oboe callback renders natively; the Kotlin model is mirrored as project text and applied as a diff, so playback continues through edits - The Kotlin engine moves to the unit tests as ReferenceEngine (golden reference) - Shared MasterRecorder in core/src/host, used by both apps; host_test - versions.properties: one versionName/versionCode for both platforms, continuing Android's numbering (iOS 0.6.0/11 -> 0.37.0/71) Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
324 lines
11 KiB
C++
324 lines
11 KiB
C++
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
// Port of audio/GrainSources.kt, line for line, in the same (double) precision.
|
|
|
|
#pragma clang fp contract(off)
|
|
|
|
#include "GrainSources.hpp"
|
|
|
|
#include <algorithm>
|
|
#include <atomic>
|
|
#include <cmath>
|
|
#include <cstdint>
|
|
#include <cstring>
|
|
#include <mutex>
|
|
#include <thread>
|
|
#include <vector>
|
|
#include "EchofieldDefs.hpp"
|
|
|
|
namespace sizzle::echofield::GrainSources {
|
|
namespace {
|
|
|
|
constexpr double SECONDS = 6.0;
|
|
const int N = (int)(RATE * SECONDS);
|
|
|
|
using Buf = std::vector<float>;
|
|
|
|
// ------------------------------------------------------------------ helpers ---
|
|
constexpr double C4 = 261.6256;
|
|
double hz(int semisFromC4) { return C4 * std::pow(2.0, semisFromC4 / 12.0); }
|
|
constexpr double TAU = 2 * M_PI;
|
|
|
|
/** A struck note: partials (ratio, level, decay per second) from [t0] with a soft
|
|
* [attack], each partial's decay scaled by its ratio for brighter-dies-first. */
|
|
void strike(Buf &out, double t0, double f, const std::vector<std::vector<double>> &partials, double attack, double gain) {
|
|
int start = (int)(t0 * RATE);
|
|
for (const auto &p : partials) {
|
|
double fr = f * p[0];
|
|
if (fr > RATE * 0.45) continue;
|
|
double w = TAU * fr / RATE;
|
|
double ph = std::fmod(p[0] * 1.7, TAU);
|
|
for (int i = start; i < N; ++i) {
|
|
double t = (double)(i - start) / RATE;
|
|
double env = (t < attack ? t / attack : 1.0) * std::exp(-p[2] * t);
|
|
if (env < 1e-4 && t > attack) break;
|
|
out[i] += (float)(gain * p[1] * env * std::sin(ph));
|
|
ph += w;
|
|
}
|
|
}
|
|
}
|
|
|
|
/** A band-limited saw by its harmonics, with a slow vibrato. */
|
|
template <class Env>
|
|
void saw(Buf &out, double f, double gain, double vibHz, double vibCents, double phase, Env ampEnv) {
|
|
int maxK = std::min(std::max(1, (int)((RATE * 0.45) / f)), 40);
|
|
std::vector<double> phs(maxK);
|
|
for (int it = 0; it < maxK; ++it) phs[it] = phase * (it + 1);
|
|
for (int i = 0; i < N; ++i) {
|
|
double t = (double)i / RATE;
|
|
double fm = f * std::pow(2.0, vibCents * std::sin(TAU * vibHz * t + phase) / 1200.0);
|
|
double s = 0.0;
|
|
for (int k = 1; k <= maxK; ++k) {
|
|
phs[k - 1] += TAU * fm * k / RATE;
|
|
s += std::sin(phs[k - 1]) / k;
|
|
}
|
|
out[i] += (float)(gain * ampEnv(t) * s);
|
|
}
|
|
}
|
|
|
|
/** Kotlin's Int xorshift (wrapping, `ushr` logical). */
|
|
struct Rng {
|
|
uint32_t s;
|
|
explicit Rng(uint32_t seed) : s(seed) {}
|
|
uint32_t next() { uint32_t x = s; x ^= x << 13; x ^= x >> 17; x ^= x << 5; s = x; return x; }
|
|
double f() { return (int32_t)(next() >> 8) / 16777216.0; }
|
|
double b() { return f() * 2 - 1; }
|
|
};
|
|
|
|
/** A two-pole resonant band-pass (RBJ, constant peak gain) run over [x]. */
|
|
Buf bandpass(const Buf &x, double f, double q) {
|
|
double w = TAU * f / RATE;
|
|
double alpha = std::sin(w) / (2 * q);
|
|
double a0 = 1 + alpha;
|
|
double b0 = alpha / a0, b2 = -alpha / a0;
|
|
double a1 = -2 * std::cos(w) / a0, a2 = (1 - alpha) / a0;
|
|
Buf y(x.size());
|
|
double x1 = 0.0, x2 = 0.0, y1 = 0.0, y2 = 0.0;
|
|
for (size_t i = 0; i < x.size(); ++i) {
|
|
double v = b0 * x[i] + b2 * x2 - a1 * y1 - a2 * y2;
|
|
x2 = x1; x1 = (double)x[i]; y2 = y1; y1 = v;
|
|
y[i] = (float)v;
|
|
}
|
|
return y;
|
|
}
|
|
|
|
void lowpass(Buf &x, double f) {
|
|
double a = 1 - std::exp(-TAU * f / RATE);
|
|
double z = 0.0;
|
|
for (size_t i = 0; i < x.size(); ++i) { z += a * (x[i] - z); x[i] = (float)z; }
|
|
}
|
|
|
|
Buf normalize(Buf x) {
|
|
float pk = 1e-9f;
|
|
for (float v : x) pk = std::max(pk, std::fabs(v));
|
|
float g = 0.89f / pk;
|
|
int fade = RATE / 50;
|
|
int size = (int)x.size();
|
|
for (int i = 0; i < size; ++i) {
|
|
float e = 1.f;
|
|
if (i < fade) e = (float)i / fade;
|
|
if (i > size - fade) e = (float)(size - i) / fade;
|
|
x[i] *= g * e;
|
|
}
|
|
return x;
|
|
}
|
|
|
|
// ------------------------------------------------------------------ recipes ---
|
|
const int PENT[] = {0, 2, 4, 7, 9, 12, 14, 16, 19, 21, 24};
|
|
|
|
Buf feltPiano() {
|
|
Buf out(N);
|
|
std::vector<std::vector<double>> partials(8);
|
|
for (int k = 0; k < 8; ++k)
|
|
partials[k] = {k + 1.0 + 0.0008 * (k + 1) * (k + 1), 1.0 / std::pow((double)(k + 1), 1.6), 0.9 + 0.7 * k};
|
|
const int melody[] = {0, 7, 4, 11, 2, 9, 7, 16, 12, 4, 14, 7};
|
|
for (int n = 0; n < 12; ++n) strike(out, 0.05 + n * 0.48, hz(melody[n]), partials, 0.006, 0.6);
|
|
lowpass(out, 3200.0);
|
|
return out;
|
|
}
|
|
|
|
Buf bowedGlass() {
|
|
Buf out(N);
|
|
const double ratios[] = {1.0, 2.32, 4.25, 6.63};
|
|
const double levels[] = {1.0, 0.45, 0.22, 0.1};
|
|
for (int k = 0; k < 4; ++k) {
|
|
double f = hz(12) * ratios[k];
|
|
double ph = k * 0.9;
|
|
for (int i = 0; i < N; ++i) {
|
|
double t = (double)i / RATE;
|
|
double env = (1 - std::exp(-t / 1.2)) * (0.85 + 0.15 * std::sin(TAU * 0.23 * t + k));
|
|
double vib = 1 + 0.0015 * std::sin(TAU * 4.6 * t);
|
|
ph += TAU * f * vib / RATE;
|
|
out[i] += (float)(levels[k] * env * std::sin(ph));
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
Buf musicBox() {
|
|
Buf out(N);
|
|
std::vector<std::vector<double>> partials = {{1.0, 1.0, 1.4}, {3.01, 0.32, 3.5}, {5.4, 0.14, 6.0}, {8.9, 0.05, 9.0}};
|
|
const int melody[] = {12, 16, 19, 24, 21, 19, 16, 14, 12, 19, 16, 24, 26, 24};
|
|
for (int n = 0; n < 14; ++n) strike(out, 0.04 + n * 0.41, hz(melody[n]), partials, 0.002, 0.5);
|
|
return out;
|
|
}
|
|
|
|
Buf kalimba() {
|
|
Buf out(N);
|
|
std::vector<std::vector<double>> partials = {{1.0, 1.0, 1.1}, {5.93, 0.22, 7.0}, {13.4, 0.06, 14.0}};
|
|
Rng rng(0x51A1);
|
|
double t = 0.03;
|
|
while (t < SECONDS - 0.8) {
|
|
strike(out, t, hz(PENT[(int)(rng.f() * 8)]), partials, 0.003, 0.55);
|
|
t += 0.28 + 0.3 * rng.f();
|
|
}
|
|
return out;
|
|
}
|
|
|
|
Buf glassTines() {
|
|
Buf out(N);
|
|
std::vector<std::vector<double>> partials = {{1.0, 1.0, 0.8}, {2.756, 0.4, 1.6}, {5.404, 0.18, 2.8}, {8.93, 0.07, 4.0}};
|
|
Rng rng(0x7123);
|
|
double t = 0.02;
|
|
while (t < SECONDS - 0.6) {
|
|
strike(out, t, hz(12 + PENT[(int)(rng.f() * 9)]), partials, 0.002, 0.4);
|
|
t += 0.17 + 0.25 * rng.f();
|
|
}
|
|
return out;
|
|
}
|
|
|
|
Buf warmPad() {
|
|
Buf out(N);
|
|
const int chord[] = {-12, 0, 4, 7, 11, 14};
|
|
for (int n = 0; n < 6; ++n) for (int d = 0; d < 2; ++d) {
|
|
double det = d == 0 ? -6.0 : 6.0;
|
|
saw(out, hz(chord[n]) * std::pow(2.0, det / 1200), 0.18, 0.2 + 0.05 * n, 4.0, n * 0.7 + d,
|
|
[](double t) { return 1 - std::exp(-t / 0.8); });
|
|
}
|
|
// a slow opening low-pass: three passes at a cutoff that rises over the sound
|
|
Buf a = out;
|
|
double z1 = 0.0, z2 = 0.0;
|
|
for (int i = 0; i < N; ++i) {
|
|
double t = (double)i / RATE;
|
|
double fc = 500 + 1800 * (0.5 - 0.5 * std::cos(TAU * t / SECONDS));
|
|
double k = 1 - std::exp(-TAU * fc / RATE);
|
|
z1 += k * (a[i] - z1); z2 += k * (z1 - z2);
|
|
out[i] = (float)z2;
|
|
}
|
|
return out;
|
|
}
|
|
|
|
Buf strings() {
|
|
Buf out(N);
|
|
const int voices[] = {-12, -5, 0, 4, 7};
|
|
for (int n = 0; n < 5; ++n) for (int d = 0; d < 3; ++d) {
|
|
double det = (d - 1) * 7.0;
|
|
saw(out, hz(voices[n]) * std::pow(2.0, det / 1200), 0.14, 5.1 + 0.4 * d, 9.0, n * 1.3 + d * 0.5,
|
|
[n](double t) { return (1 - std::exp(-t / 0.45)) * (0.8 + 0.2 * std::sin(TAU * 0.3 * t + n)); });
|
|
}
|
|
lowpass(out, 2600.0); lowpass(out, 5000.0);
|
|
return out;
|
|
}
|
|
|
|
Buf lowDrone() {
|
|
Buf out(N);
|
|
const double parts[][2] = {{hz(-24), 1.0}, {hz(-24) * 2.003, 0.5}, {hz(-17), 0.55},
|
|
{hz(-17) * 2.0, 0.25}, {hz(-12) * 1.002, 0.3}, {hz(-24) * 3.0, 0.18}};
|
|
for (int n = 0; n < 6; ++n) {
|
|
const double *p = parts[n];
|
|
double ph = n * 1.1;
|
|
for (int i = 0; i < N; ++i) {
|
|
double t = (double)i / RATE;
|
|
ph += TAU * p[0] / RATE;
|
|
double am = 0.75 + 0.25 * std::sin(TAU * (0.11 + 0.07 * n) * t + n);
|
|
out[i] += (float)(p[1] * am * (std::sin(ph) + 0.15 * std::sin(2 * ph)));
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
Buf breathChoir() {
|
|
Rng rng(0x0B2E);
|
|
Buf noise(N);
|
|
for (int i = 0; i < N; ++i) noise[i] = (float)rng.b();
|
|
// a voiced source under the breath: a soft buzz at C4 and its fifth
|
|
Buf buzz(N);
|
|
saw(buzz, hz(0), 0.25, 5.0, 8.0, 0.0, [](double t) { return 1 - std::exp(-t / 0.6); });
|
|
saw(buzz, hz(7), 0.18, 4.6, 8.0, 1.0, [](double t) { return 1 - std::exp(-t / 0.9); });
|
|
Buf src(N);
|
|
for (int i = 0; i < N; ++i) src[i] = noise[i] * 0.6f + buzz[i];
|
|
// "ah" formants drifting toward "oh"
|
|
Buf f1 = bandpass(src, 750.0, 6.0), f2 = bandpass(src, 1150.0, 8.0), f3 = bandpass(src, 2600.0, 10.0);
|
|
Buf g1 = bandpass(src, 500.0, 6.0), g2 = bandpass(src, 900.0, 8.0);
|
|
Buf out(N);
|
|
for (int i = 0; i < N; ++i) {
|
|
double t = (double)i / RATE;
|
|
double m = 0.5 - 0.5 * std::cos(TAU * t / SECONDS);
|
|
double swell = (1 - std::exp(-t / 0.7)) * (0.7 + 0.3 * std::sin(TAU * 0.4 * t));
|
|
out[i] = (float)(swell * ((1 - m) * (f1[i] + 0.7 * f2[i]) + m * (g1[i] + 0.6 * g2[i]) + 0.3 * f3[i]));
|
|
}
|
|
return out;
|
|
}
|
|
|
|
Buf rainField() {
|
|
Buf out(N);
|
|
Rng rng(0x2A1F);
|
|
// the bed: soft filtered noise
|
|
double z = 0.0, z2 = 0.0;
|
|
for (int i = 0; i < N; ++i) {
|
|
z += 0.08 * (rng.b() - z); z2 += 0.3 * (z - z2);
|
|
out[i] = (float)(0.5 * (z - 0.6 * z2));
|
|
}
|
|
// drops: short pitched blips at random
|
|
double t = 0.0;
|
|
while (t < SECONDS - 0.1) {
|
|
double f = 900 + 3200 * rng.f();
|
|
int start = (int)(t * RATE);
|
|
double a = 0.15 + 0.35 * rng.f();
|
|
double ph = 0.0;
|
|
int end = std::min(N, start + RATE / 10);
|
|
for (int i = start; i < end; ++i) {
|
|
double tt = (double)(i - start) / RATE;
|
|
ph += TAU * f * (1 + 0.6 * std::exp(-tt * 60)) / RATE;
|
|
out[i] += (float)(a * std::exp(-tt * 55) * std::sin(ph));
|
|
}
|
|
t += 0.01 + 0.08 * std::pow(rng.f(), 2.0);
|
|
}
|
|
return out;
|
|
}
|
|
|
|
using Recipe = Buf (*)();
|
|
// Same order as EchofieldDefs SOURCES.
|
|
const Recipe RECIPES[COUNT] = {
|
|
feltPiano, bowedGlass, musicBox, warmPad, strings,
|
|
lowDrone, rainField, kalimba, breathChoir, glassTines,
|
|
};
|
|
|
|
Sample cache[COUNT];
|
|
std::atomic<bool> ready[COUNT];
|
|
std::mutex buildLock; // builders only (never the audio thread)
|
|
std::atomic<bool> started{false};
|
|
|
|
} // namespace
|
|
|
|
int indexOf(const char *name) {
|
|
if (!name) return -1;
|
|
for (int i = 0; i < COUNT; ++i) if (std::strcmp(SOURCES[i], name) == 0) return i;
|
|
return -1;
|
|
}
|
|
|
|
const Sample *get(int index) {
|
|
if (index < 0 || index >= COUNT) return nullptr;
|
|
return ready[index].load(std::memory_order_acquire) ? &cache[index] : nullptr;
|
|
}
|
|
|
|
const Sample *get(const char *name) { return get(indexOf(name)); }
|
|
|
|
void prewarmBlocking() {
|
|
std::lock_guard<std::mutex> g(buildLock);
|
|
for (int i = 0; i < COUNT; ++i) {
|
|
if (ready[i].load(std::memory_order_acquire)) continue;
|
|
cache[i].data = normalize(RECIPES[i]());
|
|
cache[i].sampleRate = RATE;
|
|
ready[i].store(true, std::memory_order_release);
|
|
}
|
|
}
|
|
|
|
void prewarm() {
|
|
if (started.exchange(true)) return;
|
|
std::thread([] { prewarmBlocking(); }).detach();
|
|
}
|
|
|
|
} // namespace sizzle::echofield::GrainSources
|