[][src]Type Definition libsoxr::soxr::SoxrFunction

type SoxrFunction<S, T> = fn(_: &mut S, _: &mut [T], _: usize) -> Result<usize>;

Signature of an input function that supplies SOXR with input data. S is type of state data and T is type of target buffer and E is the type of Error that the fn can return. The last usize is the number of samples that Soxr asks this function to load into the buffer. The function should return Ok(0) to indicate end-of-input or an Error in case of problems getting input data.

use libsoxr::{Error, ErrorType, Soxr, SoxrFunction};

struct State {
  // data for input function to supply Soxr with source samples.
  // In this case just a value, but you could put a handle to a FLAC file into this.
  value: f32
}

let input_fn = |state: &mut State, buffer: &mut [f32], samples: usize| {
    for sample in buffer.iter_mut().take(samples) {
      *sample = state.value;
    }
    // if end-of-input: return Ok(O);
    // if error:  Err(Error::new(Some("input_fn".into()), ErrorType::ProcessError("Unexpected end of input".into())))
    Ok(samples)
  };

let mut soxr = Soxr::create(1.0, 2.0, 1, None, None, None).unwrap();
let mut state = State { value: 1.0 };
assert!(soxr.set_input(input_fn, Some(&mut state), 100).is_ok());