Skip to content

Commit 198f060

Browse files
committed
Address review comments, add loop messages
1 parent 3fe6ba1 commit 198f060

File tree

1 file changed

+23
-6
lines changed

1 file changed

+23
-6
lines changed

audio/buffer_source_node.rs

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,14 @@ use param::{Param, ParamType};
88
pub enum AudioBufferSourceNodeMessage {
99
/// Set the data block holding the audio sample data to be played.
1010
SetBuffer(Option<AudioBuffer>),
11+
/// Set loop parameter.
12+
SetLoopEnabled(bool),
13+
/// Set loop parameter.
14+
SetLoopEnd(f64),
15+
/// Set loop parameter.
16+
SetLoopStart(f64),
1117
/// Set start parameters (when, offset, duration).
12-
Start(f64, Option<f64>, Option<f64>),
18+
SetStartParams(f64, Option<f64>, Option<f64>),
1319
}
1420

1521
/// This specifies options for constructing an AudioBufferSourceNode.
@@ -81,7 +87,7 @@ pub(crate) struct AudioBufferSourceNode {
8187
/// Duration parameter passed to Start().
8288
start_duration: Option<f64>,
8389
/// The same as start_at, but with subsample accuracy.
84-
/// FIXME: Maybe AudioScheduledSourceNode should use this as well?
90+
/// FIXME: AudioScheduledSourceNode should use this as well.
8591
start_when: f64,
8692
/// Time at which the source should stop playing.
8793
stop_at: Option<Tick>,
@@ -116,7 +122,18 @@ impl AudioBufferSourceNode {
116122
AudioBufferSourceNodeMessage::SetBuffer(buffer) => {
117123
self.buffer = buffer;
118124
}
119-
AudioBufferSourceNodeMessage::Start(when, offset, duration) => {
125+
// XXX(collares): To fully support dynamically updating loop bounds,
126+
// Must truncate self.buffer_pos if it is now outside the loop.
127+
AudioBufferSourceNodeMessage::SetLoopEnabled(loop_enabled) => {
128+
self.loop_enabled = loop_enabled
129+
}
130+
AudioBufferSourceNodeMessage::SetLoopEnd(loop_end) => {
131+
self.loop_end = Some(loop_end)
132+
}
133+
AudioBufferSourceNodeMessage::SetLoopStart(loop_start) => {
134+
self.loop_start = Some(loop_start)
135+
}
136+
AudioBufferSourceNodeMessage::SetStartParams(when, offset, duration) => {
120137
self.start_when = when;
121138
self.start_offset = offset;
122139
self.start_duration = duration;
@@ -163,6 +180,7 @@ impl AudioNodeEngine for AudioBufferSourceNode {
163180
}
164181
}
165182

183+
// https://webaudio.github.io/web-audio-api/#computedplaybackrate
166184
self.playback_rate.update(info, Tick(0));
167185
self.detune.update(info, Tick(0));
168186
// computed_playback_rate can be negative or zero.
@@ -189,9 +207,7 @@ impl AudioNodeEngine for AudioBufferSourceNode {
189207
if forward && self.buffer_pos >= actual_loop_end {
190208
self.buffer_pos = actual_loop_start;
191209
}
192-
// XXX(collares): This is technically not in the spec, but it's the
193-
// only thing that makes sense. I suspect the spec was not fully
194-
// updated for negative playbackRates.
210+
// https://github.com/WebAudio/web-audio-api/issues/2031
195211
if !forward && self.buffer_pos < actual_loop_start {
196212
self.buffer_pos = actual_loop_end;
197213
}
@@ -374,6 +390,7 @@ impl AudioBuffer {
374390
// XXX(collares): There are better fast interpolation algorithms.
375391
// Firefox uses (via Speex's resampler) the algorithm described in
376392
// https://ccrma.stanford.edu/~jos/resample/resample.pdf
393+
// There are Rust bindings: https://github.com/rust-av/speexdsp-rs
377394
pub fn interpolate(&self, chan: u8, pos: f64) -> f32 {
378395
debug_assert!(pos >= 0. && pos < self.len() as f64);
379396

0 commit comments

Comments
 (0)