@@ -64,10 +64,17 @@ where
6464 self . input
6565 }
6666
67+ /// Optionally the truncated source end with a FadeOut. The fade-out
68+ /// covers the entire length of the take source.
69+ pub fn fadeout ( & mut self , enabled : bool ) {
70+ self . fadeout = enabled;
71+ }
72+
6773 /// Make the truncated source end with a FadeOut. The fade-out covers the
6874 /// entire length of the take source.
69- pub fn set_filter_fadeout ( & mut self ) {
70- self . fadeout = true ;
75+ pub fn with_fadeout ( mut self , enabled : bool ) -> Self {
76+ self . fadeout = enabled;
77+ self
7178 }
7279
7380 /// Remove any filter set.
@@ -98,6 +105,14 @@ where
98105 type Item = <I as Iterator >:: Item ;
99106
100107 // implementation is adapted of skip_duration
108+ //
109+ // if tuples are frames you could define fadeout as this:
110+ // [(1.0, 1.0), (1.0, 1.0), (1.0, 1.0)]
111+ // -> [(1.0, 1.0), (0.5, 0.5), (0.0, 0.0)]
112+ // instead because its simpler, faster and what previous rodio versions did we do:
113+ // [(1.0, 1.0), (1.0, 1.0), (1.0, 1.0)]
114+ // -> [(1.0, .83), (.66, 0.5), (.33, .16)]
115+ // at normal sample_rates you do not hear a difference
101116 fn next ( & mut self ) -> Option < <I as Iterator >:: Item > {
102117 if self . input . parameters_changed ( ) {
103118 self . remaining_ns -= self . duration_taken ( ) ;
@@ -114,13 +129,15 @@ where
114129 return None ;
115130 } ;
116131
117- self . samples_taken += 1 ;
118- if self . fadeout {
132+ let ret = if self . fadeout {
119133 let total = self . requested_duration . as_nanos ( ) as u64 ;
120- Some ( sample * self . remaining_ns as f32 / total as f32 )
134+ let remaining = self . remaining_ns - self . duration_taken ( ) ;
135+ Some ( sample * remaining as f32 / total as f32 )
121136 } else {
122137 Some ( sample)
123- }
138+ } ;
139+ self . samples_taken += 1 ;
140+ ret
124141 }
125142}
126143
0 commit comments