aboutsummaryrefslogblamecommitdiffstats
path: root/src/music.rs
blob: d44eabff7e9b2c698f6775184fa96da057a68c75 (plain) (tree)




































                                                                           
use sdl2::audio::AudioCallback;

use crate::vendored::micromod::MmC2r;

pub struct Music {
  pub pcm: Vec<i16>,
  pub offset: usize,
}

impl AudioCallback for Music {
  type Channel = f32;

  fn callback(&mut self, out_f: &mut [f32]) {
    let len = out_f.len();
    pcm_to_f32pcm(&self.pcm[self.offset..(self.offset + len)], out_f, len);
    self.offset += len;
  }
}

pub fn mmc2r_to_pcm(state: &mut MmC2r) -> Vec<i16> {
  let mut destination = Vec::<i16>::new();
  loop {
    let mut out = [0; 4096];
    if !state.get_audio(&mut out, 2048) {
      break;
    }
    for sample in out {
      destination.push(sample);
    }
  }
  destination
}
pub fn pcm_to_f32pcm(input: &[i16], output: &mut [f32], len: usize) {
  for i in 0..len {
    output[i] = f32::from(input[i]) / f32::from(0x7FFF as i16);
  }
}