1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2024-11-19 12:12:44 +00:00
doukutsu-rs/src/sound/pixtone.rs

314 lines
9.8 KiB
Rust
Raw Normal View History

2020-09-16 13:21:30 +00:00
use std::collections::HashMap;
2020-09-16 19:53:42 +00:00
use num_traits::clamp;
2020-09-16 13:21:30 +00:00
use vec_mut_scan::VecMutScan;
2020-08-18 16:46:07 +00:00
use lazy_static::lazy_static;
2020-09-16 13:25:28 +00:00
use crate::sound::pixtone_sfx::PIXTONE_TABLE;
2020-09-16 19:53:42 +00:00
use crate::sound::stuff::cubic_interp;
2020-09-16 13:25:28 +00:00
2020-08-18 16:46:07 +00:00
lazy_static! {
static ref WAVEFORMS: [[i8; 0x100]; 6] = {
let mut sine = [0i8; 0x100];
let mut triangle = [0i8; 0x100];
let mut saw_up = [0i8; 0x100];
let mut saw_down = [0i8; 0x100];
let mut square = [0i8; 0x100];
let mut random = [0i8; 0x100];
2020-09-16 13:21:30 +00:00
let ref_data = include_bytes!("pixtone_ref.dat");
unsafe {
sine.copy_from_slice(&*(&ref_data[0..0x100] as *const [u8] as *const [i8]));
triangle.copy_from_slice(&*(&ref_data[0x100..0x200] as *const [u8] as *const [i8]));
saw_up.copy_from_slice(&*(&ref_data[0x200..0x300] as *const [u8] as *const [i8]));
saw_down.copy_from_slice(&*(&ref_data[0x300..0x400] as *const [u8] as *const [i8]));
square.copy_from_slice(&*(&ref_data[0x400..0x500] as *const [u8] as *const [i8]));
random.copy_from_slice(&*(&ref_data[0x500..0x600] as *const [u8] as *const [i8]));
}
// todo i can't get this shit right
2020-09-16 19:53:42 +00:00
/*
2020-09-16 13:21:30 +00:00
let mut seed = 0i32;
2020-08-18 16:46:07 +00:00
for i in 0..255 {
2020-09-16 13:21:30 +00:00
seed = seed.wrapping_mul(214013).wrapping_add(2531011);
sine[i] = (64.0 * (i as f64 * std::f64::consts::PI).sin()) as i8;
triangle[i] = (if (0x40i32.wrapping_add(i as i32)) & 0x80 != 0 { 0x80i32.wrapping_sub(i as i32) } else { i as i32 }) as i8;
saw_up[i] = (-0x40i32).wrapping_add(i as i32 / 2) as i8;
saw_down[i] = (0x40i32.wrapping_sub(i as i32 / 2)) as i8;
square[i] = (0x40i32.wrapping_sub(i as i32 & 0x80)) as i8;
2020-09-16 19:53:42 +00:00
random[i] = (seed >> 16) as i8 / 2;
2020-09-16 13:21:30 +00:00
}*/
2020-08-18 16:46:07 +00:00
[sine, triangle, saw_up, saw_down, square, random]
};
}
2020-09-16 13:25:28 +00:00
/*#[test]
2020-09-16 13:21:30 +00:00
fn test_waveforms() {
let reference = include_bytes!("pixtone_ref.dat");
2020-08-18 16:46:07 +00:00
2020-09-16 13:21:30 +00:00
for n in 1..(WAVEFORMS.len()) {
for (i, &val) in WAVEFORMS[n].iter().enumerate() {
assert_eq!((val as u8, i, n), (reference[n as usize * 256 + i], i, n));
}
}
2020-09-16 13:25:28 +00:00
}*/
2020-09-16 13:21:30 +00:00
pub struct Waveform {
2020-09-16 13:25:28 +00:00
pub waveform_type: u8,
pub pitch: f32,
pub level: i32,
pub offset: i32,
2020-09-16 13:21:30 +00:00
}
impl Waveform {
pub fn get_waveform(&self) -> &[i8; 0x100] {
&WAVEFORMS[self.waveform_type as usize % WAVEFORMS.len()]
}
}
pub struct Envelope {
2020-09-16 13:25:28 +00:00
pub initial: i32,
pub time_a: i32,
pub value_a: i32,
pub time_b: i32,
pub value_b: i32,
pub time_c: i32,
pub value_c: i32,
2020-09-16 13:21:30 +00:00
}
impl Envelope {
pub fn evaluate(&self, i: i32) -> i32 {
let (next_time, next_val) = {
if i < self.time_c {
(self.time_c, self.value_c)
} else if i < self.time_b {
(self.time_b, self.value_b)
} else if i < self.time_a {
(self.time_a, self.value_a)
} else {
(256, 0)
}
};
2020-09-16 19:53:42 +00:00
let (prev_time, prev_val) = {
if i >= self.time_a {
(self.time_a, self.value_a)
} else if i >= self.time_b {
(self.time_b, self.value_b)
} else if i >= self.time_c {
(self.time_c, self.value_c)
} else {
(0, self.initial)
}
};
2020-09-16 13:21:30 +00:00
if next_time <= prev_time {
return prev_val;
}
(i - prev_time) * (next_val - prev_val) / (next_time - prev_time) + prev_val
}
}
pub struct Channel {
2020-09-16 13:25:28 +00:00
pub enabled: bool,
pub length: u32,
pub carrier: Waveform,
pub frequency: Waveform,
pub amplitude: Waveform,
pub envelope: Envelope,
2020-09-16 13:21:30 +00:00
}
impl Channel {
pub const fn disabled() -> Channel {
Channel {
enabled: false,
length: 0,
carrier: Waveform {
waveform_type: 0,
pitch: 0.0,
level: 0,
offset: 0,
},
frequency: Waveform {
waveform_type: 0,
pitch: 0.0,
level: 0,
offset: 0,
},
amplitude: Waveform {
waveform_type: 0,
pitch: 0.0,
level: 0,
offset: 0,
},
envelope: Envelope {
initial: 0,
time_a: 0,
value_a: 0,
time_b: 0,
value_b: 0,
time_c: 0,
value_c: 0,
},
}
}
}
pub struct PixToneParameters {
2020-09-16 13:25:28 +00:00
pub channels: [Channel; 4],
2020-09-16 13:21:30 +00:00
}
impl PixToneParameters {
pub const fn empty() -> PixToneParameters {
PixToneParameters {
channels: [Channel::disabled(), Channel::disabled(), Channel::disabled(), Channel::disabled()]
}
}
2020-08-18 16:46:07 +00:00
2020-09-16 13:21:30 +00:00
pub fn synth(&self) -> Vec<i16> {
let length = self.channels.iter().map(|c| c.length as usize).max().unwrap_or(0);
if length == 0 {
return Vec::new();
}
2020-09-18 22:39:26 +00:00
let mut samples = vec![0i16; length + 100];
2020-09-16 13:21:30 +00:00
for channel in self.channels.iter() {
if !channel.enabled { continue; }
fn s(p: f32, i: usize, length: u32) -> f32 {
256.0 * p * i as f32 / length as f32
}
let mut phase = channel.carrier.offset as f32;
let delta = 256.0 * channel.carrier.pitch as f32 / channel.length as f32;
let carrier_wave = channel.carrier.get_waveform();
let frequency_wave = channel.frequency.get_waveform();
let amplitude_wave = channel.amplitude.get_waveform();
2020-09-18 22:39:26 +00:00
let mut last_wave = 0;
2020-09-16 13:21:30 +00:00
for (i, result) in samples.iter_mut().enumerate() {
2020-09-18 22:39:26 +00:00
if i >= channel.length as usize {
if i == channel.length as usize {
last_wave = *result;
} else if i == (channel.length as usize + 100) {
break;
}
2020-09-16 19:53:42 +00:00
2020-09-18 22:39:26 +00:00
let fac = (i - channel.length as usize) as i16 / 2 + 1;
last_wave /= fac;
*result = last_wave;
continue;
} else {
let carrier = carrier_wave[0xff & phase as usize] as i32 * channel.carrier.level;
let freq = frequency_wave[0xff & (channel.frequency.offset as f32 + s(channel.frequency.pitch, i, channel.length)) as usize] as i32 * channel.frequency.level;
let amp = amplitude_wave[0xff & (channel.amplitude.offset as f32 + s(channel.amplitude.pitch, i, channel.length)) as usize] as i32 * channel.amplitude.level;
2020-09-16 13:21:30 +00:00
2020-09-18 22:39:26 +00:00
*result = clamp((*result as i32) + (carrier * (amp + 4096) / 4096 * channel.envelope.evaluate(s(1.0, i, channel.length) as i32) / 4096) * 192, -32767, 32767) as i16;
2020-09-16 13:21:30 +00:00
2020-09-18 22:39:26 +00:00
phase += delta * (1.0 + (freq as f32 / (if freq < 0 { 8192.0 } else { 2048.0 })));
}
2020-09-16 13:21:30 +00:00
}
}
samples
}
}
#[derive(Copy, Clone, PartialEq)]
2020-09-18 22:39:26 +00:00
pub struct PlaybackState(u8, f32, f32, u32);
2020-09-16 13:21:30 +00:00
pub struct PixTonePlayback {
pub samples: HashMap<u8, Vec<i16>>,
pub playback_state: Vec<PlaybackState>,
}
impl PixTonePlayback {
pub fn new() -> PixTonePlayback {
PixTonePlayback {
samples: HashMap::new(),
playback_state: vec![],
}
}
pub fn create_samples(&mut self) {
for (i, params) in PIXTONE_TABLE.iter().enumerate() {
self.samples.insert(i as u8, params.synth());
}
}
pub fn play_sfx(&mut self, id: u8) {
for state in self.playback_state.iter_mut() {
2020-09-18 22:39:26 +00:00
if state.0 == id && state.3 == 0 {
state.2 = 200.0;
state.3 = 0xffffffff;
2020-09-16 13:21:30 +00:00
}
}
2020-09-18 22:39:26 +00:00
self.playback_state.push(PlaybackState(id, 0.0, 0.0, 0));
2020-09-16 13:31:25 +00:00
}
pub fn play_concurrent(&mut self, id: u8, tag: u32) {
2020-09-18 22:39:26 +00:00
self.playback_state.push(PlaybackState(id, 0.0, 0.0, tag));
2020-09-16 13:21:30 +00:00
}
pub fn mix(&mut self, dst: &mut [u16], sample_rate: f32) {
let mut scan = VecMutScan::new(&mut self.playback_state);
let delta = 22050.0 / sample_rate;
while let Some(item) = scan.next() {
let mut state = *item;
let mut remove = false;
if let Some(sample) = self.samples.get(&state.0) {
if sample.is_empty() {
item.remove();
continue;
};
for result in dst.iter_mut() {
if state.1 >= sample.len() as f32 {
remove = true;
break;
} else {
2020-09-16 19:53:42 +00:00
let pos = state.1 as usize;
let s1 = (sample[pos] as f32) / 32768.0;
let s2 = (sample[clamp(pos + 1, 0, sample.len() - 1)] as f32) / 32768.0;
let s3 = (sample[clamp(pos + 2, 0, sample.len() - 1)] as f32) / 32768.0;
let s4 = (sample[pos.saturating_sub(1)] as f32) / 32768.0;
2020-09-18 22:39:26 +00:00
let mut s = cubic_interp(s1, s2, s4, s3, state.1.fract()) * 32768.0;
if state.2 > 0.0 {
s *= (state.2 / 200.0);
2020-09-16 19:53:42 +00:00
2020-09-18 22:39:26 +00:00
state.2 -= delta;
if state.2 <= 0.0 {
remove = true;
break;
}
}
let sam = (*result ^ 0x8000) as i16;
*result = sam.wrapping_add(s as i16) as u16 ^ 0x8000;
2020-09-16 19:53:42 +00:00
2020-09-16 13:21:30 +00:00
state.1 += delta;
}
}
if remove {
item.remove();
} else {
item.replace(state);
}
}
}
}
}