populate examples; update rodio version

This commit is contained in:
2021-11-27 11:41:32 +00:00
parent cd72d9d66d
commit 4734ae81f8
9 changed files with 638 additions and 54 deletions

View File

@@ -7,55 +7,36 @@ use rodio::Source;
pub const BUFFER_SIZE: usize = 2048;
/// Example usage
///
/// ```rust
/// fn main() {
/// let mut xm = libxm::XMContext::new(
/// include_bytes!("path/to/file.xm"),
/// 48000
/// ).expect("failed to parse xm");
///
/// let (_stream, stream_handle) = rodio::OutputStream::try_default().unwrap();
///
/// let mut samples: [f32; rodio_xm::BUFFER_SIZE] = [0.0; rodio_xm::BUFFER_SIZE];
/// xm.generate_samples(&mut samples);
///
/// let source = rodio_xm::XMSource::new(xm);
/// let sink = rodio::Sink::try_new(&stream_handle).unwrap();
///
/// sink.append(source);
/// sink.sleep_until_end();
/// }
/// ```
pub struct XMSource {
xm_context: XMContext,
buffer: [f32; BUFFER_SIZE],
buffer_index: usize,
sample_rate: u32,
}
impl XMSource {
pub fn new(xm_context: XMContext) -> Self {
pub fn new(xm_context: XMContext, sample_rate: u32) -> Self {
XMSource {
xm_context,
buffer: [0.0; BUFFER_SIZE],
buffer_index: 0
buffer_index: 0,
sample_rate
}
}
pub fn from_bytes(bytes: &[u8]) -> Self {
let xm = libxm::XMContext::new(bytes, 48000)
pub fn from_bytes(bytes: &[u8], sample_rate: u32) -> Self {
let xm = libxm::XMContext::new(bytes, sample_rate)
.expect("failed to parse xm");
XMSource::new(xm)
XMSource::new(xm, sample_rate)
}
pub fn from_file(path: PathBuf) -> Self {
pub fn from_file(path: PathBuf, sample_rate: u32) -> Self {
let file = fs::read(path).expect("couldn't read file");
let xm = libxm::XMContext::new(&file, 48000)
let xm = libxm::XMContext::new(&file, sample_rate)
.expect("failed to parse xm");
XMSource::new(xm)
XMSource::new(xm, sample_rate)
}
}
@@ -66,7 +47,7 @@ impl Source for XMSource {
fn channels(&self) -> u16 { 2 }
fn sample_rate(&self) -> u32 { 48000 }
fn sample_rate(&self) -> u32 { self.sample_rate }
fn total_duration(&self) -> Option<Duration> { None }
}