Skip to content

Commit 18c0681

Browse files
committed
Move AudioScheduledSourceNode trait to src/node/scheduled_source.rs
1 parent 26b41da commit 18c0681

File tree

2 files changed

+63
-57
lines changed

2 files changed

+63
-57
lines changed

src/node/mod.rs

Lines changed: 6 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,18 @@ use std::f32::consts::PI;
44
use std::sync::{Arc, Mutex, OnceLock};
55

66
use crate::context::{AudioContextRegistration, ConcreteBaseAudioContext};
7-
use crate::events::{ErrorEvent, Event, EventHandler, EventPayload, EventType};
7+
use crate::events::{ErrorEvent, EventHandler, EventPayload, EventType};
88
use crate::message::ControlMessage;
99
use crate::render::{
1010
AudioParamValues, AudioProcessor, AudioRenderQuantum, AudioWorkletGlobalScope,
1111
};
1212
use crate::AudioBufferIter;
1313

14+
// traits
15+
mod scheduled_source;
16+
pub use scheduled_source::*;
17+
18+
// nodes
1419
mod analyser;
1520
pub use analyser::*;
1621
mod audio_buffer_source;
@@ -53,7 +58,6 @@ mod stereo_panner;
5358
pub use stereo_panner::*;
5459
mod waveshaper;
5560
pub use waveshaper::*;
56-
//use worklet::*;
5761

5862
pub(crate) const TABLE_LENGTH_USIZE: usize = 8192;
5963
pub(crate) const TABLE_LENGTH_BY_4_USIZE: usize = TABLE_LENGTH_USIZE / 4;
@@ -434,61 +438,6 @@ pub trait AudioNode {
434438
}
435439
}
436440

437-
/// Interface of source nodes, controlling start and stop times.
438-
/// The node will emit silence before it is started, and after it has ended.
439-
pub trait AudioScheduledSourceNode: AudioNode {
440-
/// Play immediately
441-
///
442-
/// # Panics
443-
///
444-
/// Panics if the source was already started
445-
fn start(&mut self);
446-
447-
/// Schedule playback start at given timestamp
448-
///
449-
/// # Panics
450-
///
451-
/// Panics if the source was already started
452-
fn start_at(&mut self, when: f64);
453-
454-
/// Stop immediately
455-
///
456-
/// # Panics
457-
///
458-
/// Panics if the source was already stopped
459-
fn stop(&mut self);
460-
461-
/// Schedule playback stop at given timestamp
462-
///
463-
/// # Panics
464-
///
465-
/// Panics if the source was already stopped
466-
fn stop_at(&mut self, when: f64);
467-
468-
/// Register callback to run when the source node has stopped playing
469-
///
470-
/// For all [`AudioScheduledSourceNode`]s, the ended event is dispatched when the stop time
471-
/// determined by stop() is reached. For an [`AudioBufferSourceNode`], the event is also
472-
/// dispatched because the duration has been reached or if the entire buffer has been played.
473-
///
474-
/// Only a single event handler is active at any time. Calling this method multiple times will
475-
/// override the previous event handler.
476-
fn set_onended<F: FnOnce(Event) + Send + 'static>(&self, callback: F) {
477-
let callback = move |_| callback(Event { type_: "ended" });
478-
479-
self.context().set_event_handler(
480-
EventType::Ended(self.registration().id()),
481-
EventHandler::Once(Box::new(callback)),
482-
);
483-
}
484-
485-
/// Unset the callback to run when the source node has stopped playing
486-
fn clear_onended(&self) {
487-
self.context()
488-
.clear_event_handler(EventType::Ended(self.registration().id()));
489-
}
490-
}
491-
492441
// `MediaStreamRenderer` is internally used by `MediaElementAudioSourceNode` and
493442
// `MediaStreamAudioSourceNode`.
494443
struct MediaStreamRenderer<R> {

src/node/scheduled_source.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
use super::AudioNode;
2+
use crate::events::{Event, EventHandler, EventType};
3+
4+
/// Interface of source nodes, controlling start and stop times.
5+
/// The node will emit silence before it is started, and after it has ended.
6+
pub trait AudioScheduledSourceNode: AudioNode {
7+
/// Play immediately
8+
///
9+
/// # Panics
10+
///
11+
/// Panics if the source was already started
12+
fn start(&mut self);
13+
14+
/// Schedule playback start at given timestamp
15+
///
16+
/// # Panics
17+
///
18+
/// Panics if the source was already started
19+
fn start_at(&mut self, when: f64);
20+
21+
/// Stop immediately
22+
///
23+
/// # Panics
24+
///
25+
/// Panics if the source was already stopped
26+
fn stop(&mut self);
27+
28+
/// Schedule playback stop at given timestamp
29+
///
30+
/// # Panics
31+
///
32+
/// Panics if the source was already stopped
33+
fn stop_at(&mut self, when: f64);
34+
35+
/// Register callback to run when the source node has stopped playing
36+
///
37+
/// For all [`AudioScheduledSourceNode`]s, the ended event is dispatched when the stop time
38+
/// determined by stop() is reached. For an [`AudioBufferSourceNode`], the event is also
39+
/// dispatched because the duration has been reached or if the entire buffer has been played.
40+
///
41+
/// Only a single event handler is active at any time. Calling this method multiple times will
42+
/// override the previous event handler.
43+
fn set_onended<F: FnOnce(Event) + Send + 'static>(&self, callback: F) {
44+
let callback = move |_| callback(Event { type_: "ended" });
45+
46+
self.context().set_event_handler(
47+
EventType::Ended(self.registration().id()),
48+
EventHandler::Once(Box::new(callback)),
49+
);
50+
}
51+
52+
/// Unset the callback to run when the source node has stopped playing
53+
fn clear_onended(&self) {
54+
self.context()
55+
.clear_event_handler(EventType::Ended(self.registration().id()));
56+
}
57+
}

0 commit comments

Comments
 (0)