1- use std:: { cell:: RefCell , fmt:: Debug , iter, marker:: PhantomData , rc:: Rc } ;
1+ use std:: {
2+ fmt:: Debug ,
3+ iter,
4+ marker:: PhantomData ,
5+ sync:: { Arc , RwLock } ,
6+ } ;
27
38use crate :: arith:: signed_to_01;
49
5- #[ derive( Clone , Copy ) ]
10+ #[ derive( Clone , Copy , Debug ) ]
611pub struct SigCtx {
712 pub sample_rate_hz : f32 ,
813 pub batch_index : u64 ,
2732 out. push ( x. clone ( ) ) ;
2833 }
2934 }
35+
36+ /// Clone each sample into a slice with a given offset and stride. This is intended to be used
37+ /// to populate a single channel worth of samples into an audio buffer containing multiple
38+ /// interleaved channels.
39+ fn clone_to_slice ( & self , stride : usize , offset : usize , out : & mut [ T ] ) {
40+ let out_offset = & mut out[ offset..] ;
41+ for ( sample, out_chunk) in
42+ self . iter ( ) . zip ( out_offset. chunks_mut ( stride) )
43+ {
44+ out_chunk[ 0 ] = sample;
45+ }
46+ }
3047}
3148
3249impl < T > Buf < T > for & Vec < T >
@@ -232,6 +249,14 @@ pub trait SigSampleIntoBufT {
232249 type Item : Clone ;
233250
234251 fn sample_into_buf ( & mut self , ctx : & SigCtx , buf : & mut Vec < Self :: Item > ) ;
252+
253+ fn sample_into_slice (
254+ & mut self ,
255+ ctx : & SigCtx ,
256+ stride : usize ,
257+ offset : usize ,
258+ out : & mut [ Self :: Item ] ,
259+ ) ;
235260}
236261
237262impl SigT for f32 {
@@ -291,9 +316,22 @@ impl<S: SigT> SigSampleIntoBufT for Sig<S> {
291316 let buf_internal = self . 0 . sample ( ctx) ;
292317 buf_internal. clone_to_vec ( buf) ;
293318 }
319+
320+ fn sample_into_slice (
321+ & mut self ,
322+ ctx : & SigCtx ,
323+ stride : usize ,
324+ offset : usize ,
325+ out : & mut [ Self :: Item ] ,
326+ ) {
327+ let buf_internal = self . 0 . sample ( ctx) ;
328+ buf_internal. clone_to_slice ( stride, offset, out) ;
329+ }
294330}
295331
296- pub struct SigBoxed < T > ( Box < dyn SigSampleIntoBufT < Item = T > > )
332+ pub struct SigBoxed < T > (
333+ Box < dyn SigSampleIntoBufT < Item = T > + Send + Sync + ' static > ,
334+ )
297335where
298336 T : Clone ;
299337
@@ -306,6 +344,16 @@ where
306344 fn sample_into_buf ( & mut self , ctx : & SigCtx , buf : & mut Vec < Self :: Item > ) {
307345 self . 0 . sample_into_buf ( ctx, buf) ;
308346 }
347+
348+ fn sample_into_slice (
349+ & mut self ,
350+ ctx : & SigCtx ,
351+ stride : usize ,
352+ offset : usize ,
353+ out : & mut [ Self :: Item ] ,
354+ ) {
355+ self . 0 . sample_into_slice ( ctx, stride, offset, out) ;
356+ }
309357}
310358
311359impl < S > Sig < S >
@@ -379,7 +427,7 @@ where
379427
380428impl < S > Sig < S >
381429where
382- S : SigT + ' static ,
430+ S : SigT + Send + Sync + ' static ,
383431{
384432 pub fn boxed ( self ) -> SigBoxed < S :: Item > {
385433 SigBoxed ( Box :: new ( self ) )
@@ -710,7 +758,7 @@ pub struct SigShared<S>
710758where
711759 S : SigT ,
712760{
713- shared_cached_sig : Rc < RefCell < SigCached < S > > > ,
761+ shared_cached_sig : Arc < RwLock < SigCached < S > > > ,
714762 buf : Vec < S :: Item > ,
715763}
716764
@@ -720,7 +768,7 @@ where
720768{
721769 fn clone ( & self ) -> Self {
722770 SigShared {
723- shared_cached_sig : Rc :: clone ( & self . shared_cached_sig ) ,
771+ shared_cached_sig : Arc :: clone ( & self . shared_cached_sig ) ,
724772 buf : self . buf . clone ( ) ,
725773 }
726774 }
@@ -733,7 +781,7 @@ where
733781 type Item = S :: Item ;
734782
735783 fn sample ( & mut self , ctx : & SigCtx ) -> impl Buf < Self :: Item > {
736- let mut shared_cached_sig = self . shared_cached_sig . borrow_mut ( ) ;
784+ let mut shared_cached_sig = self . shared_cached_sig . write ( ) . unwrap ( ) ;
737785 // This will only actually run the underlying signal if it hasn't been computed yet this
738786 // frame. If it has already been computed this frame then the already-populated buffer will
739787 // be returned. We still need to copy the buffer to the buffer inside `self` so that the
@@ -749,7 +797,7 @@ where
749797 S : SigT ,
750798{
751799 Sig ( SigShared {
752- shared_cached_sig : Rc :: new ( RefCell :: new ( SigCached :: new ( sig) ) ) ,
800+ shared_cached_sig : Arc :: new ( RwLock :: new ( SigCached :: new ( sig) ) ) ,
753801 buf : Vec :: new ( ) ,
754802 } )
755803}
0 commit comments