Skip to content

Commit

Permalink
move volume/panning application to the main process function
Browse files Browse the repository at this point in the history
this will make the next change easier
  • Loading branch information
tesselode committed Dec 21, 2024
1 parent b4b9e1f commit cb54874
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 27 deletions.
30 changes: 15 additions & 15 deletions crates/kira/src/sound/static_sound/sound.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,19 +115,14 @@ impl StaticSound {

/// Updates the current frame index by 1 and pushes a new sample to the resampler.
fn update_position(&mut self) {
if !self.playback_state_manager.playback_state().is_advancing() {
self.resampler
.push_frame(Frame::ZERO, self.transport.position);
return;
}
self.push_frame_to_resampler();
if self.is_playing_backwards() {
self.transport.decrement_position();
} else {
self.transport
.increment_position(num_frames(&self.frames, self.slice));
}
if !self.transport.playing && self.resampler.outputting_silence() {
if !self.transport.playing && self.resampler.empty() {
self.playback_state_manager.mark_as_stopped();
self.update_shared_playback_state();
}
Expand All @@ -145,15 +140,12 @@ impl StaticSound {

fn push_frame_to_resampler(&mut self) {
let frame = if self.transport.playing {
let frame_index: usize = self.transport.position;
let fade_volume = self.playback_state_manager.fade_volume().as_amplitude();
let volume = self.volume.value().as_amplitude();
(frame_at_index(frame_index, &self.frames, self.slice).unwrap_or_default()
* fade_volume
* volume)
.panned(self.panning.value())
Some(
frame_at_index(self.transport.position, &self.frames, self.slice)
.unwrap_or_default(),
)
} else {
Frame::ZERO
None
};
self.resampler.push_frame(frame, self.transport.position);
}
Expand Down Expand Up @@ -229,6 +221,11 @@ impl Sound for StaticSound {
return;
}

if !self.playback_state_manager.playback_state().is_advancing() {
out.fill(Frame::ZERO);
return;
}

// play back audio
for frame in out {
let out = self.resampler.get(self.fractional_position as f32);
Expand All @@ -238,7 +235,10 @@ impl Sound for StaticSound {
self.fractional_position -= 1.0;
self.update_position();
}
*frame = out;
*frame = (out
* self.playback_state_manager.fade_volume().as_amplitude()
* self.volume.value().as_amplitude())
.panned(self.panning.value());
}
}

Expand Down
18 changes: 9 additions & 9 deletions crates/kira/src/sound/static_sound/sound/resampler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::frame::{interpolate_frame, Frame};
#[derive(Debug, Clone, Copy, PartialEq)]
struct RecentFrame {
/// A frame of audio.
frame: Frame,
frame: Option<Frame>,
/// The current frame index of the source sound at the
/// time this frame was pushed to the resampler.
frame_index: usize,
Expand All @@ -18,13 +18,13 @@ impl Resampler {
pub fn new(starting_frame_index: usize) -> Self {
Self {
frames: [RecentFrame {
frame: Frame::ZERO,
frame: None,
frame_index: starting_frame_index,
}; 4],
}
}

pub fn push_frame(&mut self, frame: Frame, sample_index: usize) {
pub fn push_frame(&mut self, frame: Option<Frame>, sample_index: usize) {
for i in 0..self.frames.len() - 1 {
self.frames[i] = self.frames[i + 1];
}
Expand All @@ -37,10 +37,10 @@ impl Resampler {
#[must_use]
pub fn get(&self, fractional_position: f32) -> Frame {
interpolate_frame(
self.frames[0].frame,
self.frames[1].frame,
self.frames[2].frame,
self.frames[3].frame,
self.frames[0].frame.unwrap_or_default(),
self.frames[1].frame.unwrap_or_default(),
self.frames[2].frame.unwrap_or_default(),
self.frames[3].frame.unwrap_or_default(),
fractional_position,
)
}
Expand All @@ -59,9 +59,9 @@ impl Resampler {
}

#[must_use]
pub fn outputting_silence(&self) -> bool {
pub fn empty(&self) -> bool {
self.frames
.iter()
.all(|RecentFrame { frame, .. }| *frame == Frame::ZERO)
.all(|RecentFrame { frame, .. }| frame.is_none())
}
}
6 changes: 3 additions & 3 deletions crates/kira/src/sound/static_sound/sound/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,23 +172,23 @@ fn pauses_and_resumes_with_fades() {
);
assert_eq!(
sound.playback_state_manager.playback_state(),
PlaybackState::Playing
PlaybackState::Resuming
);
assert_eq!(
sound.process_one(1.0, &MockInfoBuilder::new().build()),
Frame::from_mono(Decibels(-30.0).as_amplitude()).panned(Panning::CENTER)
);
assert_eq!(
sound.playback_state_manager.playback_state(),
PlaybackState::Playing
PlaybackState::Resuming
);
assert_eq!(
sound.process_one(1.0, &MockInfoBuilder::new().build()),
Frame::from_mono(Decibels(-15.0).as_amplitude()).panned(Panning::CENTER)
);
assert_eq!(
sound.playback_state_manager.playback_state(),
PlaybackState::Playing
PlaybackState::Resuming
);

for _ in 0..3 {
Expand Down

0 comments on commit cb54874

Please sign in to comment.