File tree Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Expand file tree Collapse file tree 2 files changed +23
-1
lines changed Original file line number Diff line number Diff line change @@ -75,7 +75,7 @@ impl AudioWorkletProcessor for WhiteNoiseProcessor {
75
75
_inputs : & ' b [ & ' a [ & ' a [ f32 ] ] ] ,
76
76
outputs : & ' b mut [ & ' a mut [ & ' a mut [ f32 ] ] ] ,
77
77
_params : AudioParamValues < ' _ > ,
78
- _scope : & RenderScope ,
78
+ scope : & RenderScope ,
79
79
) -> bool {
80
80
// edit the output buffer in place
81
81
outputs[ 0 ] . iter_mut ( ) . for_each ( |buf| {
@@ -93,6 +93,10 @@ impl AudioWorkletProcessor for WhiteNoiseProcessor {
93
93
} )
94
94
} ) ;
95
95
96
+ if scope. current_frame % 12800 == 0 {
97
+ scope. post_message ( Box :: new ( scope. current_frame ) ) ;
98
+ }
99
+
96
100
true // tail time, source node will always be active
97
101
}
98
102
@@ -125,6 +129,12 @@ fn main() {
125
129
// connect to speakers
126
130
noise. node ( ) . connect ( & context. destination ( ) ) ;
127
131
132
+ // add event handling for the heartbeat events from the render thread
133
+ noise. node ( ) . port ( ) . set_onmessage ( |m| {
134
+ let frame = m. downcast :: < u64 > ( ) . unwrap ( ) ;
135
+ println ! ( "rendered frame {frame}" ) ;
136
+ } ) ;
137
+
128
138
// enjoy listening
129
139
println ! ( "White noise" ) ;
130
140
std:: thread:: sleep ( std:: time:: Duration :: from_secs ( 2 ) ) ;
Original file line number Diff line number Diff line change @@ -38,6 +38,18 @@ impl std::fmt::Debug for RenderScope {
38
38
}
39
39
40
40
impl RenderScope {
41
+ /// Send a message to the corresponding AudioWorkletNode of this processor
42
+ ///
43
+ /// This method is just a shim of the full
44
+ /// [`MessagePort`](https://webaudio.github.io/web-audio-api/#dom-audioworkletprocessor-port)
45
+ /// `onmessage` functionality of the AudioWorkletProcessor.
46
+ pub fn post_message ( & self , msg : Box < dyn Any + Send + ' static > ) {
47
+ if let Some ( sender) = self . event_sender . as_ref ( ) {
48
+ // sending could fail if the channel is saturated or the main thread is shutting down
49
+ let _ = sender. try_send ( EventDispatch :: message ( self . node_id . get ( ) , msg) ) ;
50
+ }
51
+ }
52
+
41
53
pub ( crate ) fn send_ended_event ( & self ) {
42
54
if let Some ( sender) = self . event_sender . as_ref ( ) {
43
55
// sending could fail if the channel is saturated or the main thread is shutting down
You can’t perform that action at this time.
0 commit comments