11use std:: sync:: Arc ;
22use std:: time:: Duration ;
33
4+ use crate :: source:: SeekError ;
45use crate :: FixedSource ;
56use crate :: { ChannelCount , Sample , SampleRate } ;
67
@@ -19,7 +20,7 @@ impl SamplesBuffer {
1920 where
2021 D : Into < Vec < Sample > > ,
2122 {
22- let data: Arc < [ f32 ] > = data. into ( ) . into ( ) ;
23+ let data: Arc < [ Sample ] > = data. into ( ) . into ( ) ;
2324 SamplesBuffer {
2425 data,
2526 pos : 0 ,
@@ -38,52 +39,9 @@ impl FixedSource for SamplesBuffer {
3839 fn sample_rate ( & self ) -> SampleRate {
3940 self . sample_rate
4041 }
41- /// # Panics
42- /// If the length of the buffer is larger than approximately 16 billion elements.
43- /// This is because the calculation of the duration would overflow.
44- #[ inline]
45- fn total_duration ( & self ) -> Option < Duration > {
46- let duration_ns = 1_000_000_000u64
47- . checked_mul ( self . data . len ( ) as u64 )
48- . unwrap ( )
49- / self . sample_rate . get ( ) as u64
50- / self . channels . get ( ) as u64 ;
51- let duration = Duration :: new (
52- duration_ns / 1_000_000_000 ,
53- ( duration_ns % 1_000_000_000 ) as u32 ,
54- ) ;
55-
56- Some ( duration)
57- }
58- // /// This jumps in memory till the sample for `pos`.
59- // #[inline]
60- // fn try_seek(&mut self, pos: Duration) -> Result<(), SeekError> {
61- // // This is fast because all the samples are in memory already
62- // // and due to the constant sample_rate we can jump to the right
63- // // sample directly.
64- // let curr_channel = self.pos % self.channels() as usize;
65- // let new_pos = pos.as_secs_f32() * self.sample_rate() as f32 * self.channels() as f32;
66- // // saturate pos at the end of the source
67- // let new_pos = new_pos as usize;
68- // let new_pos = new_pos.min(self.data.len());
69- // // make sure the next sample is for the right channel
70- // let new_pos = new_pos.next_multiple_of(self.channels() as usize);
71- // let new_pos = new_pos - curr_channel;
72- // self.pos = new_pos;
73- // Ok(())
74- // }
42+ crate :: common:: source:: buffer:: source_impl! { }
7543}
7644
7745impl Iterator for SamplesBuffer {
78- type Item = Sample ;
79- #[ inline]
80- fn next ( & mut self ) -> Option < Self :: Item > {
81- let sample = self . data . get ( self . pos ) ?;
82- self . pos += 1 ;
83- Some ( * sample)
84- }
85- #[ inline]
86- fn size_hint ( & self ) -> ( usize , Option < usize > ) {
87- ( self . data . len ( ) , Some ( self . data . len ( ) ) )
88- }
46+ crate :: common:: source:: buffer:: iter_impl! { }
8947}
0 commit comments