Skip to content

Instantly share code, notes, and snippets.

@cubetastic33
Created May 8, 2019 17:49
Show Gist options
  • Save cubetastic33/4b0f064094b362448ca4f0501dfc68b4 to your computer and use it in GitHub Desktop.
Save cubetastic33/4b0f064094b362448ca4f0501dfc68b4 to your computer and use it in GitHub Desktop.
A workaround to using claxon to play music with rodio
#![feature(proc_macro_hygiene)]
use inline_python::python;
use rodio::Sink;
fn main() {
let device = rodio::default_output_device().unwrap();
let sink = Sink::new(&device);
let c = inline_python::Context::new();
let file_path = "/path/to/file.flac";
python! {
#![context = &c]
import soundfile as sf
data, samplerate = sf.read('file_path)
data = data.tolist()
}
let gil = inline_python::pyo3::Python::acquire_gil();
let python_globals = python_player.globals(gil.python());
let x: Vec<Vec<f32>> = inline_python::pyo3::FromPyObject::extract(python_globals.get_item("data").unwrap()).unwrap();
let channels = x[0].len();
let data: Vec<f32> = x.into_iter().flatten().collect();
let sample_rate = inline_python::pyo3::FromPyObject::extract(python_globals.get_item("samplerate").unwrap()).unwrap();
let source = rodio::buffer::SamplesBuffer::new(channels as u16, sample_rate, data);
sink.append(source);
sink.sleep_until_end();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment