Verified contractExactMIT
PanoramaMoodCodecP4M1
Address0xc492a40680e447b56ca4e76b795ddaad32160c77
- 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
- Via IR
- yes
- License
- MIT
- Match
- Exactcreation code: yes, runtime code: yes
- Verified on Sourcify
- Created in block
- 26,443,312
- Creation transaction
- 0xc0b568a866…1abd70c6c3
contracts/1of1s/contracts/contracts 11-17b/PanoramaMoodCodecP4M1.sol
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.20;
/// @title PanoramaMoodCodecP4M1
/// @notice Stateless P4M1 decoder for compact Panorama V2 mood profiles.
/// @dev Returns standard ABI-encoded Mood, keeping it independent of any controller ABI.
/// The type widths/order below match the V2 Mood positional tuple ABI.
/// A standalone codec keeps its code outside the near-24KB MoodDataV2 contract.
/// No deployable changes are needed for the existing controller/renderer ABIs.
contract PanoramaMoodCodecP4M1 {
bytes4 public constant MAGIC = 0x50344d31; // "P4M1"
uint256 public constant MAX_PACKET = 100_000;
error BadPacket();
error BadProfile();
struct Cursor { uint256 i; }
struct Range { uint16 min; uint16 max; }
struct Pattern { uint16[] durations; Range steps; uint16 targetTicks; }
struct Chords {
uint8[7][] scales;
uint8[] modes; // enum values 0..2; same canonical ABI as enum[]
uint8[][] shapes;
Range count; Range thirds; Range low; Range high; Range voices; Range shuffle;
}
struct Part {
Pattern groups; Pattern rhythm;
Range tones; Range density; Range gate;
int16[] transpose; // int16 ABI words equivalent to V2 signed int types for values [-48,48]
}
struct Drum {
Range bars; uint16[] subdivisions; Range complexity;
Range density1; Range density2; Range density3;
Range wonk; Range wonkSteps; Range accent; Range accentOffset;
}
struct Audio {
uint8 track; bytes32 parameter; int32 min; int32 max;
uint32 step; uint32 scale; int32[] choices;
}
struct Mood {
Range bpm; Chords chords; Pattern harmony;
Part[3] parts; Drum[3] drums; Audio[] audio;
}
/// @notice Decode and validate. Return Solidity ABI encoding of the one Mood tuple.
/// @dev Consumer can use abi.decode(retval, (T.Mood)); this works regardless
/// of the native struct's integer widths, for values within those widths.
function unpackToABI(bytes calldata packed) external pure returns (bytes memory) {
return abi.encode(_unpack(packed));
}
/// @notice Remix-friendly decoded representation for inspecting compact data.
function unpack(bytes calldata packed) external pure returns (Mood memory) {
return _unpack(packed);
}
function _unpack(bytes calldata b) private pure returns (Mood memory m) {
if (b.length < 4 || b.length > MAX_PACKET ||
b[0] != 0x50 || b[1] != 0x34 || b[2] != 0x4d || b[3] != 0x31) revert BadPacket();
Cursor memory c = Cursor(4);
m.bpm = _range(b,c);
uint256 n = _len(b,c,16,1);
m.chords.scales = new uint8[7][](n);
for (uint256 i; i<n; ++i) for (uint256 j; j<7; ++j) m.chords.scales[i][j] = _u8(b,c);
n = _len(b,c,16,1);
m.chords.modes = new uint8[](n);
for (uint256 i; i<n; ++i) m.chords.modes[i] = _u8(b,c);
n = _len(b,c,16,1);
m.chords.shapes = new uint8[][](n);
for (uint256 i; i<n; ++i) m.chords.shapes[i] = _array8(b,c,12,1);
m.chords.count = _range(b,c);
m.chords.thirds = _range(b,c);
m.chords.low = _range(b,c);
m.chords.high = _range(b,c);
m.chords.voices = _range(b,c);
m.chords.shuffle = _range(b,c);
m.harmony = _pattern(b,c);
for(uint256 i; i<3; ++i){
m.parts[i].groups = _pattern(b,c);
m.parts[i].rhythm = _pattern(b,c);
m.parts[i].tones = _range(b,c);
m.parts[i].density = _range(b,c);
m.parts[i].gate = _range(b,c);
n = _len(b,c,16,1);
m.parts[i].transpose = new int16[](n);
for(uint256 j; j<n; ++j){uint8 x=_u8(b,c);m.parts[i].transpose[j] = x>=128 ? int16(int256(uint256(x)) - 256) : int16(uint16(x));}
}
for(uint256 i; i<3; ++i){
Drum memory d;
d.bars = _range(b,c);
d.subdivisions = _array16(b,c,32,1);
d.complexity = _range(b,c);
d.density1 = _range(b,c);
d.density2 = _range(b,c);
d.density3 = _range(b,c);
d.wonk = _range(b,c);
d.wonkSteps = _range(b,c);
d.accent = _range(b,c);
d.accentOffset = _range(b,c);
m.drums[i] = d;
}
n = _len(b,c,160,0);
m.audio = new Audio[](n);
for(uint256 i; i<n; ++i){
uint8 code = _u8(b,c);
uint8 id = code & 31;
m.audio[i].track = code >> 5;
m.audio[i].parameter = id == 31 ? _raw32(b,c) : _param(id);
m.audio[i].min = _svar32(b,c);
m.audio[i].max = _svar32(b,c);
m.audio[i].step = _var32(b,c);
m.audio[i].scale = _var32(b,c);
uint256 k = _len(b,c,32,0);
m.audio[i].choices = new int32[](k);
for(uint256 j; j<k; ++j)m.audio[i].choices[j] = _svar32(b,c);
}
if(c.i != b.length) revert BadPacket();
_validate(m);
}
function _u8(bytes calldata b,Cursor memory c) private pure returns(uint8 v){
if(c.i>=b.length)revert BadPacket();v=uint8(b[c.i]);unchecked{++c.i;}
}
function _u16(bytes calldata b,Cursor memory c) private pure returns(uint16){
return (uint16(_u8(b,c)) << 8) | uint16(_u8(b,c));
}
function _len(bytes calldata b, Cursor memory c, uint256 max_,uint256 min_) private pure returns(uint256 n){
n=_u8(b,c);if(n<min_||n>max_)revert BadPacket();
}
function _range(bytes calldata b, Cursor memory c) private pure returns (Range memory r){
r.min=_u16(b,c);r.max=_u16(b,c);if(r.min>r.max)revert BadPacket();
}
function _array16(bytes calldata b,Cursor memory c,uint256 max_,uint256 min_) private pure returns(uint16[] memory out){
uint256 n=_len(b,c,max_,min_);out=new uint16[](n);for(uint256 i;i<n;++i)out[i]=_u16(b,c);
}
function _array8(bytes calldata b,Cursor memory c,uint256 max_,uint256 min_) private pure returns(uint8[] memory out){
uint256 n=_len(b,c,max_,min_);out=new uint8[](n);for(uint256 i;i<n;++i)out[i]=_u8(b,c);
}
function _pattern(bytes calldata b,Cursor memory c) private pure returns(Pattern memory p){
p.durations=_array16(b,c,32,1);p.steps=_range(b,c);p.targetTicks=_u16(b,c);
}
function _var32(bytes calldata b,Cursor memory c) private pure returns(uint32 out){
uint256 x;
for(uint256 i;i<5;++i){
uint8 v=_u8(b,c);
x|=uint256(v&0x7f)<<(7*i);
if((v & 0x80)==0){
if((i>0&&(v&0x7f)==0)||x>type(uint32).max)revert BadPacket();
return uint32(x);
}
}
revert BadPacket();
}
function _svar32(bytes calldata b,Cursor memory c) private pure returns(int32){
uint32 x=_var32(b,c);
int64 value=(x & 1)==1 ? -int64(uint64(x>>1))-1 : int64(uint64(x>>1));
return int32(value);
}
function _raw32(bytes calldata b,Cursor memory c) private pure returns(bytes32 v){
if(c.i+32>b.length)revert BadPacket();
// calldata word copy; no escaping or normalization, so unknown names survive exactly
assembly { v := calldataload(add(b.offset,mload(c))) }
c.i+=32;
}
function _param(uint8 id) private pure returns(bytes32){
if(id==0)return bytes32('attack');
if(id==1)return bytes32('decay');
if(id==2)return bytes32('delay');
if(id==3)return bytes32('delayBeats');
if(id==4)return bytes32('detune');
if(id==5)return bytes32('distWet');
if(id==6)return bytes32('drive');
if(id==7)return bytes32('feedback');
if(id==8)return bytes32('filter');
if(id==9)return bytes32('hold');
if(id==10)return bytes32('pan');
if(id==11)return bytes32('pitch');
if(id==12)return bytes32('release');
if(id==13)return bytes32('reverb');
if(id==14)return bytes32('reverbTime');
if(id==15)return bytes32('sub');
if(id==16)return bytes32('sustain');
revert BadPacket();
}
function _check(Range memory r,uint16 lo,uint16 hi)private pure{
if(r.min<lo||r.max>hi||r.min>r.max)revert BadProfile();
}
function _checkPattern(Pattern memory p) private pure {
_check(p.steps,0,32);
if(p.steps.min==0&&(p.steps.max!=0||p.targetTicks==0))revert BadProfile();
if(p.targetTicks>4096||p.durations.length==0||p.durations.length>32)revert BadProfile();
for(uint256 i;i<p.durations.length;++i)if(p.durations[i]==0||p.durations[i]>128)revert BadProfile();
}
function _validate(Mood memory m)private pure{
_check(m.bpm,30,240);
Chords memory ch=m.chords;
_check(ch.count,2,32);_check(ch.thirds,3,7);
_check(ch.low,0,127);_check(ch.high,0,127);
if(ch.low.max>ch.high.min)revert BadProfile();
_check(ch.voices,1,16);_check(ch.shuffle,0,100);
if(ch.scales.length==0||ch.scales.length>16||ch.modes.length==0||ch.modes.length>16||ch.shapes.length==0||ch.shapes.length>16)revert BadProfile();
for(uint256 i;i<ch.scales.length;++i){
if(ch.scales[i][0]!=0||ch.scales[i][6]>11)revert BadProfile();
for(uint256 j=1;j<7;++j)if(ch.scales[i][j]<=ch.scales[i][j-1])revert BadProfile();
}
for(uint256 i;i<ch.modes.length;++i)if(ch.modes[i]>2)revert BadProfile();
for(uint256 i;i<ch.shapes.length;++i){
uint16 mask;uint256 n=ch.shapes[i].length;
if(n==0||n>12)revert BadProfile();
for(uint256 j;j<n;++j){uint8 note=ch.shapes[i][j];if(note>11||(mask&(uint16(1)<<note))!=0)revert BadProfile();mask|=uint16(1)<<note;}
}
_checkPattern(m.harmony);
for(uint256 i;i<3;++i){
Part memory p=m.parts[i];
_checkPattern(p.groups);_checkPattern(p.rhythm);
_check(p.tones,0,16);_check(p.density,0,100);_check(p.gate,1,100);
if(p.transpose.length==0||p.transpose.length>16)revert BadProfile();
for(uint256 j;j<p.transpose.length;++j){
int16 x=p.transpose[j];
if(x< -48||x>48||int256(uint256(ch.low.min))+x<0||int256(uint256(ch.high.max))+x>127)revert BadProfile();
}
Drum memory d=m.drums[i];
_check(d.bars,1,32);_check(d.complexity,0,100);
_check(d.density1,0,100);_check(d.density2,0,100);_check(d.density3,0,100);
_check(d.wonk,0,1000);_check(d.wonkSteps,1,1024);
_check(d.accent,1,128);_check(d.accentOffset,0,128);
if(d.subdivisions.length==0||d.subdivisions.length>32)revert BadProfile();
for(uint256 j;j<d.subdivisions.length;++j)
if(d.subdivisions[j]<4||d.subdivisions[j]>128||d.subdivisions[j]%4!=0)revert BadProfile();
}
if(m.audio.length>160)revert BadProfile();
for(uint256 i;i<m.audio.length;++i){
Audio memory a=m.audio[i];
if(a.track>6||a.parameter==bytes32(0)||a.scale==0||a.scale>1_000_000||
a.min>a.max||a.step==0||a.choices.length>32)revert BadProfile();
if((int256(a.max)-a.min)%int256(uint256(a.step))!=0)revert BadProfile();
for(uint256 j;j<a.choices.length;++j)if(a.choices[j]<a.min||a.choices[j]>a.max)revert BadProfile();
for(uint256 j;j<i;++j)if(m.audio[j].track==a.track&&m.audio[j].parameter==a.parameter)revert BadProfile();
}
}
}
Compiler settingsShow
{
"viaIR": true,
"optimizer": {
"runs": 200,
"enabled": true
},
"remappings": []
}Source code and metadata: Sourcify.
The source code belongs to the authors of the contract.