Verified contractExactMIT
ksh
Address0x324eb6054b98ccc772faffffc649bb87e0d2c603
- Balance
0 ETH
- Transactions
1
- Compiler
solc 0.8.34
- Same code
Only this contract
Details
- Compiler
- solc 0.8.34+commit.80d5c536
- Language
- solidity
- Optimization
- Yes, 200 runs
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,436,929
- Creation transaction
- 0x9153dd32a4…f39732c963
contracts/1of1s/contracts/contracts 11-17b/ksh.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/// @title ksh — deterministic kick, snare and hi-hat patterns for Open Music
/// @notice Fully deterministic from arguments, independent of contract deployment.
/// @dev Uses the Branches / panoramaParts entropy convention:
/// uint256(keccak256(abi.encode(seed, bytes32(domain), index))).
/// Output is packed, big-endian uint32 milliseconds, four bytes per hit.
contract ksh {
struct PatternParams {
uint256 bpm;
uint256 bars;
uint256 subDiv; // subdivisions PER BAR; divisible by 4
uint256 density1; // 0..10000
uint256 density2; // 0..10000
uint256 density3; // 0..10000
uint256 maxWonk; // positive-only timing delay, milliseconds
uint256 wonkArraySize;
uint256 seed;
}
struct HihatParams {
PatternParams pattern;
uint256 accent;
uint256 accentOffset;
}
uint256 public constant MAX_STEPS = 4096;
uint256 public constant MAX_WONK_ENTRIES = 1024;
function generateKick(
uint256 bpm, uint256 bars, uint256 subDiv,
uint256 density1, uint256 density2, uint256 density3,
uint256 maxWonk, uint256 wonkArraySize, uint256 seed
) external pure returns (bytes memory) {
return _generateKick(PatternParams(bpm,bars,subDiv,density1,density2,density3,maxWonk,wonkArraySize,seed));
}
function generateSnare(
uint256 bpm, uint256 bars, uint256 subDiv,
uint256 density1, uint256 density2, uint256 density3,
uint256 maxWonk, uint256 wonkArraySize, uint256 seed
) external pure returns (bytes memory) {
return _generateSnare(PatternParams(bpm,bars,subDiv,density1,density2,density3,maxWonk,wonkArraySize,seed));
}
function generateHihat(
uint256 bpm, uint256 bars, uint256 subDiv,
uint256 density1, uint256 density2, uint256 density3,
uint256 accent, uint256 accentOffset,
uint256 maxWonk, uint256 wonkArraySize, uint256 seed
) external pure returns (bytes memory) {
PatternParams memory p = PatternParams(bpm,bars,subDiv,density1,density2,density3,maxWonk,wonkArraySize,seed);
return _generateHihat(HihatParams(p,accent,accentOffset));
}
function _generateKick(PatternParams memory p) internal pure returns (bytes memory packed) {
_validate(p);
uint256 msPerStep = _msPerSubDiv(p);
uint256 count = p.bars * p.subDiv;
uint256[] memory wonk = _wonk(p);
packed = new bytes(count * 4);
uint256 offset;
for (uint256 i; i < count; ++i) {
uint256 position = i % p.subDiv;
uint256 chance = position == 0 ? p.density1 : (position == 3 || position == 6 ? p.density2 : p.density3);
if (_rand(p.seed, "KICK", i) % 10000 < chance) {
offset = _packUint32(packed, offset, i * msPerStep + wonk[i % wonk.length]);
}
}
assembly { mstore(packed, offset) }
}
function _generateSnare(PatternParams memory p) internal pure returns (bytes memory packed) {
_validate(p);
uint256 msPerStep = _msPerSubDiv(p);
uint256 count = p.bars * p.subDiv;
uint256 stepsPerBeat = p.subDiv / 4;
uint256[] memory wonk = _wonk(p);
packed = new bytes(count * 4);
uint256 offset;
for (uint256 i; i < count; ++i) {
uint256 position = i % p.subDiv;
bool backbeat = position == stepsPerBeat || position == 3 * stepsPerBeat;
if (backbeat || _rand(p.seed, "SNARE_GHOST", i) % 10000 < p.density1) {
offset = _packUint32(packed, offset, i * msPerStep + wonk[i % wonk.length]);
}
}
assembly { mstore(packed, offset) }
}
function _generateHihat(HihatParams memory h) internal pure returns (bytes memory packed) {
PatternParams memory p = h.pattern;
_validate(p);
require(h.accent != 0, "accent is zero");
uint256 msPerStep = _msPerSubDiv(p);
uint256 count = p.bars * p.subDiv;
uint256 densityRange = p.density2 == 0 ? 1 : p.density2;
uint256 density = p.density1 + (_rand(p.seed, "HAT_DENSITY", 0) % densityRange);
uint256[] memory wonk = _wonk(p);
packed = new bytes(count * 4);
uint256 offset;
for (uint256 i; i < count; ++i) {
bool strong = (i + h.accentOffset) % h.accent == 0;
uint256 chance = strong ? 9800 : density;
if (_rand(p.seed, "HAT", i) % 10000 < chance) {
offset = _packUint32(packed, offset, i * msPerStep + wonk[i % wonk.length]);
}
}
assembly { mstore(packed, offset) }
}
function _validate(PatternParams memory p) internal pure {
require(p.bpm != 0, "bpm is zero");
require(p.bars != 0, "bars is zero");
require(p.subDiv >= 4 && p.subDiv % 4 == 0, "bad subdivision");
require(p.bars * p.subDiv <= MAX_STEPS, "too many steps");
require(p.density1 <= 10000 && p.density2 <= 10000 && p.density3 <= 10000, "bad density");
require(p.maxWonk == 0 || (p.wonkArraySize != 0 && p.wonkArraySize <= MAX_WONK_ENTRIES), "bad wonk size");
require(_msPerSubDiv(p) > 0, "subdivision too short");
}
function _msPerSubDiv(PatternParams memory p) internal pure returns (uint256) {
return 240000 / p.bpm / p.subDiv; // retain v1 integer timing
}
function _wonk(PatternParams memory p) internal pure returns (uint256[] memory wonk) {
if (p.maxWonk == 0) {
wonk = new uint256[](1);
return wonk;
}
wonk = new uint256[](p.wonkArraySize);
for (uint256 i; i < wonk.length; ++i) {
wonk[i] = _rand(p.seed, "WONK", i) % p.maxWonk;
}
}
/// @notice The same 3-argument seed/domain/index hash used in Branches and panoramaParts.
function _rand(uint256 seed, bytes32 domain, uint256 index) private pure returns (uint256) {
return uint256(keccak256(abi.encode(seed, domain, index)));
}
function unpackMsValues(bytes memory packed) external pure returns (uint256[] memory values) {
require(packed.length % 4 == 0, "bad packed length");
values = new uint256[](packed.length / 4);
for (uint256 i; i < values.length; ++i) {
uint256 o = i * 4;
values[i] = (uint32(uint8(packed[o])) << 24) |
(uint32(uint8(packed[o+1])) << 16) |
(uint32(uint8(packed[o+2])) << 8) |
uint32(uint8(packed[o+3]));
}
}
function _packUint32(bytes memory packed, uint256 offset, uint256 msValue) internal pure returns (uint256) {
require(msValue <= type(uint32).max, "ms overflow");
uint32 v = uint32(msValue);
packed[offset] = bytes1(uint8(v >> 24));
packed[offset+1] = bytes1(uint8(v >> 16));
packed[offset+2] = bytes1(uint8(v >> 8));
packed[offset+3] = bytes1(uint8(v));
return offset + 4;
}
}
Compiler settingsShow
{
"optimizer": {
"runs": 200,
"enabled": true
},
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.