5
5
6
6
#![ cfg_attr( not( feature = "std" ) , no_std) ]
7
7
8
+ use core:: iter:: DoubleEndedIterator ;
9
+
8
10
use dasp_sample:: Sample ;
9
11
10
12
/// Represents one sample from each channel at a single discrete instance in time within a
@@ -81,9 +83,34 @@ pub trait Frame: Copy + Clone + PartialEq {
81
83
/// Converts the frame into an iterator yielding the sample for each channel in the frame.
82
84
fn channels ( self ) -> Self :: Channels ;
83
85
86
+ /// Returns an iterator yielding references to the sample for each channel in the frame.
87
+ fn channels_ref ( & self ) -> ChannelsRef < ' _ , Self > ;
88
+
89
+ /// Like [`channels_ref()`], but yields mutable references instead.
90
+ ///
91
+ /// # Example
92
+ ///
93
+ /// ```rust
94
+ /// use dasp_frame::Frame;
95
+ ///
96
+ /// fn main() {
97
+ /// let mut foo = [1000i32, 2000, 3000];
98
+ /// let mut offset = 100i32;
99
+ /// for f in foo.channels_mut() {
100
+ /// *f = *f + offset;
101
+ /// offset += 100;
102
+ /// }
103
+ /// assert_eq!(foo, [1100i32, 2200, 3300]);
104
+ /// }
105
+ /// ```
106
+ fn channels_mut ( & mut self ) -> ChannelsMut < ' _ , Self > ;
107
+
84
108
/// Yields a reference to the `Sample` of the channel at the given index if there is one.
85
109
fn channel ( & self , idx : usize ) -> Option < & Self :: Sample > ;
86
110
111
+ /// Like [`channel()`], but yields a mutable reference instead.
112
+ fn channel_mut ( & mut self , idx : usize ) -> Option < & mut Self :: Sample > ;
113
+
87
114
/// Returns a pointer to the sample of the channel at the given index, without doing bounds
88
115
/// checking.
89
116
///
@@ -92,6 +119,9 @@ pub trait Frame: Copy + Clone + PartialEq {
92
119
/// at *compile-time*.
93
120
unsafe fn channel_unchecked ( & self , idx : usize ) -> & Self :: Sample ;
94
121
122
+ /// Like [`channel_unchecked()`], but yields a mutable reference instead.
123
+ unsafe fn channel_unchecked_mut ( & mut self , idx : usize ) -> & mut Self :: Sample ;
124
+
95
125
/// Applies the given function to each sample in the `Frame` in channel order and returns the
96
126
/// result as a new `Frame`.
97
127
///
@@ -258,6 +288,13 @@ pub struct Channels<F> {
258
288
frame : F ,
259
289
}
260
290
291
+ /// An iterator that yields the sample for each channel in the frame by reference.
292
+ #[ derive( Clone ) ]
293
+ pub struct ChannelsRef < ' a , F : Frame > ( core:: slice:: Iter < ' a , F :: Sample > ) ;
294
+
295
+ /// Like [`ChannelsRef`], but yields mutable references instead.
296
+ pub struct ChannelsMut < ' a , F : Frame > ( core:: slice:: IterMut < ' a , F :: Sample > ) ;
297
+
261
298
macro_rules! impl_frame_for_fixed_size_array {
262
299
( $( $NChan: ident $N: expr, [ $( $idx: expr) * ] , ) * ) => {
263
300
$(
@@ -286,11 +323,26 @@ macro_rules! impl_frame_for_fixed_size_array {
286
323
}
287
324
}
288
325
326
+ #[ inline]
327
+ fn channels_ref( & self ) -> ChannelsRef <' _, Self > {
328
+ ChannelsRef ( self . iter( ) )
329
+ }
330
+
331
+ #[ inline]
332
+ fn channels_mut( & mut self ) -> ChannelsMut <' _, Self > {
333
+ ChannelsMut ( self . iter_mut( ) )
334
+ }
335
+
289
336
#[ inline]
290
337
fn channel( & self , idx: usize ) -> Option <& Self :: Sample > {
291
338
self . get( idx)
292
339
}
293
340
341
+ #[ inline]
342
+ fn channel_mut( & mut self , idx: usize ) -> Option <& mut Self :: Sample > {
343
+ self . get_mut( idx)
344
+ }
345
+
294
346
#[ inline]
295
347
fn from_fn<F >( mut from: F ) -> Self
296
348
where
@@ -318,6 +370,11 @@ macro_rules! impl_frame_for_fixed_size_array {
318
370
self . get_unchecked( idx)
319
371
}
320
372
373
+ #[ inline( always) ]
374
+ unsafe fn channel_unchecked_mut( & mut self , idx: usize ) -> & mut Self :: Sample {
375
+ self . get_unchecked_mut( idx)
376
+ }
377
+
321
378
#[ inline]
322
379
fn to_signed_frame( self ) -> Self :: Signed {
323
380
self . map( |s| s. to_sample( ) )
@@ -445,6 +502,16 @@ macro_rules! impl_frame_for_sample {
445
502
}
446
503
}
447
504
505
+ #[ inline]
506
+ fn channels_ref( & self ) -> ChannelsRef <' _, Self > {
507
+ ChannelsRef ( core:: slice:: from_ref( self ) . iter( ) )
508
+ }
509
+
510
+ #[ inline]
511
+ fn channels_mut( & mut self ) -> ChannelsMut <' _, Self > {
512
+ ChannelsMut ( core:: slice:: from_mut( self ) . iter_mut( ) )
513
+ }
514
+
448
515
#[ inline]
449
516
fn channel( & self , idx: usize ) -> Option <& Self :: Sample > {
450
517
if idx == 0 {
@@ -454,6 +521,15 @@ macro_rules! impl_frame_for_sample {
454
521
}
455
522
}
456
523
524
+ #[ inline]
525
+ fn channel_mut( & mut self , idx: usize ) -> Option <& mut Self :: Sample > {
526
+ if idx == 0 {
527
+ Some ( self )
528
+ } else {
529
+ None
530
+ }
531
+ }
532
+
457
533
#[ inline]
458
534
fn from_fn<F >( mut from: F ) -> Self
459
535
where
@@ -475,6 +551,11 @@ macro_rules! impl_frame_for_sample {
475
551
self
476
552
}
477
553
554
+ #[ inline( always) ]
555
+ unsafe fn channel_unchecked_mut( & mut self , _idx: usize ) -> & mut Self :: Sample {
556
+ self
557
+ }
558
+
478
559
#[ inline]
479
560
fn to_signed_frame( self ) -> Self :: Signed {
480
561
self . to_signed_sample( )
@@ -577,3 +658,59 @@ where
577
658
F :: CHANNELS - self . next_idx
578
659
}
579
660
}
661
+
662
+ impl < ' a , F : Frame > Iterator for ChannelsRef < ' a , F > {
663
+ type Item = & ' a F :: Sample ;
664
+
665
+ #[ inline]
666
+ fn next ( & mut self ) -> Option < Self :: Item > {
667
+ self . 0 . next ( )
668
+ }
669
+
670
+ #[ inline]
671
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
672
+ self . 0 . size_hint ( )
673
+ }
674
+ }
675
+
676
+ impl < ' a , F : Frame > ExactSizeIterator for ChannelsRef < ' a , F > {
677
+ #[ inline]
678
+ fn len ( & self ) -> usize {
679
+ self . 0 . len ( )
680
+ }
681
+ }
682
+
683
+ impl < ' a , F : Frame > DoubleEndedIterator for ChannelsRef < ' a , F > {
684
+ #[ inline]
685
+ fn next_back ( & mut self ) -> Option < Self :: Item > {
686
+ self . 0 . next_back ( )
687
+ }
688
+ }
689
+
690
+ impl < ' a , F : Frame > Iterator for ChannelsMut < ' a , F > {
691
+ type Item = & ' a mut F :: Sample ;
692
+
693
+ #[ inline]
694
+ fn next ( & mut self ) -> Option < Self :: Item > {
695
+ self . 0 . next ( )
696
+ }
697
+
698
+ #[ inline]
699
+ fn size_hint ( & self ) -> ( usize , Option < usize > ) {
700
+ self . 0 . size_hint ( )
701
+ }
702
+ }
703
+
704
+ impl < ' a , F : Frame > ExactSizeIterator for ChannelsMut < ' a , F > {
705
+ #[ inline]
706
+ fn len ( & self ) -> usize {
707
+ self . 0 . len ( )
708
+ }
709
+ }
710
+
711
+ impl < ' a , F : Frame > DoubleEndedIterator for ChannelsMut < ' a , F > {
712
+ #[ inline]
713
+ fn next_back ( & mut self ) -> Option < Self :: Item > {
714
+ self . 0 . next_back ( )
715
+ }
716
+ }
0 commit comments