8000 Exposed rodio API for skipping first part of a sample by 0----0 · Pull Request #1001 · ggez/ggez · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

Exposed rodio API for skipping first part of a sample #1001

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

Merged
merged 2 commits into from
Dec 25, 2021
Merged
Changes from all commits
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
28 changes: 28 additions & 0 deletions src/audio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,17 @@ pub trait SoundSource {
/// Sets the fade-in time of the source
fn set_fade_in(&mut self, dur: time::Duration);

/// Sets the time from which playback begins, skipping audio up to that point.
///
/// Calls to [`elapsed()`](#tymethod.elapsed) will measure from this point, ignoring skipped time.
///
/// Effects such as [`set_fade_in()`](#tymethod.set_fade_in) or [`set_pitch()`](#tymethod.set_pitch)
/// will apply from this new start.
///
/// If [`set_repeat()`](#tymethod.set_repeat) is set to true, then after looping, the audio will return
/// to the original beginning of the source, rather than the time specified here.
fn set_start(&mut self, dur: time::Duration);

/// Sets the speed ratio (by adjusting the playback speed)
fn set_pitch(&mut self, ratio: f32);

Expand Down Expand Up @@ -211,6 +222,7 @@ pub(crate) struct SourceState {
data: io::Cursor<SoundData>,
repeat: bool,
fade_in: time::Duration,
skip_duration: time::Duration,
speed: f32,
query_interval: time::Duration,
play_time: Arc<AtomicUsize>,
Expand All @@ -223,6 +235,7 @@ impl SourceState {
data: cursor,
repeat: false,
fade_in: time::Duration::from_millis(0),
skip_duration: time::Duration::from_millis(0),
speed: 1.0,
query_interval: time::Duration::from_millis(100),
play_time: Arc::new(AtomicUsize::new(0)),
Expand All @@ -238,6 +251,10 @@ impl SourceState {
self.fade_in = dur;
}

pub fn set_start(&mut self, dur: time::Duration) {
self.skip_duration = dur;
}

/// Sets the pitch ratio (by adjusting the playback speed).
pub fn set_pitch(&mut self, ratio: f32) {
self.speed = ratio;
Expand Down Expand Up @@ -323,6 +340,7 @@ impl SoundSource for Source {
if self.state.repeat {
let sound = rodio::Decoder::new(cursor)?
.repeat_infinite()
.skip_duration(self.state.skip_duration)
.speed(self.state.speed)
.fade_in(self.state.fade_in)
.periodic_access(self.state.query_interval, move |_| {
Expand All @@ -331,6 +349,7 @@ impl SoundSource for Source {
self.sink.append(sound);
} else {
let sound = rodio::Decoder::new(cursor)?
.skip_duration(self.state.skip_duration)
.speed(self.state.speed)
.fade_in(self.state.fade_in)
.periodic_access(self.state.query_interval, move |_| {
Expand Down Expand Up @@ -359,6 +378,9 @@ impl SoundSource for Source {
fn set_fade_in(&mut self, dur: time::Duration) {
self.state.set_fade_in(dur)
}
fn set_start(&mut self, dur: time::Duration) {
self.state.set_start(dur)
}
fn set_pitch(&mut self, ratio: f32) {
self.state.set_pitch(ratio)
}
Expand Down Expand Up @@ -492,6 +514,7 @@ impl SoundSource for SpatialSource {
if self.state.repeat {
let sound = rodio::Decoder::new(cursor)?
.repeat_infinite()
.skip_duration(self.state.skip_duration)
.speed(self.state.speed)
.fade_in(self.state.fade_in)
.periodic_access(self.state.query_interval, move |_| {
Expand All @@ -500,6 +523,7 @@ impl SoundSource for SpatialSource {
self.sink.append(sound);
} else {
let sound = rodio::Decoder::new(cursor)?
.skip_duration(self.state.skip_duration)
.speed(self.state.speed)
.fade_in(self.state.fade_in)
.periodic_access(self.state.query_interval, move |_| {
Expand Down Expand Up @@ -536,6 +560,10 @@ impl SoundSource for SpatialSource {
self.state.set_fade_in(dur)
}

fn set_start(&mut self, dur: time::Duration) {
self.state.set_start(dur)
}

fn set_pitch(&mut self, ratio: f32) {
self.state.set_pitch(ratio)
}
Expand Down
0