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

psx_seq/
tables.rs

1//! Constant tables and fixed-point helpers.
2//!
3//! Every table is a closed-form function written out as integer literals,
4//! because the runtime has no floating point. The tests recompute each one
5//! from its formula, so a typo in a literal cannot survive. `psx-seq-cook
6//! gen-tables` prints them.
7
8/// See the module documentation.
9pub const PITCH_Q16: [u32; 128] = [
10    6889, 7298, 7732, 8192, 8679, 9195, 9742, 10321, 10935, 11585, 12274, 13004, 13777, 14596,
11    15464, 16384, 17358, 18390, 19484, 20643, 21870, 23170, 24548, 26008, 27554, 29193, 30929,
12    32768, 34716, 36781, 38968, 41285, 43740, 46341, 49097, 52016, 55109, 58386, 61858, 65536,
13    69433, 73562, 77936, 82570, 87480, 92682, 98193, 104032, 110218, 116772, 123715, 131072,
14    138866, 147123, 155872, 165140, 174960, 185364, 196386, 208064, 220436, 233544, 247431, 262144,
15    277732, 294247, 311744, 330281, 349920, 370728, 392772, 416128, 440872, 467088, 494862, 524288,
16    555464, 588493, 623487, 660561, 699841, 741455, 785544, 832255, 881744, 934175, 989724,
17    1048576, 1110928, 1176987, 1246974, 1321123, 1399681, 1482910, 1571089, 1664511, 1763488,
18    1868350, 1979448, 2097152, 2221855, 2353974, 2493948, 2642246, 2799362, 2965821, 3142178,
19    3329021, 3526975, 3736700, 3958896, 4194304, 4443710, 4707947, 4987896, 5284492, 5598724,
20    5931642, 6284355, 6658043, 7053950, 7473400, 7917792, 8388608, 8887421, 9415894, 9975792,
21    10568984,
22];
23
24/// See the module documentation.
25pub const BEND_OCTAVE_Q16: [u32; 256] = [
26    32768, 32768, 32947, 33128, 33309, 33491, 33675, 33859, 34044, 34230, 34418, 34606, 34796,
27    34986, 35177, 35370, 35564, 35758, 35954, 36151, 36348, 36547, 36747, 36949, 37151, 37354,
28    37558, 37764, 37971, 38178, 38387, 38598, 38809, 39021, 39235, 39449, 39665, 39882, 40101,
29    40320, 40541, 40763, 40986, 41210, 41436, 41662, 41890, 42120, 42350, 42582, 42815, 43049,
30    43285, 43522, 43760, 43999, 44240, 44482, 44726, 44971, 45217, 45464, 45713, 45963, 46215,
31    46468, 46722, 46978, 47235, 47493, 47753, 48014, 48277, 48541, 48807, 49074, 49343, 49613,
32    49884, 50157, 50432, 50708, 50985, 51264, 51545, 51827, 52111, 52396, 52683, 52971, 53261,
33    53552, 53845, 54140, 54436, 54734, 55034, 55335, 55638, 55942, 56249, 56556, 56866, 57177,
34    57490, 57805, 58121, 58439, 58759, 59080, 59404, 59729, 60056, 60384, 60715, 61047, 61381,
35    61717, 62055, 62395, 62736, 63079, 63425, 63772, 64121, 64472, 64825, 65179, 65536, 65895,
36    66255, 66618, 66982, 67349, 67718, 68088, 68461, 68836, 69212, 69591, 69972, 70355, 70740,
37    71127, 71516, 71908, 72301, 72697, 73095, 73495, 73897, 74301, 74708, 75117, 75528, 75941,
38    76357, 76775, 77195, 77618, 78042, 78469, 78899, 79331, 79765, 80201, 80640, 81082, 81525,
39    81971, 82420, 82871, 83325, 83781, 84239, 84700, 85164, 85630, 86099, 86570, 87043, 87520,
40    87999, 88480, 88965, 89452, 89941, 90433, 90928, 91426, 91926, 92429, 92935, 93444, 93955,
41    94469, 94986, 95506, 96029, 96554, 97083, 97614, 98148, 98686, 99226, 99769, 100315, 100864,
42    101416, 101971, 102529, 103090, 103654, 104221, 104792, 105365, 105942, 106522, 107105, 107691,
43    108280, 108873, 109469, 110068, 110670, 111276, 111885, 112497, 113113, 113732, 114354, 114980,
44    115609, 116242, 116878, 117518, 118161, 118808, 119458, 120112, 120769, 121430, 122094, 122763,
45    123434, 124110, 124789, 125472, 126159, 126849, 127544, 128242, 128943, 129649, 130359, 131072,
46];
47
48/// See the module documentation.
49pub const BEND_TWO_SEMITONES_Q16: [u32; 256] = [
50    58386, 58386, 58439, 58492, 58545, 58599, 58652, 58705, 58759, 58812, 58866, 58919, 58973,
51    59027, 59080, 59134, 59188, 59242, 59296, 59350, 59404, 59458, 59512, 59566, 59620, 59675,
52    59729, 59783, 59838, 59892, 59947, 60001, 60056, 60110, 60165, 60220, 60275, 60330, 60384,
53    60439, 60494, 60549, 60605, 60660, 60715, 60770, 60825, 60881, 60936, 60992, 61047, 61103,
54    61158, 61214, 61270, 61326, 61381, 61437, 61493, 61549, 61605, 61661, 61717, 61773, 61830,
55    61886, 61942, 61999, 62055, 62111, 62168, 62225, 62281, 62338, 62395, 62451, 62508, 62565,
56    62622, 62679, 62736, 62793, 62850, 62908, 62965, 63022, 63079, 63137, 63194, 63252, 63309,
57    63367, 63425, 63482, 63540, 63598, 63656, 63714, 63772, 63830, 63888, 63946, 64004, 64062,
58    64121, 64179, 64238, 64296, 64354, 64413, 64472, 64530, 64589, 64648, 64707, 64766, 64825,
59    64884, 64943, 65002, 65061, 65120, 65179, 65239, 65298, 65357, 65417, 65476, 65536, 65596,
60    65655, 65715, 65775, 65835, 65895, 65955, 66015, 66075, 66135, 66195, 66255, 66316, 66376,
61    66436, 66497, 66557, 66618, 66679, 66739, 66800, 66861, 66922, 66982, 67043, 67104, 67166,
62    67227, 67288, 67349, 67410, 67472, 67533, 67595, 67656, 67718, 67779, 67841, 67903, 67964,
63    68026, 68088, 68150, 68212, 68274, 68336, 68399, 68461, 68523, 68586, 68648, 68710, 68773,
64    68836, 68898, 68961, 69024, 69086, 69149, 69212, 69275, 69338, 69401, 69465, 69528, 69591,
65    69654, 69718, 69781, 69845, 69908, 69972, 70036, 70099, 70163, 70227, 70291, 70355, 70419,
66    70483, 70547, 70611, 70676, 70740, 70804, 70869, 70933, 70998, 71062, 71127, 71192, 71257,
67    71321, 71386, 71451, 71516, 71581, 71647, 71712, 71777, 71842, 71908, 71973, 72039, 72104,
68    72170, 72235, 72301, 72367, 72433, 72499, 72565, 72631, 72697, 72763, 72829, 72896, 72962,
69    73028, 73095, 73161, 73228, 73295, 73361, 73428, 73495, 73562,
70];
71
72/// See the module documentation.
73pub const PAN_Q15: [u16; 128] = [
74    32768, 32765, 32758, 32745, 32728, 32705, 32678, 32645, 32608, 32565, 32518, 32465, 32408,
75    32345, 32278, 32206, 32128, 32046, 31959, 31867, 31771, 31669, 31562, 31451, 31335, 31214,
76    31088, 30958, 30823, 30683, 30538, 30389, 30235, 30076, 29913, 29745, 29573, 29396, 29215,
77    29029, 28839, 28644, 28445, 28242, 28034, 27822, 27606, 27386, 27161, 26932, 26699, 26462,
78    26221, 25976, 25727, 25474, 25217, 24956, 24692, 24424, 24151, 23876, 23596, 23313, 23027,
79    22737, 22443, 22146, 21846, 21542, 21235, 20925, 20611, 20294, 19975, 19652, 19326, 18997,
80    18666, 18331, 17994, 17654, 17311, 16965, 16617, 16267, 15914, 15558, 15200, 14840, 14478,
81    14113, 13746, 13377, 13006, 12633, 12258, 11882, 11503, 11123, 10741, 10357, 9972, 9585, 9196,
82    8807, 8416, 8023, 7630, 7235, 6839, 6442, 6045, 5646, 5246, 4846, 4444, 4043, 3640, 3237, 2833,
83    2430, 2025, 1620, 1216, 810, 405, 0,
84];
85
86/// See the module documentation.
87pub const VIBRATO_SINE: [i16; 64] = [
88    0, 3211, 6392, 9511, 12539, 15446, 18204, 20787, 23169, 25329, 27244, 28897, 30272, 31356,
89    32137, 32609, 32767, 32609, 32137, 31356, 30272, 28897, 27244, 25329, 23169, 20787, 18204,
90    15446, 12539, 9511, 6392, 3211, 0, -3211, -6392, -9511, -12539, -15446, -18204, -20787, -23169,
91    -25329, -27244, -28897, -30272, -31356, -32137, -32609, -32767, -32609, -32137, -31356, -30272,
92    -28897, -27244, -25329, -23169, -20787, -18204, -15446, -12539, -9511, -6392, -3211,
93];
94
95/// Unity in Q16.
96pub const Q16_ONE: u32 = 1 << 16;
97/// Unity in Q15.
98pub const Q15_ONE: u32 = 1 << 15;
99
100/// `a * b` for two Q16 numbers, without a 64-bit intermediate. The result
101/// must fit in `u32`.
102///
103/// ```
104/// use psx_seq::tables::mul_q16;
105/// assert_eq!(mul_q16(1 << 16, 3 << 15), 3 << 15);
106/// assert_eq!(mul_q16(2 << 16, 2 << 16), 4 << 16);
107/// ```
108pub const fn mul_q16(a: u32, b: u32) -> u32 {
109    let (ah, al) = (a >> 16, a & 0xffff);
110    let (bh, bl) = (b >> 16, b & 0xffff);
111    (ah * b) + (al * bh) + ((al * bl) >> 16)
112}
113
114/// `a * b` for two Q15 numbers (each at most 1.0 or a little more).
115pub const fn mul_q15(a: u32, b: u32) -> u32 {
116    (a * b) >> 15
117}
118
119/// Per-update decrement of a releasing envelope, in Q24 (1.0 = 1 << 24), for
120/// release index `idx`. Index 0 never decays on its own. The scale is the
121/// number of 60 Hz frames a full-scale level takes to fall to zero, times
122/// three updates per frame.
123///
124/// ```
125/// use psx_seq::tables::release_step_q24;
126/// assert_eq!(release_step_q24(0), 0);
127/// // index 200 is 51 frames: 153 updates
128/// assert_eq!(release_step_q24(200), (1u32 << 24) / 153);
129/// ```
130pub const fn release_step_q24(idx: u8) -> u32 {
131    // scale in hundredths of a frame
132    let i = idx as u32;
133    let scale100: u32 = match idx {
134        0 => return 0,
135        255 => 25,
136        254 => 33,
137        253 => 50,
138        252 => 66,
139        251 => 75,
140        128..=250 => (251 - i) * 100,
141        16..=127 => 4 * (143 - i) * 100,
142        _ => 60 * (23 - i) * 100,
143    };
144    // 2^24 * 100 / (3 * scale100) with 2^24 * 100 < 2^32
145    ((1u32 << 24) * 100) / (3 * scale100)
146}
147
148/// Default envelope used by a channel before any instrument is chosen:
149/// `{delay, arg}` pairs: ramp to 32000 in one unit, hold 1000, hang.
150pub const DEFAULT_ENVELOPE: [(i16, i16); 4] = [(1, 32000), (1000, 32000), (-1, 0), (0, 0)];
151
152/// Default short-note velocity table.
153pub const DEFAULT_SHORT_VELOCITY: [u8; 16] = [
154    12, 25, 38, 51, 57, 64, 71, 76, 83, 89, 96, 102, 109, 115, 121, 127,
155];
156
157/// Default short-note gate table.
158pub const DEFAULT_SHORT_GATE: [u8; 16] = [
159    229, 203, 177, 151, 139, 126, 113, 100, 87, 74, 61, 48, 36, 23, 10, 0,
160];
161
162#[cfg(test)]
163mod tests {
164    use super::*;
165
166    fn q16(x: f64) -> u32 {
167        (x * 65536.0).round() as u32
168    }
169
170    #[test]
171    fn pitch_table_is_equal_tempered_around_c4() {
172        for (i, &v) in PITCH_Q16.iter().enumerate() {
173            assert_eq!(v, q16(2f64.powf((i as f64 - 39.0) / 12.0)), "semitone {i}");
174        }
175        assert_eq!(PITCH_Q16[39], Q16_ONE);
176        assert_eq!(PITCH_Q16[51], 2 * Q16_ONE);
177    }
178
179    #[test]
180    fn bend_tables_span_their_range() {
181        assert_eq!(BEND_OCTAVE_Q16[0], Q16_ONE / 2);
182        assert_eq!(BEND_OCTAVE_Q16[128], Q16_ONE);
183        assert_eq!(BEND_OCTAVE_Q16[255], 2 * Q16_ONE);
184        for (i, &v) in BEND_OCTAVE_Q16.iter().enumerate().skip(1) {
185            assert_eq!(
186                v,
187                q16(0.5 * 2f64.powf((i as f64 - 1.0) / 127.0)),
188                "octave {i}"
189            );
190        }
191        assert_eq!(BEND_TWO_SEMITONES_Q16[0], BEND_TWO_SEMITONES_Q16[1]);
192        assert_eq!(BEND_TWO_SEMITONES_Q16[128], Q16_ONE);
193        assert_eq!(BEND_TWO_SEMITONES_Q16[255], q16(2f64.powf(2.0 / 12.0)));
194    }
195
196    #[test]
197    fn pan_law_is_a_quarter_cosine() {
198        assert_eq!(PAN_Q15[0], 32768);
199        assert_eq!(PAN_Q15[127], 0);
200        // equal power at the centre (index 64 is the cosine of a bit past 45 degrees)
201        let (l, r) = (PAN_Q15[64] as u64, PAN_Q15[127 - 64] as u64);
202        assert!((l * l + r * r).abs_diff(1 << 30) < (1 << 24));
203    }
204
205    #[test]
206    fn vibrato_sine_is_one_period_in_64_steps() {
207        assert_eq!(VIBRATO_SINE[0], 0);
208        assert_eq!(VIBRATO_SINE[16], 32767);
209        assert_eq!(VIBRATO_SINE[48], -32767);
210        for (i, &v) in VIBRATO_SINE.iter().enumerate() {
211            let want = (32767.0 * (core::f64::consts::TAU * i as f64 / 64.0).sin()).trunc() as i16;
212            assert_eq!(v, want, "step {i}");
213        }
214    }
215
216    #[test]
217    fn release_steps_fall_with_the_index() {
218        // a smaller index is a longer release
219        assert!(release_step_q24(1) < release_step_q24(16));
220        assert!(release_step_q24(16) < release_step_q24(128));
221        assert!(release_step_q24(128) < release_step_q24(250));
222        assert!(release_step_q24(250) < release_step_q24(255));
223        // 255 releases in under a single update
224        assert!(release_step_q24(255) > 1 << 24);
225    }
226
227    #[test]
228    fn mul_q16_matches_wide_arithmetic() {
229        for &(a, b) in &[
230            (0u32, 5u32),
231            (65536, 65536),
232            (123456, 98765),
233            (6889, 1_000_000),
234            (65535, 131071),
235        ] {
236            let want = ((a as u64 * b as u64) >> 16) as u32;
237            let got = mul_q16(a, b);
238            assert!(want.abs_diff(got) <= 2, "{a} * {b}: {want} vs {got}");
239        }
240    }
241}