Hi,
I'm trying to play an audio file (which I open with sndfile) with portaudio.
I first load the whole audio file as a vec of samples.
Then, I have to create an audio callback for portaudio that periodically writes audio samples into the output buffer.
Hence, I use chunks() on the input samples and then copy the samples into the output buffer (with clone_from_slice).
Here is a minimal example (without the dependencies to portaudio, sndfile) Rust Playground
use std::thread;
fn main() {
let data = vec![1, 2, 3, 4, 5, 6, 8, 9, 10, 34, 56];
let mut it = data.chunks(2);
let mut audio_callback = move || {
if let Some(mut v) = it.next() {
println!("{:?}", v);
Some(v)
}
else {
None
}
};
//In the actual code, I use open_non_blocking_stream
// which creates an audio thread and calls the audio callback regurlarly
let handle = thread::spawn( move || {
while let Some(v) = audio_callback() {
}
});
//No join on a portaudio stream but I can close the stream to terminate it
handle.join().unwrap();
}
with the following errors:
<anon>:7:18: 7:22 error: `data` does not live long enough
<anon>:7 let mut it = data.chunks(2);
^~~~
note: reference must be valid for the static lifetime...
<anon>:5:57: 28:2 note: ...but borrowed value is only valid for the block suffix following statement 0 at 5:56
I think I understand the error: chunks
takes &self
as argument and so only borrows data
and data
only lives until the end of the main function, whereas the thread could live longer (it's not the case in practice because of the join
). The code would work with something like into_iter
but there is no version of chunks
that would take ownership.
I'm still quite a beginner in rust and I can't find a solution...