Skip to main content
← PSoXide SDK guides and examples · SDK 80d1c329e977 · All Cargo features enabled; check feature gates before use.

psx_seq/
types.rs

1//! State records for the three levels and the note table.
2//!
3//! Everything is `Copy` plain data in fixed arrays, so a [`Sequencer`] can
4//! live in a `static` and its size is known at compile time.
5//!
6//! [`Sequencer`]: crate::Sequencer
7
8use crate::format::NONE16;
9
10/// "No note", "no layer" and similar.
11pub(crate) const NONE8: u8 = 0xff;
12/// "No owner" for a note.
13pub(crate) const NO_OWNER: u16 = 0xffff;
14/// IO port value meaning "never written".
15pub const IO_NONE: u8 = 0xff;
16/// Channels per player.
17pub const CHANNELS: usize = 16;
18/// Layers per channel.
19pub const LAYERS: usize = 4;
20/// IO ports per player and per channel.
21pub const IO_PORTS: usize = 8;
22
23/// Bit in [`Player::mute_behavior`]: lower volume while muted.
24pub(crate) const MUTE_SOFTEN: u8 = 0x20;
25/// Bit: stop running the sequence script while muted.
26pub(crate) const MUTE_STOP_SCRIPT: u8 = 0x80;
27/// Bit: stop sounding notes while muted.
28pub(crate) const MUTE_STOP_NOTES: u8 = 0x40;
29/// Bit: silence notes while muted.
30pub(crate) const MUTE_SILENCE: u8 = 0x08;
31/// Bit: skip notes in layer scripts while muted.
32pub(crate) const MUTE_SKIP_LAYER: u8 = 0x10;
33
34/// One of the three scripts' execution state.
35#[derive(Clone, Copy, Debug)]
36pub(crate) struct Script {
37    pub pc: u16,
38    pub depth: u8,
39    pub stack: [u16; 4],
40    pub loops: [u8; 4],
41    pub value: i8,
42}
43
44impl Script {
45    pub const INIT: Self = Self {
46        pc: 0,
47        depth: 0,
48        stack: [0; 4],
49        loops: [0; 4],
50        value: 0,
51    };
52}
53
54/// Where an envelope's points live.
55#[derive(Clone, Copy, Debug, PartialEq, Eq)]
56pub(crate) enum EnvRef {
57    /// The built-in default.
58    Default,
59    /// Byte offset into the song's bytecode.
60    Song(u16),
61    /// Word index into the bank.
62    Bank(u16),
63}
64
65/// An instrument chosen by a channel or layer.
66#[derive(Clone, Copy, Debug, PartialEq, Eq)]
67pub(crate) struct InstSel {
68    pub font: u8,
69    pub id: u8,
70}
71
72impl InstSel {
73    pub const NONE: Self = Self {
74        font: NONE8,
75        id: NONE8,
76    };
77    pub fn is_none(self) -> bool {
78        self.id == NONE8
79    }
80}
81
82/// A sample with its tuning, as a layer holds it.
83#[derive(Clone, Copy, Debug, PartialEq, Eq)]
84pub(crate) struct TunedRef {
85    pub sample: u16,
86    pub tuning_q16: u32,
87}
88
89impl TunedRef {
90    pub const NONE: Self = Self {
91        sample: NONE16,
92        tuning_q16: 0,
93    };
94}
95
96/// Portamento settings of a layer (copied into a note when it starts).
97#[derive(Clone, Copy, Debug)]
98pub(crate) struct Porta {
99    /// 0 off; bit 7 selects the tempo-relative speed; low bits are the mode.
100    pub mode: u8,
101    pub extent_q16: i32,
102    pub speed: u16,
103    pub cur: u16,
104}
105
106impl Porta {
107    pub const OFF: Self = Self {
108        mode: 0,
109        extent_q16: 0,
110        speed: 0,
111        cur: 0,
112    };
113}
114
115/// Fields of a channel that changed since the last property update.
116#[derive(Clone, Copy, Debug)]
117pub(crate) struct Changes {
118    pub volume: bool,
119    pub pan: bool,
120    pub freq: bool,
121}
122
123impl Changes {
124    pub const fn any(self) -> bool {
125        self.volume || self.pan || self.freq
126    }
127
128    pub const ALL: Self = Self {
129        volume: true,
130        pan: true,
131        freq: true,
132    };
133    pub const NONE: Self = Self {
134        volume: false,
135        pan: false,
136        freq: false,
137    };
138}
139
140/// A sequence channel.
141#[derive(Clone, Copy, Debug)]
142pub(crate) struct Channel {
143    pub enabled: bool,
144    pub dirty: bool,
145    pub muted: bool,
146    pub finished: bool,
147    pub stop_script: bool,
148    pub has_instrument: bool,
149    pub large_notes: bool,
150    pub script: Script,
151    pub delay: u16,
152    pub font: u8,
153    pub inst_or_wave: u8,
154    pub inst: InstSel,
155    pub transpose: i8,
156    pub mute_behavior: u8,
157    pub note_alloc_policy: u8,
158    pub env: EnvRef,
159    pub decay_idx: u8,
160    pub sustain: u8,
161    pub volume_q15: u16,
162    pub volume_scale_q15: u16,
163    pub applied_volume_q15: u16,
164    pub new_pan: u8,
165    pub pan_weight: u8,
166    pub pan: u16,
167    pub freq_q16: u32,
168    pub changes: Changes,
169    pub vib_rate_target: u16,
170    pub vib_rate_start: u16,
171    pub vib_depth_target: u16,
172    pub vib_depth_start: u16,
173    pub vib_rate_delay: u16,
174    pub vib_depth_delay: u16,
175    pub vib_delay: u16,
176    pub reverb: u8,
177    pub note_priority: u8,
178    pub low_priority: u8,
179    pub vel_variance: u8,
180    pub gate_variance: u8,
181    pub io: [u8; IO_PORTS],
182    pub ptr: u16,
183    /// Start of the table `dyncall` indexes (a list of big-endian script offsets).
184    pub dyn_table: u16,
185    pub layers: [Layer; LAYERS],
186}
187
188impl Channel {
189    pub const INIT: Self = Self {
190        enabled: false,
191        dirty: true,
192        muted: false,
193        finished: false,
194        stop_script: false,
195        has_instrument: false,
196        large_notes: false,
197        script: Script::INIT,
198        delay: 0,
199        font: NONE8,
200        inst_or_wave: 0,
201        inst: InstSel::NONE,
202        transpose: 0,
203        mute_behavior: 0,
204        note_alloc_policy: 0,
205        env: EnvRef::Default,
206        decay_idx: 0xf0,
207        sustain: 0,
208        volume_q15: 32768,
209        volume_scale_q15: 32768,
210        applied_volume_q15: 0,
211        new_pan: 0x40,
212        pan_weight: 0x80,
213        pan: 0,
214        freq_q16: 1 << 16,
215        changes: Changes::ALL,
216        vib_rate_target: 0x800,
217        vib_rate_start: 0x800,
218        vib_depth_target: 0,
219        vib_depth_start: 0,
220        vib_rate_delay: 0,
221        vib_depth_delay: 0,
222        vib_delay: 0,
223        reverb: 0,
224        note_priority: 3,
225        low_priority: 1,
226        vel_variance: 0,
227        gate_variance: 0,
228        io: [IO_NONE; IO_PORTS],
229        ptr: 0,
230        dyn_table: 0,
231        layers: [Layer::INIT; LAYERS],
232    };
233}
234
235/// A layer: one voice's worth of notes within a channel.
236#[derive(Clone, Copy, Debug)]
237pub(crate) struct Layer {
238    pub alloc: bool,
239    pub enabled: bool,
240    pub finished: bool,
241    pub muted: bool,
242    pub continuous: bool,
243    pub bit1: bool,
244    pub bit3: bool,
245    pub ignore_drum_pan: bool,
246    pub props_need_init: bool,
247    pub script: Script,
248    pub delay: u16,
249    pub gate_delay: i16,
250    pub gate_time: u8,
251    pub last_delay: u16,
252    pub short_delay: u16,
253    pub pan: u8,
254    pub transpose: i8,
255    pub vel_sq_q15: u16,
256    pub vel_sq2_q15: u16,
257    pub inst_or_wave: u8,
258    pub inst: InstSel,
259    pub env: EnvRef,
260    pub decay_idx: u8,
261    pub tuned: TunedRef,
262    pub freq_q16: u32,
263    pub bend_q16: u32,
264    pub porta: Porta,
265    pub porta_target: u8,
266    pub porta_time: u16,
267    pub note: u8,
268    pub note_freq_q16: u32,
269    pub note_vel_q15: u16,
270    pub note_pan: u8,
271}
272
273impl Layer {
274    pub const INIT: Self = Self {
275        alloc: false,
276        enabled: false,
277        finished: false,
278        muted: false,
279        continuous: false,
280        bit1: false,
281        bit3: false,
282        ignore_drum_pan: false,
283        props_need_init: false,
284        script: Script::INIT,
285        delay: 0,
286        gate_delay: 0,
287        gate_time: 0x80,
288        last_delay: 0,
289        short_delay: 0,
290        pan: 0x40,
291        transpose: 0,
292        vel_sq_q15: 0,
293        vel_sq2_q15: 0,
294        inst_or_wave: 0xff,
295        inst: InstSel::NONE,
296        env: EnvRef::Default,
297        decay_idx: 0,
298        tuned: TunedRef::NONE,
299        freq_q16: 1 << 16,
300        bend_q16: 1 << 16,
301        porta: Porta::OFF,
302        porta_target: 0,
303        porta_time: 0,
304        note: NONE8,
305        note_freq_q16: 1 << 16,
306        note_vel_q15: 0,
307        note_pan: 0x40,
308    };
309}
310
311/// Envelope states.
312#[derive(Clone, Copy, Debug, PartialEq, Eq)]
313pub(crate) enum EnvState {
314    Disabled,
315    Initial,
316    Loop,
317    Fade,
318    Hang,
319    Decay,
320    Release,
321    Sustain,
322}
323
324/// A note's envelope.
325#[derive(Clone, Copy, Debug)]
326pub(crate) struct Adsr {
327    pub player: u8,
328    pub state: EnvState,
329    pub env: EnvRef,
330    pub index: u8,
331    pub delay: i16,
332    pub target: i32,
333    pub velocity: i32,
334    pub current: i32,
335    pub sustain: i32,
336    pub fade_out: i32,
337    pub want_decay: bool,
338    pub want_release: bool,
339}
340
341impl Adsr {
342    pub const INIT: Self = Self {
343        player: 0,
344        state: EnvState::Disabled,
345        env: EnvRef::Default,
346        index: 0,
347        delay: 0,
348        target: 0,
349        velocity: 0,
350        current: 0,
351        sustain: 0,
352        fade_out: 0,
353        want_decay: false,
354        want_release: false,
355    };
356}
357
358/// A note's vibrato.
359#[derive(Clone, Copy, Debug)]
360pub(crate) struct Vibrato {
361    pub active: bool,
362    /// Driven by the game (a live note), not by a channel's targets.
363    pub live: bool,
364    pub time: u32,
365    /// Q8.
366    pub depth: u32,
367    /// Q8.
368    pub rate: u32,
369    pub delay: u16,
370    pub depth_timer: u16,
371    pub rate_timer: u16,
372    /// `player << 4 | channel` of the channel whose targets it follows.
373    pub chan: u8,
374}
375
376impl Vibrato {
377    pub const INIT: Self = Self {
378        active: false,
379        live: false,
380        time: 0,
381        depth: 0,
382        rate: 0,
383        delay: 0,
384        depth_timer: 0,
385        rate_timer: 0,
386        chan: 0,
387    };
388}
389
390/// Which list a note is on.
391#[derive(Clone, Copy, Debug, PartialEq, Eq)]
392pub(crate) enum NoteList {
393    Disabled,
394    Active,
395    Decaying,
396    Releasing,
397}
398
399/// A note: one SPU voice's current assignment.
400#[derive(Clone, Copy, Debug)]
401pub(crate) struct Note {
402    pub list: NoteList,
403    /// Started by the game through `note_on`, not by a layer.
404    pub live: bool,
405    /// Bumped each time a live note starts, so a stale handle is harmless.
406    pub live_gen: u8,
407    /// Release rate index of a live note's instrument.
408    pub live_decay: u8,
409    /// 0 normal, 1 decaying, 2 releasing.
410    pub phase: u8,
411    pub priority: u8,
412    pub parent: u16,
413    pub wanted: u16,
414    pub prev_parent: u16,
415    pub pos: i32,
416    pub adsr: Adsr,
417    pub vib: Vibrato,
418    pub vib_freq_q16: u32,
419    pub porta: Porta,
420    pub porta_freq_q16: u32,
421    pub tuned: TunedRef,
422    pub font: u8,
423    /// Attributes saved when the layer lets go of the note.
424    pub saved_freq_q16: u32,
425    pub saved_vel_q15: u16,
426    pub saved_pan: u8,
427    pub saved_reverb: u8,
428    pub reverb: u8,
429    // hardware side
430    pub sample_addr: u32,
431    pub sample_rate: u16,
432    pub sample_blocks: u16,
433    pub sample_looped: bool,
434    pub needs_key_on: bool,
435    pub keyed: bool,
436    pub finished: bool,
437    pub remaining_q8: u32,
438    pub last_l: i16,
439    pub last_r: i16,
440    pub last_pitch: u16,
441}
442
443impl Note {
444    pub const INIT: Self = Self {
445        list: NoteList::Disabled,
446        live: false,
447        live_gen: 0,
448        live_decay: 0,
449        phase: 0,
450        priority: 0,
451        parent: NO_OWNER,
452        wanted: NO_OWNER,
453        prev_parent: NO_OWNER,
454        pos: 0,
455        adsr: Adsr::INIT,
456        vib: Vibrato::INIT,
457        vib_freq_q16: 1 << 16,
458        porta: Porta::OFF,
459        porta_freq_q16: 1 << 16,
460        tuned: TunedRef::NONE,
461        font: NONE8,
462        saved_freq_q16: 1 << 16,
463        saved_vel_q15: 0,
464        saved_pan: 0x40,
465        saved_reverb: 0,
466        reverb: 0,
467        sample_addr: 0,
468        sample_rate: 0,
469        sample_blocks: 0,
470        sample_looped: false,
471        needs_key_on: false,
472        keyed: false,
473        finished: false,
474        remaining_q8: 0,
475        last_l: 0,
476        last_r: 0,
477        last_pitch: 0,
478    };
479}
480
481/// A sequence player.
482#[derive(Clone, Copy, Debug)]
483pub(crate) struct Player {
484    pub enabled: bool,
485    pub finished: bool,
486    pub muted: bool,
487    pub stop_script: bool,
488    pub script: Script,
489    pub delay: u16,
490    pub tempo: u16,
491    pub tempo_acc: u16,
492    pub tempo_change: i16,
493    pub transpose: i8,
494    pub note_alloc_policy: u8,
495    pub mute_behavior: u8,
496    pub mute_scale_q15: u16,
497    pub default_font: u8,
498    pub fonts: [u8; 8],
499    pub n_fonts: u8,
500    pub short_vel: Option<u16>,
501    pub short_gate: Option<u16>,
502    pub io: [u8; IO_PORTS],
503    /// 0 idle, 1 start, 2 stop.
504    pub state: u8,
505    pub fade_q24: i32,
506    pub fade_vel_q24: i32,
507    pub fade_timer: u16,
508    pub fade_timer_default: u16,
509    pub fade_scale_q15: u16,
510    pub applied_fade_q15: u16,
511    pub recalc_volume: bool,
512    pub bend_q16: u32,
513    pub script_counter: u16,
514    pub channels: [Channel; CHANNELS],
515}
516
517impl Player {
518    /// Write a player at its start values into `slot`, field by field, so the
519    /// seven kilobytes of it are never built on the stack first.
520    ///
521    /// # Safety
522    ///
523    /// `slot` must be valid for writes of a `Player` and aligned for one; any
524    /// player it held is overwritten without being read.
525    pub(crate) unsafe fn init_at(slot: *mut Self) {
526        use core::ptr::addr_of_mut;
527        macro_rules! put {
528            ($field:ident = $value:expr) => {
529                addr_of_mut!((*slot).$field).write($value)
530            };
531        }
532        // SAFETY: the caller guarantees `slot` is valid and aligned; each field is written once,
533        // through a raw place, without forming a reference to the uninitialised player.
534        unsafe {
535            put!(enabled = false);
536            put!(finished = false);
537            put!(muted = false);
538            put!(stop_script = false);
539            put!(script = Script::INIT);
540            put!(delay = 0);
541            put!(tempo = 120 * 48);
542            put!(tempo_acc = 0);
543            put!(tempo_change = 0);
544            put!(transpose = 0);
545            put!(note_alloc_policy = 0);
546            put!(mute_behavior = MUTE_SOFTEN | MUTE_STOP_NOTES);
547            put!(mute_scale_q15 = 16384);
548            put!(default_font = NONE8);
549            put!(fonts = [0; 8]);
550            put!(n_fonts = 0);
551            put!(short_vel = None);
552            put!(short_gate = None);
553            put!(io = [IO_NONE; IO_PORTS]);
554            put!(state = 1);
555            put!(fade_q24 = 1 << 24);
556            put!(fade_vel_q24 = 0);
557            put!(fade_timer = 0);
558            put!(fade_timer_default = 0);
559            put!(fade_scale_q15 = 32768);
560            put!(applied_fade_q15 = 32768);
561            put!(recalc_volume = true);
562            put!(bend_q16 = 1 << 16);
563            put!(script_counter = 0);
564            for c in 0..CHANNELS {
565                addr_of_mut!((*slot).channels[c]).write(Channel::INIT);
566            }
567        }
568    }
569}