iOS core: Morph Pad, Wavetable and Grain Cloud ported, bit-exact with Kotlin
Ported by parallel agents against reference renders. The export now records Kotlin's global voice-seed counters and every factory wavetable's data, and adds Sampler/SF2 scenarios (not ported yet). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
bf5f2b53af
commit
8d85b14a67
@@ -0,0 +1,79 @@
|
||||
// SPDX-FileCopyrightText: 2026 Reactorcoremeltdown
|
||||
// SPDX-License-Identifier: GPL-3.0-or-later
|
||||
#pragma once
|
||||
|
||||
// java.util.Random, bit for bit: the 48-bit LCG and the derivations of
|
||||
// nextInt / nextInt(bound) / nextLong / nextBoolean / nextFloat / nextDouble /
|
||||
// nextGaussian as OpenJDK implements them. Seeded factory tables (WaveTables'
|
||||
// Glass) come out exactly as on Android.
|
||||
|
||||
#include <cmath>
|
||||
#include <cstdint>
|
||||
|
||||
namespace sizzle::wavetable {
|
||||
|
||||
class JavaRandom {
|
||||
public:
|
||||
explicit JavaRandom(int64_t seed) { setSeed(seed); }
|
||||
|
||||
void setSeed(int64_t seed) {
|
||||
seed_ = ((uint64_t)seed ^ MULT) & MASK;
|
||||
haveNextNextGaussian_ = false;
|
||||
}
|
||||
|
||||
/** Random.next(bits): the top [bits] of the 48-bit state, as a (wrapping) Int. */
|
||||
int32_t next(int bits) {
|
||||
seed_ = (seed_ * MULT + ADD) & MASK;
|
||||
return (int32_t)(uint32_t)(seed_ >> (48 - bits));
|
||||
}
|
||||
|
||||
int32_t nextInt() { return next(32); }
|
||||
|
||||
/** nextInt(bound), bound > 0 (Java throws otherwise; here 0 is returned). */
|
||||
int32_t nextInt(int32_t bound) {
|
||||
if (bound <= 0) return 0;
|
||||
int32_t r = next(31);
|
||||
int32_t m = bound - 1;
|
||||
if ((bound & m) == 0) {
|
||||
r = (int32_t)(((int64_t)bound * (int64_t)r) >> 31);
|
||||
} else {
|
||||
// for (int u = r; u - (r = u % bound) + m < 0; u = next(31)); with Int wrap
|
||||
for (int32_t u = r;; u = next(31)) {
|
||||
r = u % bound;
|
||||
if ((int32_t)((uint32_t)u - (uint32_t)r + (uint32_t)m) >= 0) break;
|
||||
}
|
||||
}
|
||||
return r;
|
||||
}
|
||||
|
||||
int64_t nextLong() { return (int64_t)((uint64_t)(int64_t)next(32) << 32) + (int64_t)next(32); }
|
||||
bool nextBoolean() { return next(1) != 0; }
|
||||
float nextFloat() { return (float)next(24) / (float)(1 << 24); }
|
||||
double nextDouble() { return (double)(((int64_t)next(26) << 27) + next(27)) * 0x1.0p-53; }
|
||||
|
||||
/** Marsaglia polar method with the cached second value. Java uses StrictMath
|
||||
* (fdlibm) log/sqrt; libm's log may differ by an ulp. */
|
||||
double nextGaussian() {
|
||||
if (haveNextNextGaussian_) { haveNextNextGaussian_ = false; return nextNextGaussian_; }
|
||||
double v1, v2, s;
|
||||
do {
|
||||
v1 = 2 * nextDouble() - 1;
|
||||
v2 = 2 * nextDouble() - 1;
|
||||
s = v1 * v1 + v2 * v2;
|
||||
} while (s >= 1 || s == 0);
|
||||
double multiplier = std::sqrt(-2 * std::log(s) / s);
|
||||
nextNextGaussian_ = v2 * multiplier;
|
||||
haveNextNextGaussian_ = true;
|
||||
return v1 * multiplier;
|
||||
}
|
||||
|
||||
private:
|
||||
static constexpr uint64_t MULT = 0x5DEECE66DULL;
|
||||
static constexpr uint64_t ADD = 0xBULL;
|
||||
static constexpr uint64_t MASK = (1ULL << 48) - 1;
|
||||
uint64_t seed_ = 0;
|
||||
double nextNextGaussian_ = 0;
|
||||
bool haveNextNextGaussian_ = false;
|
||||
};
|
||||
|
||||
} // namespace sizzle::wavetable
|
||||
Reference in New Issue
Block a user