st/
quantum_wave_signature.rs1use std::fmt;
6
7#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
10pub struct QuantumWaveSignature {
11 pub signature: u32,
12}
13
14impl QuantumWaveSignature {
15 pub fn new(frequency: u8, phase: u8, amplitude: u8, torsion: u8) -> Self {
17 let signature = ((torsion as u32) << 24)
18 | ((amplitude as u32) << 16)
19 | ((phase as u32) << 8)
20 | (frequency as u32);
21 Self { signature }
22 }
23
24 pub fn from_raw(signature: u32) -> Self {
26 Self { signature }
27 }
28
29 pub fn frequency(&self) -> u8 {
31 (self.signature & 0xFF) as u8
32 }
33
34 pub fn phase(&self) -> u8 {
36 ((self.signature >> 8) & 0xFF) as u8
37 }
38
39 pub fn amplitude(&self) -> u8 {
41 ((self.signature >> 16) & 0xFF) as u8
42 }
43
44 pub fn torsion(&self) -> u8 {
46 ((self.signature >> 24) & 0xFF) as u8
47 }
48
49 pub fn to_hz(&self) -> f32 {
51 (self.frequency() as f32 / 255.0) * 200.0
52 }
53
54 pub fn to_radians(&self) -> f32 {
56 (self.phase() as f32 / 255.0) * 2.0 * std::f32::consts::PI
57 }
58
59 pub fn amplitude_percent(&self) -> f32 {
61 (self.amplitude() as f32 / 255.0) * 100.0
62 }
63
64 pub fn interference(&self, other: &Self) -> f32 {
66 let freq_diff = (self.frequency() as i16 - other.frequency() as i16).abs() as f32;
67 let phase_diff = (self.phase() as i16 - other.phase() as i16).abs() as f32;
68
69 let harmonic_factor = if freq_diff as i32 % 12 == 0 { 2.0 } else { 1.0 };
72 let phase_factor = 1.0 - (phase_diff / 255.0);
73
74 harmonic_factor * phase_factor * (self.amplitude_percent() + other.amplitude_percent())
75 / 200.0
76 }
77
78 pub fn is_horse_apple(&self) -> bool {
80 let bytes = [
82 self.frequency(),
83 self.phase(),
84 self.amplitude(),
85 self.torsion(),
86 ];
87
88 bytes.windows(2).all(|w| w[0] == w[1])
90 }
91
92 pub fn golden_ratio() -> Self {
94 Self::new(
96 162, 100, 161, 89, )
101 }
102
103 pub fn marine_salience() -> Self {
105 Self::new(
106 44, 128, 200, 73, )
111 }
112
113 pub fn from_emotion(valence: f32, arousal: f32, dominance: f32) -> Self {
115 let frequency = (2.0 + arousal * 198.0) as u8;
116 let phase = ((valence + 1.0) * 127.5) as u8;
117 let amplitude = (arousal * 255.0) as u8;
118 let torsion = (dominance * 255.0) as u8;
119
120 Self::new(frequency, phase, amplitude, torsion)
121 }
122}
123
124impl fmt::Display for QuantumWaveSignature {
125 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
126 if self.is_horse_apple() {
127 write!(f, "0x{:08X} 💩 (Andy disapproves!)", self.signature)
128 } else {
129 write!(
130 f,
131 "0x{:08X} [{}Hz ∠{}° {}% τ{}]",
132 self.signature,
133 self.to_hz() as u32,
134 (self.to_radians() * 180.0 / std::f32::consts::PI) as u32,
135 self.amplitude_percent() as u32,
136 self.torsion()
137 )
138 }
139 }
140}
141
142pub mod patterns {
144 pub const DEEP_SLEEP: u32 = 0x02050A01; pub const REM_SLEEP: u32 = 0x28B4E619; pub const FLOW_STATE: u32 = 0x73A9E2F5; pub const MEDITATION: u32 = 0x0A7F7F0A; pub const PANIC: u32 = 0xFFC8FF9B; pub const LOVE: u32 = 0x1B8D4C7A; pub const MEM8_BASELINE: u32 = 0x2C7DB5A3; pub const SMART_TREE: u32 = 0x9F2E6B31; pub const HORSE_APPLE: u32 = 0xCCCCCCCC; }
172
173#[cfg(test)]
174mod tests {
175 use super::*;
176
177 #[test]
178 fn test_quantum_signatures() {
179 let sig1 = QuantumWaveSignature::golden_ratio();
180 assert_eq!(sig1.frequency(), 162);
181 assert!(!sig1.is_horse_apple());
182
183 let horse = QuantumWaveSignature::from_raw(0xCCCCCCCC);
184 assert!(horse.is_horse_apple());
185
186 let flow = QuantumWaveSignature::from_raw(patterns::FLOW_STATE);
187 println!("Flow state: {}", flow);
188 }
189
190 #[test]
191 fn test_interference() {
192 let sig1 = QuantumWaveSignature::new(100, 0, 128, 50);
193 let sig2 = QuantumWaveSignature::new(100, 0, 128, 50);
194
195 let interference = sig1.interference(&sig2);
196 assert!(interference > 0.5); }
198}