aboutsummaryrefslogtreecommitdiff
path: root/crates/fparkan-fx/src/ambient_sound.rs
blob: bcc18149fa41bb09a3f396cc5edaa19a0db6ce69 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
//! Opcode-2 sound commands, independent of an audio device.

use crate::environment::{SoundAction, SoundEvent};
use crate::{FxCommand, FxOpcode};

/// A validated ordinary Op2 command. Modes 1/3 request an unverified backend
/// flag and are left to the caller's unsupported-command report.
#[derive(Clone, Debug)]
pub struct Op2SoundCommand {
    looping: bool,
    start: f32,
    end: f32,
    position: [f32; 3],
    position_delta: [f32; 3],
    min_distance: f32,
    max_distance: f32,
    frequency: f32,
    frequency_delta: f32,
    archive: String,
    name: String,
}

/// Playback crossing state belongs to a command instance, even when several
/// instances share the same sample.
#[derive(Clone, Debug, Default)]
pub struct Op2SoundState {
    previous_phase: f32,
}

impl Op2SoundCommand {
    /// Decodes the supported native modes 0 (once) and 2 (loop).
    #[must_use]
    pub fn from_command(command: &FxCommand) -> Option<Self> {
        let body = command.raw_body.as_ref();
        if command.opcode != FxOpcode::Op2 || body.len() != 144 {
            return None;
        }
        let mode = u32::from_le_bytes(body[..4].try_into().ok()?);
        // shortcut: backend flag 0x200 and body+76 need a native playback
        // comparison before modes 1/3 or nonzero backend parameters are enabled.
        if !matches!(mode, 0 | 2) || body[76..80] != [0; 4] {
            return None;
        }
        let float = |offset| {
            let value = f32::from_le_bytes(body[offset..offset + 4].try_into().ok()?);
            value.is_finite().then_some(value)
        };
        let vector = |offset| Some([float(offset)?, float(offset + 4)?, float(offset + 8)?]);
        let start = float(4)?;
        let end = float(8)?;
        let position = vector(12)?;
        let position_end = vector(24)?;
        let min_distance = float(60)?;
        let max_distance = float(64)?;
        let frequency = float(68)?;
        let frequency_end = float(72)?;
        let position_delta = std::array::from_fn(|axis| position_end[axis] - position[axis]);
        let frequency_delta = frequency_end - frequency;
        if end <= start
            || !(end - start).is_finite()
            || !position_delta.iter().all(|value: &f32| value.is_finite())
            || !frequency_delta.is_finite()
            || min_distance < 0.0
            || max_distance < min_distance
            || frequency <= 0.0
            || frequency_end <= 0.0
        {
            return None;
        }
        let text = |bytes: &[u8]| {
            let end = bytes
                .iter()
                .position(|&byte| byte == 0)
                .unwrap_or(bytes.len());
            (!bytes[..end].is_empty()).then(|| String::from_utf8_lossy(&bytes[..end]).into_owned())
        };
        Some(Self {
            looping: mode == 2,
            start,
            end,
            position,
            position_delta,
            min_distance,
            max_distance,
            frequency,
            frequency_delta,
            archive: text(&body[80..112])?,
            name: text(&body[112..144])?,
        })
    }

    /// Evaluates Effect.dll's phase crossings and current source parameters.
    /// `playing` comes from the audio owner, since a one-shot can finish between
    /// updates. The matrix maps local command coordinates into world space.
    #[must_use]
    pub fn update(
        &self,
        state: &mut Op2SoundState,
        phase: f32,
        world_from_effect: [[f32; 4]; 4],
        playing: bool,
    ) -> Option<SoundEvent> {
        if !phase.is_finite() || !world_from_effect.iter().flatten().all(|v| v.is_finite()) {
            return None;
        }
        let action = if self.looping {
            if phase >= self.start && phase <= self.end {
                if playing {
                    SoundAction::SetLoopVolume
                } else {
                    SoundAction::StartLoop
                }
            } else if playing {
                SoundAction::StopLoop
            } else {
                return None;
            }
        } else {
            let crossing_phase = if phase == 1.0 { 0.0 } else { phase };
            let crossed = state.previous_phase <= crossing_phase
                && state.previous_phase <= self.start
                && self.start < crossing_phase;
            state.previous_phase = crossing_phase;
            if crossed {
                SoundAction::OneShot
            } else if playing {
                SoundAction::SetLoopVolume
            } else {
                return None;
            }
        };
        let t = (phase - self.start) / (self.end - self.start);
        let local: [f32; 3] =
            std::array::from_fn(|axis| self.position[axis] + t * self.position_delta[axis]);
        let position = std::array::from_fn(|axis| {
            let row = world_from_effect[axis];
            row[0] * local[0] + row[1] * local[1] + row[2] * local[2] + row[3]
        });
        let frequency_ratio = self.frequency + t * self.frequency_delta;
        if !position.iter().all(|value| value.is_finite()) || !frequency_ratio.is_finite() {
            return None;
        }
        Some(SoundEvent {
            archive: self.archive.clone(),
            name: self.name.clone(),
            position,
            volume: 1.0,
            min_distance: self.min_distance,
            max_distance: self.max_distance,
            frequency_ratio,
            action,
        })
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::sync::Arc;

    const IDENTITY: [[f32; 4]; 4] = [
        [1.0, 0.0, 0.0, 0.0],
        [0.0, 1.0, 0.0, 0.0],
        [0.0, 0.0, 1.0, 0.0],
        [0.0, 0.0, 0.0, 1.0],
    ];

    fn command(mode: u32) -> FxCommand {
        let mut body = vec![0; 144];
        body[..4].copy_from_slice(&mode.to_le_bytes());
        for (offset, value) in [
            (4, 0.25_f32),
            (8, 0.75),
            (60, 12.0),
            (64, 80.0),
            (68, 1.0),
            (72, 2.0),
        ] {
            body[offset..offset + 4].copy_from_slice(&value.to_le_bytes());
        }
        body[80..90].copy_from_slice(b"sounds.lib");
        body[112..122].copy_from_slice(b"f_defl.wav");
        FxCommand {
            word: 2,
            opcode: FxOpcode::Op2,
            native_flag: false,
            raw_body: Arc::from(body),
            resource_refs: vec![],
        }
    }

    #[test]
    fn loop_has_inclusive_boundaries_and_updates_without_restarting() {
        let command = Op2SoundCommand::from_command(&command(2)).unwrap();
        let mut state = Op2SoundState::default();
        assert!(command.update(&mut state, 0.0, IDENTITY, false).is_none());
        assert_eq!(
            command
                .update(&mut state, 0.25, IDENTITY, false)
                .unwrap()
                .action,
            SoundAction::StartLoop
        );
        assert_eq!(
            command
                .update(&mut state, 0.75, IDENTITY, true)
                .unwrap()
                .action,
            SoundAction::SetLoopVolume
        );
        assert_eq!(
            command
                .update(&mut state, 0.8, IDENTITY, true)
                .unwrap()
                .action,
            SoundAction::StopLoop
        );
        assert!(command.update(&mut state, 0.8, IDENTITY, false).is_none());
    }

    #[test]
    fn one_shot_crossing_is_strict_and_rearms_after_phase_one() {
        let command = Op2SoundCommand::from_command(&command(0)).unwrap();
        let mut state = Op2SoundState::default();
        assert!(command.update(&mut state, 0.25, IDENTITY, false).is_none());
        assert_eq!(
            command
                .update(&mut state, 0.3, IDENTITY, false)
                .unwrap()
                .action,
            SoundAction::OneShot
        );
        assert_eq!(
            command
                .update(&mut state, 0.5, IDENTITY, true)
                .unwrap()
                .action,
            SoundAction::SetLoopVolume
        );
        assert!(command.update(&mut state, 0.6, IDENTITY, false).is_none());
        assert!(command.update(&mut state, 1.0, IDENTITY, false).is_none());
        assert_eq!(
            command
                .update(&mut state, 0.3, IDENTITY, false)
                .unwrap()
                .action,
            SoundAction::OneShot
        );
    }

    #[test]
    fn parameters_keep_native_constant_ranges_and_transform_the_position() {
        let mut source = command(2);
        let mut body = source.raw_body.to_vec();
        body[24..28].copy_from_slice(&4.0_f32.to_le_bytes());
        source.raw_body = Arc::from(body);
        let command = Op2SoundCommand::from_command(&source).unwrap();
        let mut matrix = IDENTITY;
        matrix[0] = [0.0, -1.0, 0.0, 10.0];
        matrix[1] = [2.0, 0.0, 0.0, 20.0];
        let event = command
            .update(&mut Op2SoundState::default(), 0.5, matrix, false)
            .unwrap();
        assert_eq!(event.position, [10.0, 24.0, 0.0]);
        assert_eq!(
            (
                event.min_distance,
                event.max_distance,
                event.frequency_ratio
            ),
            (12.0, 80.0, 1.5)
        );
        assert_eq!(
            (event.archive.as_str(), event.name.as_str()),
            ("sounds.lib", "f_defl.wav")
        );
    }

    #[test]
    fn backend_modes_and_malformed_commands_are_explicitly_unsupported() {
        assert!(Op2SoundCommand::from_command(&command(1)).is_none());
        let mut source = command(2);
        let mut body = source.raw_body.to_vec();
        body[8..12].copy_from_slice(&f32::NAN.to_le_bytes());
        source.raw_body = Arc::from(body);
        assert!(Op2SoundCommand::from_command(&source).is_none());
    }
}