pyxel-engine 2.6.8

Core engine for Pyxel, a retro game engine for Python
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
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
use std::sync::OnceLock;

use blip_buf::BlipBuf;

const A4_MIDI_NOTE: f32 = 69.0;
const A4_FREQUENCY: f32 = 440.0;
const VOICE_GAIN_SHIFT: u32 = 14;
const VOICE_GAIN_SCALE: i64 = 1_i64 << VOICE_GAIN_SHIFT;
const VOICE_GAIN_ROUNDING: i64 = 1_i64 << (VOICE_GAIN_SHIFT - 1);
const PITCH_LUT_MIN_SEMITONE: f32 = -96.0;
const PITCH_LUT_MAX_SEMITONE: f32 = 96.0;
const PITCH_LUT_STEPS_PER_SEMITONE: usize = 64;
const PITCH_LUT_SIZE: usize =
    ((PITCH_LUT_MAX_SEMITONE - PITCH_LUT_MIN_SEMITONE) as usize * PITCH_LUT_STEPS_PER_SEMITONE) + 1;
static PITCH_RATIO_LUT: OnceLock<Box<[f32]>> = OnceLock::new();

fn pitch_ratio_lut() -> &'static [f32] {
    PITCH_RATIO_LUT
        .get_or_init(|| {
            (0..PITCH_LUT_SIZE)
                .map(|index| {
                    let semitone_offset =
                        PITCH_LUT_MIN_SEMITONE + index as f32 / PITCH_LUT_STEPS_PER_SEMITONE as f32;
                    2.0_f32.powf(semitone_offset / 12.0)
                })
                .collect::<Vec<_>>()
                .into_boxed_slice()
        })
        .as_ref()
}

fn semitone_to_pitch_multiplier(semitone_offset: f32) -> f32 {
    if !(PITCH_LUT_MIN_SEMITONE..=PITCH_LUT_MAX_SEMITONE).contains(&semitone_offset) {
        return 2.0_f32.powf(semitone_offset / 12.0);
    }

    let index = (semitone_offset - PITCH_LUT_MIN_SEMITONE) * PITCH_LUT_STEPS_PER_SEMITONE as f32;
    let left_index = index as usize;
    let frac = index - left_index as f32;
    let lut = pitch_ratio_lut();
    let left = lut[left_index];

    if frac <= 0.0 || left_index + 1 >= lut.len() {
        left
    } else {
        left + (lut[left_index + 1] - left) * frac
    }
}

pub struct Oscillator {
    waveform_samples: Vec<i16>,
    waveform_index: usize,

    lfsr: u16,
    tap_bit: u8,

    sample: i32,
}

impl Oscillator {
    fn new() -> Self {
        Self {
            waveform_samples: Vec::new(),
            waveform_index: 0,

            lfsr: 0,
            tap_bit: 0,

            sample: 0,
        }
    }

    pub fn set(&mut self, waveform: &[f32]) {
        assert!(!waveform.is_empty());

        let quantized_samples: Vec<i16> = waveform
            .iter()
            .map(|&sample| Self::quantize_sample(sample))
            .collect();

        if quantized_samples != self.waveform_samples {
            self.waveform_samples = quantized_samples;
            self.waveform_index = 0;
        }

        self.tap_bit = 0;

        self.update();
    }

    pub fn set_noise(&mut self, short_period: bool) {
        let tap_bit = if short_period { 6 } else { 1 };
        if tap_bit != self.tap_bit {
            self.lfsr = if short_period { 0x0201 } else { 0x7001 };
            self.tap_bit = tap_bit;
        }
        // The LFSR originally starts with 0x0001, but to avoid leading zeros, it is advanced first.
        // For short-period noise, it's shifted 15 times (half of the 32-sample period), resulting in 0x0201.
        // For long-period noise, it's shifted 45 times (half of the 93-sample period), resulting in 0x7001.

        if !self.waveform_samples.is_empty() {
            self.waveform_samples.clear();
        }

        self.update();
    }

    fn sample(&self) -> i32 {
        self.sample
    }

    fn cycle_resolution(&self) -> u32 {
        if self.tap_bit == 0 {
            self.waveform_samples.len() as u32
        } else {
            1
        }
    }

    fn advance_sample(&mut self) {
        if self.tap_bit == 0 {
            self.waveform_index += 1;
            if self.waveform_index >= self.waveform_samples.len() {
                self.waveform_index = 0;
            }
        } else {
            let feedback = (self.lfsr ^ (self.lfsr >> self.tap_bit)) & 1;
            self.lfsr = ((self.lfsr >> 1) | (feedback << 14)) & 0x7FFF;
        }

        self.update();
    }

    fn update(&mut self) {
        self.sample = if self.tap_bit == 0 {
            self.waveform_samples[self.waveform_index] as i32
        } else if (self.lfsr & 1) == 0 {
            i16::MAX as i32
        } else {
            -(i16::MAX as i32)
        };
    }

    fn quantize_sample(sample: f32) -> i16 {
        ((sample * i16::MAX as f32).round() as i32).clamp(i16::MIN as i32, i16::MAX as i32) as i16
    }
}

#[derive(Debug)]
struct EnvelopeSegment {
    start_tick: u32,
    start_level: f32,
    slope: f32,
}

pub struct Envelope {
    segments: Vec<EnvelopeSegment>,
    segment_index: usize,

    enabled: bool,
    elapsed_ticks: u32,
    level: f32,
}

impl Envelope {
    fn new() -> Self {
        Self {
            segments: Vec::new(),
            segment_index: 0,
            enabled: false,
            elapsed_ticks: 0,
            level: 0.0,
        }
    }

    pub fn set(&mut self, initial_level: f32, segments: &[(u32, f32)]) {
        self.segments.clear();

        let mut start_tick = 0;
        let mut start_level = initial_level;

        for &(duration, target_level) in segments {
            let slope = if duration > 0 {
                (target_level - start_level) / duration as f32
            } else {
                0.0
            };

            self.segments.insert(
                0,
                EnvelopeSegment {
                    start_tick,
                    start_level,
                    slope,
                },
            );

            start_tick += duration;
            start_level = target_level;
        }

        self.segments.insert(
            0,
            EnvelopeSegment {
                start_tick,
                start_level,
                slope: 0.0,
            },
        );

        self.segment_index = self.segments.len() - 1;
        while self.segment_index > 0
            && self.elapsed_ticks >= self.segments[self.segment_index - 1].start_tick
        {
            self.segment_index -= 1;
        }
    }

    pub fn enable(&mut self) {
        self.enabled = true;
    }

    pub fn disable(&mut self) {
        self.enabled = false;
    }

    fn level(&self) -> f32 {
        self.level
    }

    pub fn reset_tick(&mut self) {
        self.elapsed_ticks = 0;
        self.segment_index = self.segments.len() - 1;
        self.update();
    }

    fn advance_tick(&mut self, ticks: u32) {
        if self.enabled {
            self.elapsed_ticks += ticks;
        }

        self.update();
    }

    fn update(&mut self) {
        if !self.enabled {
            self.level = 1.0;
            return;
        }

        while self.segment_index > 0
            && self.elapsed_ticks >= self.segments[self.segment_index - 1].start_tick
        {
            self.segment_index -= 1;
        }

        let segment = &self.segments[self.segment_index];
        self.level = if segment.slope == 0.0 {
            segment.start_level
        } else {
            segment.start_level + segment.slope * (self.elapsed_ticks - segment.start_tick) as f32
        };
    }
}

pub struct Vibrato {
    delay_ticks: u32,
    period_ticks: u32,
    inv_period_ticks: f32,
    semitone_depth: f32,

    enabled: bool,
    elapsed_ticks: u32,
    pitch_multiplier: f32,
}

impl Vibrato {
    fn new() -> Self {
        Self {
            delay_ticks: 0,
            period_ticks: 1,
            inv_period_ticks: 0.0,
            semitone_depth: 0.0,

            enabled: false,
            elapsed_ticks: 0,
            pitch_multiplier: 1.0,
        }
    }

    pub fn set(&mut self, delay_ticks: u32, period_ticks: u32, semitone_depth: f32) {
        self.delay_ticks = delay_ticks;
        self.semitone_depth = semitone_depth;

        if period_ticks != self.period_ticks {
            self.period_ticks = period_ticks;
            self.inv_period_ticks = if period_ticks > 0 {
                1.0 / period_ticks as f32
            } else {
                0.0
            };
        }
    }

    pub fn enable(&mut self) {
        self.enabled = true;
    }

    pub fn disable(&mut self) {
        self.enabled = false;
    }

    fn pitch_multiplier(&self) -> f32 {
        self.pitch_multiplier
    }

    fn reset_tick(&mut self) {
        if self.delay_ticks > 0 {
            self.elapsed_ticks = 0;
        }

        self.update();
    }

    fn advance_tick(&mut self, ticks: u32) {
        if self.enabled {
            self.elapsed_ticks += ticks;
        }

        self.update();
    }

    fn update(&mut self) {
        if !self.enabled || self.elapsed_ticks < self.delay_ticks {
            self.pitch_multiplier = 1.0;
            return;
        }

        let phase = (self.elapsed_ticks - self.delay_ticks) as f32 * self.inv_period_ticks;
        let modulation = 1.0 - 4.0 * ((phase + 0.25).fract() - 0.5).abs();
        let semitone_offset = modulation * self.semitone_depth;

        self.pitch_multiplier = semitone_to_pitch_multiplier(semitone_offset);
    }
}

pub struct Glide {
    semitone_offset: f32,
    duration_ticks: u32,
    semitone_slope: f32,

    enabled: bool,
    elapsed_ticks: u32,
    pitch_multiplier: f32,
}

impl Glide {
    fn new() -> Self {
        Self {
            semitone_offset: 0.0,
            duration_ticks: 0,
            semitone_slope: 0.0,

            enabled: false,
            elapsed_ticks: 0,
            pitch_multiplier: 1.0,
        }
    }

    pub fn set(&mut self, semitone_offset: f32, duration_ticks: u32) {
        if semitone_offset != self.semitone_offset || duration_ticks != self.duration_ticks {
            self.semitone_offset = semitone_offset;
            self.duration_ticks = duration_ticks;
            self.semitone_slope = if duration_ticks > 0 {
                -semitone_offset / duration_ticks as f32
            } else {
                0.0
            };
        }
    }

    pub fn enable(&mut self) {
        self.enabled = true;
    }

    pub fn disable(&mut self) {
        self.enabled = false;
    }

    fn pitch_multiplier(&self) -> f32 {
        self.pitch_multiplier
    }

    fn reset_tick(&mut self) {
        self.elapsed_ticks = 0;
        self.update();
    }

    fn advance_tick(&mut self, ticks: u32) {
        if self.enabled {
            self.elapsed_ticks += ticks;
        }

        self.update();
    }

    fn update(&mut self) {
        if !self.enabled || self.elapsed_ticks >= self.duration_ticks {
            self.pitch_multiplier = 1.0;
            return;
        }

        let semitone_offset =
            self.semitone_offset + self.semitone_slope * self.elapsed_ticks as f32;
        self.pitch_multiplier = semitone_to_pitch_multiplier(semitone_offset);
    }
}

pub struct Voice {
    pub oscillator: Oscillator,
    pub envelope: Envelope,
    pub vibrato: Vibrato,
    pub glide: Glide,

    clock_rate: u32,
    clocks_per_tick: u32,
    base_frequency: f32,
    velocity: f32,
    remaining_note_clocks: u32,
    elapsed_note_clocks: u32,
    sample_clocks: u32,
    carryover_sample_clocks: u32,
    control_interval_clocks: u32,
    control_elapsed_clocks: u32,
    last_amplitude: i32,

    note_interp_clocks: u32,
    interp_start_gain: Option<i32>,
    interp_end_gain: Option<i32>,
    last_gain: i32,
}

impl Voice {
    pub fn new(clock_rate: u32, control_rate: u32, note_interp_clocks: u32) -> Self {
        assert!(clock_rate > 0 && control_rate > 0 && note_interp_clocks > 0);
        let _ = pitch_ratio_lut();

        let control_interval_clocks = clock_rate / control_rate;

        Self {
            oscillator: Oscillator::new(),
            envelope: Envelope::new(),
            vibrato: Vibrato::new(),
            glide: Glide::new(),

            clock_rate,
            clocks_per_tick: 1,
            base_frequency: 0.0,
            velocity: 0.0,
            remaining_note_clocks: 0,
            elapsed_note_clocks: 0,
            sample_clocks: 0,
            carryover_sample_clocks: 0,
            control_interval_clocks,
            control_elapsed_clocks: 0,
            last_amplitude: 0,

            note_interp_clocks,
            interp_start_gain: None,
            interp_end_gain: None,
            last_gain: 0,
        }
    }

    pub fn set_clocks_per_tick(&mut self, clocks_per_tick: u32) {
        assert!(clocks_per_tick > 0);

        self.clocks_per_tick = clocks_per_tick;
    }

    pub fn play_note(&mut self, midi_note: f32, velocity: f32, duration_clocks: u32) {
        self.base_frequency = A4_FREQUENCY * ((midi_note - A4_MIDI_NOTE) / 12.0).exp2();
        self.velocity = velocity;
        self.remaining_note_clocks = duration_clocks + self.note_interp_clocks;
        self.elapsed_note_clocks = 0;
        self.interp_start_gain = None;
        self.interp_end_gain = None;

        self.reset_control_clock();
    }

    pub fn cancel_note(&mut self) {
        self.remaining_note_clocks = self.remaining_note_clocks.min(self.note_interp_clocks);
    }

    pub(crate) fn needs_processing(&self) -> bool {
        self.remaining_note_clocks > 0
            || self.carryover_sample_clocks > 0
            || self.last_amplitude != 0
    }

    pub fn process(&mut self, blip_buf: Option<&mut BlipBuf>, clock_offset: u32, clock_count: u32) {
        if clock_count == 0 {
            return;
        }

        let mut blip_buf = blip_buf;
        let mut clock_offset = clock_offset + self.carryover_sample_clocks;
        let mut clock_count = clock_count;

        if self.carryover_sample_clocks > 0 {
            let process_clocks = self.carryover_sample_clocks.min(clock_count);
            self.remaining_note_clocks = self.remaining_note_clocks.saturating_sub(process_clocks);
            self.elapsed_note_clocks += process_clocks;
            self.carryover_sample_clocks -= process_clocks;
            clock_count -= process_clocks;

            if self.carryover_sample_clocks > 0 {
                return;
            }

            self.oscillator.advance_sample();
            self.advance_control_clock(self.sample_clocks);
        }

        while self.remaining_note_clocks > 0 && clock_count > 0 {
            // Calculate amplitude and write sample
            let mut gain = Self::gain_to_fixed(self.envelope.level() * self.velocity);

            if self.remaining_note_clocks < self.note_interp_clocks {
                if self.interp_end_gain.is_none() {
                    self.interp_end_gain = Some(self.last_gain);
                }
                let interp_end_gain = self.interp_end_gain.unwrap();
                gain = ((interp_end_gain as i64 * self.remaining_note_clocks as i64
                    + self.note_interp_clocks as i64 / 2)
                    / self.note_interp_clocks as i64) as i32;
            } else if self.elapsed_note_clocks < self.note_interp_clocks {
                if self.interp_start_gain.is_none() {
                    self.interp_start_gain = Some(self.last_gain);
                }
                let interp_start_gain = self.interp_start_gain.unwrap();
                let note_interp_clocks = self.note_interp_clocks as i64;
                let elapsed_note_clocks = self.elapsed_note_clocks as i64;
                gain = ((interp_start_gain as i64 * (note_interp_clocks - elapsed_note_clocks)
                    + gain as i64 * elapsed_note_clocks
                    + note_interp_clocks / 2)
                    / note_interp_clocks) as i32;
            }

            let amplitude = Self::apply_gain_fixed(self.oscillator.sample(), gain);
            self.write_sample(blip_buf.as_deref_mut(), clock_offset, amplitude);
            self.last_gain = gain;

            // Advance oscillator and control clock
            let process_clocks = self.sample_clocks.min(clock_count);
            self.remaining_note_clocks = self.remaining_note_clocks.saturating_sub(process_clocks);
            self.elapsed_note_clocks += process_clocks;
            clock_offset += process_clocks;
            clock_count -= process_clocks;

            if process_clocks < self.sample_clocks {
                self.carryover_sample_clocks = self.sample_clocks - process_clocks;
                return;
            }

            self.oscillator.advance_sample();
            self.advance_control_clock(self.sample_clocks);
        }

        if self.remaining_note_clocks == 0 && clock_count > 0 {
            self.write_sample(blip_buf, clock_offset, 0);
            self.last_gain = 0;
        }
    }

    fn gain_to_fixed(gain: f32) -> i32 {
        (gain * VOICE_GAIN_SCALE as f32).round() as i32
    }

    fn apply_gain_fixed(sample: i32, gain: i32) -> i32 {
        let product = sample as i64 * gain as i64;
        if product >= 0 {
            ((product + VOICE_GAIN_ROUNDING) >> VOICE_GAIN_SHIFT) as i32
        } else {
            ((product - VOICE_GAIN_ROUNDING) >> VOICE_GAIN_SHIFT) as i32
        }
    }

    fn reset_control_clock(&mut self) {
        self.envelope.reset_tick();
        self.vibrato.reset_tick();
        self.glide.reset_tick();

        self.update_sample_clocks();
    }

    fn advance_control_clock(&mut self, clocks: u32) {
        self.control_elapsed_clocks += clocks;

        if self.control_elapsed_clocks >= self.control_interval_clocks {
            let ticks = self.control_elapsed_clocks / self.clocks_per_tick;

            if ticks > 0 {
                self.control_elapsed_clocks -= self.clocks_per_tick * ticks;

                self.envelope.advance_tick(ticks);
                self.vibrato.advance_tick(ticks);
                self.glide.advance_tick(ticks);
            }

            self.update_sample_clocks();
        }
    }

    fn update_sample_clocks(&mut self) {
        let frequency =
            self.base_frequency * self.vibrato.pitch_multiplier() * self.glide.pitch_multiplier();
        self.sample_clocks = (self.clock_rate as f32
            / frequency
            / self.oscillator.cycle_resolution() as f32)
            .round() as u32;
    }

    fn write_sample(&mut self, blip_buf: Option<&mut BlipBuf>, clock_offset: u32, amplitude: i32) {
        if let Some(blip_buf) = blip_buf {
            if amplitude != self.last_amplitude {
                blip_buf.add_delta(clock_offset, amplitude - self.last_amplitude);
                self.last_amplitude = amplitude;
            }
        }
    }
}