Skip to content

Commit e2198b4

Browse files
Merge branch 'master' into RustAudio#145 to include changes from RustAudio#146
2 parents cfdf077 + 39015e6 commit e2198b4

File tree

2 files changed

+141
-4
lines changed

2 files changed

+141
-4
lines changed

dasp_frame/src/lib.rs

+137
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
66
#![cfg_attr(not(feature = "std"), no_std)]
77

8+
use core::iter::DoubleEndedIterator;
9+
810
use dasp_sample::Sample;
911

1012
/// 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 {
8183
/// Converts the frame into an iterator yielding the sample for each channel in the frame.
8284
fn channels(self) -> Self::Channels;
8385

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+
84108
/// Yields a reference to the `Sample` of the channel at the given index if there is one.
85109
fn channel(&self, idx: usize) -> Option<&Self::Sample>;
86110

111+
/// Like [`channel()`], but yields a mutable reference instead.
112+
fn channel_mut(&mut self, idx: usize) -> Option<&mut Self::Sample>;
113+
87114
/// Returns a pointer to the sample of the channel at the given index, without doing bounds
88115
/// checking.
89116
///
@@ -92,6 +119,9 @@ pub trait Frame: Copy + Clone + PartialEq {
92119
/// at *compile-time*.
93120
unsafe fn channel_unchecked(&self, idx: usize) -> &Self::Sample;
94121

122+
/// Like [`channel_unchecked()`], but yields a mutable reference instead.
123+
unsafe fn channel_unchecked_mut(&mut self, idx: usize) -> &mut Self::Sample;
124+
95125
/// Applies the given function to each sample in the `Frame` in channel order and returns the
96126
/// result as a new `Frame`.
97127
///
@@ -258,6 +288,13 @@ pub struct Channels<F> {
258288
frame: F,
259289
}
260290

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+
261298
macro_rules! impl_frame_for_fixed_size_array {
262299
($($NChan:ident $N:expr, [$($idx:expr)*],)*) => {
263300
$(
@@ -286,11 +323,26 @@ macro_rules! impl_frame_for_fixed_size_array {
286323
}
287324
}
288325

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+
289336
#[inline]
290337
fn channel(&self, idx: usize) -> Option<&Self::Sample> {
291338
self.get(idx)
292339
}
293340

341+
#[inline]
342+
fn channel_mut(&mut self, idx: usize) -> Option<&mut Self::Sample> {
343+
self.get_mut(idx)
344+
}
345+
294346
#[inline]
295347
fn from_fn<F>(mut from: F) -> Self
296348
where
@@ -318,6 +370,11 @@ macro_rules! impl_frame_for_fixed_size_array {
318370
self.get_unchecked(idx)
319371
}
320372

373+
#[inline(always)]
374+
unsafe fn channel_unchecked_mut(&mut self, idx: usize) -> &mut Self::Sample {
375+
self.get_unchecked_mut(idx)
376+
}
377+
321378
#[inline]
322379
fn to_signed_frame(self) -> Self::Signed {
323380
self.map(|s| s.to_sample())
@@ -445,6 +502,16 @@ macro_rules! impl_frame_for_sample {
445502
}
446503
}
447504

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+
448515
#[inline]
449516
fn channel(&self, idx: usize) -> Option<&Self::Sample> {
450517
if idx == 0 {
@@ -454,6 +521,15 @@ macro_rules! impl_frame_for_sample {
454521
}
455522
}
456523

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+
457533
#[inline]
458534
fn from_fn<F>(mut from: F) -> Self
459535
where
@@ -475,6 +551,11 @@ macro_rules! impl_frame_for_sample {
475551
self
476552
}
477553

554+
#[inline(always)]
555+
unsafe fn channel_unchecked_mut(&mut self, _idx: usize) -> &mut Self::Sample {
556+
self
557+
}
558+
478559
#[inline]
479560
fn to_signed_frame(self) -> Self::Signed {
480561
self.to_signed_sample()
@@ -577,3 +658,59 @@ where
577658
F::CHANNELS - self.next_idx
578659
}
579660
}
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+
}

dasp_interpolate/src/sinc/mod.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ where
9292
v = {
9393
let a = PI * (phil + n as f64);
9494
let first = if a == 0.0 { 1.0 } else { sin(a) / a };
95-
let second = 0.5 + 0.5 * cos(a / (phil + max_depth as f64));
96-
v.zip_map(self.frames[nr - n], |vs, r_lag| {
95+
let second = 0.5 + 0.5 * cos(a / depth as f64);
96+
v.zip_map(self.frames[nl - n], |vs, r_lag| {
9797
vs.add_amp(
9898
(first * second * r_lag.to_sample::<f64>())
9999
.to_sample::<<Self::Frame as Frame>::Sample>()
@@ -104,8 +104,8 @@ where
104104

105105
let a = PI * (phir + n as f64);
106106
let first = if a == 0.0 { 1.0 } else { sin(a) / a };
107-
let second = 0.5 + 0.5 * cos(a / (phir + max_depth as f64));
108-
v.zip_map(self.frames[nl + n], |vs, r_lag| {
107+
let second = 0.5 + 0.5 * cos(a / depth as f64);
108+
v.zip_map(self.frames[nr + n], |vs, r_lag| {
109109
vs.add_amp(
110110
(first * second * r_lag.to_sample::<f64>())
111111
.to_sample::<<Self::Frame as Frame>::Sample>()

0 commit comments

Comments
 (0)