-
Notifications
You must be signed in to change notification settings - Fork 26
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add time seek similar as ov_time_seek #73
Comments
There is seeking support in lewton! The |
Hm, I tried it as |
Maybe it not working because of this. I'm trying to "rewind" decoder to beginning of data after I read all the contents in source by just calling |
I think currently if you use the ogg reader module, the only way is to call the packet decoding functions enough times while keeping count of the number of their samples. |
Not sure if I get you right, do I need to track total amount of samples I read and then when I need to seek beginning I need to do something like (pseudocode)
Also when I do |
No you need to track the number of samples read after you call
Hmm yeah it makes sense that this happens. It might be a good idea to let lewton be responsible for this so maybe I should fix it. In the meantime, you can create a completely new OggStreamReader when you want to rewind completely. |
Yeah, that would be awesome if it would be fixed on library side. New OggStreamReader instance is ok for now, but in future I want to be able to seek at any time pos I need. Simplest use case: set playback position for streaming sound after loaded a saved game, this is useful for long ambient sounds that eats too much memory when fully decoded. I'm curious why seeking produces |
Ok, this solution works (I wish pub struct OggDecoder {
reader: Option<OggStreamReader<DataSource>>,
samples: vec::IntoIter<i16>,
channel_count: usize,
sample_rate: usize,
}
fn i16_to_f32_sample(sample: i16) -> f32 {
sample as f32 / 32767.0
}
impl Iterator for OggDecoder {
type Item = f32;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
if let Some(sample) = self.samples.next() {
Some(i16_to_f32_sample(sample))
} else {
self.samples = self.reader
.as_mut()
.unwrap()
.read_dec_packet_itl()
.ok()
.and_then(|data| data)
.unwrap_or_default()
.into_iter();
self.samples
.next()
.and_then(|v| Some(i16_to_f32_sample(v)))
}
}
}
impl OggDecoder {
pub fn new(source: DataSource) -> Result<Self, SoundError> {
let mut reader = OggStreamReader::new(source)?;
Ok(Self {
samples: reader.read_dec_packet_itl()?
.ok_or(SoundError::UnsupportedFormat)?
.into_iter(),
channel_count: reader.ident_hdr.audio_channels as usize,
sample_rate: reader.ident_hdr.audio_sample_rate as usize,
reader: Some(reader),
})
}
fn get_channel_count(&self) -> usize {
self.channel_count
}
fn rewind(&mut self) -> Result<(), SoundError> {
let mut source = self.reader
.take()
.unwrap()
.into_inner()
.into_inner();
source.seek(SeekFrom::Start(0))?;
*self = Self::new(source)?;
Ok(())
}
fn get_sample_rate(&self) -> usize {
self.sample_rate
}
} |
Wonderful that you found a solution! Note: you can avoid doing the conversion in |
Great, thanks! Still API of |
Should this function be included into the library's API instead of having the user implement it themselves? |
@JackRedstonia In my opinion (I'm not an owner) It'd be extremely useful, but it's not as simple as it may seem. I've been working on it for while, but I don't have time recently so it's not clear I'll make it. If you have time go ahead, and I can help you time to time. At least, I really want that future. |
seek is broken, RustAudio/lewton#73. We could work around it by: - using unsafe to create an instance of Self - use mem::swap to turn the &mut self into a mut self - take out the underlying Read+Seek - make a new self and seek If this issue is fixed support use the implementation in commit: 3bafe32
seek is broken, RustAudio/lewton#73. We could work around it by: - using unsafe to create an instance of Self - use mem::swap to turn the &mut self into a mut self - take out the underlying Read+Seek - make a new self and seek If this issue is fixed use the implementation in commit: 3bafe32
Time seeking similar to
ov_time_seek
would be very useful. Or maybe it is already there?The text was updated successfully, but these errors were encountered: