Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for offsetting the phase of a signal #183

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
add support for offsetting phase
viktaur committed Feb 26, 2024
commit 43f5ba85d7903301b5a8ef4605857a9ad9251151
Binary file added .DS_Store
Binary file not shown.
8 changes: 8 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

27 changes: 27 additions & 0 deletions .idea/dasp.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

65 changes: 64 additions & 1 deletion dasp_signal/src/lib.rs
Original file line number Diff line number Diff line change
@@ -143,7 +143,8 @@ pub trait Signal {
///
/// fn main() {
/// // Infinite signals always return `false`.
/// let sine_signal = signal::rate(44_100.0).const_hz(400.0).sine();
/// use dasp_signal::Step;
/// let sine_signal = signal::rate(44_100.0).const_hz(400.0).offset_phase(0.2).sine();
/// assert_eq!(sine_signal.is_exhausted(), false);
///
/// // Signals over iterators return `true` when the inner iterator is exhausted.
@@ -960,6 +961,16 @@ pub struct NoiseSimplex<S> {
phase: Phase<S>,
}

// A signal generator with offset phase.
#[derive(Clone)]
pub struct OffsetPhase<S>
where
S: Signal
{
const_hz: S,
offset: f64
}

/// An iterator that yields the sum of the frames yielded by both `other` and `self` in lock-step.
#[derive(Clone)]
pub struct AddAmp<A, B> {
@@ -1850,6 +1861,14 @@ where
pub fn noise_simplex(self) -> NoiseSimplex<Self> {
self.phase().noise_simplex()
}

#[inline]
pub fn offset_phase(self, offset: f64) -> OffsetPhase<Self> {
OffsetPhase {
const_hz: self,
offset,
}
}
}

impl ConstHz {
@@ -1882,6 +1901,14 @@ impl ConstHz {
pub fn noise_simplex(self) -> NoiseSimplex<Self> {
self.phase().noise_simplex()
}

#[inline]
pub fn offset_phase(self, offset: f64) -> OffsetPhase<Self> {
OffsetPhase {
const_hz: self,
offset,
}
}
}

/// Types that may be used to give a phase step size based on some `hz / sample rate`.
@@ -2097,6 +2124,42 @@ where
}
}

impl<S: Signal + Step> OffsetPhase<S> {
/// Construct a `Phase` iterator that, for every `hz` yielded by `self`, yields a phase that is
/// stepped by `hz / self.rate.hz`.
#[inline]
pub fn phase(self) -> Phase<S> {
Phase {
step: self.const_hz,
next: self.offset,
}
}

/// A composable alternative to the `signal::sine` function.
#[inline]
pub fn sine(self) -> Sine<S> {
self.phase().sine()
}

/// A composable alternative to the `signal::saw` function.
#[inline]
pub fn saw(self) -> Saw<S> {
self.phase().saw()
}

/// A composable alternative to the `signal::square` function.
#[inline]
pub fn square(self) -> Square<S> {
self.phase().square()
}

/// A composable alternative to the `signal::noise_simplex` function.
#[inline]
pub fn noise_simplex(self) -> NoiseSimplex<S> {
self.phase().noise_simplex()
}
}

impl<A, B> Signal for AddAmp<A, B>
where
A: Signal,